From a07125b3141c0e451397d4eb17bc3db614e7f725 Mon Sep 17 00:00:00 2001 From: hboisgon Date: Fri, 29 Sep 2023 11:02:46 +0800 Subject: [PATCH 1/6] overwrite model in update mode #524 --- hydromt/models/model_api.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/hydromt/models/model_api.py b/hydromt/models/model_api.py index 4c5b4e6eb..844fc7726 100644 --- a/hydromt/models/model_api.py +++ b/hydromt/models/model_api.py @@ -299,7 +299,10 @@ def update( '"model_out" directory required when updating in "read-only" mode' ) self.read() - self.set_root(model_out, mode="w") + if forceful_overwrite: + self.set_root(model_out, mode="w+") + else: + self.set_root(model_out, mode="w") # check if model has a region if self.region is None: From bae546ff464bdc0e3e3cc64f337591bbdf1b37d1 Mon Sep 17 00:00:00 2001 From: hboisgon Date: Fri, 29 Sep 2023 11:16:32 +0800 Subject: [PATCH 2/6] rename generic setup methods #475 --- docs/api.rst | 12 ++++++------ docs/dev/plugin-quickstart.rst | 4 ++-- examples/grid_model_build.yaml | 12 ++++++------ examples/working_with_models.ipynb | 6 +++--- examples/working_with_models_basics.ipynb | 10 +++++----- hydromt/models/model_grid.py | 10 +++++----- hydromt/models/model_mesh.py | 4 ++-- tests/test_model.py | 18 +++++++++--------- 8 files changed, 38 insertions(+), 38 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 66ec0347a..5297d975e 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -273,10 +273,10 @@ Setup methods GridModel.setup_maps_from_rasterdataset GridModel.setup_maps_from_raster_reclass GridModel.setup_grid - GridModel.setup_grid_from_constant - GridModel.setup_grid_from_rasterdataset - GridModel.setup_grid_from_raster_reclass - GridModel.setup_grid_from_geodataframe + GridModel.setup_grid_data_from_constant + GridModel.setup_grid_data_from_rasterdataset + GridModel.setup_grid_data_from_raster_reclass + GridModel.setup_grid_data_from_geodataframe .. _lumped_model_api: @@ -369,8 +369,8 @@ Setup methods MeshModel.setup_config MeshModel.setup_region MeshModel.setup_mesh2d - MeshModel.setup_mesh2d_from_rasterdataset - MeshModel.setup_mesh2d_from_raster_reclass + MeshModel.setup_mesh2d_data_from_rasterdataset + MeshModel.setup_mesh2d_data_from_raster_reclass MeshModel.setup_maps_from_rasterdataset MeshModel.setup_maps_from_raster_reclass diff --git a/docs/dev/plugin-quickstart.rst b/docs/dev/plugin-quickstart.rst index 12beb89f3..5db266283 100644 --- a/docs/dev/plugin-quickstart.rst +++ b/docs/dev/plugin-quickstart.rst @@ -59,14 +59,14 @@ Using HydroMT generic methods, for your user, the build configuration file would .. code-block:: yaml - setup_grid_from_rasterdataset: + setup_grid_data_from_rasterdataset: raster_fn: vito fill_method: nearest reproject_method: mode rename: vito: landuse - setup_grid_from_raster_reclass: + setup_grid_data_from_raster_reclass: raster_fn: vito reclass_table_fn: vito_reclass reclass_variables: diff --git a/examples/grid_model_build.yaml b/examples/grid_model_build.yaml index 5986ed381..7e7007bfe 100644 --- a/examples/grid_model_build.yaml +++ b/examples/grid_model_build.yaml @@ -10,13 +10,13 @@ setup_grid: basin_index_fn: merit_hydro_index hydrography_fn: merit_hydro -setup_grid_from_constant: +setup_grid_data_from_constant: constant: 0.01 name: c1 dtype: float32 nodata: -99.0 -setup_grid_from_rasterdataset: +setup_grid_data_from_rasterdataset: raster_fn: merit_hydro_1k variables: - elevtn @@ -25,14 +25,14 @@ setup_grid_from_rasterdataset: - average - mode -setup_grid_from_rasterdataset2: +setup_grid_data_from_rasterdataset2: raster_fn: vito fill_method: nearest reproject_method: mode rename: vito: landuse -setup_grid_from_raster_reclass: +setup_grid_data_from_raster_reclass: raster_fn: vito reclass_table_fn: vito_reclass reclass_variables: @@ -40,7 +40,7 @@ setup_grid_from_raster_reclass: reproject_method: - average -setup_grid_from_geodataframe: +setup_grid_data_from_geodataframe: vector_fn: hydro_lakes variables: - waterbody_id @@ -53,7 +53,7 @@ setup_grid_from_geodataframe: waterbody_id: lake_id Detph_avg: lake_depth -setup_grid_from_geodataframe2: +setup_grid_data_from_geodataframe2: vector_fn: hydro_lakes rasterize_method: fraction rename: diff --git a/examples/working_with_models.ipynb b/examples/working_with_models.ipynb index 5a9a2ac65..ebafe54c0 100644 --- a/examples/working_with_models.ipynb +++ b/examples/working_with_models.ipynb @@ -267,7 +267,7 @@ "source": [ "We can setup maps data using the ``setup_maps_from_raster`` and ``setup_maps_from_raster_reclass`` methods. Both methods add data to the **maps** component based on input raster data (RasterDataset type), but the second method additionally reclassifies the input data based on a reclassification table. The **maps** component gathers any raster input data without any requirements for a specific grid (CRS and resolution). It can contain, for example, direct model input data for models like Delft3D-FM that will interpolate input data on the fly to the model mesh, or auxiliary data that are not used by the model kernel but can be used by HydroMT to build the model (e.g. a gridded DEM), etc.\n", "\n", - "For models that require all their input data to be resampled to the exact computation grid (all raster at the same resolution and projection), then the input data would go into the **grid** component. The corresponding ``setup_grid_from_raster`` and ``setup_grid_from_raster_reclass`` functions for the **grid** components are also available.\n", + "For models that require all their input data to be resampled to the exact computation grid (all raster at the same resolution and projection), then the input data would go into the **grid** component. The corresponding ``setup_grid_data_from_raster`` and ``setup_grid_data_from_raster_reclass`` functions for the **grid** components are also available.\n", "\n", "But back to our example, let's add both a DEM map from the data source *merit_hydro_1k* and a manning roughness map based on reclassified landuse data from the *vito* dataset to our model grid object." ] @@ -278,14 +278,14 @@ "metadata": {}, "outputs": [], "source": [ - "mod.setup_grid_from_rasterdataset(\n", + "mod.setup_grid_data_from_rasterdataset(\n", " raster_fn=\"merit_hydro_1k\",\n", " variables=\"elevtn\",\n", " fill_method=None,\n", " reproject_method=\"bilinear\",\n", " rename={\"elevtn\": \"DEM\"},\n", ")\n", - "mod.setup_grid_from_raster_reclass(\n", + "mod.setup_grid_data_from_raster_reclass(\n", " raster_fn=\"vito\",\n", " fill_method=\"nearest\",\n", " reclass_table_fn=\"vito_reclass\", # Note: from local data catalog\n", diff --git a/examples/working_with_models_basics.ipynb b/examples/working_with_models_basics.ipynb index f3d5a4e33..a4bd47844 100644 --- a/examples/working_with_models_basics.ipynb +++ b/examples/working_with_models_basics.ipynb @@ -167,10 +167,10 @@ "source": [ "In this configuration, you see that we will prepare quite a lot of data for our grid model using some of the generic model methods for grid. We will prepare:\n", "\n", - "- A grid with constant values 0.01 using [setup_grid_from_constant](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_from_constant.html)\n", - "- A couple of grid based on reprojection of MERIT Hydro IHU (elevation, basins) and VITO (landuse) using [setup_grid_from_rasterdataset](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_from_rasterdataset.html). Note that to use the same method twice within the same configuration file, you can end the method name with a number.\n", - "- A grid generated by mapping roughness values to the land use classes in VITO using [setup_grid_from_raster_reclass](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_from_raster_reclass.html)\n", - "- A couple of lake properties including the fraction of the grid cells covered by the lake geometry using [setup_grid_from_geodataframe](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_from_geodataframe.html)" + "- A grid with constant values 0.01 using [setup_grid_data_from_constant](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_data_from_constant.html)\n", + "- A couple of grid based on reprojection of MERIT Hydro IHU (elevation, basins) and VITO (landuse) using [setup_grid_data_from_rasterdataset](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_data_from_rasterdataset.html). Note that to use the same method twice within the same configuration file, you can end the method name with a number.\n", + "- A grid generated by mapping roughness values to the land use classes in VITO using [setup_grid_data_from_raster_reclass](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_data_from_raster_reclass.html)\n", + "- A couple of lake properties including the fraction of the grid cells covered by the lake geometry using [setup_grid_data_from_geodataframe](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_data_from_geodataframe.html)" ] }, { @@ -297,7 +297,7 @@ "outputs": [], "source": [ "%%writefile ./tmp_grid_model1/grid_model_update.yaml\n", - "setup_grid_from_rasterdataset:\n", + "setup_grid_data_from_rasterdataset:\n", " raster_fn: merit_hydro_1k\n", " variables: uparea\n", " reproject_method: max\n", diff --git a/hydromt/models/model_grid.py b/hydromt/models/model_grid.py index d3f799485..3f8b6ed96 100644 --- a/hydromt/models/model_grid.py +++ b/hydromt/models/model_grid.py @@ -31,7 +31,7 @@ def __init__(self, *args, **kwargs) -> None: self._grid = None # xr.Dataset() # generic grid methods - def setup_grid_from_constant( + def setup_grid_data_from_constant( self, constant: Union[int, float], name: str, @@ -73,7 +73,7 @@ def setup_grid_from_constant( return [name] - def setup_grid_from_rasterdataset( + def setup_grid_data_from_rasterdataset( self, raster_fn: Union[str, Path, xr.DataArray, xr.Dataset], variables: Optional[List] = None, @@ -143,7 +143,7 @@ def setup_grid_from_rasterdataset( return list(ds_out.data_vars.keys()) - def setup_grid_from_raster_reclass( + def setup_grid_data_from_raster_reclass( self, raster_fn: Union[str, Path, xr.DataArray], reclass_table_fn: Union[str, Path, pd.DataFrame], @@ -231,7 +231,7 @@ def setup_grid_from_raster_reclass( return list(ds_vars.data_vars.keys()) - def setup_grid_from_geodataframe( + def setup_grid_data_from_geodataframe( self, vector_fn: Union[str, Path, gpd.GeoDataFrame], variables: Optional[Union[List, str]] = None, @@ -291,7 +291,7 @@ def setup_grid_from_geodataframe( if gdf.empty: self.logger.warning( f"No shapes of {vector_fn} found within region," - " skipping setup_grid_from_vector." + " skipping setup_grid_data_from_geodataframe." ) return # Data resampling diff --git a/hydromt/models/model_mesh.py b/hydromt/models/model_mesh.py index 0037a98b8..b68171011 100644 --- a/hydromt/models/model_mesh.py +++ b/hydromt/models/model_mesh.py @@ -33,7 +33,7 @@ def __init__(self, *args, **kwargs) -> None: self._mesh = None ## general setup methods - def setup_mesh2d_from_rasterdataset( + def setup_mesh2d_data_from_rasterdataset( self, raster_fn: Union[str, Path, xr.DataArray, xr.Dataset], grid_name: Optional[str] = "mesh2d", @@ -112,7 +112,7 @@ def setup_mesh2d_from_rasterdataset( return list(ds_sample.data_vars.keys()) - def setup_mesh2d_from_raster_reclass( + def setup_mesh2d_data_from_raster_reclass( self, raster_fn: Union[str, Path, xr.DataArray], reclass_table_fn: Union[str, Path, pd.DataFrame], diff --git a/tests/test_model.py b/tests/test_model.py index f9c77a102..104786430 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -524,44 +524,44 @@ def test_gridmodel_setup(tmpdir): add_mask=True, ) # Add data with setup_* methods - mod.setup_grid_from_constant( + mod.setup_grid_data_from_constant( constant=0.01, name="c1", nodata=-99.0, ) - mod.setup_grid_from_constant( + mod.setup_grid_data_from_constant( constant=2, name="c2", dtype=np.int8, nodata=-1, ) - mod.setup_grid_from_rasterdataset( + mod.setup_grid_data_from_rasterdataset( raster_fn="merit_hydro", variables=["elevtn", "basins"], reproject_method=["average", "mode"], mask_name="mask", ) - mod.setup_grid_from_rasterdataset( + mod.setup_grid_data_from_rasterdataset( raster_fn="vito", fill_method="nearest", reproject_method="mode", rename={"vito": "landuse"}, ) - mod.setup_grid_from_raster_reclass( + mod.setup_grid_data_from_raster_reclass( raster_fn="vito", fill_method="nearest", reclass_table_fn="vito_mapping", reclass_variables=["roughness_manning"], reproject_method=["average"], ) - mod.setup_grid_from_geodataframe( + mod.setup_grid_data_from_geodataframe( vector_fn="hydro_lakes", variables=["waterbody_id", "Depth_avg"], nodata=[-1, -999.0], rasterize_method="value", rename={"waterbody_id": "lake_id", "Depth_avg": "lake_depth"}, ) - mod.setup_grid_from_geodataframe( + mod.setup_grid_data_from_geodataframe( vector_fn="hydro_lakes", rasterize_method="fraction", rename={"hydro_lakes": "water_frac"}, @@ -712,9 +712,9 @@ def test_meshmodel_setup(griduda, world): region = {"mesh": griduda} mod1 = MeshModel(data_libs=["artifact_data", dc_param_fn]) mod1.setup_mesh2d(region, grid_name="mesh2d") - mod1.setup_mesh2d_from_rasterdataset("vito", grid_name="mesh2d") + mod1.setup_mesh2d_data_from_rasterdataset("vito", grid_name="mesh2d") assert "vito" in mod1.mesh.data_vars - mod1.setup_mesh2d_from_raster_reclass( + mod1.setup_mesh2d_data_from_raster_reclass( raster_fn="vito", reclass_table_fn="vito_mapping", reclass_variables=["roughness_manning"], From de46d5415b2aa2bc5376cce25ec6fb9f8e428796 Mon Sep 17 00:00:00 2001 From: hboisgon Date: Fri, 29 Sep 2023 11:30:28 +0800 Subject: [PATCH 3/6] set_forcing with pd.DataFrame #498 --- hydromt/models/model_api.py | 11 +++++++++-- tests/test_model.py | 3 +++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/hydromt/models/model_api.py b/hydromt/models/model_api.py index 844fc7726..e23ff78dc 100644 --- a/hydromt/models/model_api.py +++ b/hydromt/models/model_api.py @@ -1338,21 +1338,28 @@ def forcing(self) -> Dict[str, Union[xr.Dataset, xr.DataArray]]: def set_forcing( self, - data: Union[xr.DataArray, xr.Dataset], + data: Union[xr.DataArray, xr.Dataset, pd.DataFrame], name: Optional[str] = None, split_dataset: Optional[bool] = True, ): """Add data to forcing attribute. + Data can be xarray.DataArray, xarray.Dataset or pandas.DataFrame. + If pandas.DataFrame, indices should be the DataFrame index and the columns + the variable names. the DataFrame will then be converted to xr.Dataset using + :py:meth:`pandas.DataFrame.to_xarray` method. + Arguments --------- - data: xarray.Dataset or xarray.DataArray + data: xarray.Dataset or xarray.DataArray or pd.DataFrame New forcing data to add name: str, optional Results name, required if data is xarray.Dataset is and split_dataset=False. split_dataset: bool, optional If True (default), split a Dataset to store each variable as a DataArray. """ + if isinstance(data, pd.DataFrame): + data = data.to_xarray() data_dict = _check_data(data, name, split_dataset) for name in data_dict: if name in self.forcing: # trigger init / read diff --git a/tests/test_model.py b/tests/test_model.py index 104786430..a4c523707 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -226,6 +226,9 @@ def test_model_append(demda, df, tmpdir): assert "dem" in mod1.maps mod1.set_forcing(demda, name="dem1") assert "dem" in mod1.forcing + mod1.set_forcing(df, name="df1", split_dataset=False) + assert "df1" in mod1.forcing + assert isinstance(mod1.forcing["df1"], xr.Dataset) mod1.set_states(demda, name="dem1") assert "dem" in mod1.states mod1.set_geoms(demda.raster.box, name="dem1") From 9ed8ce1e00f018e636657960cc3c799acd59bb61 Mon Sep 17 00:00:00 2001 From: hboisgon Date: Fri, 29 Sep 2023 11:34:33 +0800 Subject: [PATCH 4/6] update changelog --- docs/changelog.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index a766ba00d..fa963b4c1 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -28,10 +28,12 @@ Added - Added documentation for how to start your own plugin (PR #446) - New raster method ``to_slippy_tiles``: tiling of a raster dataset according to the slippy tile structure for e.g., webviewers (PR #440). - Support for http and other *filesystems* in path of data source (PR #515). +- ``set_forcing`` can now add pandas.DataFrame object to frocing. (PR #534) Changed ------- - Updated ``MeshModel`` and related methods to support multigrids instead of one single 2D grid. (PR #412) +- Updated generic setup grid and mesh2d functions names to distinguish better if they add data or create schematisation. (PR #534) - possibility to ``load`` the data in the model read_ functions for netcdf files (default for read_grid in r+ mode). (PR #460) - Internal model components (e.g. `Models._maps`, `GridModel._grid``) are now initialized with None and should not be accessed directly, call the corresponding model property (e.g. `Model.maps`, `GridModel.grid`) instead. (PR #473) @@ -41,6 +43,7 @@ Fixed ----- - when a model component (eg maps, forcing, grid) is updated using the set_ methods, it will first be read to avoid loosing data. (PR #460) - open_geodataset with driver vector also works for other geometry type than points. (PR #509) +- overwrite model in update mode. (PR #534) Deprecated ---------- From 88ff9299b5a778a414a0b9bf746ec7d55c01913a Mon Sep 17 00:00:00 2001 From: hboisgon Date: Fri, 29 Sep 2023 17:35:49 +0800 Subject: [PATCH 5/6] Revert "rename generic setup methods #475" This reverts commit bae546ff464bdc0e3e3cc64f337591bbdf1b37d1. --- docs/api.rst | 12 ++++++------ docs/dev/plugin-quickstart.rst | 4 ++-- examples/grid_model_build.yaml | 12 ++++++------ examples/working_with_models.ipynb | 6 +++--- examples/working_with_models_basics.ipynb | 10 +++++----- hydromt/models/model_grid.py | 10 +++++----- hydromt/models/model_mesh.py | 4 ++-- tests/test_model.py | 18 +++++++++--------- 8 files changed, 38 insertions(+), 38 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 5297d975e..66ec0347a 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -273,10 +273,10 @@ Setup methods GridModel.setup_maps_from_rasterdataset GridModel.setup_maps_from_raster_reclass GridModel.setup_grid - GridModel.setup_grid_data_from_constant - GridModel.setup_grid_data_from_rasterdataset - GridModel.setup_grid_data_from_raster_reclass - GridModel.setup_grid_data_from_geodataframe + GridModel.setup_grid_from_constant + GridModel.setup_grid_from_rasterdataset + GridModel.setup_grid_from_raster_reclass + GridModel.setup_grid_from_geodataframe .. _lumped_model_api: @@ -369,8 +369,8 @@ Setup methods MeshModel.setup_config MeshModel.setup_region MeshModel.setup_mesh2d - MeshModel.setup_mesh2d_data_from_rasterdataset - MeshModel.setup_mesh2d_data_from_raster_reclass + MeshModel.setup_mesh2d_from_rasterdataset + MeshModel.setup_mesh2d_from_raster_reclass MeshModel.setup_maps_from_rasterdataset MeshModel.setup_maps_from_raster_reclass diff --git a/docs/dev/plugin-quickstart.rst b/docs/dev/plugin-quickstart.rst index 5db266283..12beb89f3 100644 --- a/docs/dev/plugin-quickstart.rst +++ b/docs/dev/plugin-quickstart.rst @@ -59,14 +59,14 @@ Using HydroMT generic methods, for your user, the build configuration file would .. code-block:: yaml - setup_grid_data_from_rasterdataset: + setup_grid_from_rasterdataset: raster_fn: vito fill_method: nearest reproject_method: mode rename: vito: landuse - setup_grid_data_from_raster_reclass: + setup_grid_from_raster_reclass: raster_fn: vito reclass_table_fn: vito_reclass reclass_variables: diff --git a/examples/grid_model_build.yaml b/examples/grid_model_build.yaml index 7e7007bfe..5986ed381 100644 --- a/examples/grid_model_build.yaml +++ b/examples/grid_model_build.yaml @@ -10,13 +10,13 @@ setup_grid: basin_index_fn: merit_hydro_index hydrography_fn: merit_hydro -setup_grid_data_from_constant: +setup_grid_from_constant: constant: 0.01 name: c1 dtype: float32 nodata: -99.0 -setup_grid_data_from_rasterdataset: +setup_grid_from_rasterdataset: raster_fn: merit_hydro_1k variables: - elevtn @@ -25,14 +25,14 @@ setup_grid_data_from_rasterdataset: - average - mode -setup_grid_data_from_rasterdataset2: +setup_grid_from_rasterdataset2: raster_fn: vito fill_method: nearest reproject_method: mode rename: vito: landuse -setup_grid_data_from_raster_reclass: +setup_grid_from_raster_reclass: raster_fn: vito reclass_table_fn: vito_reclass reclass_variables: @@ -40,7 +40,7 @@ setup_grid_data_from_raster_reclass: reproject_method: - average -setup_grid_data_from_geodataframe: +setup_grid_from_geodataframe: vector_fn: hydro_lakes variables: - waterbody_id @@ -53,7 +53,7 @@ setup_grid_data_from_geodataframe: waterbody_id: lake_id Detph_avg: lake_depth -setup_grid_data_from_geodataframe2: +setup_grid_from_geodataframe2: vector_fn: hydro_lakes rasterize_method: fraction rename: diff --git a/examples/working_with_models.ipynb b/examples/working_with_models.ipynb index ebafe54c0..5a9a2ac65 100644 --- a/examples/working_with_models.ipynb +++ b/examples/working_with_models.ipynb @@ -267,7 +267,7 @@ "source": [ "We can setup maps data using the ``setup_maps_from_raster`` and ``setup_maps_from_raster_reclass`` methods. Both methods add data to the **maps** component based on input raster data (RasterDataset type), but the second method additionally reclassifies the input data based on a reclassification table. The **maps** component gathers any raster input data without any requirements for a specific grid (CRS and resolution). It can contain, for example, direct model input data for models like Delft3D-FM that will interpolate input data on the fly to the model mesh, or auxiliary data that are not used by the model kernel but can be used by HydroMT to build the model (e.g. a gridded DEM), etc.\n", "\n", - "For models that require all their input data to be resampled to the exact computation grid (all raster at the same resolution and projection), then the input data would go into the **grid** component. The corresponding ``setup_grid_data_from_raster`` and ``setup_grid_data_from_raster_reclass`` functions for the **grid** components are also available.\n", + "For models that require all their input data to be resampled to the exact computation grid (all raster at the same resolution and projection), then the input data would go into the **grid** component. The corresponding ``setup_grid_from_raster`` and ``setup_grid_from_raster_reclass`` functions for the **grid** components are also available.\n", "\n", "But back to our example, let's add both a DEM map from the data source *merit_hydro_1k* and a manning roughness map based on reclassified landuse data from the *vito* dataset to our model grid object." ] @@ -278,14 +278,14 @@ "metadata": {}, "outputs": [], "source": [ - "mod.setup_grid_data_from_rasterdataset(\n", + "mod.setup_grid_from_rasterdataset(\n", " raster_fn=\"merit_hydro_1k\",\n", " variables=\"elevtn\",\n", " fill_method=None,\n", " reproject_method=\"bilinear\",\n", " rename={\"elevtn\": \"DEM\"},\n", ")\n", - "mod.setup_grid_data_from_raster_reclass(\n", + "mod.setup_grid_from_raster_reclass(\n", " raster_fn=\"vito\",\n", " fill_method=\"nearest\",\n", " reclass_table_fn=\"vito_reclass\", # Note: from local data catalog\n", diff --git a/examples/working_with_models_basics.ipynb b/examples/working_with_models_basics.ipynb index a4bd47844..f3d5a4e33 100644 --- a/examples/working_with_models_basics.ipynb +++ b/examples/working_with_models_basics.ipynb @@ -167,10 +167,10 @@ "source": [ "In this configuration, you see that we will prepare quite a lot of data for our grid model using some of the generic model methods for grid. We will prepare:\n", "\n", - "- A grid with constant values 0.01 using [setup_grid_data_from_constant](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_data_from_constant.html)\n", - "- A couple of grid based on reprojection of MERIT Hydro IHU (elevation, basins) and VITO (landuse) using [setup_grid_data_from_rasterdataset](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_data_from_rasterdataset.html). Note that to use the same method twice within the same configuration file, you can end the method name with a number.\n", - "- A grid generated by mapping roughness values to the land use classes in VITO using [setup_grid_data_from_raster_reclass](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_data_from_raster_reclass.html)\n", - "- A couple of lake properties including the fraction of the grid cells covered by the lake geometry using [setup_grid_data_from_geodataframe](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_data_from_geodataframe.html)" + "- A grid with constant values 0.01 using [setup_grid_from_constant](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_from_constant.html)\n", + "- A couple of grid based on reprojection of MERIT Hydro IHU (elevation, basins) and VITO (landuse) using [setup_grid_from_rasterdataset](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_from_rasterdataset.html). Note that to use the same method twice within the same configuration file, you can end the method name with a number.\n", + "- A grid generated by mapping roughness values to the land use classes in VITO using [setup_grid_from_raster_reclass](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_from_raster_reclass.html)\n", + "- A couple of lake properties including the fraction of the grid cells covered by the lake geometry using [setup_grid_from_geodataframe](https://deltares.github.io/hydromt/latest/_generated/hydromt.GridModel.setup_grid_from_geodataframe.html)" ] }, { @@ -297,7 +297,7 @@ "outputs": [], "source": [ "%%writefile ./tmp_grid_model1/grid_model_update.yaml\n", - "setup_grid_data_from_rasterdataset:\n", + "setup_grid_from_rasterdataset:\n", " raster_fn: merit_hydro_1k\n", " variables: uparea\n", " reproject_method: max\n", diff --git a/hydromt/models/model_grid.py b/hydromt/models/model_grid.py index 3f8b6ed96..d3f799485 100644 --- a/hydromt/models/model_grid.py +++ b/hydromt/models/model_grid.py @@ -31,7 +31,7 @@ def __init__(self, *args, **kwargs) -> None: self._grid = None # xr.Dataset() # generic grid methods - def setup_grid_data_from_constant( + def setup_grid_from_constant( self, constant: Union[int, float], name: str, @@ -73,7 +73,7 @@ def setup_grid_data_from_constant( return [name] - def setup_grid_data_from_rasterdataset( + def setup_grid_from_rasterdataset( self, raster_fn: Union[str, Path, xr.DataArray, xr.Dataset], variables: Optional[List] = None, @@ -143,7 +143,7 @@ def setup_grid_data_from_rasterdataset( return list(ds_out.data_vars.keys()) - def setup_grid_data_from_raster_reclass( + def setup_grid_from_raster_reclass( self, raster_fn: Union[str, Path, xr.DataArray], reclass_table_fn: Union[str, Path, pd.DataFrame], @@ -231,7 +231,7 @@ def setup_grid_data_from_raster_reclass( return list(ds_vars.data_vars.keys()) - def setup_grid_data_from_geodataframe( + def setup_grid_from_geodataframe( self, vector_fn: Union[str, Path, gpd.GeoDataFrame], variables: Optional[Union[List, str]] = None, @@ -291,7 +291,7 @@ def setup_grid_data_from_geodataframe( if gdf.empty: self.logger.warning( f"No shapes of {vector_fn} found within region," - " skipping setup_grid_data_from_geodataframe." + " skipping setup_grid_from_vector." ) return # Data resampling diff --git a/hydromt/models/model_mesh.py b/hydromt/models/model_mesh.py index b68171011..0037a98b8 100644 --- a/hydromt/models/model_mesh.py +++ b/hydromt/models/model_mesh.py @@ -33,7 +33,7 @@ def __init__(self, *args, **kwargs) -> None: self._mesh = None ## general setup methods - def setup_mesh2d_data_from_rasterdataset( + def setup_mesh2d_from_rasterdataset( self, raster_fn: Union[str, Path, xr.DataArray, xr.Dataset], grid_name: Optional[str] = "mesh2d", @@ -112,7 +112,7 @@ def setup_mesh2d_data_from_rasterdataset( return list(ds_sample.data_vars.keys()) - def setup_mesh2d_data_from_raster_reclass( + def setup_mesh2d_from_raster_reclass( self, raster_fn: Union[str, Path, xr.DataArray], reclass_table_fn: Union[str, Path, pd.DataFrame], diff --git a/tests/test_model.py b/tests/test_model.py index a4c523707..c299d2cbf 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -527,44 +527,44 @@ def test_gridmodel_setup(tmpdir): add_mask=True, ) # Add data with setup_* methods - mod.setup_grid_data_from_constant( + mod.setup_grid_from_constant( constant=0.01, name="c1", nodata=-99.0, ) - mod.setup_grid_data_from_constant( + mod.setup_grid_from_constant( constant=2, name="c2", dtype=np.int8, nodata=-1, ) - mod.setup_grid_data_from_rasterdataset( + mod.setup_grid_from_rasterdataset( raster_fn="merit_hydro", variables=["elevtn", "basins"], reproject_method=["average", "mode"], mask_name="mask", ) - mod.setup_grid_data_from_rasterdataset( + mod.setup_grid_from_rasterdataset( raster_fn="vito", fill_method="nearest", reproject_method="mode", rename={"vito": "landuse"}, ) - mod.setup_grid_data_from_raster_reclass( + mod.setup_grid_from_raster_reclass( raster_fn="vito", fill_method="nearest", reclass_table_fn="vito_mapping", reclass_variables=["roughness_manning"], reproject_method=["average"], ) - mod.setup_grid_data_from_geodataframe( + mod.setup_grid_from_geodataframe( vector_fn="hydro_lakes", variables=["waterbody_id", "Depth_avg"], nodata=[-1, -999.0], rasterize_method="value", rename={"waterbody_id": "lake_id", "Depth_avg": "lake_depth"}, ) - mod.setup_grid_data_from_geodataframe( + mod.setup_grid_from_geodataframe( vector_fn="hydro_lakes", rasterize_method="fraction", rename={"hydro_lakes": "water_frac"}, @@ -715,9 +715,9 @@ def test_meshmodel_setup(griduda, world): region = {"mesh": griduda} mod1 = MeshModel(data_libs=["artifact_data", dc_param_fn]) mod1.setup_mesh2d(region, grid_name="mesh2d") - mod1.setup_mesh2d_data_from_rasterdataset("vito", grid_name="mesh2d") + mod1.setup_mesh2d_from_rasterdataset("vito", grid_name="mesh2d") assert "vito" in mod1.mesh.data_vars - mod1.setup_mesh2d_data_from_raster_reclass( + mod1.setup_mesh2d_from_raster_reclass( raster_fn="vito", reclass_table_fn="vito_mapping", reclass_variables=["roughness_manning"], From 095fd59b448c0ead78a58d6932d929bef9ae51cc Mon Sep 17 00:00:00 2001 From: hboisgon Date: Fri, 29 Sep 2023 17:37:01 +0800 Subject: [PATCH 6/6] update changelog --- docs/changelog.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index fa963b4c1..69ed4345e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -33,7 +33,6 @@ Added Changed ------- - Updated ``MeshModel`` and related methods to support multigrids instead of one single 2D grid. (PR #412) -- Updated generic setup grid and mesh2d functions names to distinguish better if they add data or create schematisation. (PR #534) - possibility to ``load`` the data in the model read_ functions for netcdf files (default for read_grid in r+ mode). (PR #460) - Internal model components (e.g. `Models._maps`, `GridModel._grid``) are now initialized with None and should not be accessed directly, call the corresponding model property (e.g. `Model.maps`, `GridModel.grid`) instead. (PR #473)