tsdat.utils

Classes

ParameterizedClass

Base class for any class that accepts ‘parameters’ as an argument. Sets the default

Functions

assign_data

Assigns the data to the specified variable in the dataset.

decode_cf

Decodes the dataset according to CF conventions. This helps ensure that the dataset

get_filename

Returns a key consisting of the dataset’s datastream, starting date/time, the

get_start_date_and_time_str

Gets the start date and start time strings from a Dataset. The strings are formatted

get_start_time

Gets the earliest ‘time’ value and returns it as a pandas Timestamp, which resembles

record_corrections_applied

Records the message on the ‘corrections_applied’ attribute of the specified variable

Function Descriptions

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

The dataset with data assigned to it.

Return type

xr.Dataset

tsdat.utils.decode_cf(dataset: xarray.Dataset)xarray.Dataset[source]

Decodes the dataset according to CF conventions. This helps ensure that the dataset is formatted and encoded correctly after it has been constructed or modified. This method is a thin wrapper around xarray.decode_cf() which

Parameters

dataset (xr.Dataset) – The dataset to decode.

Returns

The decoded dataset.

Return type

xr.Dataset

tsdat.utils.get_filename(dataset: xarray.Dataset, extension: str, title: Optional[str] = None)str[source]

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 /

  • times) (ending) –

  • extension (str) – The file extension that should be used.

  • title (Optional[str]) – An optional title that will be placed between the start

  • and the extension in the generated filename. (time) –

Returns

The filename constructed from provided parameters.

Return type

str

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

The start date and time as strings like “YYYYmmdd”, “HHMMSS”.

Return type

Tuple[str, str]

tsdat.utils.get_start_time(dataset: xarray.Dataset)pandas.Timestamp[source]

Gets the earliest ‘time’ value and returns it as a pandas Timestamp, which resembles the built-in python datetime.datetime module.

Parameters

dataset (xr.Dataset) – The dataset whose start time should be retrieved.

Returns

The timestamp of the earliest time value in the dataset.

Return type

pd.Timestamp

tsdat.utils.record_corrections_applied(dataset: xarray.Dataset, variable_name: str, message: str)None[source]

Records the message on the ‘corrections_applied’ attribute of the specified variable in the dataset.

Parameters
  • dataset (xr.Dataset) – The corrected dataset.

  • variable_name (str) – The name of the variable in the dataset.

  • message (str) – The