Skip to content

Commit

Permalink
Merge pull request #142 from Deltares/bug/138_datetime
Browse files Browse the repository at this point in the history
Bugfix #138 in write_forcing
  • Loading branch information
laurenebouaziz committed Mar 23, 2023
2 parents 43960f7 + cb53893 commit 36c95ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Expand Up @@ -26,6 +26,8 @@ Fixed
- Bugfix with wrong nodata value in the hydrography method which caused errors for model which where not based on (sub)basins `PR #144 <https://github.com/Deltares/hydromt_wflow/pull/144>`_
- Bugfix with wrong indexing in the river method that could cause memory issues `PR #147 <https://github.com/Deltares/hydromt_wflow/pull/147>`_
- fix error in **setup_reservoirs** when gdf contains no data in np.nanmax calculation for i.e. damheight #35
- write_forcing with time cftime.DatetimeNoLeap #138 by removing slicing forcing if missings (not needed)
- write_forcing automatic adjustment of starttime and endtime based on forcing content
- When clipping a model from a model with multiple forcing files, a single netcdf is made in write_forcing and the * is removed from the filename.

Deprecated
Expand Down
25 changes: 12 additions & 13 deletions hydromt_wflow/wflow.py
Expand Up @@ -2043,33 +2043,32 @@ def write_forcing(
fn_out = fn_default_path

# Check if all dates between (starttime, endtime) are in all da forcing
# Check if starttime and endtime timestamps are correct
start = pd.to_datetime(self.get_config("starttime"))
end = pd.to_datetime(self.get_config("endtime"))
missings = False
correct_times = False
for da in self.forcing.values():
if "time" in da.coords:
if hasattr(da.indexes["time"], "to_datetimeindex"):
times = da.indexes["time"].to_datetimeindex().values
else:
#only correct dates in toml for standard calendars:
if not hasattr(da.indexes["time"], "to_datetimeindex"):
times = da.time.values
if start < pd.to_datetime(times[0]):
start = pd.to_datetime(times[0])
missings = True
if end > pd.to_datetime(times[-1]):
end = pd.to_datetime(times[-1])
missings = True
if (start < pd.to_datetime(times[0])) | (start not in times):
start = pd.to_datetime(times[0])
correct_times = True
if (end > pd.to_datetime(times[-1])) | (end not in times):
end = pd.to_datetime(times[-1])
correct_times = True
# merge, process and write forcing
ds = xr.merge([da.reset_coords(drop=True) for da in self.forcing.values()])
ds.raster.set_crs(self.crs)
# Send warning, slice ds and update config with new start and end time
if missings:
# Send warning, and update config with new start and end time
if correct_times:
self.logger.warning(
f"Not all dates found in precip_fn changing starttime to {start} and endtime to {end} in the toml."
)
self.set_config("starttime", start.to_pydatetime())
self.set_config("endtime", end.to_pydatetime())
self.write_config()
ds = ds.sel({"time": slice(start, end)})

if decimals is not None:
ds = ds.round(decimals)
Expand Down

0 comments on commit 36c95ec

Please sign in to comment.