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:
AttrSchemaRe-implementation of
xarray_schema.AttrSchemabut correctly validating and raising `SchemaError`s
- 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_unittoto_unit.- Return type:
float
- Raises:
ValueError – If either
from_unitorto_unitis 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
dsagainst the schemadss.- 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_minandvalid_max.- Parameters:
filepath (str) – Path where the netCDF file is located
mask_fill_value (bool | Mapping[str, bool]) – If True, tell
xarrayreplace array values equal to_FillValuewithNAand 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_minor higher thanvalid_maxwithNA, 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