Skip to content

get_filename

Functions:

Name Description
get_filename

Attributes#

Functions#

get_filename #

get_filename(
    dataset: xr.Dataset,
    extension: str,
    title: Optional[str] = None,
) -> str

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 '.20220424.165314.plot_title.png'.

Parameters:

Name Type Description Default
dataset Dataset

The dataset (used to extract the datastream and starting / ending times).

required
extension str

The file extension that should be used.

required
title Optional[str]

An optional title that will be placed between the start time and the extension in the generated filename.

None

Returns:

Name Type Description
str str

The filename constructed from provided parameters.


Source code in tsdat/utils/get_filename.py
def get_filename(
    dataset: xr.Dataset, extension: str, title: Optional[str] = None
) -> str:
    """---------------------------------------------------------------------------------
    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'.

    Args:
        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.

    ---------------------------------------------------------------------------------"""
    substitutions: dict[str, str] = dict()
    substitutions.update(get_fields_from_dataset(dataset))
    substitutions.update(extension=extension.lstrip("."), title=title)
    return FILENAME_TEMPLATE.substitute(substitutions)