Skip to content

Commit

Permalink
rename generic setup methods #475
Browse files Browse the repository at this point in the history
  • Loading branch information
hboisgon committed Sep 29, 2023
1 parent a07125b commit bae546f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 38 deletions.
12 changes: 6 additions & 6 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/dev/plugin-quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions examples/grid_model_build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,22 +25,22 @@ 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:
- manning
reproject_method:
- average

setup_grid_from_geodataframe:
setup_grid_data_from_geodataframe:
vector_fn: hydro_lakes
variables:
- waterbody_id
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions examples/working_with_models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
]
Expand All @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions examples/working_with_models_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions hydromt/models/model_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions hydromt/models/model_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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],
Expand Down
18 changes: 9 additions & 9 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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"],
Expand Down

0 comments on commit bae546f

Please sign in to comment.