Skip to content

Commit

Permalink
used outside time buffer in cmems_nc_to_ini(), expanded testbank (#1087)
Browse files Browse the repository at this point in the history
* used outside time buffer in cmems_nc_to_ini()

* expanded testbank

* updated whatsnew
  • Loading branch information
veenstrajelmer authored Jan 28, 2025
1 parent a20993e commit d516cc9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
8 changes: 4 additions & 4 deletions dfm_tools/modelbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from dfm_tools.interpolate_grid2bnd import (ext_add_boundary_object_per_polyline,
open_prepare_dataset,
ds_apply_conversion_dict,
_ds_sel_time_outside,
)

__all__ = [
Expand Down Expand Up @@ -110,10 +111,9 @@ def cmems_nc_to_ini(ext_old, dir_output, list_quantities, tstart, dir_pattern, c

tstart_pd = pd.Timestamp(tstart)
tstart_str = tstart_pd.strftime("%Y-%m-%d_%H-%M-%S")
# tstop_pd is slightly higher than tstart_pd to ensure >1 timesteps in all cases
tstop_pd = tstart_pd + pd.Timedelta(hours=1)

# FM needs two timesteps, so convert timestamp to two surrounding timestamps
tstart_round = pd.Timestamp(tstart).floor('1d')
tstop_round = (pd.Timestamp(tstart) + pd.Timedelta(hours=24)).ceil('1d')
for quan_bnd in list_quantities:

if quan_bnd in ["temperaturebnd","uxuyadvectionvelocitybnd"]:
Expand Down Expand Up @@ -145,7 +145,7 @@ def cmems_nc_to_ini(ext_old, dir_output, list_quantities, tstart, dir_pattern, c

# subset two times. interp to tstart would be the proper way to do it,
# but FM needs two timesteps for nudge_salinity_temperature and initial waq vars
data_xr = data_xr.sel(time=slice(tstart_round, tstop_round))
data_xr = _ds_sel_time_outside(ds=data_xr, tstart=tstart_pd, tstop=tstop_pd)

# assert that there are at least two timesteps in the resulting dataset
# delft3dfm will crash if there is only one timestep
Expand Down
3 changes: 3 additions & 0 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## UNRELEASED

### Feat
- usage of outside time buffer in `dfmt.cmems_nc_to_ini()` so noon-centered or monthly timestamps are also supported in [#1087](https://github.com/Deltares/dfm_tools/pull/1087)

### Fix
- made p-drive paths for tide models and gesla3 work on linux also in [#1083](https://github.com/Deltares/dfm_tools/pull/1083) and [#1085](https://github.com/Deltares/dfm_tools/pull/1085)

Expand Down
23 changes: 18 additions & 5 deletions tests/test_modelbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,20 @@ def test_get_ncvarname_list():
assert "quantity 'nonexistingbnd' not in conversion_dict" in str(e.value)


@pytest.mark.parametrize("timecase", [pytest.param(x, id=x) for x in ['midnight','noon','monthly']])
@pytest.mark.systemtest
def test_cmems_nc_to_ini_midnight_centered(tmp_path):
def test_cmems_nc_to_ini(tmp_path, timecase):
"""
tests for midnight-centered data, noon-centered data and monthly timestamped data
"""
ds1 = cmems_dataset_4times().isel(time=slice(None,2))
ds1["time"] = ds1["time"] + pd.Timedelta(hours=12)
ds2 = cmems_dataset_4times().isel(time=slice(2,None))
ds2["time"] = ds2["time"] + pd.Timedelta(hours=12)
if timecase == "midnight":
ds1["time"] = ds1["time"] + pd.Timedelta(hours=12)
ds2["time"] = ds2["time"] + pd.Timedelta(hours=12)
elif timecase == "monthly":
ds1["time"] = [pd.Timestamp("2019-11-01"), pd.Timestamp("2019-12-01")]
ds2["time"] = [pd.Timestamp("2020-01-01"), pd.Timestamp("2020-02-01")]

dir_pattern = os.path.join(tmp_path, "temp_cmems_2day_*.nc")
file_nc1 = dir_pattern.replace("*","so_p1")
Expand All @@ -67,8 +75,13 @@ def test_cmems_nc_to_ini_midnight_centered(tmp_path):

file_expected = tmp_path / "nudge_salinity_temperature_2020-01-01_00-00-00.nc"

times_expected = ['2020-01-01 00:00:00', '2020-01-02 00:00:00']

if timecase == "midnight":
times_expected = ['2020-01-01 00:00:00', '2020-01-02 00:00:00']
elif timecase == "noon":
times_expected = ['2019-12-31 12:00:00', '2020-01-01 12:00:00']
elif timecase == "monthly":
times_expected = ['2020-01-01 00:00:00', '2020-02-01 00:00:00']

assert os.path.exists(file_expected)
ds_out = xr.open_dataset(file_expected)

Expand Down

0 comments on commit d516cc9

Please sign in to comment.