lime_tbx.application.filedata.netcdfcommon module

Common functionalities for reading netCDF files with xarray.

class lime_tbx.application.filedata.netcdfcommon.AttrSchema(type: Any | None = None, value: Any | None = None)[source]

Bases: AttrSchema

Re-implementation of xarray_schema.AttrSchema but correctly validating and raising `SchemaError`s

validate(attr: Any)[source]

Validate that attr follows the schema.

Parameters:

attr (Any) – xarray dataset attribute that is going to be validated against the schema.

Raises:

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

class lime_tbx.application.filedata.netcdfcommon.DTypeSchema(dtype: dtype[Any] | None | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any])[source]

Bases: DTypeSchema

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 (xarray_schema.DatasetSchema) – Schema that the dataset is going to be validated against.

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

  • other_schemas (Iterable[xarray_schema.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 True by default.

Returns:

ds – Dataset with the information of the netCDF file.

Return type:

xr.Dataset