Source code for pycif.plugins.datastreams.fluxes.edgar_v5.read
import datetime
import glob
import os
from netCDF4 import Dataset, num2date
import numpy as np
import xarray as xr
from logging import info
from .....utils.hdf5 import _hdf5_lock
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
tracer=None,
model=None,
**kwargs
):
"""Read EDGAR v5 fluxes for the requested dates into a pyCIF array.
For each requested date/file pair, if the file has a ``time`` variable,
the exact matching time index is located and that slice is read;
otherwise the full 2D field is read directly (single-record file). The
longitude axis is then split at column 1800 and re-concatenated
(``data[:, :1800]`` followed by ``data[:, 1800:]``, i.e. in the same
order) before being appended to the output.
Args:
self: the fluxes Plugin.
name: the name of the component; unused directly, kept for
interface compatibility.
varnames (str): variable name to read from the file.
dates (list): list of the date intervals to extract.
files (list): list of files matching ``dates``.
interpol_flx (bool): unused, kept for interface compatibility.
tracer: unused, kept for interface compatibility.
model: unused, kept for interface compatibility.
**kwargs: unused, kept for interface compatibility.
Returns:
xr.DataArray: the flux data with dimensions
``(time, lev, lat, lon)``.
"""
# Reading fluxes for periods within the simulation window
trcr_flx = []
trcr_dates = []
for dd, file_flx in zip(dates, files):
# Check if "time" is in variables
# Do not do it with open_dataset which is slow with big files...
with _hdf5_lock:
with Dataset(file_flx, "r") as f:
available_time = "time" in f.variables
if available_time:
with _hdf5_lock:
with Dataset(file_flx, "r") as f:
times = f.variables["time"]
times = num2date(times[:], times.units,
only_use_python_datetimes=True,
only_use_cftime_datetimes=False)
ind_data = np.where(times == dd[1])[0][0]
# Now read the data at the correct index
with _hdf5_lock:
with Dataset(file_flx, "r") as f:
data = f.variables[varnames][ind_data].data
else:
with _hdf5_lock:
nc = xr.open_dataset(file_flx)
data = nc[varnames].values
trcr_flx.append(
np.concatenate([data[:, :1800], data[:, 1800:]], axis=1))
trcr_dates.append(dd[0])
return xr.DataArray(
np.array(trcr_flx)[:, np.newaxis, ...],
coords={"time": trcr_dates},
dims=("time", "lev", "lat", "lon"),
)