Source code for pycif.plugins.datastreams.fields.lmdz_prodloss3d.read
import xarray as xr
import numpy as np
from netCDF4 import Dataset
from .....utils.hdf5 import _hdf5_lock
[docs]
def read(
self,
name,
tracdir,
tracfile,
varnames,
dates,
interpol_flx=False,
comp_type=None,
model=None,
**kwargs
):
"""Get pre-computed production/loss fields and load them into a pyCIF
variable.
Opens the monthly file matching each requested date, appends a
duplicated first-longitude column to close the cyclic LMDZ grid, and
picks the time slice matching the requested day of month (falling
back to the first day in the file if no exact match is found).
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): unused, accepted for interface compatibility
comp_type: unused, accepted for interface compatibility
model: unused, accepted for interface compatibility
Returns:
xarray.DataArray with dims ``(time, lev, lat, lon)``.
"""
list_file_prodloss = [dd.strftime(tracfile) for dd in dates]
trcr_prodloss = []
for dd, file_prodloss in zip(dates, list_file_prodloss):
with _hdf5_lock:
ds = xr.open_dataset(f"{tracdir}/{file_prodloss}", decode_times=True)
data = ds[f'{name}_prod'].values
# Loops zonally for consistency with other call to gridded values
data = np.append(data, data[..., np.newaxis, 0], axis=3)
# Searching for the right day
day = dd.day
ds_days = ds["time_counter.day"]
if day in ds_days:
data = data[day - 1]
else:
data = data[0]
trcr_prodloss.append(data)
xmod = xr.DataArray(
np.array(trcr_prodloss),
coords={"time": dates},
dims=("time", "lev", "lat", "lon")
)
return xmod