Source code for pycif.plugins.transforms.system.sparse2sample.adjoint
import numpy as np
import pandas as pd
import xarray as xr
from logging import info, debug
[docs]
def adjoint(
transform,
inout_datastore,
controlvect,
obsvect,
mapper,
di,
df,
mode,
runsubdir,
workdir,
onlyinit=False,
**kwargs
):
"""Extract gridded adjoint sensitivities at sparse observation locations.
The adjoint of :func:`forward`: reads ``'adj_out'`` from the gridded
output DataArray at the ``(tstep, lev, i, j)`` indices of each
observation and writes the result into the ``'adj_out'`` column of the
sparse input DataFrame.
.. warning::
The current implementation contains an active debug
``code.interact()`` call at the start of the function body that
will pause execution. **This must be removed before production
use.**
Args:
transform (Plugin): sparse2sample instance.
inout_datastore (dict): mutable datastore; ``'outputs'`` has the
gridded sensitivity array, ``'inputs'`` has the sparse
DataFrame to be updated.
controlvect: unused.
obsvect: unused.
mapper (dict): transform mapper.
di (datetime): sub-simulation start date.
df (datetime): sub-simulation end date.
mode (str): ``'adj'``.
runsubdir (str): unused.
workdir (str): unused.
onlyinit (bool): if ``True``, return immediately.
**kwargs: unused.
"""
print(__file__)
import code
code.interact(local=dict(locals(), **globals()))
if onlyinit:
return
ddi = min(di, df)
for trid in mapper["inputs"]:
xmod_in = inout_datastore["inputs"][trid][ddi]
xmod_out = inout_datastore["outputs"][trid][ddi]
t = xmod_in["metadata"]["tstep"].astype(int).values
lev = xmod_in["metadata"]["level"].astype(int).values
i = xmod_in["metadata"]["i"].astype(int).values
j = xmod_in["metadata"]["j"].astype(int).values
if "adj_out" not in xmod_in["maindata"]:
xmod_in.loc[:, ("maindata", "adj_out")] = \
0 * xmod_in["maindata"]["spec"]
xmod_in.loc[:, ("maindata", "adj_out")] = \
xmod_out["adj_out"].values[t, lev, i, j]