seagrid.download.cache#

Local cache files that survive interrupted downloads.

Every download in seagrid is written to <target>.partial and moved into place only when it is complete (partial(), finish()). A file or Zarr store that exists under its final name is therefore either complete or was written by something else, and is_complete() checks the second case: a Zarr store with metadata but no data chunks, or a netCDF file that cannot be opened.

Adapted from tracmass.seagrid.zarr_cache (tracmass_jax).

Functions

cached(path[, data_var, force, label])

Return True if path is cached and should not be downloaded again.

discard(path)

Remove the partial download of path, if there is one.

finish(path)

Move a completed download from partial() into place at path.

has_zarr_data(path[, name])

Return True if a Zarr store holds data chunks.

is_complete(path[, data_var])

Return True if path is a usable cached file or store.

partial(path)

Return where a download of path is written until it is complete.

seagrid.download.cache.cached(path, data_var=None, *, force=False, label='seagrid')[source]#

Return True if path is cached and should not be downloaded again.

Parameters:
  • path (str or os.PathLike) – The cached file or store.

  • data_var (str, optional) – Variable that must be present.

  • force (bool, optional) – If True, never cached (download again). Default False.

  • label (str, optional) – Name used in the warning.

Returns:

bool

Warns:

UserWarning – If path exists but is incomplete; it will be downloaded again.

Return type:

bool

seagrid.download.cache.discard(path)[source]#

Remove the partial download of path, if there is one.

Parameters:

path (str or os.PathLike) – Final file or Zarr store (not the .partial name).

Return type:

None

seagrid.download.cache.finish(path)[source]#

Move a completed download from partial() into place at path.

An existing file or store at path is replaced.

Parameters:

path (str or os.PathLike) – Final file or Zarr store.

Returns:

pathlib.Path – path.

Raises:

FileNotFoundError – If there is no partial download of path.

Return type:

Path

seagrid.download.cache.has_zarr_data(path, name=None)[source]#

Return True if a Zarr store holds data chunks.

Parameters:
  • path (str or os.PathLike) – The store.

  • name (str, optional) – Variable to check. Default: any variable.

Returns:

bool – False if the store or variable is missing or holds only metadata.

Return type:

bool

seagrid.download.cache.is_complete(path, data_var=None)[source]#

Return True if path is a usable cached file or store.

  • A Zarr store (a directory) must hold data chunks, for data_var if given.

  • A netCDF/HDF file must open with xarray and contain data_var if given.

  • Any other file must be non-empty.

Parameters:
  • path (str or os.PathLike) – The cached file or store.

  • data_var (str, optional) – Variable that must be present.

Returns:

bool

Return type:

bool

seagrid.download.cache.partial(path)[source]#

Return where a download of path is written until it is complete.

Parameters:

path (str or os.PathLike) – Final file or Zarr store.

Returns:

pathlib.Path – path with .partial appended to its name.

Return type:

Path

Examples

>>> partial("/data/x.nc").name
'x.nc.partial'