From bae546ff464bdc0e3e3cc64f337591bbdf1b37d1 Mon Sep 17 00:00:00 2001 From: hboisgon Date: Fri, 29 Sep 2023 11:16:32 +0800 Subject: [PATCH] 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"],