access_nri_intake.source.utils#

Shared utilities for writing Intake-ESM builders and their parsers

Attributes#

Exceptions#

EmptyFileError

Common base class for all non-exit exceptions.

Classes#

HashableIndexes

Consumes either an xarray dataset or its _indexes attribute, and creates a

Functions#

get_timeinfo(ds, filename_frequency, time_dim)

Get start date, end date and frequency of a xarray dataset. Stolen and adapted from the

open_dataset_cached(*args, **kwargs)

Cache xarray open dataset so that multiple opens of the same file can reuse

open_nc_view(file[, time_dim])

Read a netcdf file's header with netCDF4 and return a dataset-shaped view

Module Contents#

access_nri_intake.source.utils.FREQUENCY_STATIC = 'fx'#
access_nri_intake.source.utils.FILENAME_TO_FREQ#
exception access_nri_intake.source.utils.EmptyFileError#

Bases: Exception

Common base class for all non-exit exceptions.

Initialize self. See help(type(self)) for accurate signature.

class access_nri_intake.source.utils.HashableIndexes(*, ds=None, _indexes=None, drop_indices=None)#

Consumes either an xarray dataset or its _indexes attribute, and creates a hashable representation of the indexes. Can be used to compare datasets & whether they are mergeable based on their indexes, and potentially for labelling grids in a catalog.

dict#
xxh#
keys()#
access_nri_intake.source.utils.get_timeinfo(ds, filename_frequency, time_dim)#

Get start date, end date and frequency of a xarray dataset. Stolen and adapted from the cosima cookbook, see COSIMA/cosima-cookbook

Parameters:
ds: :py:class:`xarray.Dataset`

The dataset to parse the time info from

filename_frequency: str

Frequency as determined from the filename

time_dim: str

The name of the time dimension

Returns:
start_date: str

The start date of the dataset

end_date: str

The end date of the dataset

frequency: str

The frequency of the dataset

Raises:
EmptyFileError

If the dataset has a valid unlimited dimension, but no data

access_nri_intake.source.utils.open_dataset_cached(*args, **kwargs)#

Cache xarray open dataset so that multiple opens of the same file can reuse the returned object. As we don’t currently access the data variable data then we shouldn’t need to worry about xarray’s laziness and the Dataset object should have the needed info even if close()ed.

access_nri_intake.source.utils.open_nc_view(file, time_dim='time')#

Read a netcdf file’s header with netCDF4 and return a dataset-shaped view carrying the information parse_ncfile needs: dimension sizes, every variable’s attributes and dimensions, the global attributes, and the raw array data of the time coordinate and its bounds variable (the only data get_timeinfo() reads).

Parameters:
file: str

The path to the netcdf file.

time_dim: str

The name of the time dimension. Defaults to “time”.

Returns:
view: _NCDatasetView