Source code for pycif.plugins.datastreams.fluxes.VPRM_nc.read
import datetime
import os
import numpy as np
import xarray as xr
from netCDF4 import Dataset
from .....utils.netcdf import readnc
from .....utils.hdf5 import _hdf5_lock
from logging import info, debug
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
comp_type=None,
tracer=None,
**kwargs
):
"""Get fluxes from VPRM files and load them into a pyCIF variable.
For each date/file pair, opens the file and reads the field at the
hour-of-day index matching the requested date (each file is expected to
hold 24 hourly time steps for one day).
Args:
name (str): Unused directly, kept for interface consistency with
other flux plugins.
varnames (str): Name of the variable to read in the file.
dates (list): list of ``[start, end]`` date intervals to extract;
only the start of each interval is used to select the hour of
day.
files (list): list of files matching `dates` (each a single-element
list, per `fetch`'s output).
interpol_flx (bool): Unused, kept for interface consistency.
comp_type: Unused, kept for interface consistency.
tracer: Unused directly, kept for interface consistency.
Return:
xr.DataArray: the flux data with dimensions
``(time, lev, lat, lon)``.
"""
# list of the various fields read:
data = []
outdate = []
for dd, ff in zip(dates, files):
debug(f'Here put the reading of {[varnames]} in {ff} for {dd}')
with _hdf5_lock:
nc = xr.open_dataset(ff[0], decode_times=False)
read_field = nc[varnames][dd[0].hour].values
debug('e.g. get a 3d array read_field')
data.append(read_field)
outdate.append(dd[0])
debug("check")
debug(dates)
debug(len(data[0]))
debug(np.array(data).shape)
# if only one level for emissions, create the axis:
xmod = xr.DataArray(
np.array(data)[:, np.newaxis, ...],
coords={"time": outdate},
dims=("time", "lev", "lat", "lon"),
)
return xmod