cordex.cmor.cmorize_variable

cordex.cmor.cmorize_variable#

cmorize_variable(ds, out_name, cmor_table, dataset_table, mapping_table=None, grids_table=None, inpath=None, replace_coords=False, allow_units_convert=False, allow_resample=False, input_freq=None, domain_id=None, crop=None, time_units=None, rewrite_time_axis=False, outpath=None, **kwargs)[source]#

Cmorizes a variable.

This functions call the python cmor API and creates a cmorized NetCDF file from the input dataset. Coordinates of the input dataset should be understandable by cf_xarray if they follow basic CF conventions. If a vertical coordinate is available, it should have an axis="Z" attribute so it can be understood by cf_xarray and it should be named after the unique key of the coordinate in the cmor grids table (not the out_name), e.g., name it sdepth if you need a soil layer coordinate instead of depth. Variables may have one of the following attributes that are used as keyword arguments in the call to cmor_variable, e.g., positive, missing_value, original_name, history or comment.

Parameters:
  • ds (xr.Dataset) – Dataset containing at least the variable that should be cmorized.

  • out_name (str) – CF out_name of the variable that should be cmorized. The corresponding variable name in the dataset is looked up from the mapping_table if provided.

  • cmor_table (str or dict) – Cmor table dict of filepath to cmor table (json).

  • dataset_table (str or dict) – Dataset table dict of filepath to dataset cmor table (json).

  • mapping_table (dict) – Mapping of input variable names and meta data to CF out_name. Required if the variable name in the input dataset is not equal to out_name.

  • grids_table (str) – Filepath to cmor grids table.

  • inpath (str) – Path to cmor tables, if inpath == None, inpath is the path to cmor_table. This is required to find additional cmor tables, like CMIP6_coordinates, CMIP6_grids etc.

  • replace_coords (bool) – Replace coordinates from input file and create them from archive specifications. Only possible, if a domain identifier is given in the global attributes or as a keyword argument, e.g., see the domain_id keyword.

  • allow_units_convert (bool) – Allow units to be converted if they do not agree with the units in the cmor table. Defaults to False to make the user aware of having correct units attributes set.

  • allow_resample (bool) – Allow to resample temporal data to the frequency required from the cmor table. Handles both downsampling and upsampling. Defaults to False to make users aware of the correct frequency input.

  • input_freq (str) – The frequency of the input dataset in pandas notation. It None and the dataset contains a time axis, the frequency will be determined automatically using pandas.infer_freq if possible.

  • domain_id (str) – Cordex domain identifier. If None, the domain will be determined by the domain_id global attribute if available. If domain_id is given as a keyword, it will override a possible domain_id global attribute.

  • crop (bool) – Crop dataset to official Cordex domain if it contains, e.g., a nudging zone. If set to None, cropping will be done if a domain identifier is given or the domain can be identified automatically. Set to crop=False, if you have an ‘unofficial’ domain_id.

  • time_units (str) – Time units of the cmorized dataset (ISO 8601). If None, time units will be set to default ("days since 1950-01-01T00:00:00"). If time_units='input', the original time units of the input dataset are used.

  • rewrite_time_axis (bool) – Rewrite the time axis to CF compliant timestamps.

  • outpath (str) – Root directory for output (can be either a relative or full path). This will override the outpath defined in the dataset cmor input table (dataset_table).

  • **kwargs – Argumets passed to prepare_variable.

Returns:

filename – Filepath to cmorized file.

Example

To cmorize a dataset, you can use ,e.g.,:

import cordex as cx
from cordex.cmor import cmorize_variable
from cordex.tables import cordex_cmor_table

ds = cx.domain("EUR-11", dummy="topo").rename(topo="orog")
dataset_table = cordex_cmor_table(f"CORDEX-CMIP6_remo_example")

filename = cmorize_variable(
    ds,
    "orog",
    cmor_table=cordex_cmor_table("CORDEX-CMIP6_fx"),
    dataset_table=dataset_table,
    replace_coords=True,
    allow_units_convert=True,
)