Source code for pycif.plugins.transforms.complex.moist2dry.forward
import xarray as xr
[docs]
def forward(
transf,
inout_datastore,
controlvect,
obsvect,
mapper,
di,
df,
mode,
runsubdir,
workdir,
onlyinit=False,
**kwargs
):
r"""Convert a moist-air mixing ratio to a dry-air number concentration.
Applies the conversion:
.. math::
C_{dry} = C_{moist}
\cdot \underbrace{\left(1 - \beta \frac{q}{1-q}\right)
\cdot \frac{R}{R_{dry} \cdot M} \cdot 10^{12}}_{\text{dry\_coef}}
where:
* :math:`\beta = (R_{vapor} - R_{dry}) / R_{vapor}` (≈ 0.378)
* :math:`R = 8.314` J mol⁻¹ K⁻¹ (universal gas constant)
* :math:`R_{dry} = 287.05` J kg⁻¹ K⁻¹ (dry-air gas constant)
* :math:`M` = ``transf.molmass`` (g mol⁻¹)
* ``q`` = specific humidity from the ``specific_humidity`` input
In TL mode the same scalar coefficient is applied to the increment field.
Args:
transf (Plugin): moist2dry plugin instance (must carry ``molmass``
in g mol⁻¹ and ``component``/``parameter`` attributes).
inout_datastore (dict): mutable datastore; ``'inputs'`` must contain
both the species field and the ``specific_humidity`` field.
controlvect: unused.
obsvect: unused.
mapper (dict): transform mapper.
di (datetime): sub-simulation start date.
df (datetime): sub-simulation end date.
mode (str): ``'fwd'`` or ``'tl'``.
runsubdir (str): unused.
workdir (str): unused.
onlyinit (bool): unused.
**kwargs: unused.
"""
ddi = min(di, df)
# Constants
Rdry = 287.05
Rvapor = 461.5
R = 8.314462
beta = (Rvapor - Rdry) / Rvapor
# Converts to dry
trid_out = (transf.component[0], transf.parameter[0])
spec = inout_datastore["inputs"][trid_out][ddi]["spec"]
q = inout_datastore["inputs"][
("specific_humidity", transf.parameter[0])][ddi]["spec"]
dry_coef = (1 - beta * q / (1 - q)) \
* R / Rdry / transf.molmass * 1000 * 1e9
inout_datastore["outputs"][trid_out][ddi] = xr.Dataset(
{"spec": spec * dry_coef})
if mode == "tl":
inout_datastore["outputs"][trid_out][ddi]["incr"] = \
inout_datastore["inputs"][trid_out][ddi]["incr"] * dry_coef