diff --git a/docs/advanced/workflows.rst b/docs/advanced/workflows.rst deleted file mode 100644 index e6fb04d8..00000000 --- a/docs/advanced/workflows.rst +++ /dev/null @@ -1,20 +0,0 @@ -.. currentmodule:: hydromt_wflow.workflows - -Workflows -========= - -Several workflows were created for the wflow plugin. They are: - -.. autosummary:: - :toctree: ../generated - :nosignatures: - - glaciers.glaciermaps - glaciers.glacierattrs - river.river - river.river_width - soilgrids.soilgrids - soilgrids.soilgrids_sediment - waterbodies.waterbodymaps - waterbodies.reservoirattrs - diff --git a/docs/api/api.rst b/docs/api/api.rst new file mode 100644 index 00000000..b074c135 --- /dev/null +++ b/docs/api/api.rst @@ -0,0 +1,244 @@ +.. currentmodule:: hydromt_wflow + +============= +API reference +============= + +.. _api_model: + +Wflow model class +================= + +Initialize +---------- + +.. autosummary:: + :toctree: ../generated/ + + WflowModel + +.. _components: + +Setup components +---------------- + +.. autosummary:: + :toctree: ../generated/ + + WflowModel.setup_config + WflowModel.setup_basemaps + WflowModel.setup_rivers + WflowModel.setup_lakes + WflowModel.setup_reservoirs + WflowModel.setup_glaciers + WflowModel.setup_lulcmaps + WflowModel.setup_laimaps + WflowModel.setup_soilmaps + WflowModel.setup_riverwidth + WflowModel.setup_gauges + WflowModel.setup_areamap + WflowModel.setup_precip_forcing + WflowModel.setup_temp_pet_forcing + WflowModel.setup_constant_pars + +Attributes +---------- + +.. autosummary:: + :toctree: ../generated/ + + WflowModel.region + WflowModel.crs + WflowModel.res + WflowModel.root + WflowModel.config + WflowModel.staticmaps + WflowModel.staticgeoms + WflowModel.forcing + WflowModel.states + WflowModel.results + WflowModel.flwdir + WflowModel.basins + WflowModel.rivers + +High level methods +------------------ + +.. autosummary:: + :toctree: ../generated/ + + WflowModel.read + WflowModel.write + WflowModel.build + WflowModel.update + WflowModel.set_root + +General methods +--------------- + +.. autosummary:: + :toctree: ../generated/ + + WflowModel.setup_config + WflowModel.get_config + WflowModel.set_config + WflowModel.read_config + WflowModel.write_config + + WflowModel.set_staticmaps + WflowModel.read_staticmaps + WflowModel.write_staticmaps + WflowModel.clip_staticmaps + + WflowModel.set_staticgeoms + WflowModel.read_staticgeoms + WflowModel.write_staticgeoms + + WflowModel.set_forcing + WflowModel.read_forcing + WflowModel.write_forcing + WflowModel.clip_forcing + + WflowModel.set_states + WflowModel.read_states + WflowModel.write_states + + WflowModel.set_results + WflowModel.read_results + + WflowModel.set_flwdir + +.. _api_model_sediment: + +WflowSediment model class +========================= + +Initialize +---------- + +.. autosummary:: + :toctree: ../generated/ + + WflowSedimentModel + +.. _components: + +Setup components +---------------- + +.. autosummary:: + :toctree: ../generated/ + + WflowSedimentModel.setup_config + WflowSedimentModel.setup_basemaps + WflowSedimentModel.setup_rivers + WflowSedimentModel.setup_lakes + WflowSedimentModel.setup_reservoirs + WflowSedimentModel.setup_lulcmaps + WflowSedimentModel.setup_laimaps + WflowSedimentModel.setup_canopymaps + WflowSedimentModel.setup_soilmaps + WflowSedimentModel.setup_riverwidth + WflowSedimentModel.setup_riverbedsed + WflowSedimentModel.setup_gauges + WflowSedimentModel.setup_areamap + WflowSedimentModel.setup_constant_pars + +Attributes +---------- + +.. autosummary:: + :toctree: ../generated/ + + WflowSedimentModel.region + WflowSedimentModel.crs + WflowSedimentModel.res + WflowSedimentModel.root + WflowSedimentModel.config + WflowSedimentModel.staticmaps + WflowSedimentModel.staticgeoms + WflowSedimentModel.forcing + WflowSedimentModel.states + WflowSedimentModel.results + WflowSedimentModel.flwdir + WflowSedimentModel.basins + WflowSedimentModel.rivers + +High level methods +------------------ + +.. autosummary:: + :toctree: ../generated/ + + WflowSedimentModel.read + WflowSedimentModel.write + WflowSedimentModel.build + WflowSedimentModel.update + WflowSedimentModel.set_root + +General methods +--------------- + +.. autosummary:: + :toctree: ../generated/ + + WflowSedimentModel.setup_config + WflowSedimentModel.get_config + WflowSedimentModel.set_config + WflowSedimentModel.read_config + WflowSedimentModel.write_config + + WflowSedimentModel.set_staticmaps + WflowSedimentModel.read_staticmaps + WflowSedimentModel.write_staticmaps + WflowSedimentModel.clip_staticmaps + + WflowSedimentModel.set_staticgeoms + WflowSedimentModel.read_staticgeoms + WflowSedimentModel.write_staticgeoms + + WflowSedimentModel.set_forcing + WflowSedimentModel.read_forcing + WflowSedimentModel.write_forcing + WflowSedimentModel.clip_forcing + + WflowSedimentModel.set_states + WflowSedimentModel.read_states + WflowSedimentModel.write_states + + WflowSedimentModel.set_results + WflowSedimentModel.read_results + + WflowSedimentModel.set_flwdir + +.. _workflows: + +Wflow workflows +=============== + +.. autosummary:: + :toctree: ../generated/ + + workflows.landuse + workflows.river + workflows.river_width + workflows.soilgrids + workflows.soilgrids_sediment + workflows.waterbodymaps + workflows.reservoirattrs + workflows.glaciermaps + workflows.glacierattrs + + +.. _methods: + +Wflow low-level methods +======================= + +Input/Output methods +--------------------- + +.. autosummary:: + :toctree: ../generated/ + + read_csv_results diff --git a/docs/api/api_index.rst b/docs/api/api_index.rst deleted file mode 100644 index 9e206eef..00000000 --- a/docs/api/api_index.rst +++ /dev/null @@ -1,12 +0,0 @@ -============= -API reference -============= - -This page provides an auto-generated summary of the hydroMT's plugin API for wflow model. For more details -and examples, refer to the relevant chapters in the main part of the documentation. - -.. toctree:: - :maxdepth: 2 - - Model - Workflows diff --git a/docs/api/api_model_index.rst b/docs/api/api_model_index.rst deleted file mode 100644 index 987bf4fa..00000000 --- a/docs/api/api_model_index.rst +++ /dev/null @@ -1,11 +0,0 @@ -====== -Models -====== - -Two models are implemented in the wflow plugin: - -.. toctree:: - :maxdepth: 2 - - Wflow Model - Wflow Sediment Model diff --git a/docs/api/api_sediment.rst b/docs/api/api_sediment.rst deleted file mode 100644 index a8dc96c1..00000000 --- a/docs/api/api_sediment.rst +++ /dev/null @@ -1,53 +0,0 @@ -.. currentmodule:: hydromt_wflow.wflow_sediment -.. _api_model_sediment: - -Wflow Sediment model -==================== - -Initialize ----------- - -.. autosummary:: - :toctree: ../generated/ - - WflowSedimentModel - -Build components ----------------- - -.. autosummary:: - :toctree: ../generated/ - - WflowSedimentModel.setup_config - WflowSedimentModel.setup_basemaps - WflowSedimentModel.setup_rivers - WflowSedimentModel.setup_lakes - WflowSedimentModel.setup_reservoirs - WflowSedimentModel.setup_lulcmaps - WflowSedimentModel.setup_laimaps - WflowSedimentModel.setup_canopymaps - WflowSedimentModel.setup_soilmaps - WflowSedimentModel.setup_riverwidth - WflowSedimentModel.setup_riverbedsed - WflowSedimentModel.setup_gauges - WflowSedimentModel.setup_constant_pars - -Model specific attributes -------------------------- - -.. autosummary:: - :toctree: ../generated/ - - WflowSedimentModel.flwdir - WflowSedimentModel.basins - WflowSedimentModel.rivers - -Model specific methods ----------------------- - -.. autosummary:: - :toctree: ../generated/ - - WflowSedimentModel.set_flwdir - WflowSedimentModel.clip_staticmaps - WflowSedimentModel.clip_forcing \ No newline at end of file diff --git a/docs/api/api_wflow.rst b/docs/api/api_wflow.rst deleted file mode 100644 index 12e21376..00000000 --- a/docs/api/api_wflow.rst +++ /dev/null @@ -1,54 +0,0 @@ -.. currentmodule:: hydromt_wflow.wflow -.. _api_model: - -Wflow model -=========== - -Initialize ----------- - -.. autosummary:: - :toctree: ../generated/ - - WflowModel - -Build components ----------------- - -.. autosummary:: - :toctree: ../generated/ - - WflowModel.setup_config - WflowModel.setup_basemaps - WflowModel.setup_rivers - WflowModel.setup_lakes - WflowModel.setup_reservoirs - WflowModel.setup_glaciers - WflowModel.setup_lulcmaps - WflowModel.setup_laimaps - WflowModel.setup_soilmaps - WflowModel.setup_riverwidth - WflowModel.setup_gauges - WflowModel.setup_precip_forcing - WflowModel.setup_temp_pet_forcing - WflowModel.setup_constant_pars - -Model specific attributes -------------------------- - -.. autosummary:: - :toctree: ../generated/ - - WflowModel.flwdir - WflowModel.basins - WflowModel.rivers - -Model specific methods ----------------------- - -.. autosummary:: - :toctree: ../generated/ - - WflowModel.set_flwdir - WflowModel.clip_staticmaps - WflowModel.clip_forcing \ No newline at end of file diff --git a/docs/api/api_workflows.rst b/docs/api/api_workflows.rst deleted file mode 100644 index 706570b9..00000000 --- a/docs/api/api_workflows.rst +++ /dev/null @@ -1,42 +0,0 @@ -.. currentmodule:: hydromt_wflow - -========= -Workflows -========= -API documentation of model specific workflows. - -Glaciers -======== - -.. autosummary:: - :toctree: ../generated/ - - workflows.glaciers.glaciermaps - workflows.glaciers.glacierattrs - -River -===== - -.. autosummary:: - :toctree: ../generated/ - - workflows.river.river - workflows.river.river_width - -Soilgrids -========= - -.. autosummary:: - :toctree: ../generated/ - - workflows.soilgrids.soilgrids - workflows.soilgrids.soilgrids_sediment - -Waterbodies -=========== - -.. autosummary:: - :toctree: ../generated/ - - workflows.waterbodies.waterbodymaps - workflows.waterbodies.reservoirattrs \ No newline at end of file diff --git a/docs/changelog.rst b/docs/changelog.rst index 012dfb1b..03744532 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -8,6 +8,10 @@ The format is based on `Keep a Changelog`_, and this project adheres to [Unreleased] ------------ +v0.1.2 (1 September 2021) +------------------------- +This release implements the new results attributes for Wflow. + Added ^^^^^ @@ -16,6 +20,7 @@ Added - Support soilgrids version 2020 - Setup_areamap component to prepare maps of areas of interest to save wflow outputs at. - Support wflow_sediment with vito landuse. +- New utils.py script for low_level wflow methods. Changed ^^^^^^^ @@ -29,6 +34,8 @@ Fixed ^^^^^ - Fix f parameter in soilgrids +- Full reading and writting of wflow filepaths depending on the toml file (including subfolders). +- The wflow_gauges now contains river outlets only (instead of all outlets). Documentation ^^^^^^^^^^^^^ diff --git a/docs/index.rst b/docs/index.rst index 606d03a3..c2491656 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -2,8 +2,8 @@ HydroMT plugin: Wflow ===================== -`HydroMT `_ is a python package, developed by Deltares, to build -and analyse environmental models. It provides a generic model api with attributes to access the model schematization, +`HydroMT `_ is a python package, developed by Deltares, to build, update +and analyse water systems models. It provides a generic model api with attributes to access the model schematization, (dynamic) forcing data, results and states. This plugin provides an implementation for the @@ -32,31 +32,20 @@ Documentation **User Guide** -* :doc:`user_guide/wflow/index` -* :doc:`user_guide/sediment/index` +* :doc:`user_guide/wflow` +* :doc:`user_guide/sediment` .. toctree:: :maxdepth: 1 :hidden: :caption: User Guide - user_guide/wflow/index - user_guide/sediment/index - -**Advanced topics** - -* :doc:`advanced/workflows` - -.. toctree:: - :maxdepth: 1 - :hidden: - :caption: Advanced topics - - advanced/workflows + user_guide/wflow + user_guide/sediment **References & Help** -* :doc:`api/api_index` +* :doc:`api/api` * :doc:`contributing` * :doc:`changelog` @@ -65,7 +54,7 @@ Documentation :hidden: :caption: References & Help - api/api_index + api/api contributing changelog diff --git a/docs/installation.rst b/docs/installation.rst index 002a92ca..87267f87 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -3,13 +3,22 @@ Installation User install ------------ +HydroMT and its plugins are python packages. If you do not have a python installation we recommend using +conda and `Miniconda `_. -The hydromt_wflow plugin is available from PyPi and conda-forge, but we recommend installing with conda. +The hydromt_wflow plugin is available from PyPi and conda-forge, but we recommend installing with conda in +a specific environment. To create a new **hydromt-wflow** environment from the command or conda prompt, do: -To install hydromt_wflow using conda do: +.. code-block:: console + + conda create --name hydromt-wflow + +This will create a new empty conda environment named hydromt-wflow. To install hydromt and the hydromt_wflow plugin +in this new environment using conda do: .. code-block:: console + conda activate hydromt-wflow conda install -c conda-forge hydromt_wflow This will install **almost** all dependencies including the core hydroMT library and the model API as well @@ -21,7 +30,7 @@ from pypi (not available from conda): pip install hydroengine -To install hydromt_wflow using pip do: +**Alternatively** to install hydromt_wflow using pip do: Note: make sure this is installed in the same environment as hydromt. .. code-block:: console diff --git a/docs/user_guide/sediment/build_configuration.rst b/docs/user_guide/sediment.rst similarity index 51% rename from docs/user_guide/sediment/build_configuration.rst rename to docs/user_guide/sediment.rst index 1a2a8490..c81073eb 100644 --- a/docs/user_guide/sediment/build_configuration.rst +++ b/docs/user_guide/sediment.rst @@ -1,7 +1,86 @@ +.. currentmodule:: hydromt_wflow.wflow_sediment + +============== +Wflow Sediment +============== +With the hydromt_wflow plugin, you can easily work with wflow sediment models. This plugin helps you preparing or updating +several :ref:`components ` of a wflow sediment model such as topography information, landuse or soil. +The :ref:`main interactions ` are available from the HydroMT Command Line Interface and allow you to configure +HydroMT in order to build or update or clip wflow sediment models. + +When building or updating a model from command line a model region_; a model setup +configuration (.ini file) with model components and options and, optionally, +a data_ sources (.yml) file should be prepared. + +Note that the order in which the components are listed in the ini file is important: + +- `setup_basemaps` should always be run first to determine the model domain +- `setup_rivers` should be run right after `setup_basemaps` as it influences several other setup components (lakes, reservoirs, riverwidth, riverbedsed, gauges) +- `setup_riverwidth` should be listed after `setup_lakes` and `setup_reservoirs`. + +For python users all WflowSediment attributes and methods are available, see :ref:`api_model_sediment` + +.. _model_components_sed: + +WflowSediment model components +============================== + +An overview of the available WflowSedimentModel setup components +is provided in the table below. When using hydromt from the command line only the +setup components are exposed. Click on +a specific method see its documentation. + +.. autosummary:: + :nosignatures: + + ~WflowSedimentModel.setup_config + ~WflowSedimentModel.setup_basemaps + ~WflowSedimentModel.setup_rivers + ~WflowSedimentModel.setup_lakes + ~WflowSedimentModel.setup_reservoirs + ~WflowSedimentModel.setup_lulcmaps + ~WflowSedimentModel.setup_laimaps + ~WflowSedimentModel.setup_canopymaps + ~WflowSedimentModel.setup_soilmaps + ~WflowSedimentModel.setup_riverwidth + ~WflowSedimentModel.setup_riverbedsed + ~WflowSedimentModel.setup_gauges + ~WflowModel.setup_areamap + ~WflowSedimentModel.setup_constant_pars + +WflowSediment datamodel +======================= + +The following table provides an overview of which :py:class:`~hydromt_wflow.WflowSedimentModel` +attribute contains which WflowSediment in- and output files. The files are read and written with the associated +read- and write- methods, i.e. :py:func:`~hydromt_wflow.WflowSedimentModel.read_config` +and :py:func:`~hydromt_wflow.WflowSedimentModel.write_config` for the +:py:attr:`~hydromt_wflow.WflowSedimentModel.config` attribute. + + +.. list-table:: WflowSedimentModel data + :widths: 30 70 + :header-rows: 1 + + * - :py:class:`~hydromt_wflow.WflowSedimentModel` attribute + - Wflow sediment files + * - :py:attr:`~hydromt_wflow.WflowSedimentModel.config` + - wflow_sediment.toml + * - :py:attr:`~hydromt_wflow.WflowSedimentModel.staticmaps` + - staticmaps.nc + * - :py:attr:`~hydromt_wflow.WflowSedimentModel.staticgeoms` + - geometries from the staticgeoms folder (basins.geojson, rivers.geojson etc.) + * - :py:attr:`~hydromt_wflow.WflowSedimentModel.forcing` + - inmaps.nc + * - :py:attr:`~hydromt_wflow.WflowSedimentModel.states` + - instates.nc + * - :py:attr:`~hydromt_wflow.WflowSedimentModel.results` + - output.nc, output_scalar.nc, output.csv + .. _model_config_sed: -Model configuration -=================== +WflowSediment configuration +=========================== This HydroMT plugin provides an implementation for the wflow_sediment model in order to build, update or clip from command line. Specific details on the HydroMT CLI methods can be found in @@ -14,10 +93,10 @@ every option from each :ref:`model component ` can be chan in its corresponding section. Below is an example of ini file that can be used to build a complete wflow_sediment model -:download:`.ini file <../../examples/examples/wflow_sediment_build.ini>`. Each section corresponds +:download:`.ini file <../examples/examples/wflow_sediment_build.ini>`. Each section corresponds to a model component with the same name. -.. literalinclude:: ../../examples/examples/wflow_sediment_build.ini +.. literalinclude:: ../examples/examples/wflow_sediment_build.ini :language: Ini Selecting data @@ -89,7 +168,7 @@ Extending a wflow model with a wflow_sediment model --------------------------------------------------- If you already have a wflow model and you want to extend it in order to include sediment as well, then you do not need to build the wflow_sediment from scratch. You can instead ``update`` the wflow model with the additional components needed by wflow_sediment. -These components are available in a template :download:`.ini file <../../examples/examples/wflow_extend_sediment.ini>` and shown below. The corresponding +These components are available in a template :download:`.ini file <../examples/examples/wflow_extend_sediment.ini>` and shown below. The corresponding command line would be: .. code-block:: console @@ -97,5 +176,8 @@ command line would be: activate hydromt-wflow hydromt update wflow_sediment path/to/wflow_model_to_extend -i wflow_extend_sediment.ini -d data_sources.yml -vvv -.. literalinclude:: ../../examples/examples/wflow_extend_sediment.ini - :language: Ini \ No newline at end of file +.. literalinclude:: ../examples/examples/wflow_extend_sediment.ini + :language: Ini + +.. _data: https://deltares.github.io/hydromt/latest/user_guide/data.html +.. _region: https://deltares.github.io/hydromt/latest/user_guide/cli.html#region-options \ No newline at end of file diff --git a/docs/user_guide/sediment/attributes.rst b/docs/user_guide/sediment/attributes.rst deleted file mode 100644 index cf13230d..00000000 --- a/docs/user_guide/sediment/attributes.rst +++ /dev/null @@ -1,16 +0,0 @@ -.. _model_attributes_sed: -.. currentmodule:: hydromt_wflow.wflow_sediment - -Model attributes -================ - -A wflow_sediment model in HydroMT also has a set of specific attributes, on top of the ones -from the model API. These are (same as wflow): - -.. autosummary:: - :toctree: ../../generated/ - :nosignatures: - - ~WflowSedimentModel.flwdir - ~WflowSedimentModel.basins - ~WflowSedimentModel.rivers \ No newline at end of file diff --git a/docs/user_guide/sediment/components.rst b/docs/user_guide/sediment/components.rst deleted file mode 100644 index 41e3c363..00000000 --- a/docs/user_guide/sediment/components.rst +++ /dev/null @@ -1,33 +0,0 @@ -.. _model_components_sed: -.. currentmodule:: hydromt_wflow.wflow_sediment - -Model components -================ - -For wflow_sediment, the different components available for building or updating are: - -.. autosummary:: - :toctree: ../../generated/ - :nosignatures: - - ~WflowSedimentModel.setup_config - ~WflowSedimentModel.setup_basemaps - ~WflowSedimentModel.setup_rivers - ~WflowSedimentModel.setup_lakes - ~WflowSedimentModel.setup_reservoirs - ~WflowSedimentModel.setup_lulcmaps - ~WflowSedimentModel.setup_laimaps - ~WflowSedimentModel.setup_canopymaps - ~WflowSedimentModel.setup_soilmaps - ~WflowSedimentModel.setup_riverwidth - ~WflowSedimentModel.setup_riverbedsed - ~WflowSedimentModel.setup_gauges - ~WflowModel.setup_areamap - ~WflowSedimentModel.setup_constant_pars - - -.. warning:: - - As for wflow, in wflow_sediment the order in which the components are listed in the ini file is important: setup_rivers should be run - right after setup_basemaps as it influences several other setup components (lakes, reservoirs, riverwidth, riverbedsed, gauges); - setup_riverwidth should be listed after setup_lakes and setup_reservoirs. \ No newline at end of file diff --git a/docs/user_guide/sediment/index.rst b/docs/user_guide/sediment/index.rst deleted file mode 100644 index c9f49d64..00000000 --- a/docs/user_guide/sediment/index.rst +++ /dev/null @@ -1,16 +0,0 @@ -============== -Wflow Sediment -============== -With the hydromt_wflow plugin, you can easily work with wflow sediment models. This plugin helps you preparing or updating -several :ref:`components ` of a wflow sediment model such as topography information, landuse or soil. -The :ref:`main interactions ` are available from the HydroMT Command Line Interface and allow you to configure -HydroMT in order to build or update or clip wflow sediment models. Finally for python users, all wflow sediment objects such as forcing, -staticmaps etc. are available as :ref:`model attributes ` including some wflow specific ones (on top of the -`HydroMT model API attributes `_). - -.. toctree:: - :maxdepth: 1 - - components - build_configuration - attributes diff --git a/docs/user_guide/wflow.rst b/docs/user_guide/wflow.rst new file mode 100644 index 00000000..ce7f10c3 --- /dev/null +++ b/docs/user_guide/wflow.rst @@ -0,0 +1,168 @@ +.. currentmodule:: hydromt_wflow.wflow + +===== +Wflow +===== +With the hydromt_wflow plugin, you can easily work with wflow (SBM) models. This plugin helps you preparing or updating +several :ref:`components ` of a wflow model such as topography information, landuse, soil or forcing. +The :ref:`main interactions ` are available from the HydroMT Command Line Interface and allow you to configure +HydroMT in order to build or update or clip wflow models. + +When building or updating a model from command line a model region_; a model setup +configuration (.ini file) with model components and options and, optionally, +a data_ sources (.yml) file should be prepared. + +Note that the order in which the components are listed in the ini file is important: + +- `setup_basemaps` should always be run first to determine the model domain +- `setup_rivers` should be run right after `setup_basemaps` as it influences several other setup components (lakes, reservoirs, riverwidth, gauges) +- `setup_riverwidth` should be listed after `setup_lakes` and `setup_reservoirs`. + +For python users all Wflow attributes and methods are available, see :ref:`api_model` + +.. _model_components: + +Wflow model components +====================== + +An overview of the available WflowModel setup components +is provided in the table below. When using hydromt from the command line only the +setup components are exposed. Click on +a specific method see its documentation. + +.. autosummary:: + :nosignatures: + + ~WflowModel.setup_config + ~WflowModel.setup_basemaps + ~WflowModel.setup_rivers + ~WflowModel.setup_lakes + ~WflowModel.setup_reservoirs + ~WflowModel.setup_glaciers + ~WflowModel.setup_lulcmaps + ~WflowModel.setup_laimaps + ~WflowModel.setup_soilmaps + ~WflowModel.setup_riverwidth + ~WflowModel.setup_gauges + ~WflowModel.setup_areamap + +Wflow datamodel +=============== + +The following table provides an overview of which :py:class:`~hydromt_wflow.WflowModel` +attribute contains which Wflow in- and output files. The files are read and written with the associated +read- and write- methods, i.e. :py:func:`~hydromt_wflow.wflow.WflowModel.read_config` +and :py:func:`~hydromt_wflow.wflow.WflowModel.write_config` for the +:py:attr:`~hydromt_wflow.wflow.WflowModel.config` attribute. + + +.. list-table:: WflowModel data + :widths: 30 70 + :header-rows: 1 + + * - :py:class:`~hydromt_wflow.WflowModel` attribute + - Wflow files + * - :py:attr:`~hydromt_wflow.WflowModel.config` + - wflow_sbm.toml + * - :py:attr:`~hydromt_wflow.WflowModel.staticmaps` + - staticmaps.nc + * - :py:attr:`~hydromt_wflow.WflowModel.staticgeoms` + - geometries from the staticgeoms folder (basins.geojson, rivers.geojson etc.) + * - :py:attr:`~hydromt_wflow.WflowModel.forcing` + - inmaps.nc + * - :py:attr:`~hydromt_wflow.WflowModel.states` + - instates.nc + * - :py:attr:`~hydromt_wflow.WflowModel.results` + - output.nc, output_scalar.nc, output.csv + +.. _model_config: + +Wflow configuration +=================== +This HydroMT plugin provides an implementation for the wflow model in order to build, update or clip from +command line. Specific details on the HydroMT CLI methods can be found in +https://deltares.github.io/hydromt/latest/user_guide/cli.html + +Configuration file +------------------ +Settings to build or update a wflow model are managed in a configuration file. In this file, +every option from each :ref:`model component ` can be changed by the user +in its corresponding section. + +Below is an example of ini file that can be used to build a complete wflow model +:download:`.ini file <../examples/examples/wflow_build.ini>`. Each section corresponds +to a model component with the same name. + +.. literalinclude:: ../examples/examples/wflow_build.ini + :language: Ini + +Selecting data +-------------- +Data sources in HydroMT are provided in one of several yaml libraries. These libraries contain required +information on the different data sources so that HydroMT can process them for the different models. There +are three ways for the user to select which data libraries to use: + +- If no yaml file is selected, HydroMT will use the data stored in the + `hydromt-artifacts `_ + which contains an extract of global data for a small region around the Piave river in Northern Italy. +- Another options for Deltares users is to select the deltares-data library (requires access to the Deltares + P-drive). In the command lines examples below, this is done by adding either **-dd** or **--deltares-data** + to the build / update command line. +- Finally, the user can prepare its own yaml libary (or libraries) (see + `HydroMT documentation `_ to check the guidelines). + These user libraries can be added either in the command line using the **-d** option and path/to/yaml or in the **ini file** + with the **data_libs** option in the [global] sections. + +Building a model +---------------- +This plugin allows to build a complete model from available data. Once the configuration and +data libraries are set, you can build a model by using: + +.. code-block:: console + + activate hydromt-wflow + hydromt build wflow path/to/built_model "{'basin': [x, y]}" -i wflow_build.ini -d data_sources.yml -vvv + +The recommended `region options `_ +for a proper implementation of this model are: + +- basin +- subbasin + +Updating a model +---------------- +This plugin allows to update any components from a wflow model. To do so, list the components to update in a configuration file, +if needed edit your data library with new data sources required for the update and use the command: + +.. code-block:: console + + activate hydromt-wflow + hydromt update wflow path/to/model_to_update -o path/to/updated_model -i wflow_update.ini -d data_sources.yml -vvv + + +Clipping a model +---------------- +This plugin allows to clip the following parts of an existing model for a smaller region from command line: + +- staticmaps +- forcing + +To clip a smaller model from an existing one use: + +.. code-block:: console + + activate hydromt-wflow + hydromt clip wflow path/to/model_to_clip path/to/clipped_model "{'basin' [1001]}" -vvv + +As for building, the recommended `region options `_ +for a proper implementation of the clipped model are: + +- basin +- subbasin + ~WflowModel.setup_precip_forcing + ~WflowModel.setup_temp_pet_forcing + ~WflowModel.setup_constant_pars + + +.. _data: https://deltares.github.io/hydromt/latest/user_guide/data.html +.. _region: https://deltares.github.io/hydromt/latest/user_guide/cli.html#region-options \ No newline at end of file diff --git a/docs/user_guide/wflow/attributes.rst b/docs/user_guide/wflow/attributes.rst deleted file mode 100644 index 684104b1..00000000 --- a/docs/user_guide/wflow/attributes.rst +++ /dev/null @@ -1,16 +0,0 @@ -.. _model_attributes: -.. currentmodule:: hydromt_wflow.wflow - -Model attributes -================ - -A wflow model in HydroMT also has a set of specific attributes, on top of the ones -from the model API. These are: - -.. autosummary:: - :toctree: ../../generated/ - :nosignatures: - - ~WflowModel.flwdir - ~WflowModel.basins - ~WflowModel.rivers \ No newline at end of file diff --git a/docs/user_guide/wflow/build_configuration.rst b/docs/user_guide/wflow/build_configuration.rst deleted file mode 100644 index dd4a1d79..00000000 --- a/docs/user_guide/wflow/build_configuration.rst +++ /dev/null @@ -1,85 +0,0 @@ -.. _model_config: - -Model configuration -=================== - -This HydroMT plugin provides an implementation for the wflow model in order to build, update or clip from -command line. Specific details on the HydroMT CLI methods can be found in -https://deltares.github.io/hydromt/latest/user_guide/cli.html - -Configuration file ------------------- -Settings to build or update a wflow model are managed in a configuration file. In this file, -every option from each :ref:`model component ` can be changed by the user -in its corresponding section. - -Below is an example of ini file that can be used to build a complete wflow model -:download:`.ini file <../../examples/examples/wflow_build.ini>`. Each section corresponds -to a model component with the same name. - -.. literalinclude:: ../../examples/examples/wflow_build.ini - :language: Ini - -Selecting data --------------- -Data sources in HydroMT are provided in one of several yaml libraries. These libraries contain required -information on the different data sources so that HydroMT can process them for the different models. There -are three ways for the user to select which data libraries to use: - -- If no yaml file is selected, HydroMT will use the data stored in the - `hydromt-artifacts `_ - which contains an extract of global data for a small region around the Piave river in Northern Italy. -- Another options for Deltares users is to select the deltares-data library (requires access to the Deltares - P-drive). In the command lines examples below, this is done by adding either **-dd** or **--deltares-data** - to the build / update command line. -- Finally, the user can prepare its own yaml libary (or libraries) (see - `HydroMT documentation `_ to check the guidelines). - These user libraries can be added either in the command line using the **-d** option and path/to/yaml or in the **ini file** - with the **data_libs** option in the [global] sections. - -Building a model ----------------- -This plugin allows to build a complete model from available data. Once the configuration and -data libraries are set, you can build a model by using: - -.. code-block:: console - - activate hydromt-wflow - hydromt build wflow path/to/built_model "{'basin': [x, y]}" -i wflow_build.ini -d data_sources.yml -vvv - -The recommended `region options `_ -for a proper implementation of this model are: - -- basin -- subbasin - -Updating a model ----------------- -This plugin allows to update any components from a wflow model. To do so, list the components to update in a configuration file, -if needed edit your data library with new data sources required for the update and use the command: - -.. code-block:: console - - activate hydromt-wflow - hydromt update wflow path/to/model_to_update -o path/to/updated_model -i wflow_update.ini -d data_sources.yml -vvv - - -Clipping a model ----------------- -This plugin allows to clip the following parts of an existing model for a smaller region from command line: - -- staticmaps -- forcing - -To clip a smaller model from an existing one use: - -.. code-block:: console - - activate hydromt-wflow - hydromt clip wflow path/to/model_to_clip path/to/clipped_model "{'basin' [1001]}" -vvv - -As for building, the recommended `region options `_ -for a proper implementation of the clipped model are: - -- basin -- subbasin \ No newline at end of file diff --git a/docs/user_guide/wflow/components.rst b/docs/user_guide/wflow/components.rst deleted file mode 100644 index a6f82bae..00000000 --- a/docs/user_guide/wflow/components.rst +++ /dev/null @@ -1,34 +0,0 @@ -.. _model_components: -.. currentmodule:: hydromt_wflow.wflow - -Model components -================ - -For wflow, the different components available for building or updating are: - -.. autosummary:: - :toctree: ../../generated/ - :nosignatures: - - ~WflowModel.setup_config - ~WflowModel.setup_basemaps - ~WflowModel.setup_rivers - ~WflowModel.setup_lakes - ~WflowModel.setup_reservoirs - ~WflowModel.setup_glaciers - ~WflowModel.setup_lulcmaps - ~WflowModel.setup_laimaps - ~WflowModel.setup_soilmaps - ~WflowModel.setup_riverwidth - ~WflowModel.setup_gauges - ~WflowModel.setup_areamap - ~WflowModel.setup_precip_forcing - ~WflowModel.setup_temp_pet_forcing - ~WflowModel.setup_constant_pars - - -.. warning:: - - In wflow, the order in which the components are listed in the ini file is important: setup_rivers should be run - right after setup_basemaps as it influences several other setup components (lakes, reservoirs, riverwidth, gauges); - setup_riverwidth should be listed after setup_lakes and setup_reservoirs. \ No newline at end of file diff --git a/docs/user_guide/wflow/index.rst b/docs/user_guide/wflow/index.rst deleted file mode 100644 index 95cca300..00000000 --- a/docs/user_guide/wflow/index.rst +++ /dev/null @@ -1,16 +0,0 @@ -===== -Wflow -===== -With the hydromt_wflow plugin, you can easily work with wflow (SBM) models. This plugin helps you preparing or updating -several :ref:`components ` of a wflow model such as topography information, landuse, soil or forcing. -The :ref:`main interactions ` are available from the HydroMT Command Line Interface and allow you to configure -HydroMT in order to build or update or clip wflow models. Finally for python users, all wflow objects such as forcing, -staticmaps etc. are available as :ref:`model attributes ` including some wflow specific ones (on top of the -`HydroMT model API attributes `_). - -.. toctree:: - :maxdepth: 1 - - components - build_configuration - attributes diff --git a/examples/plot_wflow_results.ipynb b/examples/plot_wflow_results.ipynb index 551c6218..55dece25 100644 --- a/examples/plot_wflow_results.ipynb +++ b/examples/plot_wflow_results.ipynb @@ -113,7 +113,7 @@ "\n", "* a **\"output\" xarray.Dataset** for the gridded netcdf file (output section of the TOML)\n", "* a **\"netcdf\" xarray.Dataset** for the netcdf point timeseries file (netcdf section of the TOML)\n", - "* different **hydromt.GeoDataArrays for the csv file** , one per column (csv section and csv.column sections of the TOML). The xy coordinates are the coordinates of the station or of the centroid of the subcatch/area. The variable name in the GeoDataArray corresponds to the csv header attribute or header_map when available.\n", + "* different **hydromt.GeoDataArrays for the csv file** , one per column (csv section and csv.column sections of the TOML). The xy coordinates are the coordinates of the station or of the representative point of the subcatch/area. The variable name in the GeoDataArray corresponds to the csv header attribute or header_map when available.\n", "\n", "Below you can see how to access to the results of *run1* and its contents:" ] @@ -252,7 +252,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -266,7 +266,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.4" + "version": "3.9.6" } }, "nbformat": 4, diff --git a/hydromt_wflow/__init__.py b/hydromt_wflow/__init__.py index 2d3120e4..4182d862 100644 --- a/hydromt_wflow/__init__.py +++ b/hydromt_wflow/__init__.py @@ -2,7 +2,7 @@ from os.path import join, dirname, abspath -__version__ = "0.1.2.dev" +__version__ = "0.1.2" try: import pcraster as pcr @@ -12,3 +12,7 @@ HAS_PCRASTER = False DATADIR = join(dirname(abspath(__file__)), "data") + +from .wflow import * +from .wflow_sediment import * +from .utils import * diff --git a/hydromt_wflow/utils.py b/hydromt_wflow/utils.py new file mode 100644 index 00000000..cd840a45 --- /dev/null +++ b/hydromt_wflow/utils.py @@ -0,0 +1,169 @@ +import numpy as np +import xarray as xr +from typing import Dict, Union +from pathlib import Path + +from hydromt.io import open_timeseries_from_table +from hydromt.vector import GeoDataArray + + +__all__ = ["read_csv_results"] + + +def read_csv_results(fn: Union[str, Path], config: Dict, maps: xr.Dataset) -> Dict: + """Read wflow results csv timeseries and parse to dictionnary + + Parses the wflow csv results file into different ``hydromt.GeoDataArrays``, one per + column (csv section and csv.column sections of the TOML). The xy coordinates are the + coordinates of the station or of the representative point of the subcatch/area. The + variable name in the ``GeoDataArray`` corresponds to the csv header attribute or + header_map when available. + + Parameters + ---------- + fn: str + Path to the wflow csv results file. + config: dict + wflow.toml configuration. + maps: xr.Dataset + wflow staticmaps.nc dataset + + Returns + ------- + csv_dict: dict + Dictionnary of hydromt.GeoDataArrays for the different csv.column section of the config. + """ + # Count items by csv.column + count = 1 + csv_dict = dict() + # Loop over csv.column + for col in config["csv"].get("column"): + header = col["header"] + # Column based on map + if "map" in col.keys(): + # Read the corresponding map and derive the different locations + # The centroid of the geometry is used as coordinates for the timeseries + map_name = config["input"].get(f"{col['map']}") + da = maps[map_name] + gdf = da.raster.vectorize() + gdf.geometry = gdf.geometry.representative_point() + gdf.index = gdf.value.astype(da.dtype) + gdf.index.name = "index" + # Read the timeseries + usecols = [0] + usecols = np.append(usecols, np.arange(count, count + len(gdf.index))) + count += len(gdf.index) + da_ts = open_timeseries_from_table( + fn, name=f'{header}_{col["map"]}', usecols=usecols + ) + da = GeoDataArray.from_gdf(gdf, da_ts, index_dim="index") + # Column based on xy coordinates or reducer for the full model domain domain + else: + # Read the timeseries + usecols = [0] + usecols = np.append(usecols, np.arange(count, count + 1)) + count += 1 + try: + da_ts = open_timeseries_from_table(fn, name=header, usecols=usecols) + except: + colnames = ["time", "0"] + da_ts = open_timeseries_from_table( + fn, + name=header, + usecols=usecols, + header=0, + names=colnames, + ) + # Add point coordinates + # Column based on xy coordinates + if "coordinate" in col.keys(): + scoords = { + "x": xr.IndexVariable("index", [col["coordinate"]["x"]]), + "y": xr.IndexVariable("index", [col["coordinate"]["y"]]), + } + # Column based on index + elif "index" in col.keys(): + # x and y index, works on the full 2D grid + if isinstance(col["index"], dict): + # index in julia starts at 1 + # coordinates are always ascending + xi = maps.raster.xcoords.values[col["index"]["x"] - 1] + yi = np.sort(maps.raster.ycoords.values)[col["index"]["y"] - 1] + scoords = { + "x": xr.IndexVariable("index", [xi]), + "y": xr.IndexVariable("index", [yi]), + } + # index of the full array + else: + # Create grid with full 2D Julia indices + # Dimensions are ascending and ordered as (x,y,layer,time) + # Indices are created before ordering for compatibility with raster.idx_to_xy + full_index = maps[f'{config["input"].get("subcatchment")}'].copy() + res_x, res_y = full_index.raster.res + if res_y < 0: + full_index = full_index.reindex( + { + full_index.raster.y_dim: full_index[ + full_index.raster.y_dim + ][::-1] + } + ) + data = np.arange(0, np.size(full_index)).reshape( + np.size(full_index, 0), np.size(full_index, 1) + ) + full_index[:, :] = data + full_index = full_index.transpose( + full_index.raster.x_dim, full_index.raster.y_dim + ) + # Index depends on the struct + # For land uses the active subcatch IDs + if ( + "vertical" in col["parameter"] + or "lateral.land" in col["parameter"] + ): + mask = maps[f'{config["input"].get("subcatchment")}'].copy() + elif "reservoir" in col["parameter"]: + mask = maps[ + f'{config["input"]["lateral"]["river"]["reservoir"].get("locs")}' + ].copy() + elif "lake" in col["parameter"]: + mask = maps[ + f'{config["input"]["lateral"]["river"]["lake"].get("locs")}' + ].copy() + # Else lateral.river + else: + mask = maps[f'{config["input"].get("river_location")}'].copy() + # Rearrange the mask + res_x, res_y = mask.raster.res + if res_y < 0: + mask = mask.reindex( + {mask.raster.y_dim: mask[mask.raster.y_dim][::-1]} + ) + mask = mask.transpose(mask.raster.x_dim, mask.raster.y_dim) + # Filter and reduce full_index based on mask + full_index = full_index.where(mask != mask.raster.nodata, 0) + full_index.attrs.update(_FillValue=0) + mask_index = full_index.values.flatten() + mask_index = mask_index[mask_index != 0] + # idx corresponding to the wflow index + idx = mask_index[col["index"] - 1] + # Reorder full_index as (y,x) to use raster.idx_to_xy method + xi, yi = full_index.transpose( + full_index.raster.y_dim, full_index.raster.x_dim + ).raster.idx_to_xy(idx) + scoords = { + "x": xr.IndexVariable("index", xi), + "y": xr.IndexVariable("index", yi), + } + # Based on model bbox center for column based on reducer for the full model domain + else: + xmin, ymin, xmax, ymax = maps.raster.bounds + scoords = { + "x": xr.IndexVariable("index", [(xmax + xmin) / 2]), + "y": xr.IndexVariable("index", [(ymax + ymin) / 2]), + } + da = da_ts.assign_coords(scoords) + + csv_dict[f"{da.name}"] = da + + return csv_dict diff --git a/hydromt_wflow/wflow.py b/hydromt_wflow/wflow.py index fa1b7ad8..b13d92a0 100644 --- a/hydromt_wflow/wflow.py +++ b/hydromt_wflow/wflow.py @@ -2,7 +2,7 @@ # Implement model class following model API import os -from os.path import join, dirname, basename, isfile +from os.path import join, dirname, basename, isfile, isdir from typing import Optional import glob import numpy as np @@ -21,8 +21,7 @@ # from hydromt.workflows.basin_mask import parse_region from hydromt import workflows, flw -from hydromt.io import open_mfraster, open_timeseries_from_table -from hydromt.vector import GeoDataArray +from hydromt.io import open_mfraster from .workflows import ( river, @@ -33,7 +32,7 @@ glaciermaps, ) from .workflows import landuse, lai -from . import DATADIR +from . import utils, DATADIR import logging @@ -579,13 +578,19 @@ def setup_gauges( self.staticgeoms if derive_outlet: - self.logger.info(f"Gauges locations set based on outlets.") + self.logger.info(f"Gauges locations set based on river outlets.") da, idxs, ids = flw.gaugemap(self.staticmaps, idxs=self.flwdir.idxs_pit) + # Only keep river outlets for gauges + da = da.where(self.staticmaps[self._MAPS["rivmsk"]]) + ids_da = np.unique(da.values[da.values > 0]) + idxs_da = idxs[np.isin(ids, ids_da)] self.set_staticmaps(da, name=self._MAPS["gauges"]) - points = gpd.points_from_xy(*self.staticmaps.raster.idx_to_xy(idxs)) - gdf = gpd.GeoDataFrame(index=ids, geometry=points, crs=self.crs) + points = gpd.points_from_xy(*self.staticmaps.raster.idx_to_xy(idxs_da)) + gdf = gpd.GeoDataFrame( + index=ids_da.astype(np.int32), geometry=points, crs=self.crs + ) self.set_staticgeoms(gdf, name="gauges") - self.logger.info(f"Gauges map based on catchment outlets added.") + self.logger.info(f"Gauges map based on catchment river outlets added.") if gauges_fn is not None or source_gdf is not None: # append location from geometry @@ -1396,6 +1401,9 @@ def write_staticmaps(self): ds_out = self.staticmaps fn_default = join(self.root, "staticmaps.nc") fn = self.get_config("input.path_static", abs_path=True, fallback=fn_default) + # Check if all sub-folders in fn exists and if not create them + if not isdir(dirname(fn)): + os.makedirs(dirname(fn)) self.logger.info(f"Write staticmaps to {fn}") mask = ds_out[self._MAPS["basins"]] > 0 for v in ds_out.data_vars: @@ -1454,7 +1462,7 @@ def write_staticmaps_pcr(self): ds_out[f"c_{layer.item():d}"].raster.set_nodata( ds_out["c"].raster.nodata ) - ds_out = ds_out.drop(["c", "layer"]) + ds_out = ds_out.drop_vars(["c", "layer"]) self.logger.info("Writing (updated) staticmap files.") # add datatypes for maps with same basenames, e.g. wflow_gauges_grdc pcr_vs_map = PCR_VS_MAP.copy() @@ -1602,6 +1610,9 @@ def write_forcing(self, fn_out=None, chunksize=1, decimals=2, **kwargs): } self.logger.info(f"Process forcing; saving to {fn_out}") + # Check if all sub-folders in fn_out exists and if not create them + if not isdir(dirname(fn_out)): + os.makedirs(dirname(fn_out)) # with compute=False we get a delayed object which is executed when # calling .compute where we can pass more arguments to the dask.compute method delayed_obj = ds.to_netcdf( @@ -1654,152 +1665,12 @@ def read_results(self): # Read csv timeseries (csv section) csv_fn = self.get_config("csv.path", abs_path=True) if csv_fn is not None and isfile(csv_fn): - # Count items by csv.column - count = 1 - # Loop over csv.column - for col in self.get_config("csv.column"): - header = col["header"] - # Column based on map - if "map" in col.keys(): - # Read the corresponding map and derive the different locations - # The centroid of the geometry is used as coordinates for the timeseries - map_name = self.get_config(f"input.{col['map']}") - da = self.staticmaps[map_name] - gdf = da.raster.vectorize() - gdf.geometry = gdf.geometry.centroid - gdf.index = gdf.value.astype(da.dtype) - gdf.index.name = "index" - # Read the timeseries - usecols = [0] - usecols = np.append( - usecols, np.arange(count, count + len(gdf.index)) - ) - count += len(gdf.index) - da_ts = open_timeseries_from_table( - csv_fn, name=f'{header}_{col["map"]}', usecols=usecols - ) - da = GeoDataArray.from_gdf(gdf, da_ts, index_dim="index") - # Column based on xy coordinates or reducer for the full model domain domain - else: - # Read the timeseries - usecols = [0] - usecols = np.append(usecols, np.arange(count, count + 1)) - count += 1 - try: - da_ts = open_timeseries_from_table( - csv_fn, name=header, usecols=usecols - ) - except: - colnames = ["time", "0"] - da_ts = open_timeseries_from_table( - csv_fn, - name=header, - usecols=usecols, - header=0, - names=colnames, - ) - # Add point coordinates - # Column based on xy coordinates - if "coordinate" in col.keys(): - scoords = { - "x": xr.IndexVariable("index", [col["coordinate"]["x"]]), - "y": xr.IndexVariable("index", [col["coordinate"]["y"]]), - } - # Column based on index - elif "index" in col.keys(): - # x and y index, works on the full 2D grid - if isinstance(col["index"], dict): - # index in julia starts at 1 - # coordinates are always ascending - xi = self.staticmaps.raster.xcoords.values[ - col["index"]["x"] - 1 - ] - yi = np.sort(self.staticmaps.raster.ycoords.values)[ - col["index"]["y"] - 1 - ] - scoords = { - "x": xr.IndexVariable("index", [xi]), - "y": xr.IndexVariable("index", [yi]), - } - # index of the full array - else: - # Create grid with full 2D Julia indices - # Dimensions are ascending and ordered as (x,y,layer,time) - # Indices are created before ordering for compatibility with raster.idx_to_xy - full_index = self.staticmaps[ - f'{self.get_config("input.subcatchment")}' - ].copy() - res_x, res_y = full_index.raster.res - if res_y < 0: - full_index = full_index.reindex( - { - full_index.raster.y_dim: full_index[ - full_index.raster.y_dim - ][::-1] - } - ) - data = np.arange(0, np.size(full_index)).reshape( - np.size(full_index, 0), np.size(full_index, 1) - ) - full_index[:, :] = data - full_index = full_index.transpose( - full_index.raster.x_dim, full_index.raster.y_dim - ) - # Index depends on the struct - # For land uses the active subcatch IDs - if ( - "vertical" in col["parameter"] - or "lateral.land" in col["parameter"] - ): - mask = self.staticmaps[ - f'{self.get_config("input.subcatchment")}' - ].copy() - elif "reservoir" in col["parameter"]: - mask = self.staticmaps[ - f'{self.get_config("input.lateral.river.reservoir.locs")}' - ].copy() - elif "lake" in col["parameter"]: - mask = self.staticmaps[ - f'{self.get_config("input.lateral.river.lake.locs")}' - ].copy() - # Else lateral.river - else: - mask = self.staticmaps[ - f'{self.get_config("input.river_location")}' - ].copy() - # Rearrange the mask - res_x, res_y = mask.raster.res - if res_y < 0: - mask = mask.reindex( - {mask.raster.y_dim: mask[mask.raster.y_dim][::-1]} - ) - mask = mask.transpose(mask.raster.x_dim, mask.raster.y_dim) - # Filter and reduce full_index based on mask - full_index = full_index.where(mask != mask.raster.nodata, 0) - full_index.attrs.update(_FillValue=0) - mask_index = full_index.values.flatten() - mask_index = mask_index[mask_index != 0] - # idx corresponding to the wflow index - idx = mask_index[col["index"] - 1] - # Reorder full_index as (y,x) to use raster.idx_to_xy method - xi, yi = full_index.transpose( - full_index.raster.y_dim, full_index.raster.x_dim - ).raster.idx_to_xy(idx) - scoords = { - "x": xr.IndexVariable("index", xi), - "y": xr.IndexVariable("index", yi), - } - # Based on model bbox center for column based on reducer for the full model domain domain - else: - xmin, ymin, xmax, ymax = self.bounds - scoords = { - "x": xr.IndexVariable("index", [(xmax + xmin) / 2]), - "y": xr.IndexVariable("index", [(ymax + ymin) / 2]), - } - da = da_ts.assign_coords(scoords) - + csv_dict = utils.read_csv_results( + csv_fn, config=self.config, maps=self.staticmaps + ) + for key in csv_dict: # Add to results - self.set_results(da) + self.set_results(csv_dict[f"{key}"]) def write_results(self): """write results at in model ready format""" @@ -2007,7 +1878,7 @@ def clip_staticmaps( "ResMaxRelease", "ResMaxVolume", ] - self._staticmaps = self.staticmaps.drop(remove_maps) + self._staticmaps = self.staticmaps.drop_vars(remove_maps) remove_lake = False if self._MAPS["lakeareas"] in self.staticmaps: @@ -2027,7 +1898,7 @@ def clip_staticmaps( "Lake_b", "Lake_e", ] - self._staticmaps = self.staticmaps.drop(remove_maps) + self._staticmaps = self.staticmaps.drop_vars(remove_maps) # Update config # Remove the absolute path and if needed remove lakes and reservoirs diff --git a/hydromt_wflow/workflows/glaciers.py b/hydromt_wflow/workflows/glaciers.py index b3f507f1..47aa4661 100644 --- a/hydromt_wflow/workflows/glaciers.py +++ b/hydromt_wflow/workflows/glaciers.py @@ -67,10 +67,10 @@ def glaciermaps( # gdf["AREA2"] = gdf.to_crs(3857).area / 1e6 # km2, area calculation needs projected crs def calcStore(areakm2, k=0.2055, gamma=1.375): - return np.int(k * (areakm2 * 1e6) ** gamma) + return int(k * (areakm2 * 1e6) ** gamma) def convStoremm(areakm2, volm3, rhoice=0.9): - return np.int(rhoice * volm3 / (areakm2 * 1e6) * 1000) + return int(rhoice * volm3 / (areakm2 * 1e6) * 1000) gdf["glaciervolume"] = gdf.apply(lambda row: calcStore(row["AREA"]), axis=1) gdf["glacierstore"] = gdf.apply( diff --git a/hydromt_wflow/workflows/soilgrids.py b/hydromt_wflow/workflows/soilgrids.py index 1c8915a6..16dd8362 100644 --- a/hydromt_wflow/workflows/soilgrids.py +++ b/hydromt_wflow/workflows/soilgrids.py @@ -365,7 +365,7 @@ def soilgrids(ds, ds_like, ptfKsatVer, soil_fn, logger=logger): for i in np.arange(1, len(soildepth_cm_midpoint) + 1): da_prop.append(ds[f"{var}_sl{i}"]) # remove layer from ds - ds = ds.drop(f"{var}_sl{i}") + ds = ds.drop_vars(f"{var}_sl{i}") da = xr.concat( da_prop, pd.Index( @@ -424,15 +424,15 @@ def soilgrids(ds, ds_like, ptfKsatVer, soil_fn, logger=logger): logger.info("calculate and resample KsatVer") kv_sl_hr = kv_layers(ds, thetas_sl, ptfKsatVer) - kv_sl = xr.ufuncs.log(kv_sl_hr) + kv_sl = np.log(kv_sl_hr) kv_sl = kv_sl.raster.reproject_like(ds_like, method="average") - kv_sl = xr.ufuncs.exp(kv_sl) + kv_sl = np.exp(kv_sl) logger.info("calculate and resample pore size distribution index") lambda_sl_hr = pore_size_distrution_index_layers(ds, thetas_sl) - lambda_sl = xr.ufuncs.log(lambda_sl_hr) + lambda_sl = np.log(lambda_sl_hr) lambda_sl = lambda_sl.raster.reproject_like(ds_like, method="average") - lambda_sl = xr.ufuncs.exp(lambda_sl) + lambda_sl = np.exp(lambda_sl) if soil_fn == "soilgrids_2020": # calculate c for each layer of lambda @@ -533,7 +533,7 @@ def soilgrids(ds, ds_like, ptfKsatVer, soil_fn, logger=logger): ) soil_texture = soil_texture.raster.reproject_like(ds_like, method="mode") - ds_out["wflow_soil"] = soil_texture.astype(np.int) + ds_out["wflow_soil"] = soil_texture.astype(np.int32) # for writing pcraster map files a scalar nodata value is required for var in ds_out: