Source code for pycif.plugins.transforms.system.dump2format.forward
import os
from logging import info
from .....utils import path
[docs]
def forward(
transform,
inout_datastore,
controlvect,
obsvect,
mapper,
di,
df,
mode,
runsubdir,
workdir,
do_simu=True,
onlyinit=False,
**kwargs
):
"""Write the current data field to a user-specified file format.
Passes the output data unchanged to ``'outputs'`` (allowing downstream
transforms to continue using it) and then calls the dump plugin's
``write`` method to serialise the field to disk.
The output file name is derived from the sub-simulation date ``ddi``
via ``strftime(transform.dump_file)``. If the file already exists
and ``overwrite = False``, the write is skipped.
Args:
transform (Plugin): dump2format instance (carries ``dump_plg``,
``dump_file``, ``dump_dir``, and ``overwrite`` attributes).
inout_datastore (dict): mutable datastore; ``'inputs'`` is
forwarded to ``'outputs'`` and the ``'spec'`` field is written.
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): default output directory when ``dump_dir`` is
not configured.
workdir (str): unused.
do_simu (bool): unused.
onlyinit (bool): unused.
**kwargs: unused.
"""
overwrite = getattr(transform, 'overwrite')
# Forwarding data for next transforms
ddi = min(di, df)
for trid in mapper["outputs"]:
inout_datastore["outputs"][trid][ddi] = \
inout_datastore["inputs"][trid][ddi]
# Output directory
out_dir = getattr(transform, 'dump_dir', runsubdir)
if not os.path.isdir(out_dir):
path.init_dir(out_dir)
# Now dump the data
datastore = inout_datastore["inputs"]
for trid in mapper["inputs"]:
name = trid[1]
file_name = ddi.strftime(os.path.join(out_dir, transform.dump_file))
if os.path.isfile(file_name) and not overwrite:
info(f"'{file_name}' already exists, skipping {trid} dumping")
else:
info(f"Dumping {trid} to '{file_name}'")
transform.dump_plg.write(
name,
file_name,
datastore[trid][ddi]["spec"],
metadata=mapper["inputs"][trid]
)