ewatercycle.esmvaltool.builder ============================== .. py:module:: ewatercycle.esmvaltool.builder .. autoapi-nested-parse:: Builder and runner for ESMValTool recipes. The recipes can be used to generate forcings. Module Contents --------------- .. py:data:: DIAGNOSTIC_NAME :value: 'diagnostic' .. py:data:: SPATIAL_PREPROCESSOR_NAME :value: 'spatial' .. py:data:: SCRIPT_NAME :value: 'script' .. py:data:: DEFAULT_DIAGNOSTIC_SCRIPT .. py:data:: logger .. py:class:: RecipeBuilder Builder for ESMValTool recipes tailored to generate forcings. .. rubric:: Example To create a recipe from ERA5 dataset and the Rhine basin: .. code-block:: python >>> from ewatercycle.testing import rhine_shape >>> from ewatercycle.forcing import RecipeBuilder >>> recipe = ( ... RecipeBuilder() ... .title("Generic distributed forcing recipe") ... .dataset("ERA5") ... .start(2000) ... .end(2001) ... .shape(rhine_shape()) ... .add_variable("pr") ... .build() ... ) >>> recipe.save("recipe.yml") To run the recipe: .. code-block:: bash esmvaltool recipe.yml Order in which methods are called matters in the following cases: * regrid before adding variables * lump after spatial selection and before adding variables * temporal selection before adding variables * spatial selection before adding variables Initialize the recipe builder. .. py:method:: build() -> ewatercycle.esmvaltool.schema.Recipe Build the recipe. Should be called after all other methods. .. py:method:: description(description: str) -> RecipeBuilder Set the description of the recipe. :param description: Description of the recipe. .. py:method:: title(title: str) -> RecipeBuilder Set the title of the recipe. :param title: Title of the recipe. .. py:method:: dataset(dataset: ewatercycle.esmvaltool.schema.Dataset | str | dict) -> RecipeBuilder Set the dataset of the recipe. :param dataset: Dataset to use for the recipe. 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. Only one dataset is allowed when generating eWaterCycle forcings. Calling this method again will overwrite the previous dataset. .. py:method:: mip(value: str) -> RecipeBuilder Set the default time frequency for all later added variables. :param value: time frequency, e.g. 'day', 'Eday', 'CFday', 'fx'. Defaults to 'day'. .. py:method:: start(value: int) -> RecipeBuilder Set the start year of the recipe. :param value: Start year of the recipe. .. py:method:: end(value: int) -> RecipeBuilder Set the end year of the recipe. :param value: End year of the recipe. .. py:method:: regrid(scheme: str, target_grid: ewatercycle.esmvaltool.schema.TargetGrid) -> RecipeBuilder Regrid the data from the dataset to a different grid. :param scheme: Regridding scheme to use. See https://docs.esmvaltool.org/projects/ESMValCore/en/latest/recipes/recipe_file.html#regrid :param target_grid: Target grid to regrid to. .. py:method:: shape(file: pathlib.Path | str, crop: bool = True, decomposed: bool = False) -> RecipeBuilder Select data within a shapefile. :param file: Path to shapefile. :param crop: Crop data to shapefile extent. Otherwise data outside shapefile extent is set to NaN. :param decomposed: Decompose shapefile into separate polygons. .. py:method:: region(start_longitude: float, end_longitude: float, start_latitude: float, end_latitude: float) -> RecipeBuilder Select data within a region. :param start_longitude: Start longitude of the region. :param end_longitude: End longitude of the region. :param start_latitude: Start latitude of the region. :param end_latitude: End latitude of the region. .. py:method:: region_by_shape(shape: pathlib.Path, pad=0) -> RecipeBuilder Select data within a region defined by extents of a shapefile. :param shape: Path to shapefile. :param pad: Pad the region with this many degrees. .. py:method:: lump(operator: Literal['mean', 'median', 'std_dev', 'sum', 'variance', 'min', 'max', 'rms'] = 'mean') -> RecipeBuilder Lump gridded data into a single value spatially. See https://docs.esmvaltool.org/projects/ESMValCore/en/latest/api/esmvalcore.preprocessor.html#esmvalcore.preprocessor.area_statistics :param operator: The operator to use for lumping. .. py:method:: add_variables(variables: collections.abc.Sequence[str]) -> RecipeBuilder Add variables to the recipe. :param variables: Names of variables to add to the recipe. .. py:method:: add_variable(variable: str, mip: str | None = None, units: str | None = None, stats: ewatercycle.esmvaltool.schema.ClimateStatistics | None = None, short_name: str | None = None, start_year: int | Literal[False] | None = None, end_year: int | Literal[False] | None = None) Add a variable to the recipe. :param variable: The name of the variable to add. :param mip: The MIP table to use for the variable. If not given then defaults to what was set with self.mip(value). :param units: The unit to convert the variable to. Default no conversion. See https://docs.esmvaltool.org/projects/ESMValCore/en/latest/recipes/recipe_file.html#convert-units :param stats: The climate statistics to apply to the variable. Defaults to not applying any statistics. :param short_name: A short name for the variable. Defaults to variable name. :param start_year: The start year of the variable. Defaults to start year of dataset. Use False to disable temporal selection. :param end_year: The end year of the variable. Defaults to end year of dataset. Use False to disable temporal selection. .. py:method:: script(script: str, arguments: dict[str, str] | None = None) -> RecipeBuilder Set script of recipe. When script has not been set will default to copying the ESMValTool preprocessed files to the output directory using the :py:mod:`ewatercycle.esmvaltool.diagnostic.copier` script. :param script: Path to script to run. :param arguments: Arguments to pass to the script. .. py:function:: build_generic_distributed_forcing_recipe(start_year: int, end_year: int, shape: pathlib.Path, dataset: ewatercycle.esmvaltool.schema.Dataset | str | dict = 'ERA5', variables: collections.abc.Sequence[str] = ('pr', 'tas', 'tasmin', 'tasmax')) Build a generic distributed forcing recipe. :param start_year: Start year of the data to retrieve. :param end_year: End year of the data to retrieve. :param shape: Path to shapefile. Used for spatial selection. :param dataset: Dataset to use for the recipe. :param variables: Names of variables to add to the recipe. Recipe will return a NetCDF file for each variable. .. py:function:: build_generic_lumped_forcing_recipe(start_year: int, end_year: int, shape: pathlib.Path, dataset: ewatercycle.esmvaltool.schema.Dataset | str | dict = 'ERA5', variables: collections.abc.Sequence[str] = ('pr', 'tas', 'tasmin', 'tasmax')) Build a generic lumped forcing recipe. :param start_year: Start year of the data to retrieve. :param end_year: End year of the data to retrieve. :param shape: Path to shapefile. Used for spatial selection. :param dataset: Dataset to use for the recipe. :param variables: Names of variables to add to the recipe. Recipe will return a NetCDF file for each variable.