Source code for pycif.plugins.datastreams.fields.oldlmdz_ic.read
import xarray as xr
from netCDF4 import Dataset
[docs]
def read(
self,
name,
tracdir,
tracfile,
varnames,
dates,
interpol_flx=False,
comp_type=None,
model=None,
tracer=None,
**kwargs
):
"""Get initial field from pre-computed fields and load them into a pyCIF
variables
Args:
self: the IC Plugin
name: the name of the component
tracdir, tracfile: raw data directory and file format
dates: list of dates to extract
interpol_flx (bool): if True, interpolates at time t from
values of surrounding available files
"""
if len(set(tracfile)) > 1:
raise Exception(
'More than one file for initial conditions: is not logical'
)
ic_file = min(dates).strftime(list(set(tracfile))[0])
with Dataset(ic_file, "r") as f:
try:
data = f.variables[varnames][:]
except KeyError:
spec_id = "q{:02d}".format(
getattr(model.chemistry.acspecies, name).restart_id
)
data = f.variables[spec_id][:]
xmod = xr.DataArray(
data, coords={"time": [min(dates)]}, dims=("time", "lev", "lat", "lon")
)
return xmod