Source code for pycif.plugins.datastreams.fields.lmdz_ic.read
from logging import debug
import xarray as xr
from .....utils.check.errclass import IllegalArgumentError
[docs]
def read(self, name, varnames, dates, files, tracer=None, **kwargs):
is_perturbated = "__sample#" in name
if varnames and not is_perturbated:
if hasattr(tracer, 'restart_id'):
raise IllegalArgumentError(
"'restart_id' and 'varname' arguments cannot be used together")
if not isinstance(varnames, str):
raise TypeError(
"'varnames' argument should be of type str, not list")
var_name = varnames
else:
if hasattr(tracer, 'restart_id'):
debug("getting 'restart_id' from initial condition plugin's arguments")
restart_id = tracer.restart_id
else:
debug("getting 'restart_id' from chemical scheme")
if not hasattr(self.chemistry.acspecies, name):
raise ValueError(
f"active species '{name}' not found in the chemical scheme")
spec = getattr(self.chemistry.acspecies, name)
restart_id = spec.restart_id
if isinstance(restart_id, str):
var_name = restart_id
else:
var_name = f'q{restart_id:02d}'
if len(files) > 1:
raise ValueError("multiple files provided for initial conditions")
path, = files
with xr.open_dataset(path) as ds:
data = ds[var_name].values
xmod = xr.DataArray(
data,
coords={'time': [dates[0][0]]},
dims=('time', 'lev', 'lat', 'lon')
)
return xmod