Add a data variable#
A data variable is one [data.<name>] section of a project TOML, loaded
by one variable class and stored in the input cache. The variables that ship are listed
in Data sources.
Add a variable only for a new physical quantity with its own unit. A source of an existing
variable is Add a data source. The set is closed, because each variable is a field
of DataConfig that the JSON schema publishes, so it is a change of HydroModPy and not a
plugin.
Pick a base class#
GridVariablefor gridded data (FieldRecord): it caches grids as NetCDF and loads a user’s NetCDF, GeoTIFF or folder of chronicles.StationVariablefor station chronicles (PointRecord): it caches one file per station and asks a provider only for the missing periods.FileVariablefor a variable read only from the user’s own files.RasterFileVariableis its base for a single-band raster and needs onlyVARIABLE_NAME.
All three live in hydromodpy/data/families/. wind in sim2_series.py
is the shortest grid variable to copy, hydrometry the shortest station variable.
A thin variable or a variable with its own folder#
A thin variable has a source config, a section config and a variable class that
names itself, and no code of its own. It gets no folder. Its three classes join the flat
module of the family it looks like under hydromodpy/data/variables/
(sim2_series.py, forcing_series.py, lake_series.py or user_rasters.py),
or open a new flat module when it starts a family. A variable that needs a provider
module, a resolver or a file loader of its own (dem, geology, hydrography,
hydrometry) takes a folder:
hydromodpy/data/variables/<name>/
|-- __init__.py a docstring, no import
|-- config.py <Name>SourceConfig, <Name>Config
|-- variable.py <Name>Variable: VARIABLE_NAME, INTERNAL_UNIT, SOURCES
`-- apis/ one module per network provider, if the variable has one
A variable never imports another variable, and a family module imports none either
(tests/unit/architecture/test_package_layouts.py checks both). A flat module
must be one that VARIABLE_SPECS names.
For a thin variable called evaporation, these classes go into
hydromodpy/data/variables/forcing_series.py, next to etp and precipitation.
class EvaporationSourceConfig(TimeseriesColumnsMixin, TimeseriesSelectionMixin):
"""One evaporation source."""
source: Annotated[Literal["file"], Profile.USER] = Field(
..., description="Data provider: 'file' for the user's files."
)
path: Annotated[str | None, Profile.USER] = Field(
default=None, description="A .nc or .tif file, or a folder of chronicles."
)
class EvaporationConfig(BaseVariableConfig):
"""Top-level evaporation configuration."""
_TOML_SECTION = "evaporation"
sources: Annotated[list[EvaporationSourceConfig], Profile.USER] = Field(
..., min_length=1, description="At least one data source."
)
class EvaporationVariable(GridVariable):
VARIABLE_NAME = "evaporation"
INTERNAL_UNIT = "mm/day"
SOURCES = {}
The base class handles file through load_file. Each network provider adds one key
in SOURCES, see Add a data source.
Contract#
Each variable is one row of VARIABLE_SPECS. The row names the config and variable
classes as text, and holds the columns of the drop zone that hmp workspace init
creates.
@dataclass(frozen=True, slots=True)
class VariableSpec:
"""Declarative spec for a data variable.
``config_module`` and ``variable_module`` name, as text, where the config
class and the variable class live. A thin family shares one flat module
(``sim2_series``, ``forcing_series``, ``lake_series``, ``user_rasters``), a rich
variable has its own folder.
The folder ``<workspace>/data/<variable>/`` is built from the other columns:
``category`` picks the README and the example files, ``kind`` is the scanner of
the files (``"timeseries"``, ``"raster"``, ``"vector"`` or ``"table"``),
``unit`` and ``label`` say what the folder holds. The token in the file names is
the variable name unless ``file_prefix`` says another.
``cfg.date_start`` / ``cfg.date_end`` always resolve ``project_period``
when both are set. ``period_source`` picks the fallback when they are not:
- ``"config"``: no fallback, ``project_period`` stays ``None`` and the
variable uses the native coverage of its sources.
- ``"simulation_or_overview"``: the simulation window first, then the
``[overview]`` dates (water_quality).
``export_stable_subdir`` triggers a post-load ``variable.export`` into
``core.state.scratch.preprocessing_dir(<workspace>) / <subdir>`` (currently intermittency).
``loader_method`` names a dedicated method on
:class:`DataLoader` for variables with unique side-effects
(``dem``, ``geology``, ``hydrography``). Generic variables leave it
``None`` and go through the standard generic load path.
"""
config_module: str
config_class: str
variable_module: str
variable_class: str
category: str
kind: str
unit: str
label: str
file_prefix: str | None = None
apply_simulation_window: bool = True
period_source: str = "config"
export_stable_subdir: str | None = None
loader_method: str | None = None
Register#
A variable joins these lists:
hydromodpy/data/loading/_dispatch.py: aVariableSpecinVARIABLE_SPECS.hydromodpy/data/loading/config_schema.py: a field ofDataConfig.hydromodpy/core/state/run_state.py: a field ofLoadedDataContext.hydromodpy/schema/sources.py: an entry (licence, network hosts) for each new provider slug.The planner, only when the variable should be activated by another section (
hydromodpy/data/loading/planner.py); otherwise the user lists it in[data].types.
tests/unit/data/test_variable_families.py compares the lists to
VARIABLE_SPECS and names the one you missed. Two pins move with a new
variable, and moving them is the point: the variable count in the same test,
and DATA_SCHEMA_SHA256 in tests/unit/data/test_data_surfaces.py.
What the TOML says#
# not checked: evaporation is the placeholder variable of this recipe
[data]
types = ["evaporation"]
[[data.evaporation.sources]]
source = "file"
path = "inputs/evaporation_basin.nc"
Test#
pytest tests/unit/architecture tests/unit/data -q runs the gates. Add:
the config: a
filesource withoutpathis refused;the variable class on a small fixture file, offline;
each provider with a recorded answer, so the tier runs without network.
See also#
Package: data for the package map and the variable template.
Add a data source for a source of an existing variable.
Add a config field for a TOML knob outside the source list.