Source code for pycif.plugins.datastreams.fluxes.becker_ocean.read
import numpy as np
import xarray as xr
from logging import debug
import warnings
import xarray as xr
from xarray import SerializationWarning
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
tracer=None,
model=None,
ddi=None,
**kwargs
):
"""Get fluxes from raw files and load them into a pyCIF
variables.
Args:
name (str): name of the component
varnames (list[str]): original names of variables to read; use `name`
if `varnames` is empty
dates (list): list of the date intervals to extract
files (list): list of the files matching dates
Return:
xr.DataArray: the actual data with dimension:
time, levels, latitudes, longitudes
"""
var2extract = varnames if varnames != "" else name
# Loop over dates/files and import data
data = []
out_dates = []
for dd, ff in zip(dates, files):
debug(
"Reading the file {} for the date interval {}".format(
ff, dd
)
)
# Read and ignore warnings
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=SerializationWarning)
times = xr.open_dataset(ff)["time"].values
ind_time = np.where(times == np.datetime64(dd[0]))[0][0]
data.append(xr.open_dataset(ff)[var2extract][ind_time].values)
out_dates.append(dd[0])
# if only one level for emissions, create the axis
if data != []:
data = np.array(data)[:, np.newaxis]
data[np.isnan(data)] = 0
xmod = xr.DataArray(
data,
coords={"time": out_dates},
dims=("time", "lev", "lat", "lon"),
)
return xmod