Skip to content

Commit abd1f52

Browse files
authored
fix: speed up linear blending through more efficient nowcasting step (#470)
1 parent d4ac4c0 commit abd1f52

File tree

1 file changed

+48
-15
lines changed

1 file changed

+48
-15
lines changed

pysteps/blending/linear_blending.py

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,26 @@ def forecast(
109109
if len(precip.shape) == 3:
110110
precip = precip[-1, :, :]
111111

112-
# Calculate the nowcasts
113-
nowcast_method_func = nowcasts.get_method(nowcast_method)
114-
precip_nowcast = nowcast_method_func(
115-
precip,
116-
velocity,
117-
timesteps,
118-
**nowcast_kwargs,
119-
)
112+
# First calculate the number of needed timesteps (up to end_blending) for the nowcast
113+
# to ensure that the nowcast calculation time is limited.
114+
timesteps_nowcast = int(end_blending / timestep)
120115

121-
# Make sure that precip_nowcast and precip_nwp are in mm/h
122-
precip_nowcast, _ = conversion.to_rainrate(precip_nowcast, metadata=precip_metadata)
116+
nowcast_method_func = nowcasts.get_method(nowcast_method)
123117

124118
# Check if NWP data is given as input
125119
if precip_nwp is not None:
120+
# Calculate the nowcast
121+
precip_nowcast = nowcast_method_func(
122+
precip,
123+
velocity,
124+
timesteps_nowcast,
125+
**nowcast_kwargs,
126+
)
127+
128+
# Make sure that precip_nowcast and precip_nwp are in mm/h
129+
precip_nowcast, _ = conversion.to_rainrate(
130+
precip_nowcast, metadata=precip_metadata
131+
)
126132
precip_nwp, _ = conversion.to_rainrate(precip_nwp, metadata=precip_nwp_metadata)
127133

128134
if len(precip_nowcast.shape) == 4:
@@ -133,10 +139,17 @@ def forecast(
133139
n_ens_members_nowcast = 1
134140

135141
if len(precip_nwp.shape) == 4:
142+
# Ensure precip_nwp has t = n_timesteps
143+
precip_nwp = precip_nwp[:, 0:timesteps, :, :]
144+
# Set the number of ensemble members
136145
n_ens_members_nwp = precip_nwp.shape[0]
137146
if n_ens_members_nwp == 1:
138147
precip_nwp = np.squeeze(precip_nwp)
148+
139149
else:
150+
# Ensure precip_nwp has t = n_timesteps
151+
precip_nwp = precip_nwp[0:timesteps, :, :]
152+
# Set the number of ensemble members
140153
n_ens_members_nwp = 1
141154

142155
# Now, repeat the nowcast ensemble members or the nwp models/members until
@@ -172,9 +185,9 @@ def forecast(
172185

173186
# Check if dimensions are correct
174187
assert (
175-
precip_nwp.shape == precip_nowcast.shape
176-
), "The dimensions of precip_nowcast and precip_nwp need to be identical: dimension of precip_nwp = {} and dimension of precip_nowcast = {}".format(
177-
precip_nwp.shape, precip_nowcast.shape
188+
precip_nwp.shape[-2:] == precip_nowcast.shape[-2:]
189+
), "The x and y dimensions of precip_nowcast and precip_nwp need to be identical: dimension of precip_nwp = {} and dimension of precip_nowcast = {}".format(
190+
precip_nwp.shape[-2:], precip_nowcast.shape[-2:]
178191
)
179192

180193
# Ensure we are not working with nans in the bleding.
@@ -184,12 +197,19 @@ def forecast(
184197
# Fill nans in precip_nowcast
185198
nan_mask = np.isnan(precip_nowcast)
186199
if fill_nwp:
187-
precip_nowcast[nan_mask] = precip_nwp[nan_mask]
200+
if len(precip_nwp.shape) == 4:
201+
precip_nowcast[nan_mask] = precip_nwp[:, 0:timesteps_nowcast, :, :][
202+
nan_mask
203+
]
204+
else:
205+
precip_nowcast[nan_mask] = precip_nwp[0:timesteps_nowcast, :, :][
206+
nan_mask
207+
]
188208
else:
189209
precip_nowcast[nan_mask] = 0.0
190210

191211
# Initialise output
192-
precip_blended = np.zeros_like(precip_nowcast)
212+
precip_blended = np.zeros_like(precip_nwp)
193213

194214
# Calculate the weights
195215
for i in range(timesteps):
@@ -240,6 +260,19 @@ def forecast(
240260
)
241261

242262
else:
263+
# Calculate the nowcast
264+
precip_nowcast = nowcast_method_func(
265+
precip,
266+
velocity,
267+
timesteps,
268+
**nowcast_kwargs,
269+
)
270+
271+
# Make sure that precip_nowcast and precip_nwp are in mm/h
272+
precip_nowcast, _ = conversion.to_rainrate(
273+
precip_nowcast, metadata=precip_metadata
274+
)
275+
243276
# If no NWP data is given, the blended field is simply equal to the nowcast field
244277
precip_blended = precip_nowcast
245278

0 commit comments

Comments
 (0)