lime_tbx.application.filedata.netcdfcommon module

Common functionalities for reading netCDF files with xarray.

lime_tbx.application.filedata.netcdfcommon.get_length_conversion_factor(from_unit: str, to_unit: str) float[source]

Get the conversion factor between two metric length units.

This function calculates the factor needed to convert a length measurement from one metric unit to another. It supports various unit spellings, including American and British English variations.

Parameters:
  • from_unit (str) – The original unit of measurement. Supported units include metres (m), centimetres (cm), millimeters (mm), micrometres (μm / um), and nanometres (nm).

  • to_unit (str) – The target unit of measurement. Must be one of the supported values listed above.

Returns:

The multiplication factor to convert from from_unit to to_unit.

Return type:

float

Raises:

ValueError – If either from_unit or to_unit is not a recognized metric length unit.

lime_tbx.application.filedata.netcdfcommon.validate_schema(dss: DatasetSchema, ds: Dataset, other_schemas: Iterable[DatasetSchema] | None = None)[source]

Validates the dataset ds against the schema dss.

Parameters:
  • dss (xarrera.DatasetSchema) – Schema that the dataset is going to be validated against.

  • ds (xarray.Dataset) – Dataset to validate.

  • other_schemas (Iterable[xarrera.DatasetSchema]) – If the dataset can have multiple schemas, it will be validated against those before the canonical one. Once it succeeds in a validation, it’s understood as validated and finishes the validation.

Raises:

SchemaError – Error indicating that the dataset is not following the schema.

lime_tbx.application.filedata.netcdfcommon.xr_open_dataset(filepath: str, mask_fillvalue: bool | Mapping[str, bool] = True, mask_limits: bool | Mapping[str, bool] = True) Dataset[source]

Open a netCDF dataset as an xarray Dataset

Read the netCDF dataset masking the appropiate values, checking against the variable attributes _FillValue, valid_min and valid_max.

Parameters:
  • filepath (str) – Path where the netCDF file is located

  • mask_fill_value (bool | Mapping[str, bool]) – If True, tell xarray replace array values equal to _FillValue with NA and scale values. Pass a mapping, e.g. {“my_variable”: False}, to toggle this feature per-variable individually. If a mapping is passed and a variable is missing, it’s understood that the variable is mapped to True by default.

  • mask_limits (bool | Mapping[str, bool]) – If True, replace array values lower than valid_min or higher than valid_max with NA, if present. Pass a mapping, e.g. {“my_variable”: False}, to toggle this feature per-variable individually. If a mapping is passed and a variable is missing, it’s understood that the variable is mapped to the default value. The default value is the one defined for the “__default__” mapping, or True if not specified.

Returns:

ds – Dataset with the information of the netCDF file.

Return type:

xr.Dataset