"""
Sampling gridded fields and meteorology at points, such as particle positions.
:func:`sample_field` reads a gridded field, such as a surface flux or a
mole fraction: an :class:`xarray.DataArray` on a regular ``lat``/``lon``
grid (``y``/``x`` for a projected grid), with an optional vertical
dimension and an optional ``time`` dimension. Each point takes the value of
the cell it falls in. Use it to read a field at particle positions: an
oxidant along each trajectory (:class:`~runtraj.transforms.Oxidation`), a
mole fraction where each particle ends, or a surface flux under each
particle. Units are not converted.
:func:`sample_met` reads ARL met files at particle positions as HYSPLIT
does, for the columns HYSPLIT writes from the met (``mlht``, ``dens``,
``pres``, ``zsfc``), which a transport model that writes only positions
leaves out. :func:`calc_pbl_height` finds the mixed-layer height from the
met's profile, as HYSPLIT does by default.
"""
from __future__ import annotations
import os
from collections.abc import Iterable
import numpy as np
import pandas as pd
import xarray as xr
from numpy.typing import ArrayLike
from pandas.errors import InvalidIndexError
from runtraj.spatial import horizontal_dims
[docs]
def nearest_cell(coords: np.ndarray, values: np.ndarray) -> np.ndarray:
"""
Return the index of the cell nearest each value, or ``-1`` outside the cells.
Each cell reaches halfway to its neighbors. The end cells reach the
same distance beyond their centers. With a single cell, every finite
value is inside it.
Parameters
----------
coords : numpy.ndarray
Cell centers along one axis, ascending or descending.
values : numpy.ndarray
Positions to look up.
Returns
-------
numpy.ndarray
Index into *coords* for each value.
"""
coords = np.asarray(coords, dtype=float)
if coords.ndim != 1 or coords.size == 0:
raise ValueError("Field coordinates must be a non-empty 1-D array.")
ascending = coords[0] <= coords[-1]
c = coords if ascending else coords[::-1]
if c.size == 1:
inside = np.isfinite(values)
return np.where(inside, 0, -1)
mids = (c[:-1] + c[1:]) / 2.0
lo = c[0] - (c[1] - c[0]) / 2.0
hi = c[-1] + (c[-1] - c[-2]) / 2.0
idx = np.searchsorted(mids, values)
inside = (values >= lo) & (values <= hi)
idx = np.where(inside, idx, -1)
if not ascending:
idx = np.where(idx >= 0, c.size - 1 - idx, -1)
return idx
[docs]
def vertical_dim(field: xr.DataArray) -> str | None:
"""
Return the name of a field's vertical dimension.
That is the one dimension that is neither horizontal nor ``time``.
Returns ``None`` for a field without one, such as a column mean or a
surface field, and raises if there is more than one candidate.
"""
y_dim, x_dim = horizontal_dims(field)
extra = [str(d) for d in field.dims if d not in (y_dim, x_dim, "time")]
if len(extra) > 1:
raise ValueError(
f"Expected at most one vertical dimension besides {y_dim!r}/{x_dim!r} "
f"and 'time'; got {extra}."
)
return extra[0] if extra else None
def _bounds_indexer(
bounds: pd.IntervalIndex, stamps: pd.DatetimeIndex
) -> tuple[np.ndarray, np.ndarray]:
"""
Return each stamp's step index from a flux's CF time bounds, and whether it falls in one.
A stamp before the first bound, after the last, or in a gap between
bounds falls in none: the bounds say where every step starts and ends,
so there is nothing to guess at either edge.
"""
try:
it = bounds.get_indexer(stamps)
except InvalidIndexError as e:
raise ValueError(
"The field's time bounds overlap, so a time can fall in more than "
"one step. Two bounds cannot cover the same instant."
) from e
in_step = it >= 0
return np.where(in_step, it, 0), in_step
[docs]
def sample_field(
field: xr.DataArray,
x: ArrayLike,
y: ArrayLike,
z: ArrayLike | None = None,
times: ArrayLike | None = None,
fill_value: float = np.nan,
step_starts: bool = False,
) -> np.ndarray:
"""
Return the field's value in the cell nearest each point.
A point outside the field horizontally, or in a missing cell, gives
``fill_value``. Longitudes are wrapped to the field's convention (-180
to 180 or 0 to 360).
Parameters
----------
field : xarray.DataArray
Field with horizontal dimensions (``lat`` and ``lon``, or ``y`` and
``x``), and optionally a vertical dimension and ``time``.
x, y : array-like
Point coordinates: longitude and latitude for a ``lat``/``lon``
field.
z : array-like, optional
Vertical coordinate of each point, in the units of the field's
vertical dimension. Required when the field has one. Matched to the
nearest level, so a point above the top level takes the top level.
times : array-like, optional
Time of each point. Required when the field has a ``time``
dimension. By default matched to the nearest time, so times
outside the field's span take the first or last step. See
``step_starts``.
fill_value : float, default NaN
Value for points outside the field and for missing cells. Use 0 for
a surface flux, where no data means no emission. ``NaN`` suits a
mole fraction, which is unknown there.
step_starts : bool, default False
Whether the field's ``time`` labels are the starts of steps, as a
flux inventory's are. A point then takes the last step that starts
at or before its time. A point before the first label takes
``fill_value`` and a point after the last takes the last step, since
the labels do not say where it ends. The labels must strictly
increase: a repeated label is an error. When ``field["time"]`` is a
:class:`pandas.IntervalIndex` (its CF time bounds, read as each
step's start and end), a point is matched by which step's bounds
hold it instead: one before the first bound, after the last, or in
a gap between bounds takes ``fill_value``, since the bounds say
where every step starts and ends and there is nothing to guess at
either edge. The bounds must not overlap.
Returns
-------
numpy.ndarray
One value per point.
"""
y_dim, x_dim = horizontal_dims(field)
xs = np.asarray(x, dtype=float).ravel()
ys = np.asarray(y, dtype=float).ravel()
if xs.shape != ys.shape:
raise ValueError("x and y must have the same length.")
lons = field[x_dim].to_numpy()
if x_dim == "lon":
xs = xs % 360.0 if lons.max() > 180.0 else ((xs + 180.0) % 360.0) - 180.0
ix = nearest_cell(lons, xs)
iy = nearest_cell(field[y_dim].to_numpy(), ys)
inside = (ix >= 0) & (iy >= 0)
indexers: dict[str, xr.DataArray] = {
x_dim: xr.DataArray(np.where(inside, ix, 0), dims="points"),
y_dim: xr.DataArray(np.where(inside, iy, 0), dims="points"),
}
zdim = vertical_dim(field)
if zdim is not None:
if z is None:
raise ValueError(f"field has a {zdim!r} dimension; pass z.")
zs = np.asarray(z, dtype=float).ravel()
if zs.shape != xs.shape:
raise ValueError("z must have the same length as x and y.")
indexers[zdim] = xr.DataArray(_nearest_level(field[zdim], zs), dims="points")
if "time" in field.dims:
if times is None:
raise ValueError("field has a time dimension; pass times.")
stamps = pd.DatetimeIndex(pd.to_datetime(np.asarray(times).ravel()))
if len(stamps) != len(xs):
raise ValueError("times must have the same length as x and y.")
index = field.indexes["time"]
if step_starts:
if isinstance(index, pd.IntervalIndex):
it, in_step = _bounds_indexer(index, stamps)
else:
if not index.is_monotonic_increasing:
raise ValueError(
"The field's time labels must strictly increase to be read "
"as step starts."
)
if not index.is_unique:
repeated = index[index.duplicated()][0]
raise ValueError(
"The field's time labels must strictly increase to be read "
f"as step starts, but {repeated} is repeated. Two steps "
"cannot start at the same time; drop one, such as the "
"overlap of two concatenated files."
)
it = index.searchsorted(stamps, side="right") - 1
in_step = it >= 0
it = np.clip(it, 0, None)
inside = inside & in_step
else:
it = index.get_indexer(stamps, method="nearest")
indexers["time"] = xr.DataArray(it, dims="points")
sampled = field.isel(indexers).to_numpy().astype(float)
return np.where(inside & ~np.isnan(sampled), sampled, fill_value)
def _nearest_level(levels: xr.DataArray, z: np.ndarray) -> np.ndarray:
"""Return the index of the level nearest each ``z``."""
coords = levels.to_numpy().astype(float)
if coords.ndim != 1 or coords.size == 0:
raise ValueError("The vertical coordinate must be a non-empty 1-D array.")
return np.abs(coords[None, :] - z[:, None]).argmin(axis=1)
#: The ARL variables each particle column is computed from, by :func:`sample_met`.
#: ``pres`` is the ``PRES`` field, not arlmet's virtual ``"pressure"``, which
#: is NaN at a height on sigma files (jmineau/arl-met#65).
_MET_VARIABLES: dict[str, tuple[str, ...]] = {
"mlht": ("PBLH",),
"dens": ("PRES", "TEMP", "SPHU"),
"pres": ("PRES",),
"zsfc": ("SHGT",),
}
#: Gas constant of dry air, J kg⁻¹ K⁻¹, the value HYSPLIT uses.
_R_DRY = 287.04
#: The top of the domain of a HYSPLIT run that does not set one, in meters
#: (``HysplitConfig.z_top``, which a test keeps this equal to: this module does
#: not import HYSPLIT's package).
_DEFAULT_Z_TOP = 25000.0
def sample_met(
particles: pd.DataFrame,
files: Iterable[str | os.PathLike[str]],
columns: Iterable[str] = ("mlht", "dens", "pres", "zsfc"),
*,
z_top: float | None = _DEFAULT_Z_TOP,
) -> pd.DataFrame:
"""
Return the particles with met columns sampled from ARL files at each particle's position.
A transport model that writes only positions can get the columns
HYSPLIT writes from the met this way. Each row is sampled at its
``lon``, ``lat``, ``zagl``, and ``time``: bilinear across the met's
grid and linear in height between the internal levels of a HYSPLIT run
with the top *z_top* (:func:`arlmet.sample_points`), then linear in time
between the two met times around the particle's, as HYSPLIT does. Each column comes
from these ARL variables:
- ``mlht`` is ``PBLH``, the met model's boundary-layer height, in m
above ground.
- ``dens`` is 100 ``PRES`` / (R ``TEMP`` (1 + 0.61 ``SPHU``)), the
density of moist air in kg m⁻³, with R = 287.04 J kg⁻¹ K⁻¹.
- ``pres`` is ``PRES``, the pressure, in hPa.
- ``zsfc`` is ``SHGT``, the terrain height, in m above sea level.
``pres`` and ``dens`` follow what HYSPLIT reads, to the differences
under Notes. ``mlht`` is the met's: HYSPLIT's is the
met's ``PBLH`` only with ``kmixd: 0``, and then it is at least ``kmix0``;
the default, ``kmixd: 3``, finds the mixed layer from the profile, as
:func:`calc_pbl_height` does.
Parameters
----------
particles : pandas.DataFrame
A particle table with ``lon``, ``lat``, ``zagl``, and ``time``.
``sim.particles`` has ``time``. For a table a transport model
returned, add it: the release time plus ``age`` minutes.
files : iterable of path-like
ARL met files that cover the particles' times, such as the files the
run read (``met_files`` of :class:`runtraj.models.ModelRun` or of
:func:`runtraj.particles.particles_metadata`).
columns : iterable of str, default ("mlht", "dens", "pres", "zsfc")
The columns to add.
z_top : float or None, default 25000
The top of the run's domain in meters (``z_top`` of
:class:`runtraj.models.hysplit.HysplitConfig`), since HYSPLIT's
internal levels depend on it. A stored run records it:
``particles_metadata(path).settings["z_top"]``. ``None`` reads the
met's own levels, for a transport model that is not HYSPLIT; a row
below their lowest or above their top is then NaN.
Returns
-------
pandas.DataFrame
A copy of *particles* with the columns added. A row off the met's
grid is NaN.
Raises
------
ValueError
If a column is not one of the four or is in *particles* already
(drop it first), *particles* has no ``time`` column, a row is outside
the files' times, or *z_top* is not positive.
KeyError
If the files lack a variable a column needs.
Notes
-----
On two 6-hour HRRR runs from Salt Lake City, one in winter and one in
summer, the sampled columns differ from the ones HYSPLIT wrote by:
- ``zsfc``: a few meters, since HYSPLIT reads the terrain where the
particle was before its turbulent step.
- ``mlht``: a median of 16 to 30 m, against a run with ``kmixd: 0``
floored at ``kmix0``. HYSPLIT reads the nearest grid point's ``PBLH``;
this function interpolates it.
- ``pres``: a median of 0.02 hPa, 0.23 to 0.30 hPa at the 95th
percentile, and 1.4 to 4.6 hPa at worst.
- ``dens``: 0.17 to 0.57 % higher, more in moister air.
Examples
--------
>>> meta = runtraj.particles.particles_metadata(
... sim.particles_path
... ) # doctest: +SKIP
>>> hysplit = sim.particles # doctest: +SKIP
>>> sampled = runtraj.sampling.sample_met( # doctest: +SKIP
... hysplit.drop(columns="pres"),
... meta.met_files,
... columns=["pres"],
... z_top=meta.settings["z_top"],
... )
>>> (sampled["pres"] - hysplit["pres"]).describe() # doctest: +SKIP
"""
import arlmet
columns = list(dict.fromkeys(columns))
unknown = [c for c in columns if c not in _MET_VARIABLES]
if unknown:
raise ValueError(
f"sample_met cannot sample {unknown}; it samples {list(_MET_VARIABLES)}."
)
present = [c for c in columns if c in particles.columns]
if present:
raise ValueError(
f"The particles already have {present}. Drop them first to sample them."
)
if "time" not in particles.columns:
raise ValueError(
"sample_met needs a 'time' column: the release time plus 'age' minutes."
)
variables = list(dict.fromkeys(v for c in columns for v in _MET_VARIABLES[c]))
handles = [arlmet.File(path) for path in files]
try:
met_times = pd.DatetimeIndex(sorted({t for h in handles for t in h.times}))
times = pd.DatetimeIndex(particles["time"])
before, after, weight = _bracket(met_times, times)
# Each row is sampled at the met time before it and, unless it is on
# a met time, at the one after. One call samples both.
later = weight > 0
position = particles[["lon", "lat", "zagl"]].to_numpy(dtype=float)
points = pd.DataFrame(
np.concatenate([position, position[later]]), columns=["lon", "lat", "z"]
)
points["time"] = np.concatenate([before, after[later]])
sampled = arlmet.sample_points(
handles, points, variables, z_kind="agl", model_top=z_top
)
finally:
for handle in handles:
handle.close()
n = len(particles)
w = weight[later]
met: dict[str, np.ndarray] = {}
for name in variables:
values = sampled[name].to_numpy(dtype=float)
at_rows = values[:n].copy()
at_rows[later] = (1.0 - w) * at_rows[later] + w * values[n:]
met[name] = at_rows
out = particles.copy()
for column in columns:
if column == "dens":
virtual_temperature = met["TEMP"] * (1.0 + 0.61 * met["SPHU"])
out[column] = 100.0 * met["PRES"] / (_R_DRY * virtual_temperature)
else:
out[column] = met[_MET_VARIABLES[column][0]]
return out
def _bracket(
met_times: pd.DatetimeIndex, times: pd.DatetimeIndex
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
"""
Return the met times before and after each time, and the weight of the one after.
A time on a met time has that time for both, and weight 0.
"""
if len(met_times) == 0:
raise ValueError("The met files hold no time steps.")
after = met_times.searchsorted(times, side="left")
outside = (after == len(met_times)) | np.asarray(times < met_times[0])
if outside.any():
raise ValueError(
f"{int(outside.sum())} particle rows are outside the met files' times "
f"({met_times[0]} to {met_times[-1]}): {times[outside].min()} "
f"to {times[outside].max()}."
)
t1 = met_times[after]
exact = np.asarray(t1 == times)
t0 = met_times[np.where(exact, after, after - 1)]
elapsed = (times - t0).to_numpy()
span = np.where(exact, np.timedelta64(1, "s"), (t1 - t0).to_numpy())
weight = np.where(exact, 0.0, elapsed / span)
return t0.to_numpy(), t1.to_numpy(), weight.astype(float)
#: HYSPLIT's constants for the mixed layer (``stbcon.f``): gravity in m s⁻²,
#: von Kármán's constant, and the heat capacity of air in J kg⁻¹ K⁻¹.
_HYSPLIT_GRAV = 9.80616
_VON_KARMAN = 0.4
_CP = 1005.0
#: The top of HYSPLIT's surface layer, in m above ground (``metlvl.f``).
_SURFACE_LAYER_TOP = 75.0
#: The bulk Richardson number at the top of the mixed layer (``stbanl.f``).
_CRITICAL_RICHARDSON = 0.25
def calc_pbl_height(
particles: pd.DataFrame,
files: Iterable[str | os.PathLike[str]],
*,
z_top: float = _DEFAULT_Z_TOP,
) -> np.ndarray:
"""
Return the mixed-layer height at each particle, found from the met's profile as HYSPLIT's ``kmixd: 3`` finds it.
This is HYSPLIT's default way to find the mixed layer, ported from its
source (``stbanl.f``, Holtslag and Boville, 1993). Going up the column,
the mixed layer ends where the bulk Richardson number between the
lowest level and the level reached passes 0.25::
Ri = g · (θ(z) − θ₁ − θₑ) · (z − z₁) / ((θ₁ + θₑ) · |V(z) − V₁|²)
with θ the potential temperature, V the horizontal wind, and level 1
HYSPLIT's lowest internal level. θₑ, the temperature excess of rising
air, is ``8.5 u* θ* / (u*³ + 0.6 w*³)^(1/3)`` in unstable air and 0
otherwise, with the friction velocity ``u*`` (``USTR``) and the
friction temperature ``θ*`` from the sensible heat flux (``SHTF``) at
the ground. The height is interpolated to where Ri is 0.25.
HYSPLIT computes the height at each met grid point and met time; a
particle takes the nearest grid point's height at the met times before
and after it, linear in time. So does this function, on HYSPLIT's
internal levels for a run with the top *z_top*. It applies no minimum,
as HYSPLIT's ``kmix0`` does: take ``numpy.maximum`` with yours. A
column with no height where Ri reaches 0.25 gets the second-highest
internal level, as in HYSPLIT.
On three 6-hour HRRR runs from Salt Lake City, the height it finds,
kept at least 150 m, has a median ratio of 0.999 to 1.000 to the
``mlht`` HYSPLIT wrote with ``kmixd: 3``, and 73 to 94 % of rows are
within 2 %.
Parameters
----------
particles : pandas.DataFrame
A particle table with ``lon``, ``lat``, and ``time``.
files : iterable of path-like
ARL met files that cover the particles' times, with ``TEMP``,
``PRES``, ``UWND``, ``VWND`` and ``SHGT``, the sensible heat flux
``SHTF``, and the friction velocity ``USTR`` or the momentum fluxes
``UMOF`` and ``VMOF``.
z_top : float, default 25000
The top of the domain in meters, which sets HYSPLIT's internal
levels (``z_top`` of :class:`runtraj.models.hysplit.HysplitConfig`).
Returns
-------
numpy.ndarray
The mixed-layer height at each row, in m above ground. A row off
the met's grid is NaN.
Raises
------
ValueError
If *particles* has no ``time`` column or a row is outside the
files' times.
KeyError
If the files lack a variable it needs.
Examples
--------
>>> meta = runtraj.particles.particles_metadata(
... sim.particles_path
... ) # doctest: +SKIP
>>> mlht = runtraj.sampling.calc_pbl_height(
... sim.particles, meta.met_files
... ) # doctest: +SKIP
"""
import arlmet
if "time" not in particles.columns:
raise ValueError(
"calc_pbl_height needs a 'time' column: the release time plus 'age' minutes."
)
handles = [arlmet.File(path) for path in files]
try:
first = handles[0]
names = {r.variable for h in handles for r in h.records}
momentum = ["USTR"] if "USTR" in names else ["UMOF", "VMOF"]
missing = sorted(
{"TEMP", "PRES", "UWND", "VWND", "SHGT", "SHTF", *momentum} - names
)
if missing:
raise KeyError(
f"calc_pbl_height needs the met variables {missing}, which the "
"files lack."
)
met_times = pd.DatetimeIndex(sorted({t for h in handles for t in h.times}))
before, after, weight = _bracket(met_times, pd.DatetimeIndex(particles["time"]))
# One column per met grid point and met time, at the nearest grid point.
x, y = first.grid.fractional_indices(
particles["lon"].to_numpy(dtype=float),
particles["lat"].to_numpy(dtype=float),
)
rows = pd.DataFrame(
{
"i": np.rint(x),
"j": np.rint(y),
"lon": particles["lon"].to_numpy(dtype=float),
"lat": particles["lat"].to_numpy(dtype=float),
}
)
later = weight > 0
both = pd.concat(
[rows.assign(time=before), rows[later].assign(time=after[later])],
ignore_index=True,
)
key = ["i", "j", "time"]
columns = both.dropna(subset=["i", "j"]).drop_duplicates(subset=key)
columns = columns.reset_index(drop=True)
levels = arlmet.internal_levels(first, model_top=z_top)
heights = levels.heights
ground = arlmet.sample_points(
handles,
columns[["lon", "lat", "time"]].assign(z=0.0),
["SHGT", "SHTF", *momentum],
z_kind="agl",
method="nearest",
model_top=z_top,
)
# The internal levels above each column's ground, (columns, levels).
zz = levels.heights_at(ground["SHGT"].to_numpy(dtype=float))
n_col, n_lev = zz.shape
at_levels = pd.DataFrame(
{
"lon": np.repeat(columns["lon"].to_numpy(), n_lev),
"lat": np.repeat(columns["lat"].to_numpy(), n_lev),
"time": np.repeat(columns["time"].to_numpy(), n_lev),
"z": zz.ravel(),
}
)
profile_variables = ["TEMP", "PRES", "UWND", "VWND"]
if "SPHU" in names:
profile_variables.append("SPHU")
profile = arlmet.sample_points(
handles,
at_levels,
profile_variables,
z_kind="agl",
method="nearest",
model_top=z_top,
)
finally:
for handle in handles:
handle.close()
def grid(name: str) -> np.ndarray:
return profile[name].to_numpy(dtype=float).reshape(n_col, n_lev)
temp, pres, uu, vv = grid("TEMP"), grid("PRES"), grid("UWND"), grid("VWND")
sphu = grid("SPHU") if "SPHU" in profile_variables else np.zeros_like(temp)
theta = temp * (1000.0 / pres) ** 0.286
density = 100.0 * pres / (_R_DRY * temp * (1.0 + 0.61 * sphu))
ks = max(int(np.sum(heights <= _SURFACE_LAYER_TOP)), 1) - 1
if "USTR" in momentum:
ustar = np.maximum(ground["USTR"].to_numpy(dtype=float), 0.0001)
else:
# u* from the momentum fluxes, as stbanl does without USTR, with the
# density of its lowest level above the first.
def component(flux: str) -> np.ndarray:
f = ground[flux].to_numpy(dtype=float)
rho = density[:, min(max(1, ks), n_lev - 1)]
return np.sign(f) * np.maximum(0.01, np.sqrt(np.abs(f) / rho))
ustar = np.hypot(component("UMOF"), component("VMOF"))
heat = ground["SHTF"].to_numpy(dtype=float)
if first.source.strip() == "ETA":
heat = -heat # NAM's (ETA's) flux is positive downward (prfcom.f)
zmix = _richardson_mixed_layer(zz, theta, uu, vv, density, ustar, heat, ks)
# Each row takes its grid point's height at the met times around it.
columns["zmix"] = zmix
found = both.merge(columns[[*key, "zmix"]], on=key, how="left")["zmix"]
value = found.to_numpy(dtype=float)
n = len(particles)
out = value[:n].copy()
w = weight[later]
out[later] = (1.0 - w) * out[later] + w * value[n:]
return out
def _richardson_mixed_layer(
zz: np.ndarray,
theta: np.ndarray,
uu: np.ndarray,
vv: np.ndarray,
density: np.ndarray,
ustar: np.ndarray,
heat: np.ndarray,
ks: int,
) -> np.ndarray:
"""
Return the mixed-layer height of each column, as ``stbanl.f`` finds it for ``kmixd: 3``.
*zz* (m above ground), *theta* (potential temperature, K), *uu* and
*vv* (m s⁻¹) and *density* (kg m⁻³) are ``(columns, levels)`` on the
internal levels; *ustar* (m s⁻¹) and *heat*, the sensible heat flux
(W m⁻², positive upward), are per column; *ks* is the index of the top
of the surface layer. The stability at the top of the surface layer
comes from the fluxes, and the temperature excess from it in unstable
air.
"""
n_col, n_lev = zz.shape
heat = np.maximum(-25.0, heat)
tstar = np.sign(-heat) * np.maximum(
0.0001, np.abs(heat / (density[:, ks] * _CP * ustar))
)
zl = zz[:, ks] * _VON_KARMAN * _HYSPLIT_GRAV * tstar / (ustar**2 * theta[:, ks])
unstable = np.clip(zl, -2.0, 15.0) < 0
# Up the column until Ri passes 0.25, interpolating to where it is 0.25.
zmix = zz[:, 0].copy()
ri = np.zeros(n_col)
done = np.zeros(n_col, dtype=bool)
for k in range(1, n_lev):
go = ~done & (ri < _CRITICAL_RICHARDSON)
done |= ~go
if not go.any():
break
tstk = tstar * density[:, ks] / density[:, k]
wstar = np.abs(_HYSPLIT_GRAV * ustar * tstk * zz[:, k] / theta[:, 0]) ** (1 / 3)
excess = np.where(
unstable,
np.abs(8.5 * ustar * tstk / (ustar**3 + 0.6 * wstar**3) ** (1 / 3)),
0.0,
)
t1 = theta[:, 0] + excess
du = np.maximum(0.0001, (uu[:, k] - uu[:, 0]) ** 2 + (vv[:, k] - vv[:, 0]) ** 2)
old = ri
new = _HYSPLIT_GRAV * (theta[:, k] - t1) * (zz[:, k] - zz[:, 0]) / t1 / du
with np.errstate(divide="ignore", invalid="ignore"):
crossed = (zz[:, k] - zz[:, k - 1]) / (new - old) * (
_CRITICAL_RICHARDSON - old
) + zz[:, k - 1]
zmix = np.where(go, np.where(new == old, zz[:, k], crossed), zmix)
ri = np.where(go, new, ri)
if n_lev >= 2:
# No crossing below the top: the second-highest level, as in HYSPLIT;
# and always a little above the lowest level.
zmix = np.minimum(zmix, zz[:, -2])
zmix = np.maximum(zmix, zz[:, 0] + (zz[:, 1] - zz[:, 0]) / 1000.0)
return zmix
__all__ = [
"calc_pbl_height",
"nearest_cell",
"sample_field",
"sample_met",
"vertical_dim",
]