Working with Remote Patches

Open in marimo

DASCore reads remote resources through UPath; tested backends include memory://, s3://, and http://.

Operation Default behavior
scan, get_format Avoid downloading a complete file
scan_payloads Same cache policy, but may transfer and retain full coordinate arrays; prefer scan or scan_to_df for broad listings
read, spool indexing/iteration Perform the heavier I/O needed to load data

Basic example

memory:// demonstrates the remote interface without network access:

from upath import UPath

import dascore as dc

remote_path = UPath("memory://dascore/tutorial/remote_patch.h5")
dc.write(dc.get_example_patch(), remote_path, "DASDAE")

summary = dc.scan(remote_path)[0]
payload = dc.scan_payloads(remote_path, snap=False)[0]
loaded = dc.read(remote_path)[0]
spool = dc.spool(remote_path)

The summary holds compact coordinate envelopes and source fields, the payload holds the format’s full coordinate manager, and indexing the spool loads the patch array. Backends that support direct access may need no local cached copy.

Cache policy

These dascore.config settings control remote caching:

Setting Purpose
remote_cache_dir Directory for materialized remote files; must be private to the current user
allow_remote_cache Permit caching for data reads
allow_remote_cache_for_metadata Permit caching for metadata operations
warn_on_remote_cache Warn on the first download into the cache
from pathlib import Path
from tempfile import mkdtemp

from dascore.config import config_context

with config_context(
    remote_cache_dir=Path(mkdtemp()),
    allow_remote_cache=True,
    allow_remote_cache_for_metadata=False,
):
    patch = dc.read(remote_path)[0]

When a metadata query needs a full download and metadata caching is off, DASCore raises RemoteCacheError. Format detection skips a reader that would need the download and raises only if no other reader claims the file. Detection reads only the header bytes of MiniSEED and SEG-Y files, but SEG-Y’s binary header sits past the first block, so on a server without byte-range support its detection also needs the opt-in. Scanning or reading either format needs a local copy, which a scan makes only after the opt-in. Opt in only for a known operation:

http_path = UPath("https://raw.githubusercontent.com/dasdae/test_data/master/das/example_dasdae_event_1.h5")

with config_context(allow_remote_cache_for_metadata=True):
    summary = dc.scan(http_path)[0]

Once per session, a cached copy is checked against the remote’s size and version fields (such as ETag) and downloaded again if the remote changed. Offline, the copy is kept; a remote that is gone or forbidden is never read from the cache.

Reads and spools

dc.read(...) may download a remote file when allow_remote_cache=True. A spool’s get_contents() uses only metadata, while indexing and iteration load data. If a backend cannot provide spool metadata directly, enable metadata caching.

remote_spool = dc.spool(http_path)
metadata = remote_spool.get_contents()
with config_context(allow_remote_cache=True):
    patch = remote_spool[0]

Remote HDF5 and garbage collection

Remote HDF5 reads can deadlock if cyclic garbage collection runs on fsspec’s event-loop thread while h5py holds its global lock (see h5py’s file-like object documentation). DASCore therefore pauses cyclic garbage collection while remote HDF5 handles are open, restores it when the last one closes, and warns once per process. Only cyclic garbage accumulates meanwhile; reference counting still frees objects immediately, and local reads are unaffected. To silence the warning:

with config_context(warn_on_gc_pause=False):
    patch = dc.read(http_path)[0]

Transfer tuning

Two settings control the remote HDF5 block cache:

Setting Default Trade-off
remote_hdf5_block_size 5 MiB Larger blocks reduce requests but may over-fetch
remote_hdf5_max_blocks 8 More blocks retain more data per open handle

Their product, about 40 MiB by default, approximates the memory each open handle retains. For metadata scans, reduce both, trading over-fetch for round trips; for sequential patch reads, use fewer, larger blocks. With many concurrent handles, lower remote_hdf5_max_blocks first. To read complete files, a local copy is usually faster than repeated range requests.

with config_context(remote_hdf5_block_size=262_144, remote_hdf5_max_blocks=4):
    summaries = dc.scan_to_df([http_path])

HTTP and HTTPS use both settings. S3 uses its own read-ahead cache and takes the block size only.