seagrid.download.opendap#

Open remote datasets over OPeNDAP and cache them locally.

open_remote() opens a THREDDS/Hyrax OPeNDAP URL lazily with xarray, either through pydap (with a retrying requests.Session, needed for dap2:///dap4:// URLs and authenticated servers) or through the netCDF4 library. cache() writes a (subset of a) dataset to a local netCDF file or Zarr store via a .partial name, and retrieve() does both. Writes can be deferred (compute=False) so that several downloads run in parallel under one dask computation (compute_all()).

Module Attributes

ENGINES

Engines accepted by open_remote().

Functions

cache_dataset(ds, dest, *[, force, compute, ...])

Write a dataset to a local netCDF file or Zarr store.

compute_all(writes, **compute_kw)

Run several PendingWrite jobs in one dask computation.

open_remote(url, *[, engine, session, ...])

Open a remote OPeNDAP dataset lazily.

retrieve(url, dest, *[, variables, isel, ...])

Open a remote dataset, subset it and cache it locally.

Classes

PendingWrite(delayed, dest)

A deferred write of a dataset to a local cache file.

class seagrid.download.opendap.PendingWrite(delayed, dest)[source]#

Bases: object

A deferred write of a dataset to a local cache file.

Variables:
  • delayed (dask.delayed.Delayed) – The pending write to the .partial path.

  • dest (pathlib.Path) – Final path, which the file is moved to by compute().

Parameters:
compute(**compute_kw)[source]#

Run the write and move the result into place.

Parameters:

**compute_kw (Any) – Passed to dask.delayed.Delayed.compute().

Returns:

pathlib.Path – dest.

Return type:

Path

seagrid.download.opendap.cache_dataset(ds, dest, *, force=False, compute=True, encoding=None)[source]#

Write a dataset to a local netCDF file or Zarr store.

Parameters:
  • ds (xarray.Dataset) – Dataset to write, typically lazily opened from a remote server.

  • dest (str or os.PathLike) – Output path. A .zarr suffix writes a Zarr store, anything else netCDF.

  • force (bool, optional) – Write even if dest is complete. Default False.

  • compute (bool, optional) – Write now (default), or return a PendingWrite.

  • encoding (mapping, optional) – Passed to xarray.Dataset.to_zarr() / to_netcdf.

Returns:

pathlib.Path, PendingWrite or None – dest when written now, a PendingWrite when deferred, or None (deferred) / dest (immediate) when it was already cached.

Raises:

DownloadError – If reading the remote data fails during the write.

Return type:

Path | PendingWrite | None

seagrid.download.opendap.compute_all(writes, **compute_kw)[source]#

Run several PendingWrite jobs in one dask computation.

Parameters:
  • writes (iterable of PendingWrite or None) – Jobs; None entries (already cached) are skipped.

  • **compute_kw – Passed to dask.compute(), e.g. scheduler="threads".

Returns:

list of pathlib.Path – The written files.

Return type:

list[Path]

seagrid.download.opendap.open_remote(url, *, engine='pydap', session=None, chunks=None, timeout=None, **xr_kwargs)[source]#

Open a remote OPeNDAP dataset lazily.

Parameters:
  • url (str) – OPeNDAP URL. For engine="pydap" a dap2:// or dap4:// scheme selects the protocol.

  • engine ({"pydap", "netcdf4"}, optional) – Access library. Default "pydap".

  • session (requests.Session, optional) – Session for pydap, e.g. an authenticated one. Default: a new retrying session (seagrid.download.http.new_session()).

  • chunks (mapping or str, optional) – Dask chunks, passed to xarray.open_dataset().

  • timeout (float or (float, float), optional) – pydap only: seconds to wait for the server, or (connect, read) timeouts, for every request including each chunk read later.

  • **xr_kwargs – Passed to xarray.open_dataset().

Returns:

xarray.Dataset – Lazily loaded dataset.

Raises:
Return type:

Dataset

seagrid.download.opendap.retrieve(url, dest, *, variables=None, isel=None, sel=None, engine='pydap', session=None, chunks=None, force=False, compute=True)[source]#

Open a remote dataset, subset it and cache it locally.

Parameters:
  • url (str) – OPeNDAP URL.

  • dest (str or os.PathLike) – Local netCDF file or .zarr store.

  • variables (sequence of str, optional) – Variables to keep. Default: all.

  • isel (mapping, optional) – Index / label selections, applied in that order.

  • sel (mapping, optional) – Index / label selections, applied in that order.

  • engine ({"pydap", "netcdf4"}, optional) – See open_remote().

  • session (requests.Session, optional) – See open_remote().

  • chunks (mapping or str, optional) – Dask chunks for reading. Default {} (the server’s chunks).

  • force (bool, optional) – Download even if dest is complete.

  • compute (bool, optional) – Write now, or return a PendingWrite.

Returns:

pathlib.Path, PendingWrite or None – See cache_dataset().

Return type:

Path | PendingWrite | None