Source code for pycif.plugins.datastreams.fields.lmdz_inicond_ico.read
from __future__ import annotations
import datetime
import numpy as np
import xarray as xr
from .....utils.check.errclass import CifValueError
from .....utils.hdf5 import _hdf5_lock
[docs]
def read(
self,
name: str,
varnames: str,
dates: list[tuple[datetime.datetime, datetime.datetime]],
files: list[str],
tracer: object | None = None,
**kwargs,
) -> xr.DataArray:
"""Read a mixing-ratio initial condition (icosahedral grid) into a pyCIF DataArray.
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; only the first date is used as the
output time coordinate.
files: List with a single inicond file path.
tracer: Unused.
**kwargs: Unused.
Returns:
xarray.DataArray: A 4-dimensional ``(time, lev, lat, lon)`` array
with singleton ``time`` and ``lat`` axes (the icosahedral cell
dimension is carried by ``lon``).
Raises:
CifValueError: If more than one file is given.
"""
varnames = varnames if varnames else name
if len(files) > 1:
raise CifValueError("multiple files provided for initial conditions")
(path,) = files
with _hdf5_lock:
with xr.open_dataset(path) as ds:
data = ds[varnames].values
xmod = xr.DataArray(
data[np.newaxis, :, np.newaxis, :],
coords={"time": [dates[0][0]]},
dims=("time", "lev", "lat", "lon"),
)
return xmod