Skip to content

Commit f4e41ea

Browse files
committed
Refactor grib_to_netcdf time_counter calculation
Update `time_counter` calculation to use forecast step ranges. This mitigates the fact that `cfgrib` no longer provides correct forecast time step values from ECCC GRIB2 files. This is a rework of commit 406e7e5 that tried to resolve the issue using the `valid_time` GRIB variable. This change makes our timestamp processing compatible with non-hourly step handling that was introduced in `cfgrib=0.9.12.0`. This change removes the need for version pinning of `cfgrib=0.9.11.0` that was introduced in PR#276. By doing that, issue #336 is resolved.
1 parent 2fa2016 commit f4e41ea

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

nowcast/workers/grib_to_netcdf.py

+27-8
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,14 @@ def _calc_nemo_ds(
266266
fcst_date, fcst_hr, fcst_step_range, msc_var, full_grid, config
267267
)
268268
nemo_datasets[nemo_var] = _calc_nemo_var_ds(
269-
msc_var, grib_var, nemo_var, grib_files, full_grid, georef_ds, config
269+
msc_var,
270+
grib_var,
271+
nemo_var,
272+
grib_files,
273+
fcst_step_range,
274+
full_grid,
275+
georef_ds,
276+
config,
270277
)
271278
nemo_ds = xarray.combine_by_coords(
272279
nemo_datasets.values(), combine_attrs="drop_conflicts"
@@ -328,7 +335,7 @@ def _trim_grib(ds, y_slice, x_slice):
328335
# Select region of interest
329336
ds = ds.sel(y=y_slice, x=x_slice)
330337
# Drop coordinates that we don't need
331-
keep_coords = ("valid_time", "latitude", "longitude")
338+
keep_coords = ("time", "step", "valid_time", "latitude", "longitude")
332339
ds = ds.reset_coords(
333340
[coord for coord in ds.coords if coord not in keep_coords],
334341
drop=True,
@@ -337,13 +344,21 @@ def _trim_grib(ds, y_slice, x_slice):
337344

338345

339346
def _calc_nemo_var_ds(
340-
msc_var, grib_var, nemo_var, grib_files, full_grid, georef_ds, config
347+
msc_var,
348+
grib_var,
349+
nemo_var,
350+
grib_files,
351+
fcst_step_range,
352+
full_grid,
353+
georef_ds,
354+
config,
341355
):
342356
"""
343357
:param str msc_var:
344358
:param str grib_var:
345359
:param str nemo_var:
346360
:param list grib_files:
361+
:param tuple fcst_step_range:
347362
:param boolean full_grid:
348363
:param :py:class:`xarray.Dataset` or None georef_ds:
349364
:param dict config:
@@ -369,14 +384,18 @@ def _calc_nemo_var_ds(
369384
grib_files,
370385
preprocess=_partial_trim_grib,
371386
combine="nested",
372-
concat_dim="valid_time",
387+
concat_dim="step",
373388
engine="cfgrib",
374389
backend_kwargs={"indexpath": ""},
375390
)
391+
start, stop = fcst_step_range
392+
time_counter = grib_ds.time.values + numpy.array(
393+
[numpy.timedelta64(fcst_step, "h") for fcst_step in range(start, stop + 1)]
394+
)
376395
nemo_da = xarray.DataArray(
377396
data=grib_ds[grib_var].data,
378397
coords={
379-
"time_counter": grib_ds.valid_time.values,
398+
"time_counter": time_counter,
380399
"y": grib_ds.y,
381400
"x": grib_ds.x,
382401
},
@@ -399,7 +418,7 @@ def _calc_nemo_var_ds(
399418
nemo_var: nemo_da,
400419
},
401420
coords={
402-
"time_counter": grib_ds.valid_time.values,
421+
"time_counter": time_counter,
403422
"y": grib_ds.y,
404423
"x": grib_ds.x,
405424
"nav_lon": nav_lon,
@@ -412,8 +431,8 @@ def _calc_nemo_var_ds(
412431
# Drop unneeded variables that come from full continental domain GRIB files.
413432
# Drop time separately from lons/lats because drop_vars() fails is any of the vars
414433
# in the list don't exist.
415-
nemo_ds = nemo_ds.drop_vars(["valid_time"])
416-
nemo_ds = nemo_ds.drop_vars(["longitude", "latitude"])
434+
nemo_ds = nemo_ds.drop_vars(["time", "valid_time"])
435+
nemo_ds = nemo_ds.drop_vars(["latitude", "longitude"])
417436
except ValueError:
418437
# We don't care if some or all of them don't exist in the dataset
419438
pass

0 commit comments

Comments
 (0)