Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rainfarm.py #311

Merged
merged 23 commits into from
Jun 10, 2024
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
787e39c
Update rainfarm.py
simonbeylat Sep 21, 2022
e10c7ce
add smoothing option {None,'Gaussian','tophat'}, update the _balance…
Oct 3, 2022
c25768c
Merge branch 'pySTEPS:master' into rainfarm_update
simonbeylat Oct 3, 2022
b67f8c9
merge _smoothconv and _balanced_spatial_average function
Oct 14, 2022
388ba45
add spectral merging from D'Onofrio et al. 2014
Oct 25, 2022
974f525
Sort alphabetically
dnerini Nov 27, 2022
377764f
Small fixes in the docstrings
dnerini Nov 27, 2022
694e9f5
Refactor code
dnerini Nov 27, 2022
e665394
Merge pull request #1 from dnerini/rainfarm_update
simonbeylat Dec 5, 2022
98db387
test smoothing option - ok, add first version of spectral fusion (sti…
Dec 6, 2022
b8a8beb
Modify application of spectral fusion
blaiDoAr May 9, 2024
4f95973
Replace R radially averaged power spectrum function by rapsd PySTEPS …
jrmiro May 9, 2024
f838b64
Include freq parameters into _apply_spectral_fusion and change variab…
ecasellas May 10, 2024
3ce2d77
Adapt downscale function to spectral_fusion
ecasellas May 10, 2024
be1bb62
Correct smoothing application in downscale function
ecasellas May 10, 2024
d500a06
Remove unused zoom and reduce code
ecasellas May 14, 2024
bc4943e
Add aggregation test and adapt docstring
blaiDoAr May 14, 2024
08cff0d
Remove print
blaiDoAr May 14, 2024
2974eaf
Set None as default alpha value
ecasellas May 14, 2024
f6f1b4c
Correct alpha value synthetic field amplitude
ecasellas May 24, 2024
5156170
Run black formatter on rainfarm.py
ecasellas May 27, 2024
c38f4e1
Some refactoring to improve readability
dnerini Jun 9, 2024
8827515
Correction of merged normalisation
ecasellas Jun 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions pysteps/downscaling/rainfarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,60 @@ def _balanced_spatial_average(x, k):
return convolve(x, k) / convolve(ones, k)


def downscale(precip, ds_factor, alpha=None, threshold=None, return_alpha=False):
def _smoothconv(P,nas):
simonbeylat marked this conversation as resolved.
Show resolved Hide resolved
simonbeylat marked this conversation as resolved.
Show resolved Hide resolved

"""
Parameters
simonbeylat marked this conversation as resolved.
Show resolved Hide resolved
----------

P: matrix
matrix with the input field to smoothen, with dimensions ns*ns

nas : int
original size
simonbeylat marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------

The smoothened field.

References
----------

Terzago et al. 2018
simonbeylat marked this conversation as resolved.
Show resolved Hide resolved

"""

print("smoothing")
simonbeylat marked this conversation as resolved.
Show resolved Hide resolved
indmask= ~ np.isfinite(P)
P[indmask] = 0.
ns = np.shape(P)[1]

sdim = (ns/nas)/2
mask=np.zeros([ns,ns])
for i in range(ns):
for j in range(ns):

kx = i
ky = j
if i > (ns/2):
kx=i-ns
if j > (ns/2):
ky=j-ns
r2 = kx * kx + ky * ky
mask[i,j] = np.exp(-(r2 / (sdim*sdim))/2)

fm = np.fft.fft2(mask)
pf = np.real(np.fft.ifft2(fm*np.fft.fft2(P))) / np.sum(mask)
if np.sum(indmask) > 0:
P[~ indmask] = 1
pf = pf / (np.real(np.fft.ifft2(fm*np.fft.fft2(P))) / np.sum(P) / len(fm))

pf[indmask]=np.nan
return pf


def downscale(precip, ds_factor, alpha=None, threshold=None, return_alpha=False , smooth=False):
simonbeylat marked this conversation as resolved.
Show resolved Hide resolved
"""
Downscale a rainfall field by increasing its spatial resolution by
a positive integer factor.
Expand Down Expand Up @@ -86,6 +139,7 @@ def downscale(precip, ds_factor, alpha=None, threshold=None, return_alpha=False)
:cite:`Rebora2006`

"""
nas = np.shape(precip)[1]

ki = np.fft.fftfreq(precip.shape[0])
kj = np.fft.fftfreq(precip.shape[1])
Expand Down Expand Up @@ -119,8 +173,18 @@ def downscale(precip, ds_factor, alpha=None, threshold=None, return_alpha=False)
tophat = ((mx**2 + my**2) <= rad**2).astype(float)
tophat /= tophat.sum()

P_agg = _balanced_spatial_average(P_u, tophat)
simonbeylat marked this conversation as resolved.
Show resolved Hide resolved
r_agg = _balanced_spatial_average(r, tophat)
if smooth:
P_agg = _balanced_spatial_average(P_u, tophat)
r_agg = _balanced_spatial_average(r, tophat)

P_agg = _smoothconv(P_agg, nas)
r_agg = _smoothconv(r_agg, nas)

else:
P_agg = _balanced_spatial_average(P_u, tophat)
r_agg = _balanced_spatial_average(r, tophat)


r *= P_agg / r_agg

if threshold is not None:
Expand Down