Source code for pycif.plugins.datastreams.fields.iconart_icbc.read
import pandas as pd
import numpy as np
import xarray as xr
from .....utils.check.errclass import CifError, CifKeyError
from .....utils.hdf5 import _hdf5_lock
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
comp_type=None,
ddi=None,
**kwargs
):
"""Read ICON-ART initial/boundary condition data into a pyCIF DataArray.
For ``comp_type == "inicond"``, reads the variable from the single
file in ``files`` and adds a singleton (fake) latitude axis. For
``"lbc"``/``"background"``, reads the variable from each file in turn,
selects the time slice matching the requested date, and stacks the
results (also adding a singleton latitude axis).
Args:
self: The field/tracer Plugin.
name: Name of the component (used as fallback variable name).
varnames: Name of the variable to read; if empty, ``name`` is used
instead.
dates: List of date entries to extract, matching ``files``.
files: List of files matching ``dates``.
interpol_flx: Unused.
comp_type: Type of file to read: ``"inicond"``, ``"lbc"`` or
``"background"``.
ddi: Unused.
**kwargs: Unused.
Returns:
xarray.DataArray: A 4-dimensional ``(time, lev, lat, lon)`` array
with a singleton ``lat`` axis (ICON-ART's native grid is
unstructured; the horizontal dimension is carried by ``lon``).
Raises:
CifError: If ``comp_type`` is ``None`` or not recognized.
CifKeyError: If the requested date is not found in an
``lbc``/``background`` file.
"""
var2extract = varnames if varnames != "" else name
# Check the type of limit condition to check
if comp_type is None:
raise CifError(
"Trying to read limit conditions for ICON-ART, "
"but did not specify the type"
)
# Read initial conditions
if comp_type == "inicond":
ic_file = files[0]
with _hdf5_lock:
ds = xr.open_dataset(ic_file)
data = ds[var2extract][:]
# -- Add fake lat dimensions at the end
data = data.values[:, :, np.newaxis, :]
xmod = xr.DataArray(
data,
coords={"time": [np.min(dates)]},
dims=("time", "lev", "lat", "lon"),
)
# Read Lateral boundary conditions
elif comp_type in ["lbc", "background"]:
trcr_lbc = []
out_dates = []
for dd, ff in zip(dates, files):
# Getting the data
with _hdf5_lock:
ds = xr.open_dataset(ff)
da = ds[var2extract]
if dd[0] in pd.to_datetime(da.time):
data = da.sel(time=dd[0])
else:
raise CifKeyError("Could not find the correct data in the lbc file")
# Appending
trcr_lbc.append(data)
out_dates.append(dd[0])
xout = np.array(trcr_lbc)[:, :, np.newaxis, :]
# Putting the data into an xarray
xmod = xr.DataArray(
xout, coords={"time": out_dates}, dims=("time", "lev", "lat", "lon")
)
else:
raise CifError(
f"Could not recognize the type of file to read in ICONART: {comp_type}"
)
return xmod