Skip to content

Commit 917c83b

Browse files
authored
Performance fix (#410)
* Improved NWP loading * performance fix and black complaince
1 parent 8bec82e commit 917c83b

File tree

3 files changed

+26
-31
lines changed

3 files changed

+26
-31
lines changed

pysteps/blending/steps.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
from pysteps.postprocessing import probmatching
5656
from pysteps.timeseries import autoregression, correlation
5757

58+
from copy import deepcopy
59+
5860
try:
5961
import dask
6062

@@ -710,15 +712,11 @@ def forecast(
710712
# 5. Repeat precip_cascade for n ensemble members
711713
# First, discard all except the p-1 last cascades because they are not needed
712714
# for the AR(p) model
713-
precip_cascade = [
714-
precip_cascade[i][-ar_order:] for i in range(n_cascade_levels)
715-
]
716715

717-
precip_cascade = [
718-
[precip_cascade[j].copy() for j in range(n_cascade_levels)]
719-
for i in range(n_ens_members)
720-
]
721-
precip_cascade = np.stack(precip_cascade)
716+
precip_cascade = np.stack(
717+
[[precip_cascade[i][-ar_order:].copy() for i in range(n_cascade_levels)]]
718+
* n_ens_members
719+
)
722720

723721
# 6. Initialize all the random generators and prepare for the forecast loop
724722
randgen_prec, vps, generate_vel_noise = _init_random_generators(
@@ -781,8 +779,10 @@ def forecast(
781779
starttime_mainloop = time.time()
782780

783781
extrap_kwargs["return_displacement"] = True
784-
forecast_prev = precip_cascade
785-
noise_prev = noise_cascade
782+
783+
forecast_prev = deepcopy(precip_cascade)
784+
noise_prev = deepcopy(noise_cascade)
785+
786786
t_prev = [0.0 for j in range(n_ens_members)]
787787
t_total = [0.0 for j in range(n_ens_members)]
788788

pysteps/blending/utils.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,11 @@ def load_NWP(input_nc_path_decomp, input_path_velocities, start_time, n_timestep
475475
ncf_decomp = netCDF4.Dataset(input_nc_path_decomp, "r", format="NETCDF4")
476476
velocities = np.load(input_path_velocities)
477477

478-
# Initialise the decomposition dictionary
479-
decomp_dict = dict()
480-
decomp_dict["domain"] = ncf_decomp.domain
481-
decomp_dict["normalized"] = bool(ncf_decomp.normalized)
482-
decomp_dict["compact_output"] = bool(ncf_decomp.compact_output)
478+
decomp_dict = {
479+
"domain": ncf_decomp.domain,
480+
"normalized": bool(ncf_decomp.normalized),
481+
"compact_output": bool(ncf_decomp.compact_output),
482+
}
483483

484484
# Convert the start time and the timestep to datetime64 and timedelta64 type
485485
zero_time = np.datetime64("1970-01-01T00:00:00", "ns")
@@ -515,23 +515,18 @@ def load_NWP(input_nc_path_decomp, input_path_velocities, start_time, n_timestep
515515
# Initialise the list of dictionaries which will serve as the output (cf: the STEPS function)
516516
R_d = list()
517517

518-
for i in range(start_i, end_i):
519-
decomp_dict_ = decomp_dict.copy()
518+
pr_decomposed = ncf_decomp.variables["pr_decomposed"][start_i:end_i, :, :, :]
519+
means = ncf_decomp.variables["means"][start_i:end_i, :]
520+
stds = ncf_decomp.variables["stds"][start_i:end_i, :]
520521

521-
# Obtain the decomposed cascades for time step i
522-
cascade_levels = ncf_decomp.variables["pr_decomposed"][i, :, :, :]
523-
# Obtain the mean values
524-
means = ncf_decomp.variables["means"][i, :]
525-
# Obtain de standard deviations
526-
stds = ncf_decomp.variables["stds"][i, :]
527-
528-
# Save the values in the dictionary as normal arrays with the filled method
529-
decomp_dict_["cascade_levels"] = np.ma.filled(cascade_levels, fill_value=np.nan)
530-
decomp_dict_["means"] = np.ma.filled(means, fill_value=np.nan)
531-
decomp_dict_["stds"] = np.ma.filled(stds, fill_value=np.nan)
522+
for i in range(n_timesteps + 1):
523+
decomp_dict["cascade_levels"] = np.ma.filled(
524+
pr_decomposed[i], fill_value=np.nan
525+
)
526+
decomp_dict["means"] = np.ma.filled(means[i], fill_value=np.nan)
527+
decomp_dict["stds"] = np.ma.filled(stds[i], fill_value=np.nan)
532528

533-
# Append the output list
534-
R_d.append(decomp_dict_)
529+
R_d.append(decomp_dict.copy())
535530

536531
ncf_decomp.close()
537532
return R_d, uv

pysteps/extrapolation/semilagrangian.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def extrapolate(
173173

174174
if xy_coords is None:
175175
x_values, y_values = np.meshgrid(
176-
np.arange(velocity.shape[2]), np.arange(velocity.shape[1])
176+
np.arange(velocity.shape[2]), np.arange(velocity.shape[1]), copy=False
177177
)
178178

179179
xy_coords = np.stack([x_values, y_values])

0 commit comments

Comments
 (0)