Skip to content

Commit

Permalink
fix: make sure netcdf exporters can handle list of timesteps instead …
Browse files Browse the repository at this point in the history
…of integers
  • Loading branch information
RubenImhoff committed Jul 3, 2024
1 parent 1525178 commit 501ce92
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions pysteps/io/exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,10 @@ def initialize_forecast_exporter_netcdf(
Start date of the forecast.
timestep: int
Time step of the forecast (minutes).
n_timesteps: int
Number of time steps in the forecast this argument is ignored if
incremental is set to 'timestep'.
n_timesteps: int or list of integers
Number of time steps to forecast or a list of time steps for which the
forecasts are computed (relative to the input time step). The elements of
the list are required to be in ascending order.
shape: tuple of int
Two-element tuple defining the shape (height,width) of the forecast
grids.
Expand Down Expand Up @@ -460,8 +461,13 @@ def initialize_forecast_exporter_netcdf(
+ "'timestep' or 'member'"
)

if isinstance(n_timesteps, list):
num_timesteps = len(n_timesteps)

Check warning on line 465 in pysteps/io/exporters.py

View check run for this annotation

Codecov / codecov/patch

pysteps/io/exporters.py#L465

Added line #L465 was not covered by tests
else:
num_timesteps = n_timesteps

if incremental == "timestep":
n_timesteps = None
num_timesteps = None
elif incremental == "member":
n_ens_members = None
elif incremental is not None:
Expand Down Expand Up @@ -498,7 +504,7 @@ def initialize_forecast_exporter_netcdf(
h, w = shape

ncf.createDimension("ens_number", size=n_ens_members)
ncf.createDimension("time", size=n_timesteps)
ncf.createDimension("time", size=num_timesteps)
ncf.createDimension("y", size=h)
ncf.createDimension("x", size=w)

Expand Down Expand Up @@ -584,8 +590,12 @@ def initialize_forecast_exporter_netcdf(
var_ens_num.units = ""

var_time = ncf.createVariable("time", int, dimensions=("time",))
if incremental != "timestep":
var_time[:] = [i * timestep * 60 for i in range(1, n_timesteps + 1)]
if isinstance(n_timesteps, list):
if incremental != "timestep":
var_time[:] = n_timesteps * timestep * 60

Check warning on line 595 in pysteps/io/exporters.py

View check run for this annotation

Codecov / codecov/patch

pysteps/io/exporters.py#L594-L595

Added lines #L594 - L595 were not covered by tests
else:
if incremental != "timestep":
var_time[:] = [i * timestep * 60 for i in range(1, n_timesteps + 1)]
var_time.long_name = "forecast time"
startdate_str = datetime.strftime(startdate, "%Y-%m-%d %H:%M:%S")
var_time.units = "seconds since %s" % startdate_str
Expand Down Expand Up @@ -635,7 +645,8 @@ def initialize_forecast_exporter_netcdf(
exporter["timestep"] = timestep
exporter["metadata"] = metadata
exporter["incremental"] = incremental
exporter["num_timesteps"] = n_timesteps
exporter["num_timesteps"] = num_timesteps
exporter["timesteps"] = n_timesteps
exporter["num_ens_members"] = n_ens_members
exporter["shape"] = shape

Expand Down Expand Up @@ -853,7 +864,12 @@ def _export_netcdf(field, exporter):
else:
var_f[var_f.shape[0], :, :] = field
var_time = exporter["var_time"]
var_time[len(var_time) - 1] = len(var_time) * exporter["timestep"] * 60
if isinstance(exporter["timesteps"], list):
var_time[len(var_time) - 1] = (

Check warning on line 868 in pysteps/io/exporters.py

View check run for this annotation

Codecov / codecov/patch

pysteps/io/exporters.py#L868

Added line #L868 was not covered by tests
exporter["timesteps"][len(var_time) - 1] * exporter["timestep"] * 60
)
else:
var_time[len(var_time) - 1] = len(var_time) * exporter["timestep"] * 60
else:
var_f[var_f.shape[0], :, :, :] = field
var_ens_num = exporter["var_ens_num"]
Expand Down

0 comments on commit 501ce92

Please sign in to comment.