tsdat.utils

Classes

ParameterizedClass

Base class for any class that accepts 'parameters' as an argument.

Functions

assign_data

Assigns the data to the specified variable in the dataset.

decode_cf

Wrapper around xarray.decode_cf() which handles additional edge cases.

generate_schema

get_datastream

get_fields_from_datastream

Extracts fields from the datastream.

get_filename

Returns the standardized filename for the provided dataset.

get_start_date_and_time_str

Gets the start date and start time strings from a Dataset.

get_start_time

Gets the earliest 'time' value and returns it as a pandas Timestamp.

record_corrections_applied

Records the message on the 'corrections_applied' attribute.

Function Descriptions

Attributes

DATASTREAM_TEMPLATE

FILENAME_TEMPLATE

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.

parameters: Any[source]
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.generate_schema(dir: pathlib.Path = typer.Option(Path('.vscode/schema/'), file_okay=False, dir_okay=True), schema_type: SchemaType = typer.Option(SchemaType.all))[source]
tsdat.utils.get_datastream(**global_attrs: str) str[source]
tsdat.utils.get_fields_from_datastream(datastream: str) Dict[str, str][source]

Extracts fields from the datastream.

WARNING: this only works for the default datastream template.

tsdat.utils.get_filename(dataset: xarray.Dataset, extension: str, title: str | None = 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.

tsdat.utils.DATASTREAM_TEMPLATE[source]
tsdat.utils.FILENAME_TEMPLATE[source]