Skip to content

Commit

Permalink
181 further improve caching by checking only relevant coordinates for…
Browse files Browse the repository at this point in the history
… certain datasets (#202)

* add option in get_ds_empty to specify dimensions

* fix error for vertex grids

---------

Co-authored-by: Ruben Caljé <[email protected]>
  • Loading branch information
OnnoEbbens and rubencalje authored Jul 12, 2023
1 parent 1011d6d commit 2813914
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
9 changes: 7 additions & 2 deletions nlmod/dims/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ def gdf_to_bool_da(gdf, ds):
return da


def gdf_to_bool_ds(gdf, ds, da_name):
def gdf_to_bool_ds(gdf, ds, da_name, keep_coords=None):
"""convert a GeoDataFrame with polygon geometries into a model dataset with
a data_array named 'da_name' in which each cell is 1 (True) if one or more
geometries are (partly) in that cell.
Expand All @@ -1319,14 +1319,17 @@ def gdf_to_bool_ds(gdf, ds, da_name):
xarray with model data
da_name : str
The name of the variable with boolean data in the ds_out
keep_coords : tuple or None, optional
the coordinates in ds the you want keep in your empty ds. If None all
coordinates are kept from original ds. The default is None.
Returns
-------
ds_out : xr.Dataset
Dataset with a single DataArray, this DataArray is 1 if polygon is in
cell, 0 otherwise. Grid dimensions according to ds and mfgrid.
"""
ds_out = util.get_ds_empty(ds)
ds_out = util.get_ds_empty(ds, keep_coords=keep_coords)
ds_out[da_name] = gdf_to_bool_da(gdf, ds)

return ds_out
Expand Down Expand Up @@ -1579,6 +1582,8 @@ def mask_model_edge(ds, idomain):
ds_out : xarray.Dataset
dataset with edge mask array
"""
ds = ds.copy() # avoid side effects

# add constant head cells at model boundaries
if "angrot" in ds.attrs and ds.attrs["angrot"] != 0.0:
raise NotImplementedError("model edge not yet calculated for rotated grids")
Expand Down
2 changes: 1 addition & 1 deletion nlmod/read/ahn.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_ahn(ds=None, identifier="AHN4_DTM_5m", method="average", extent=None):
if ds is None:
return ahn_da

ds_out = get_ds_empty(ds)
ds_out = get_ds_empty(ds, keep_coords=("y", "x"))
ds_out["ahn"] = ahn_da

return ds_out
Expand Down
2 changes: 1 addition & 1 deletion nlmod/read/jarkus.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_bathymetry(ds, northsea, kind="jarkus", method="average"):
data is resampled to the modelgrid. Maybe we can speed up things by
changing the order in which operations are executed.
"""
ds_out = get_ds_empty(ds)
ds_out = get_ds_empty(ds, keep_coords=("y", "x"))

# no bathymetry if we don't have northsea
if (northsea == 0).all():
Expand Down
2 changes: 1 addition & 1 deletion nlmod/read/knmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get_recharge(ds, method="linear", most_common_station=False):
start = pd.Timestamp(ds.time.attrs["start"])
end = pd.Timestamp(ds.time.data[-1])

ds_out = util.get_ds_empty(ds)
ds_out = util.get_ds_empty(ds, keep_coords=("time", "y", "x"))
ds_out.attrs["gridtype"] = ds.gridtype

# get recharge data array
Expand Down
4 changes: 2 additions & 2 deletions nlmod/read/rws.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get_surface_water(ds, da_basename):
peil = xr.where(area_pol > area, row["peil"], peil)
area = xr.where(area_pol > area, area_pol, area)

ds_out = util.get_ds_empty(ds)
ds_out = util.get_ds_empty(ds, keep_coords=("y", "x"))
ds_out[f"{da_basename}_area"] = area
ds_out[f"{da_basename}_area"].attrs["units"] = "m2"
ds_out[f"{da_basename}_cond"] = cond
Expand Down Expand Up @@ -127,7 +127,7 @@ def get_northsea(ds, da_name="northsea"):
)
]

ds_out = dims.gdf_to_bool_ds(swater_zee, ds, da_name)
ds_out = dims.gdf_to_bool_ds(swater_zee, ds, da_name, keep_coords=("y", "x"))

return ds_out

Expand Down
14 changes: 10 additions & 4 deletions nlmod/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,29 @@ def get_exe_path(exe_name="mf6"):
return exe_path


def get_ds_empty(ds):
"""get a copy of a model dataset with only coordinate information.
def get_ds_empty(ds, keep_coords=None):
"""get a copy of a dataset with only coordinate information.
Parameters
----------
ds : xr.Dataset
dataset with coordinates
keep_coords : tuple or None, optional
the coordinates in ds the you want keep in your empty ds. If None all
coordinates are kept from original ds. The default is None.
Returns
-------
empty_ds : xr.Dataset
dataset with only model coordinate information
dataset with only coordinate information
"""
if keep_coords is None:
keep_coords = list(ds.coords)

empty_ds = xr.Dataset()
for coord in list(ds.coords):
empty_ds = empty_ds.assign_coords(coords={coord: ds[coord]})
if coord in keep_coords:
empty_ds = empty_ds.assign_coords(coords={coord: ds[coord]})

return empty_ds

Expand Down

0 comments on commit 2813914

Please sign in to comment.