tsdat.utils
¶
Classes¶
Base class for any class that accepts 'parameters' as an argument. |
Functions¶
Assigns the data to the specified variable in the dataset. |
|
Wrapper around xarray.decode_cf() which handles additional edge cases. |
|
Returns the standardized filename for the provided dataset. |
|
Gets the start date and start time strings from a Dataset. |
|
Gets the earliest 'time' value and returns it as a pandas Timestamp. |
|
Records the message on the 'corrections_applied' attribute. |
Function Descriptions
Attributes¶
- class tsdat.utils.ParameterizedClass[source]¶
Bases:
pydantic.BaseModel
Base class for any class that accepts ‘parameters’ as an argument.
Sets the default ‘parameters’ to {}. Subclasses of ParameterizedClass should override the ‘parameters’ properties to support custom required or optional arguments from configuration files.
- tsdat.utils.assign_data(dataset: xarray.Dataset, data: numpy.typing.NDArray[Any], variable_name: str) xarray.Dataset [source]¶
Assigns the data to the specified variable in the dataset.
If the variable exists and it is a data variable, then the DataArray for the specified variable in the dataset will simply have its data replaced with the new numpy array. If the variable exists and it is a coordinate variable, then the data will replace the coordinate data. If the variable does not exist in the dataset then a KeyError will be raised.
- Parameters
dataset (xr.Dataset) – The dataset where the data should be assigned.
data (NDArray[Any]) – The data to assign.
variable_name (str) – The name of the variable in the dataset to assign data to.
- Raises
KeyError – Raises a KeyError if the specified variable is not in the dataset’s coords or data_vars dictionary.
- Returns
xr.Dataset – The dataset with data assigned to it.
- tsdat.utils.decode_cf(dataset: xarray.Dataset) xarray.Dataset [source]¶
Wrapper around xarray.decode_cf() which handles additional edge cases.
This helps ensure that the dataset is formatted and encoded correctly after it has been constructed or modified. Handles edge cases for units and data type encodings on datetime variables.
- Parameters
dataset (xr.Dataset) – The dataset to decode.
- Returns
xr.Dataset – The decoded dataset.
- tsdat.utils.get_filename(dataset: xarray.Dataset, extension: str, title: Optional[str] = None) str [source]¶
Returns the standardized filename for the provided dataset.
Returns a key consisting of the dataset’s datastream, starting date/time, the extension, and an optional title. For file-based storage systems this method may be used to generate the basename of the output data file by providing extension as ‘.nc’, ‘.csv’, or some other file ending type. For ancillary plot files this can be used in the same way by specifying extension as ‘.png’, ‘.jpeg’, etc and by specifying the title, resulting in files named like ‘<datastream>.20220424.165314.plot_title.png’.
- Parameters
dataset (xr.Dataset) – The dataset (used to extract the datastream and starting / ending times).
extension (str) – The file extension that should be used.
title (Optional[str]) – An optional title that will be placed between the start time and the extension in the generated filename.
- Returns
str – The filename constructed from provided parameters.
- tsdat.utils.get_start_date_and_time_str(dataset: xarray.Dataset) Tuple[str, str] [source]¶
Gets the start date and start time strings from a Dataset.
- The strings are formatted using strftime and the following formats:
date: “%Y%m%d”
time: “”%H%M%S”
- Parameters
dataset (xr.Dataset) – The dataset whose start date and time should be retrieved.
- Returns
Tuple[str, str] – The start date and time as strings like “YYYYmmdd”, “HHMMSS”.
- tsdat.utils.get_start_time(dataset: xarray.Dataset) pandas.Timestamp [source]¶
Gets the earliest ‘time’ value and returns it as a pandas Timestamp.
- Parameters
dataset (xr.Dataset) – The dataset whose start time should be retrieved.
- Returns
pd.Timestamp – The timestamp of the earliest time value in the dataset.
- tsdat.utils.record_corrections_applied(dataset: xarray.Dataset, variable_name: str, message: str) None [source]¶
Records the message on the ‘corrections_applied’ attribute.
- Parameters
dataset (xr.Dataset) – The corrected dataset.
variable_name (str) – The name of the variable in the dataset.
message (str) – The message to record.