Source code for pycif.plugins.transforms.system.dump2inputs.forward
[docs]
def forward(
transform,
inout_datastore,
controlvect,
obsvect,
mapper,
di,
df,
mode,
runsubdir,
workdir,
do_simu=True,
onlyinit=False,
check_transforms=False,
**kwargs
):
"""Write CIF-internal arrays to model-native input files.
Groups input tracers by reference name (stripping any ``__sample#N``
suffix for ensemble tracers), then calls ``transform.native2inputs``
for each group to write the data to the format expected by the model
executable.
After writing, the ``'spec'`` and ``'incr'`` fields are removed from
the datastore to free memory; the written files become the canonical
source for subsequent model reads. The output datastore is updated
with the data dict returned by ``native2inputs``.
Args:
transform (Plugin): dump2inputs instance (carries
``native2inputs``).
inout_datastore (dict): mutable datastore.
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): passed to ``native2inputs``.
workdir (str): unused.
do_simu (bool): passed to ``native2inputs``.
onlyinit (bool): passed to ``native2inputs``.
check_transforms (bool): passed to ``native2inputs``.
**kwargs: unused.
"""
datastore = inout_datastore["inputs"]
trids_in = list(mapper["inputs"].keys())
# Retrieve reference configuration if batch sampling
trids_in_ref = list(set(
[(trid[0], trid[1].split("__sample#")[0]) for trid in trids_in]))
for trid in trids_in_ref:
input_type = trid[0]
trids2dump = [
tr for tr in trids_in
if tr[1].split("__sample#")[0] == trid[1]
]
# Create new data to extract
data2dump = {
tr: {"data": datastore.get(tr, {di: {}}),
**mapper["inputs"][tr]}
for tr in trids2dump
}
# If the model does not need to compute a simulation,
# just skip this step
transform.native2inputs(
data2dump, input_type, di, df, runsubdir, mode, onlyinit, do_simu,
check_transforms=check_transforms
)
# Dumping data from the datastore
for t in data2dump:
if t not in datastore:
continue
if "spec" in datastore[t][di]:
del datastore[t][di]["spec"]
if "incr" in datastore[t]:
del datastore[t][di]["incr"]
# Update outputs
for t in trids2dump:
inout_datastore["outputs"][t].update(data2dump[t]["data"])