Source code for pycif.plugins.datastreams.fields.lmdz_chemfield.read

import xarray as xr
from .....utils.hdf5 import _hdf5_lock


[docs] def read( self, name, varnames, dates, files, interpol_flx=False, comp_type=None, ddi=None, **kwargs ): """Read LMDz chemical field data and load it into a pyCIF DataArray. Opens each distinct file in ``files`` once, selects the day index (relative to the file's month, computed from ``ddi``) for each requested date, concatenates the selected time steps along ``time_counter``, renames dimensions to pyCIF's ``(time, lev, lat, lon)`` convention, drops non-time coordinates, and duplicates the first longitude column to close LMDZ's cyclic grid. 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``; each distinct file is opened only once. interpol_flx: Unused. comp_type: Unused. ddi: Reference date used to compute each date's day-of-month index within its file. **kwargs: Unused. Returns: xarray.DataArray: A 4-dimensional ``(time, lev, lat, lon)`` array. """ varnames = varnames if varnames else name da_list = [] file_ref = "" for dd, file_path in zip(dates, files): if file_path != file_ref: file_ref = file_path with _hdf5_lock: with xr.open_dataset(file_path) as ds: da = ds[varnames] day_index = (dd[0] - ddi).days da_list.append(da.isel(time_counter=[day_index])) xmod = xr.concat(da_list, dim='time_counter') 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') # Loop longitude coordinate to match LMDZ grid index = list(range(xmod.shape[3])) + [0] xmod = xmod.isel(lon=index) return xmod