Source code for pycif.plugins.datastreams.fluxes.VPRM1km_nc.read
import datetime
import os
from logging import debug, info
import numpy as np
import pandas as pd
import xarray as xr
from netCDF4 import Dataset
from .....utils.netcdf import readnc
from .....utils.hdf5 import _hdf5_lock
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
comp_type=None,
tracer=None,
**kwargs,
):
"""Get fluxes from VPRM1km daily files and load them into a pyCIF variable.
For each date/file pair (re-opening only when the file path changes),
reads the full requested variable field for the day and extends it
across all 24 hourly timestamps of that 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 build the hourly
output timestamps.
files (list): list of files matching `dates`
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 = []
nc = None
opened_file = ""
for dd, ff in zip(dates, files):
debug(f'Here put the reading of {[varnames]} in {ff} for {dd}')
if ff != opened_file or nc is None:
opened_file = ff
with _hdf5_lock:
nc = xr.open_dataset(ff, decode_times=False)
read_field = nc[varnames][:].values
data.extend(read_field)
period = pd.date_range(dd[0], periods=24, freq="1h")
outdate.extend(period)
# 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