"""
:class:`stilt.Simulation`: a simulation, with the reads of what STILT makes from its particles.
It is the particle part (:class:`runtraj.simulation.Simulation`) and only
adds: the footprint, a footprint with other settings, the background, the
transport error, and the plots that draw them. A STILT project
(:class:`stilt.Project`) gives you these. The workers and the stages use the
particle part, and nothing on disk names this class.
"""
from __future__ import annotations
from collections.abc import Sequence
from datetime import timedelta
from functools import cached_property
from typing import TYPE_CHECKING, Any
import pandas as pd
import xarray as xr
from runtraj import simulation
from stilt.background import Background, background
from stilt.exceptions import EmptyFootprint
from stilt.footprint import gridding
from stilt.footprint.config import FootprintConfig
from stilt.footprint.grid import Grid
from stilt.footprint.io import read_footprint
from stilt.footprint.stage import simulation_particles, variant_footprint
from stilt.transport_error import TransportError, transport_error
if TYPE_CHECKING:
from pathlib import Path
from upath import UPath
from stilt.visualization import SimulationPlotAccessor
[docs]
class Simulation(simulation.Simulation):
"""
One release run under one variant, with its footprint, background, and transport error.
It is :class:`runtraj.simulation.Simulation`, which knows where the
particles and the file of each stage are and reads the particles, and it
adds the reads of what STILT makes from them: :attr:`footprint`,
:meth:`calc_footprint`, :meth:`background`, :meth:`transport_error`,
and :attr:`plot`. It changes nothing it inherits.
You rarely build one yourself. Get it from a project instead, as in
``project.simulation(release_id, "hrrr")``.
Parameters
----------
release : Release
Where and when particles are released.
variant : Variant
The variant, resolved: its configs, and the hashes that find its
particles and footprint folders.
output : Output
The output directory.
directory : Path, optional
The project directory, where a relative file name in the settings
starts, such as an averaging-kernel table. ``None`` starts it from
the working directory.
realization : int, optional
Which realization of an ensemble variant (``realizations: N``),
``0`` to ``N - 1``. ``None`` for a variant that runs once.
Raises
------
ValueError
If *realization* is not one the variant runs as.
"""
# -- the footprint -----------------------------------------------------
@property
def footprint_path(self) -> Path | UPath | None:
"""Path of the footprint file in the output directory, or ``None`` before its folder exists."""
return self.path("footprint")
@property
def has_footprint(self) -> bool:
"""
Whether the footprint file exists.
An empty footprint (no particles over the grid) is a finished result.
"""
return self.has("footprint")
[docs]
@cached_property
def plot(self) -> SimulationPlotAccessor:
"""Plotting methods, such as ``sim.plot.map()``."""
from stilt.visualization import SimulationPlotAccessor
return SimulationPlotAccessor(self)
@property
def _footprint_config(self) -> FootprintConfig:
"""The footprint settings of this simulation: the variant's, or the defaults with the model's depth (:func:`stilt.footprint.stage.variant_footprint`)."""
return variant_footprint(self.variant)
@property
def _transforms(self) -> Sequence[Any]:
"""The footprint's particle transforms, which weight the particles; none without a footprint."""
footprint = self.variant.stages.get("footprint")
return () if footprint is None else footprint.transforms
@property
def _near_field_settings(self) -> dict[str, Any]:
"""The near-field correction a footprint of this simulation gets: ``hnf_plume`` and ``footprint_depth``."""
footprint = self._footprint_config
return {
"hnf_plume": footprint.hnf_plume,
"footprint_depth": footprint.footprint_depth,
}
# -- what the particles give beyond the footprint ----------------------
[docs]
def background(self, field: xr.DataArray | pd.Series) -> Background:
"""
Return the background mole fraction at the release.
The background is the field where each particle ends, averaged over
the particles with the footprint's weights (its transforms), so it
adds to the enhancement. See :func:`stilt.background`.
Parameters
----------
field : xarray.DataArray or pandas.Series
A mole-fraction field such as CarbonTracker, whose vertical
dimension is named after a particle column (``pres`` or
``zagl``), or one value per particle, indexed by ``particle``.
Examples
--------
>>> ct = xr.open_dataarray("ct_ch4.nc").rename(level="pres") # doctest: +SKIP
>>> sim.background(ct).value # doctest: +SKIP
"""
return background(
self.particles,
field,
transforms=self._transforms,
release=self.release,
directory=self.directory,
)
[docs]
def transport_error(
self,
error: simulation.Simulation | Sequence[simulation.Simulation],
flux: xr.DataArray,
**options: Any,
) -> TransportError:
"""
Return the transport error of this simulation's modeled enhancement.
It is the extra spread in the per-particle enhancement that a
wind-error variant of the same release adds (Lin and Gerbig, 2005).
See :func:`stilt.transport_error`, which documents the
options (``levels``, ``length_scale``, ``percentile``,
``background``).
Parameters
----------
error : Simulation or sequence of Simulation
The same release under a wind-error variant, or every
realization of one.
flux : xarray.DataArray
Surface flux field, in µmol m⁻² s⁻¹.
Examples
--------
>>> err = project.simulation(sim.release.id, "hrrr-err") # doctest: +SKIP
>>> sim.transport_error(err, flux).variance # doctest: +SKIP
>>> ensemble = [
... project.simulation(rid, "hrrr-err", k) for k in range(10)
... ] # doctest: +SKIP
>>> sim.transport_error(ensemble, flux) # doctest: +SKIP
"""
sims = [error] if isinstance(error, simulation.Simulation) else list(error)
config = self._footprint_config
return transport_error(
simulation_particles(self, config),
[simulation_particles(e, config) for e in sims],
flux,
transforms=self._transforms,
release=self.release,
directory=self.directory,
**{**self._near_field_settings, **options},
)
__all__ = ["Simulation"]