Source code for pycif.plugins.controlvects.standard.init_structure
import numpy as np
from ....utils import dates
from .utils.dimensions import hresol2dim, vresol2dim
from logging import debug, info
[docs]
def init_structure(cntrlv, **kwargs):
"""Initializes the prior control vector. Loops over all components and
tracers and process temporal and horizontal resolution.
Args:
cntrlv (Plugin): definition of the control vector.
datei (datetime): initial date of the inversion window
datei (datetime): end date of the inversion window
"""
datei = getattr(cntrlv, "datei")
datef = getattr(cntrlv, "datef")
cntrlv.dim = 0
cntrlv.xb = np.ones(0)
cntrlv.std = np.ones(0)
# Use boundaries for control vector values or not
if cntrlv.use_boundaries:
cntrlv.lower_bounds = np.ones(0)
cntrlv.upper_bounds = np.ones(0)
datavect = cntrlv.datavect
# If no definition is specified for the control vector in the Yaml,
# return empty control vector
if not hasattr(datavect, "components"):
return cntrlv
# Else, carry on initializing
components = datavect.components
for comp in components.attributes:
component = getattr(components, comp)
# Skip if component does not have parameters
if not hasattr(component, "parameters"):
continue
for trcr in component.parameters.attributes:
tracer = getattr(component.parameters, trcr)
# Skip tracers that are not control variables
tracer.iscontrol = hasattr(tracer, "hresol")
if not tracer.iscontrol:
continue
debug("Initializing xb structure for {}/{}"
.format(comp, trcr))
# Deals with temporal resolution of the control component
# A negative period is equivalent to no period
tracer.dates = dates.date_range(
tracer.datei,
tracer.datef,
getattr(tracer, "tresol", ""),
subperiod=getattr(tracer, "tsubresol", ""),
)
# Extending with min and max dates
if tracer.min_date < tracer.datei:
tracer.dates = np.append(
tracer.min_date, tracer.dates
)
if tracer.max_date > tracer.datef and len(tracer.dates) > 1:
tracer.dates[-1] = tracer.max_date
tracer.ndates = len(tracer.dates)
# Keeping a pointer to the correct location in the whole control
tracer.xpointer = cntrlv.dim
# Updating dimension
tracer.hresoldim = hresol2dim(tracer, tracer.domain, **kwargs)
tracer.vresoldim = vresol2dim(tracer, tracer.domain, **kwargs)
tracer.dim = tracer.ndates * tracer.hresoldim * tracer.vresoldim
cntrlv.dim += tracer.dim
# Initialize empty xb and std
cntrlv.xb = np.zeros(cntrlv.dim)
cntrlv.x = np.zeros(cntrlv.dim)
cntrlv.dx = np.zeros(cntrlv.dim)
cntrlv.std = np.zeros(cntrlv.dim)
if cntrlv.use_boundaries:
cntrlv.lower_bounds = np.zeros(cntrlv.dim)
cntrlv.upper_bounds = np.zeros(cntrlv.dim)
return cntrlv