PatchAttrs

PatchAttrs stores non-coordinate metadata about a Patch, including measurement labels, instrument identity, data units, processing history, and format-specific fields. Coordinate values and summaries live elsewhere. This note explains how DASCore interprets a few important attributes and its policies for them.

Metadata boundaries

Patch.coords is the authoritative source for coordinate values, dimensions, units, and sampling. Patch.summary combines PatchAttrs with CoordSummary envelopes derived from those live coordinates. Scan-created PatchSummary objects additionally carry reload provenance in source_path, source_format, source_version, and source_patch_key.

PatchAttrs rejects nested coordinate payloads and does not own dims or coordinate-summary fields such as time_min and distance_step. Keeping them out of attrs means coordinate edits cannot leave stale copies. Use coordinate APIs for structural changes and Patch.summary.get_coord_summary(...) or PatchSummary.flat_dump(...) for summary/index views.

What an attr holds

An extra attr (one the class does not declare) holds a single string, real number (ints within 64 bits), bool, None, np.datetime64 or np.timedelta64 of any unit, or a scalar Quantity or Unit. A declared field of a subclass is checked by its own type instead.

Python and pandas times become numpy times at their own precision; a time-zone-aware one is refused, so convert it to UTC first. Bytes and numpy strings become str, pd.NA becomes None, and a one-value array, list or tuple (how HDF5 and netCDF store a scalar) becomes that value. An array of several values is skipped with a warning, and anything else, such as a dict, is refused with an error naming the attr. A reader copying every attr a file holds passes them through drop_unheld_attrs, which skips refused ones with a warning.

This is deliberate: attrs are indexed, hashed into ids and compared when patches combine, and every format would need a codec for richer types. Put an array on a coordinate, patch.update_coords(name=(dims, array)) (or (None, array) for one riding no dimension), and structured metadata in fields of a format-specific subclass or an inventory.

data_type

data_type is an optional label for the kind of data in a patch. It is useful for display defaults, plotting, grouping, and quick inspection, but the data and coordinate units and the patch history, not data_type, are the source of physical meaning.

Note

A stale or misleading data_type is much worse than an empty one.

Situation data_type behavior
Output is still the same measured quantity, just filtered/resampled/selected/reordered Preserve existing data_type.
Output is a known derived product with a stable meaning Set a specific snake_case data_type.
Output changes physical meaning but no stable label is appropriate Clear data_type to "".

DASCore-assigned data_type values should be snake_case and listed in VALID_DATA_TYPES in dascore.constants. Correctness-critical code should prefer units, coordinates, and explicit validation.

Patch functions

patch_function can manage output data_type for patch methods.

Decorator value Behavior
data_type=None Preserve the returned patch’s data_type. This is the default for backward compatibility.
data_type="" Clear the returned patch’s data_type.
data_type="some_value" Set the returned patch’s data_type to that value.

Functions may still require a specific input label with required_attrs, for example required_attrs={"data_type": "velocity"}. Use this only when the function’s documented assumptions depend on that label.

Format-specific subclasses

A format whose files carry fields the base class does not model declares a PatchAttrs subclass for them, under dascore/io/. Declared fields are validated and coerced like any other, rather than surviving as untyped extras.

A serialized patch names the class it holds, so the subclass comes back:

import dascore as dc
from dascore.io.odh4.core import ODH4PatchAttrs

patch = dc.get_example_patch()
patch = patch.update(attrs=ODH4PatchAttrs(**dict(patch.attrs), gauge_length=10.0))
patch.io.write("example.h5", "dasdae")

type(dc.read("example.h5")[0].attrs)  # ODH4PatchAttrs

The name is registered, not an import path, so moving a class between modules does not break files already written, and reading a file never imports a path it names. A file naming a class that is not installed, such as one from a plugin this environment lacks, reads as the base PatchAttrs with a warning.

Spelling an optional number

An absent number is spelled OptionalFiniteFloat, defaulting to None:

from dascore.models import OptionalFiniteFloat

class JingleV1PatchAttrs(dc.PatchAttrs):
    """Attrs for the jingle format."""

    gauge_length: OptionalFiniteFloat = None

Not float = np.nan or float = np.inf: JSON spells neither, so such a field writes null and then refuses to read it back, and the class cannot reconstruct from its own model_dump_json(). A non-finite value read from a file is taken as absent rather than refused.