libera_utils.io.netcdf.LiberaVariable#
- class libera_utils.io.netcdf.LiberaVariable(*, metadata: VariableMetadata, variable_encoding: dict | None = {'_FillValue': None, 'complevel': 4, 'zlib': True}, data: DataArray | None = None)#
Bases:
BaseModel
Pydantic model for a Libera variable.
- metadata#
The metadata associated with the variable, including its dimensions, valid range, missing value, units, and data type.
- Type:
- variable_encoding#
A dictionary specifying how the variable’s data should be encoded when written to a NetCDF file.
- Type:
dict | None
- data#
The data associated with the variable, stored as an xarray DataArray
- Type:
DataArray | None
- Attributes:
model_extra
Get extra fields set during validation.
model_fields_set
Returns the set of fields that have been explicitly set on this model instance.
Methods
Ensure the dimensions in provided data are correct on instantiation.
copy
(*[, include, exclude, update, deep])Returns a copy of the model.
copy_incoming_metadata
(input_metadata)Ensure that the metadata is copied in as a VariableMetadata object
model_construct
([_fields_set])Creates a new instance of the Model class with validated data.
model_copy
(*[, update, deep])!!! abstract "Usage Documentation"
model_dump
(*[, mode, include, exclude, ...])!!! abstract "Usage Documentation"
model_dump_json
(*[, indent, include, ...])!!! abstract "Usage Documentation"
model_json_schema
([by_alias, ref_template, ...])Generates a JSON schema for a model class.
model_parametrized_name
(params)Compute the class name for parametrizations of generic classes.
model_post_init
(context, /)Override this method to perform additional initialization after __init__ and model_construct.
model_rebuild
(*[, force, raise_errors, ...])Try to rebuild the pydantic-core schema for the model.
model_validate
(obj, *[, strict, ...])Validate a pydantic model instance.
model_validate_json
(json_data, *[, strict, ...])!!! abstract "Usage Documentation"
model_validate_strings
(obj, *[, strict, ...])Validate the given object with string data against the Pydantic model.
set_data
(data)Takes the provided data into an internal DataArray with Libera defined dimensions
set_data_as_dataarray
(input_data)Ensure that the data is internally stored as a DataArray when passed in to the model.
construct
dict
from_orm
json
parse_file
parse_obj
parse_raw
schema
schema_json
update_forward_refs
validate
- __init__(**data: Any) None #
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
Methods
Ensure the dimensions in provided data are correct on instantiation.
copy_incoming_metadata
(input_metadata)Ensure that the metadata is copied in as a VariableMetadata object
set_data
(data)Takes the provided data into an internal DataArray with Libera defined dimensions
set_data_as_dataarray
(input_data)Ensure that the data is internally stored as a DataArray when passed in to the model.
Attributes
model_computed_fields
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
Get extra fields set during validation.
model_fields
Returns the set of fields that have been explicitly set on this model instance.
- _check_for_bad_dimensions(data_array: DataArray | ndarray)#
Internal method to check provided data set has dimension that match the shape expected in the metadata.
- Parameters:
data_array (DataArray | np.ndarray) – The data array whose shape will be checked against the metadata dimensions.
- Raises:
ValueError – If the provided data array does not match the expected dimensions in the metadata.
Notes
This will ignore any “dynamic” data dimensions. If the provided data does not match the expected dimensions in the metadata, it raises a ValueError.
- _set_all_dynamic_dimension_lengths(data_array: DataArray | ndarray) None #
Internal method to set any dynamic dimension lengths to match a given array
- Parameters:
data_array (DataArray | np.ndarray) – The data array whose shape will be used to set the dynamic dimensions in the metadata.
Notes
This should be run after the _check_for_bad_dimensions below. This method iterates through the dimensions of the metadata and sets the size of any dynamic dimensions to the corresponding size in the provided data array.
- _set_data_with_dimensions_to_match_metadata(data_array: DataArray | ndarray)#
Internal method to ensure the dimensions from the metadata are used in the data itself
- Parameters:
data_array (DataArray | np.ndarray) – The data array to be set for the variable. It can be a DataArray or a numpy ndarray.
- Raises:
TypeError – If the provided data is not of type DataArray or np.ndarray.
ValueError – If the provided data does not match the expected dimensions in the metadata.
Notes
This method checks the provided data array against the metadata dimensions and sets the data to a new DataArray with the correct dimensions. It also checks for bad dimensions and sets any dynamic dimension lengths.
- confirm_and_set_data_with_dimensions()#
Ensure the dimensions in provided data are correct on instantiation.
- Returns:
The instance of LiberaVariable with the data set if it was provided.
- Return type:
Notes
This method is called after the model is validated. It checks if the data is not None and sets the data using the set_data method. If the data is None, it does nothing.
- classmethod copy_incoming_metadata(input_metadata: VariableMetadata)#
Ensure that the metadata is copied in as a VariableMetadata object
- Parameters:
input_metadata (VariableMetadata) – The metadata to be copied into the variable.
- Returns:
A deep copy of the input metadata.
- Return type:
- Raises:
TypeError – If the input metadata is not of type VariableMetadata.
Notes
This method validates that the input metadata is of type VariableMetadata and returns a deep copy of it.
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- property model_extra: dict[str, Any] | None#
Get extra fields set during validation.
- Returns:
A dictionary of extra fields, or None if config.extra is not set to “allow”.
- property model_fields_set: set[str]#
Returns the set of fields that have been explicitly set on this model instance.
- Returns:
- A set of strings representing the fields that have been set,
i.e. that were not filled from defaults.
- set_data(data: DataArray | ndarray)#
Takes the provided data into an internal DataArray with Libera defined dimensions
- Parameters:
data (DataArray | np.ndarray) – The data to be set for the variable. It can be a DataArray or a numpy ndarray.
- Raises:
TypeError – If the provided data is not of type DataArray or np.ndarray.
ValueError – If the provided data does not match the expected dimensions in the metadata.
Notes
This method is used to set the data for the variable. It checks if the data is a DataArray or a numpy ndarray, and if so, it sets the data while ensuring that the dimensions match those defined in the metadata.
- classmethod set_data_as_dataarray(input_data: DataArray | ndarray | None)#
Ensure that the data is internally stored as a DataArray when passed in to the model.
- Parameters:
input_data (DataArray | np.ndarray | None) – The data to be set for the variable. It can be a DataArray, a numpy ndarray, or None.
- Returns:
A DataArray containing the input data, or None if the input data is None.
- Return type:
DataArray | None
- Raises:
TypeError – If the input data is not of type DataArray or np.ndarray.
Notes
This method checks if the input data is None, a numpy ndarray, or an xarray DataArray. If it is None, it returns None. If it is a numpy ndarray, it converts it to a DataArray. If it is already a DataArray, it returns it as is. If the input data is of any other type, it raises a TypeError.