Source code for pycif.plugins.datastreams.fields.oldlmdz_ic.read
import xarray as xr
from netCDF4 import Dataset
from .....utils.check.errclass import CifError
from .....utils.hdf5 import _hdf5_lock
[docs]
def read(
self,
name,
tracdir,
tracfile,
varnames,
dates,
interpol_flx=False,
comp_type=None,
model=None,
tracer=None,
**kwargs
):
"""Get the initial concentration field from the legacy LMDZ4 restart
file and load it into a pyCIF variable.
All entries of ``tracfile`` must resolve to the same file (a single
restart file is expected); the variable is looked up by ``varnames``
first, falling back to ``q{restart_id:02d}`` from the chemical
scheme if ``varnames`` is not found in the file.
Args:
self: the IC Plugin
name: the name of the component
tracdir, tracfile: restart file directory and file format
varnames: name of the variable to read, or a name absent from
the file to trigger the ``restart_id``-based fallback
dates: list of dates to extract; only the earliest date is used
interpol_flx (bool): unused, accepted for interface compatibility
comp_type: unused, accepted for interface compatibility
model: the model Plugin, used to resolve the fallback variable
name via ``model.chemistry.acspecies``
tracer: unused, accepted for interface compatibility
Returns:
xarray.DataArray with dims ``(time, lev, lat, lon)``.
Raises:
CifError: if more than one distinct file is given in
``tracfile``.
"""
if len(set(tracfile)) > 1:
raise CifError(
'More than one file for initial conditions: is not logical'
)
ic_file = min(dates).strftime(list(set(tracfile))[0])
with _hdf5_lock:
with Dataset(ic_file, "r") as f:
try:
data = f.variables[varnames][:]
except KeyError:
spec_id = f"q{getattr(model.chemistry.acspecies, name).restart_id:02d}"
data = f.variables[spec_id][:]
xmod = xr.DataArray(
data, coords={"time": [min(dates)]}, dims=("time", "lev", "lat", "lon")
)
return xmod