Source code for pycif.plugins.datastreams.fields.lmdz_chemfield_reg.read
from __future__ import annotations
import datetime
from os import PathLike
import pandas as pd
import xarray as xr
[docs]
def read(
self,
name: str,
varnames: str,
dates: list[tuple[datetime.datetime, datetime.datetime]],
files: list[str | PathLike],
ddi: datetime.datetime | None = None,
**kwargs,
):
"""Get fluxes from pre-computed fluxes and load them into a pyCIF
variables
Args:
self: the fluxes Plugin
name: the name of the component
tracdir, tracfile: flux directory and file format
dates: list of dates to extract
interpol_flx (bool): if True, interpolates fluxes at time t from
values of surrounding available files
"""
varnames = varnames if varnames else name
if ddi is None:
raise ValueError("'ddi' argument is required")
da_list = []
file_ref = ""
da = None
for (di, _), file_path in zip(dates, files):
if file_path != file_ref:
file_ref = file_path
with xr.open_dataset(file_path) as ds:
if varnames not in ds:
raise ValueError(
f"Variable '{varnames}' not found in '{file_path}'"
)
da = ds[varnames]
if da is None:
raise ValueError(f"Error while reading '{file_path}'")
# Assume monthly files with daily resolution
day_index = (di - ddi).days + ddi.day - 1
da_list.append(da.isel(time_counter=[day_index]))
xmod = xr.concat(da_list, dim="time_counter")
# Adding vertical dimension for deposition velocity fields
if "presnivs" not in xmod.dims:
xmod = xmod.expand_dims("presnivs", axis=1)
xmod = xmod.rename(time_counter="time", presnivs="lev")
# Dropping coordinates
for coord_name in xmod.coords:
if coord_name != "time":
xmod = xmod.drop(coord_name)
# Reordering dimensions
xmod = xmod.transpose("time", "lev", "lat", "lon")
return xmod