ewatercycle.base.forcing ======================== .. py:module:: ewatercycle.base.forcing .. autoapi-nested-parse:: Base classes for eWaterCycle forcings. Configuring ESMValTool ---------------------- .. _esmvaltool-configuring: To download data from ESFG via ESMValTool you will need a ~/.esmvaltool/config-user.yml file with something like: .. code-block:: yaml search_esgf: when_missing download_dir: ~/climate_data rootpath: CMIP6: ~/climate_data/CMIP6 drs: CMIP6: ESGF A config file can be generated with: .. code-block:: bash esmvaltool config get-config-user See `ESMValTool configuring docs `_ for more information. Module Contents --------------- .. py:data:: logger .. py:data:: FORCING_YAML :value: 'ewatercycle_forcing.yaml' .. py:data:: AnyForcing TypeVar for forcing classes. .. py:type:: Postprocessor :canonical: Callable[[dict[str, str]], tuple[str, ...]] .. py:class:: DefaultForcing Bases: :py:obj:`pydantic.BaseModel` Container for forcing data. :param directory: Directory where forcing data files are stored. :param start_time: Start time of forcing in UTC and ISO format string e.g. 'YYYY-MM-DDTHH:MM:SSZ'. :param end_time: End time of forcing in UTC and ISO format string e.g. 'YYYY-MM-DDTHH:MM:SSZ'. :param shape: Path to a shape file. Used for spatial selection. If relative then it is relative to the given directory. :param filenames: Dictionary of the variables contained in this forcing object, as well as the file names. Default value is empty, for backwards compatibility. .. py:attribute:: start_time :type: str .. py:attribute:: end_time :type: str .. py:attribute:: directory :type: Annotated[pathlib.Path, AfterValidator(_to_absolute_path)] .. py:attribute:: shape :type: pathlib.Path | None :value: None .. py:attribute:: filenames :type: dict[str, str] .. py:method:: generate(dataset: str | ewatercycle.esmvaltool.schema.Dataset | dict, start_time: str, end_time: str, shape: str | pathlib.Path, directory: str | None = None, variables: tuple[str, ...] = (), postprocessor: Postprocessor | None = None, **model_specific_options) -> AnyForcing :classmethod: Generate forcings for a model. The forcing is generated with help of `ESMValTool `_. :param dataset: Dataset to get forcing data from. When string is given a predefined dataset is looked up in :py:const:`ewatercycle.esmvaltool.datasets.DATASETS`. When dict given it is passed to :py:class:`ewatercycle.esmvaltool.schema.Dataset` constructor. :param start_time: Start time of forcing in UTC and ISO format string e.g. 'YYYY-MM-DDTHH:MM:SSZ'. :param end_time: nd time of forcing in UTC and ISO format string e.g. 'YYYY-MM-DDTHH:MM:SSZ'. :param shape: Path to a shape file. Used for spatial selection. :param directory: Directory in which forcing should be written. If not given will create timestamped directory. :param variables: Variables which need to be downloaded/preprocessed by ESMValTool. :param postprocessor: A custom post-processor that can, e.g., derive additional variables based on the ESMValTool recipe output. Must return the names & filenames of the variables it derived. :param model_specific_options: Subclass specific options. .. py:method:: save() Export forcing data for later use. .. py:method:: load(directory: str | pathlib.Path) :classmethod: Load previously generated or imported forcing data. :param directory: forcing data directory; must contain `ewatercycle_forcing.yaml` file Returns: Forcing object .. py:method:: to_xarray() -> xarray.Dataset Return this Forcing object as an xarray Dataset. .. py:method:: variables() -> tuple[str, ...] Return the names of the variables. Shorthand for self.filenames.keys() .. py:method:: get_shape_area() -> float Return the area of the shapefile in m2. .. py:method:: plot_shape(ax: matplotlib.axes._axes.Axes | None = None) -> matplotlib.axes._axes.Axes Make a plot of the shapefile on a map of the world. The plot is padded with 10% of the north-south extend, or the east-west extend (which ever is smallest). Optionally a matplotlib axes object can be passed into which the shape is plotted. The axis object is returned. .. py:class:: DistributedUserForcing Bases: :py:obj:`DefaultForcing` Forcing object with user-specified downloaded variables and postprocessing. .. rubric:: Examples To generate forcing from ERA5 for the Rhine catchment for 2000-2001, retrieving the 'pr', 'tas' and 'rsds' variables, and applying the Makkink evaporation postprocessor. .. code-block:: python from pathlib import Path from rich import print from ewatercycle.base.forcing import DistributedUserForcing cmip_dataset = { "dataset": "EC-Earth3", "project": "CMIP6", "grid": "gr", "exp": "historical", "ensemble": "r6i1p1f1", } shape = Path("./src/ewatercycle/testing/data/Rhine/Rhine.shp") forcing = DistributedUserForcing.generate( dataset=cmip_dataset, start_time='2000-01-01T00:00:00Z', end_time='2001-01-01T00:00:00Z', shape=str(shape.absolute()), variables=("pr", "tas"), ) print(forcing) Gives something like: .. code-block:: python DistributedUserForcing( start_time='2000-01-01T00:00:00Z', end_time='2001-01-01T00:00:00Z', directory=PosixPath('/home/bart/esmvaltool_output/ewcrepvl1jeunb_20240305_131155/work/diagnostic/script'), shape=PosixPath('/home/bart/git/ewatercycle/src/ewatercycle/testing/data/Rhine/Rhine.shp'), filenames={ 'pr': 'CMIP6_EC-Earth3_day_historical_r6i1p1f1_pr_gr_2000-2001.nc', 'tas': 'CMIP6_EC-Earth3_day_historical_r6i1p1f1_tas_gr_2000-2001.nc', 'rsds': 'CMIP6_EC-Earth3_day_historical_r6i1p1f1_rsds_gr_2000-2001.nc', 'evspsblpot': 'Derived_Makkink_evspsblpot.nc' } ) .. py:class:: LumpedUserForcing Bases: :py:obj:`DistributedUserForcing` Forcing object with user-specified downloaded variables and postprocessing. .. rubric:: Examples To generate lumped forcing from ERA5 for the Rhine catchment for 2000-2001, retrieving the 'pr', 'tas' and 'rsds' variables, and applying the Makkink evaporation postprocessor. .. code-block:: python from pathlib import Path from rich import print from ewatercycle.base.forcing import LumpedUserForcing cmip_dataset = { "dataset": "EC-Earth3", "project": "CMIP6", "grid": "gr", "exp": "historical", "ensemble": "r6i1p1f1", } shape = Path("./src/ewatercycle/testing/data/Rhine/Rhine.shp") forcing = LumpedUserForcing.generate( dataset=cmip_dataset, start_time='2000-01-01T00:00:00Z', end_time='2001-01-01T00:00:00Z', shape=str(shape.absolute()), variables=("pr", "tas"), ) print(forcing) Gives something like: .. code-block:: python LumpedUserForcing( start_time='2000-01-01T00:00:00Z', end_time='2001-01-01T00:00:00Z', directory=PosixPath('/home/bart/esmvaltool_output/ewcrepvl1jeunb_20240305_131155/work/diagnostic/script'), shape=PosixPath('/home/bart/git/ewatercycle/src/ewatercycle/testing/data/Rhine/Rhine.shp'), filenames={ 'pr': 'CMIP6_EC-Earth3_day_historical_r6i1p1f1_pr_gr_2000-2001.nc', 'tas': 'CMIP6_EC-Earth3_day_historical_r6i1p1f1_tas_gr_2000-2001.nc', 'rsds': 'CMIP6_EC-Earth3_day_historical_r6i1p1f1_rsds_gr_2000-2001.nc', 'evspsblpot': 'Derived_Makkink_evspsblpot.nc' } ) .. py:class:: GenericDistributedForcing Bases: :py:obj:`_GenericForcing`, :py:obj:`DistributedUserForcing` Generic forcing data for a distributed model. .. rubric:: Examples To generate forcing from ERA5 for the Rhine catchment for 2000-2001: .. code-block:: python from pathlib import Path from rich import print from ewatercycle.base.forcing import GenericDistributedForcing shape = Path("./src/ewatercycle/testing/data/Rhine/Rhine.shp") forcing = GenericDistributedForcing.generate( dataset='ERA5', start_time='2000-01-01T00:00:00Z', end_time='2001-01-01T00:00:00Z', shape=shape.absolute(), ) print(forcing) Gives something like: .. code-block:: python GenericDistributedForcing( model='generic_distributed', start_time='2000-01-01T00:00:00Z', end_time='2001-01-01T00:00:00Z', directory=PosixPath('/home/verhoes/git/eWaterCycle/ewatercycle/esmvaltool_output/tmp05upitxoewcrep_20230815_154640/work/diagnostic/script'), shape=PosixPath('/home/verhoes/git/eWaterCycle/ewatercycle/src/ewatercycle/testing/data/Rhine/Rhine.shp'), filenames={ pr='OBS6_ERA5_reanaly_1_day_pr_2000-2001.nc', tas='OBS6_ERA5_reanaly_1_day_tas_2000-2001.nc', } ) To generate forcing from CMIP6 for the Rhine catchment for 2000-2001 (make sure :ref:`ESMValTool is configured ` correctly): .. code-block:: python from pathlib import Path from rich import print from ewatercycle.base.forcing import GenericDistributedForcing shape = Path("./src/ewatercycle/testing/data/Rhine/Rhine.shp") cmip_dataset = { "dataset": "EC-Earth3", "project": "CMIP6", "grid": "gr", "exp": ["historical",], "ensemble": "r6i1p1f1", } forcing = GenericDistributedForcing.generate( dataset=cmip_dataset, start_time="2000-01-01T00:00:00Z", end_time="2001-01-01T00:00:00Z", shape=shape.absolute(), ) print(forcing) Gives something like: .. code-block:: python GenericDistributedForcing( start_time='2000-01-01T00:00:00Z', end_time='2001-01-01T00:00:00Z', directory=PosixPath('/home/verhoes/git/eWaterCycle/ewatercycle/esmvaltool_output/ewcrep0ibzlds__20230904_082748/work/diagnostic/script'), shape=PosixPath('/home/verhoes/git/eWaterCycle/ewatercycle/src/ewatercycle/testing/data/Rhine/Rhine.shp'), filenames={ pr='CMIP6_EC-Earth3_day_historical_r6i1p1f1_pr_gr_2000-2001.nc', tas='CMIP6_EC-Earth3_day_historical_r6i1p1f1_tas_gr_2000-2001.nc', } ) .. py:class:: GenericLumpedForcing Bases: :py:obj:`_GenericForcing`, :py:obj:`LumpedUserForcing` Generic forcing data for a lumped model. .. rubric:: Example To generate forcing from ERA5 for the Rhine catchment for 2000-2001: .. code-block:: python from pathlib import Path from rich import print from ewatercycle.base.forcing import GenericLumpedForcing shape = Path("./src/ewatercycle/testing/data/Rhine/Rhine.shp") forcing = GenericLumpedForcing.generate( dataset='ERA5', start_time='2000-01-01T00:00:00Z', end_time='2001-01-01T00:00:00Z', shape=shape.absolute(), ) print(forcing) Gives something like: .. code-block:: python GenericLumpedForcing( model='generic_distributed', start_time='2000-01-01T00:00:00Z', end_time='2001-01-01T00:00:00Z', directory=PosixPath('/home/verhoes/git/eWaterCycle/ewatercycle/esmvaltool_output/ewcrep90hmnvat_20230816_124951/work/diagnostic/script'), shape=PosixPath('/home/verhoes/git/eWaterCycle/ewatercycle/src/ewatercycle/testing/data/Rhine/Rhine.shp'), pr='OBS6_ERA5_reanaly_1_day_pr_2000-2001.nc', tas='OBS6_ERA5_reanaly_1_day_tas_2000-2001.nc', tasmin='OBS6_ERA5_reanaly_1_day_tasmin_2000-2001.nc', tasmax='OBS6_ERA5_reanaly_1_day_tasmax_2000-2001.nc' ) To generate forcing from CMIP6 for the Rhine catchment for 2000-2001 (make sure :ref:`ESMValTool is configured ` correctly): .. code-block:: python from pathlib import Path from rich import print from ewatercycle.base.forcing import GenericLumpedForcing shape = Path("./src/ewatercycle/testing/data/Rhine/Rhine.shp") cmip_dataset = { "dataset": "EC-Earth3", "project": "CMIP6", "grid": "gr", "exp": ["historical",], "ensemble": "r6i1p1f1", } forcing = GenericLumpedForcing.generate( dataset=cmip_dataset, start_time="2000-01-01T00:00:00Z", end_time="2001-01-01T00:00:00Z", shape=shape.absolute(), ) print(forcing)