Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove clip from cli #1063

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions docs/api/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ Command Line Interface
:prog: hydromt update
:nested: full

.. _clip_api:

.. click:: hydromt.cli.main:clip
:prog: hydromt clip
:nested: full

.. _export_api:

Expand Down
66 changes: 0 additions & 66 deletions docs/guides/advanced_user/model_clip.rst

This file was deleted.

4 changes: 2 additions & 2 deletions docs/guides/core_dev/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ should be created with model-specific data readers, writers and setup methods as
by arguments which end with ``_fn`` (short for filename) which refer to a source in the data catalog
based on the source name or a file based on the (relative) path to the file. Within a model method the data is read
by calling any ``DataCatalog.get_<data_type>`` method which work for both source and file names.
- The Model class currently contains three high-level methods (:py:meth:`~hydromt.Model.build`,
:py:meth:`~hydromt.Model.update` and :py:meth:`~hydromt.Model.clip` which are common for all model plugins and
- The Model class currently contains two high-level methods (:py:meth:`~hydromt.Model.build`,
:py:meth:`~hydromt.Model.update` is are common for all model plugins and
exposed through the CLI. This list of methods might be extended going forward.
- A Model child class implementation for a specific model kernel can be exposed to HydroMT as a plugin by specifying a
``hydromt.models`` `entry-point <https://packaging.python.org/en/latest/specifications/entry-points/>`_ in the pyproject.toml file of a package.
Expand Down
3 changes: 1 addition & 2 deletions docs/guides/user_guide/hydromt_intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ Finally user can interact with HydroMT through the following interfaces:
- **Command Line Interface (CLI)**

The :ref:`CLI <hydromt_cli>` is a high-level interface to HydroMT. It is used to run HydroMT commands such as
:ref:`build <model_build>`, :ref:`update <model_update>` or :ref:`clip <model_clip>`.

:ref:`build <model_build>`, :ref:`update <model_update>`
- **Python Interface**

While most common functionalities can be called through the CLI, the :ref:`Python interface <hydromt_python>` offers more flexibility for advanced users.
Expand Down
4 changes: 0 additions & 4 deletions docs/guides/user_guide/model_overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ HydroMT has the following high-level functionality for setting up models from ra

* :ref:`building a model <model_build>`: building a model from scratch.
* :ref:`updating a model <model_update>`: adding or changing model components of an existing model.
* :ref:`clipping a model <model_clip>`: changing the spatial domain of an existing model (e.g. select subbasins from a larger model).

The building and clipping methods required the user to provide a :ref:`region <region>` of interest. HydroMT provides
several options to define a region based on a geospatial or hydrographic region.

The exact process of building or updating a model can be configured in a single configuration :ref:`.yaml file <model_workflow>`.
This file describes the full pipeline of model methods and their arguments. The methods vary for the
Expand Down
4 changes: 2 additions & 2 deletions docs/guides/user_guide/terminology.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ HydroMT and this documentation use a specific terminology to describe specific o
============================== ======================================================================================
Term Explanation
============================== ======================================================================================
Command Line Interface (CLI) high-level interface to HydroMT *build*, *update*, *clip*, *check* and *export* methods.
Command Line Interface (CLI) high-level interface to HydroMT *build*, *update*, *check* and *export* methods.
Configuration (HydroMT) (.yaml) file describing the complete pipeline with all methods and their arguments to
*build* or *update* a model.
Data catalog A set of data sources available for HydroMT. It is build up from *yaml* files containing
Expand All @@ -33,6 +33,6 @@ Model plugin (Plugin) Package that links the HydroMT Model cl
Plugins are installed separately from HydroMT and are not part of the HydroMT core package.
Plugins are the most common way of using HydroMT to build and update specific models.
Model kernel The model software to execute a model simulation. This is *not* part of any HydroMT plugin.
Region Argument of the *build* and *clip* CLI methods that specifies the region of interest where
Region Argument of the *build*CLI methods that specifies the region of interest where
the model should be prepared / which spatial subregion should be clipped.
============================== ======================================================================================
3 changes: 1 addition & 2 deletions docs/overview/use_case_examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ HydroMT was used to prepare the updated national hydrologic models for Indonesia
Cooperation Program between the Netherlands and Indonesia. As this is a large scale domain, seven models corresponding
to the different Indonesian regions (BaliNusaTenggara, Java, Kalimantan, Maluku, Papua, Sumatra, Sulawesi) were derived
at three different resolutions and integrated to the IWRM Indonesia Instrument in Delft-FEWS (using a new export to FEWS
function from HydroMT). The clipping functionality of HydroMT can also be used to prepare sub-models for use in specific
projects and to connect to existing RIBASIM models (using RIBASIM components within HydroMT).
function from HydroMT).

.. image:: ../_static/wflow_indonesia.png

Expand Down
67 changes: 7 additions & 60 deletions examples/working_with_models_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"\n",
"- **building** a model: building a model from scratch.\n",
"- **updating** a model: adding or changing model components of an existing model.\n",
"- **clipping** a model: changing the spatial domain of an existing model (e.g. select subbasins from a larger model).\n",
"\n",
"Here we show how to build and update a hypothetical distributed model from the command line interface (CLI) based on the generic HydroMT **Model**."
]
Expand All @@ -32,79 +31,27 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Usage: hydromt [OPTIONS] COMMAND [ARGS]...\n",
"\n",
" Command line interface for hydromt models.\n",
"\n",
"Options:\n",
" --version Show the version and exit.\n",
" --models Print available model plugins and exit.\n",
" --components Print available component plugins and exit.\n",
" --plugins Print available component plugins and exit.\n",
" --help Show this message and exit.\n",
"\n",
"Commands:\n",
" build Build models\n",
" check Validate config / data catalog / region\n",
" clip Clip models.\n",
" export Export data\n",
" update Update models\n",
"\u001b[0m"
]
}
],
"outputs": [],
"source": [
"!hydromt --help"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model plugins:\n",
"\t- model (hydromt 1.0.0a0)\n",
"\u001b[0m"
]
}
],
"outputs": [],
"source": [
"!hydromt --models"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Component plugins:\n",
"\t- ConfigComponent (hydromt 1.0.0a0)\n",
"\t- DatasetsComponent (hydromt 1.0.0a0)\n",
"\t- GeomsComponent (hydromt 1.0.0a0)\n",
"\t- GridComponent (hydromt 1.0.0a0)\n",
"\t- MeshComponent (hydromt 1.0.0a0)\n",
"\t- SpatialDatasetsComponent (hydromt 1.0.0a0)\n",
"\t- TablesComponent (hydromt 1.0.0a0)\n",
"\t- VectorComponent (hydromt 1.0.0a0)\n",
"\u001b[0m"
]
}
],
"outputs": [],
"source": [
"!hydromt --components"
]
Expand Down Expand Up @@ -142,7 +89,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.9.19"
},
"vscode": {
"interpreter": {
Expand Down
67 changes: 0 additions & 67 deletions hydromt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,72 +598,5 @@ def export(
log._wait_and_remove_file_handlers(logger) # Release locks on logs


## CLIP


@main.command(short_help="Clip models.")
@click.argument(
"MODEL",
type=str,
)
@arg_root
@click.argument(
"MODEL_DESTINATION",
type=click.Path(resolve_path=True, dir_okay=True, file_okay=False),
)
@click.argument(
"REGION",
type=str,
callback=_utils.parse_json,
)
@quiet_opt
@verbose_opt
@click.pass_context
def clip(ctx, model, model_root, model_destination, region, quiet, verbose):
"""Create a new model based on clipped region of an existing model.

If the existing model contains forcing, they will also be clipped to the new model.

Example usage:
--------------

Example usage to clip a wflow model for a subbasin derived from point coordinates
snapped to cells with upstream area >= 50 km2
hydromt clip wflow /path/to/model_root /path/to/model_destination "{'subbasin': [-7.24, 62.09], 'wflow_uparea': 50}"

Example usage basin based on ID from model_root basins map
hydromt clip wflow /path/to/model_root /path/to/model_destination "{'basin': 1}"

Example usage basins whose outlets are inside a geometry
hydromt clip wflow /path/to/model_root /path/to/model_destination "{'outlet': 'geometry.geojson'}"

All available option in the clip_grid function help.

""" # noqa: E501
log_level = max(10, 30 - 10 * (verbose - quiet))
log._setuplog(join(model_destination, "hydromt-clip.log"), log_level=log_level)
logger.info(f"Clipping instance of {model} model.")
logger.info(f"Region: {region}")

if model != "wflow":
raise NotImplementedError("Clip function only implemented for wflow model.")
try:
mod = PLUGINS.model_plugins[model](root=model_root, mode="r")
logger.info("Reading model to clip")
mod.read()
mod.root.set(model_destination, mode="w")
logger.info("Clipping grid")
mod.clip_grid(region)
logger.info("Clipping forcing")
mod.clip_forcing()
logger.info("Writting clipped model")
mod.write()
except Exception as e:
logger.exception(e) # catch and log errors
raise
finally:
log._wait_and_remove_file_handlers(logger) # Release locks on logs


if __name__ == "__main__":
main()
18 changes: 0 additions & 18 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ def test_cli_update_help():
assert r.output.startswith("Usage: main update [OPTIONS] MODEL MODEL_ROOT")


def test_cli_clip_help():
r = CliRunner().invoke(hydromt_cli, ["clip", "--help"])
assert r.exit_code == 0
assert r.output.startswith(
"Usage: main clip [OPTIONS] MODEL MODEL_ROOT MODEL_DESTINATION"
)


@pytest.fixture()
def _reset_log_level() -> Generator[None, None, None]:
yield
Expand Down Expand Up @@ -116,16 +108,6 @@ def test_cli_update_unknown_model(tmpdir):
)


@pytest.mark.usefixtures("_reset_log_level")
def test_cli_clip_unknown_model(tmpdir):
with pytest.raises(NotImplementedError):
_ = CliRunner().invoke(
hydromt_cli,
["clip", "test_model", str(tmpdir), str(tmpdir), "{'bbox': [1,2,3,4]}"],
catch_exceptions=False,
)


@pytest.mark.usefixtures("_reset_log_level")
def test_export_cli_deltares_data(tmpdir):
r = CliRunner().invoke(
Expand Down
Loading