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 accepted by |
Functions
|
Write a dataset to a local netCDF file or Zarr store. |
|
Run several |
|
Open a remote OPeNDAP dataset lazily. |
|
Open a remote dataset, subset it and cache it locally. |
Classes
|
A deferred write of a dataset to a local cache file. |
- class seagrid.download.opendap.PendingWrite(delayed, dest)[source]#
Bases:
objectA deferred write of a dataset to a local cache file.
- Variables:
delayed (
dask.delayed.Delayed) – The pending write to the.partialpath.dest (
pathlib.Path) – Final path, which the file is moved to bycompute().
- 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:
- 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 (
stroros.PathLike) – Output path. A.zarrsuffix 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 aPendingWrite.encoding (
mapping, optional) – Passed toxarray.Dataset.to_zarr()/to_netcdf.
- Returns:
pathlib.Path,PendingWriteorNone– dest when written now, aPendingWritewhen 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
PendingWritejobs in one dask computation.- Parameters:
writes (
iterableofPendingWriteorNone) – Jobs; None entries (already cached) are skipped.**compute_kw – Passed to
dask.compute(), e.g.scheduler="threads".
- Returns:
listofpathlib.Path– The written files.- Return type:
- 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. Forengine="pydap"adap2://ordap4://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 (
mappingorstr, optional) – Dask chunks, passed toxarray.open_dataset().timeout (
floator(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:
ValueError – For an unknown engine.
DownloadError – If the dataset cannot be opened.
- Return type:
- 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 (
stroros.PathLike) – Local netCDF file or.zarrstore.variables (
sequenceofstr, 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) – Seeopen_remote().session (
requests.Session, optional) – Seeopen_remote().chunks (
mappingorstr, 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 aPendingWrite.
- Returns:
pathlib.Path,PendingWriteorNone– Seecache_dataset().- Return type:
Path | PendingWrite | None