Skip to content

Commit

Permalink
Fix comments; improved reading PCR files
Browse files Browse the repository at this point in the history
  • Loading branch information
dalmijn committed Nov 15, 2023
1 parent 0326187 commit aba6f18
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/build_sediment.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.11.6"
},
"vscode": {
"interpreter": {
Expand Down
14 changes: 9 additions & 5 deletions examples/convert_staticmaps_to_mapstack.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "d480af50",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -84,7 +84,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "07e4d5b7",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -255,7 +255,8 @@
"outputs": [],
"source": [
"# save the staticmaps to PCRaster mapstacks and update them manually where needed\n",
"mod.write_staticmaps_pcr()"
"from hydromt_wflow.pcrm import write_staticmaps_pcr\n",
"write_staticmaps_pcr(mod.staticmaps, root=root)"
]
},
{
Expand All @@ -273,10 +274,13 @@
"metadata": {},
"outputs": [],
"source": [
"# Import read method for PCRaster files\n",
"from hydromt_wflow.pcrm import read_staticmaps_pcr\n",
"# read the updated staticmaps\n",
"root = \"wflow_piave_subbasin\"\n",
"mod = WflowModel(root, mode=\"r\")\n",
"mod.read_staticmaps_pcr()\n",
"staticmaps = read_staticmaps_pcr(root)\n",
"mod.set_grid(staticmaps)\n",
"mod.read_config()"
]
},
Expand Down Expand Up @@ -324,7 +328,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down
6 changes: 5 additions & 1 deletion examples/wflow_extend_sediment.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[global]
data_libs = [] # add optional paths to data yml files

[setup_config]
[setup_config] # options parsed to wflow ini file <section>.<option>
starttime = 2010-02-02T00:00:00
endtime = 2010-02-10T00:00:00
timestepsecs = 86400
input.path_forcing = inmaps-era5-2010.nc
# model.dolake = True # uncomment if there are lakes in the wflow model
# model.doreservoir = True # uncomment if there are reservoirs in the wflow model

Expand Down
19 changes: 10 additions & 9 deletions hydromt_wflow/pcrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ def read_staticmaps_pcr(
root: Path | str, crs: int = 4326, obj: object = None, **kwargs
):
"""Read and staticmaps at <root/staticmaps> and parse to xarray."""
da_lai = None
da = None

fn = join(root, "staticmaps", "*.map")
Expand All @@ -198,16 +197,14 @@ def read_staticmaps_pcr(
logger.warning(f"No staticmaps found at {fn}")
return
_staticmaps = open_mfraster(fns, **kwargs)
# add maps to staticmaps
obj.set_grid(_staticmaps)

path = join(root, "staticmaps", "clim", "LAI*")
if len(glob.glob(path)) > 0:
da_lai = open_mfraster(
ds_lai = open_mfraster(
path, concat=True, concat_dim="time", logger=logger, **kwargs
)
if obj is not None:
obj.set_grid(da_lai, "LAI")
lai_key = list(ds_lai.data_vars)[0]
_staticmaps["LAI"] = ds_lai[lai_key]

# reorganize c_0 etc maps
da_c = []
Expand All @@ -218,16 +215,20 @@ def read_staticmaps_pcr(
da = xr.concat(
da_c, pd.Index(np.arange(len(list_c), dtype=int), name="layer")
).transpose("layer", ...)
if obj is not None:
obj.set_grid(da, "c")
_staticmaps = _staticmaps.drop_vars(list_c)
_staticmaps["c"] = da

_staticmaps = _staticmaps.rename({"x": "lon", "y": "lat"})

# add maps to staticmaps
if obj is not None:
obj.set_grid(_staticmaps)
if obj.crs is None:
if crs is None:
crs = 4326 # default to 4326
obj.set_crs(crs)

return da, da_lai
return _staticmaps


def write_staticmaps_pcr(
Expand Down

0 comments on commit aba6f18

Please sign in to comment.