seagrid.utils#

Helpers from seagrid 2.x: date ranges, directories, netCDF checks, subsetting.

New code uses the DataSource members instead (dtm_vec, datadir, downloaded, open_dataset(bbox=...)).

Functions

datadir(settings)

Create and return the data directory path from a settings dictionary.

dtm_vec(settings)

Generate a pandas date range from a settings dictionary.

has_nc_data_var(full_filename, data_var)

Check if a netCDF file contains a specific data variable.

is_nc_file(full_filename)

Check if a file exists and is a valid netCDF file.

slice_dataframe(ds, settings[, latname, lonname])

Subset an xarray Dataset to a lat/lon bounding box from settings.

vprint(text[, verbose])

Log text at INFO level, or WARNING level if verbose.

seagrid.utils.datadir(settings)[source]#

Create and return the data directory path from a settings dictionary.

The directory (and any missing parents) is created if it does not already exist.

Parameters:

settings (dict) – Dictionary containing the key datadir with the desired directory path.

Returns:

str – The data directory path stored in settings["datadir"].

Return type:

str

seagrid.utils.dtm_vec(settings)[source]#

Generate a pandas date range from a settings dictionary.

Parameters:

settings (dict) – Dictionary containing the keys dtm_min, dtm_max, and dtm_freq which are forwarded to pandas.date_range.

Returns:

pandas.DatetimeIndex – Date range spanning from dtm_min to dtm_max at the frequency given by dtm_freq.

Return type:

DatetimeIndex

seagrid.utils.has_nc_data_var(full_filename, data_var)[source]#

Check if a netCDF file contains a specific data variable.

The file is opened with xarray.open_dataset and the presence of data_var is verified.

Parameters:
  • full_filename (str) – Path to the netCDF file.

  • data_var (str) – Name of the variable to look for in the dataset.

Returns:

bool – True if the file exists, is a valid netCDF file, and contains data_var; False otherwise.

Return type:

bool

seagrid.utils.is_nc_file(full_filename)[source]#

Check if a file exists and is a valid netCDF file.

Attempts to open the file with xarray.open_dataset. If the file does not exist or cannot be opened, False is returned.

Parameters:

full_filename (str) – Absolute or relative path to the file to check.

Returns:

bool – True if the file exists and can be opened as a netCDF dataset, False otherwise.

Return type:

bool

seagrid.utils.ll2dist2vec(lonvec1, latvec1, lonvec2, latvec2)[source]#

Return the distance between corresponding points of two sets.

Parameters:
  • lonvec1 (ArrayLike)

  • latvec1 (ArrayLike)

  • lonvec2 (ArrayLike)

  • latvec2 (ArrayLike)

Return type:

ndarray[tuple[Any, …], dtype[float64]]

seagrid.utils.slice_dataframe(ds, settings, latname='lat', lonname='lon')[source]#

Subset an xarray Dataset to a lat/lon bounding box from settings.

Parameters:
  • ds (xarray.Dataset) – The dataset to subset.

  • settings (dict) – Dictionary with keys latmin, latmax, lonmin, and lonmax defining the bounding box in degrees.

  • latname (str, optional) – Name of the latitude coordinate in ds. Default is "lat".

  • lonname (str, optional) – Name of the longitude coordinate in ds. Default is "lon".

Returns:

xarray.Dataset – The subset of ds whose coordinates fall within the bounding box defined by settings.

Return type:

Dataset

seagrid.utils.vprint(text, verbose=False)[source]#

Log text at INFO level, or WARNING level if verbose.

In 2.x this printed when the verbose setting was on. Configure the seagrid logger instead.

Parameters:
  • text (str) – The message.

  • verbose (bool, optional) – Log at WARNING level so the message is shown by default.

Return type:

None

Modules

grids

Legacy grid helpers, kept for the 2.x series.

gridtools

Grid-spacing helpers (2.x interface), computed by seagrid.grids.metrics.

lldist

Great-circle distances (2.x interface).