Source code for pycif.plugins.datastreams.fluxes.chimere.read
import datetime
import os
import numpy as np
import xarray as xr
import resource
from .....utils.netcdf import readnc
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
tracer=None,
model=None,
ddi=None,
**kwargs
):
"""Get fluxes from pre-computed fluxes and load them into a pyCIF
variables
Args:
self: the fluxes Plugin
name: the name of the component
tracdir, tracfile: flux directory and file format
dates: list of dates to extract
interpol_flx (bool): if True, interpolates fluxes at time t from
values of surrounding available files
"""
var2extract = varnames if varnames != "" else name
# Reading required fluxes files
trcr_flx = []
for period, ff in zip(dates, files):
ds = xr.open_dataset(ff)
# Force conversion to float64
data = ds[var2extract].values.astype(float)
# Get the correct hour in the file
if ddi is not None:
hour = int((period[0] - ddi).total_seconds() // 3600)
else:
if "Times" not in ds:
raise Exception("Times variables is not available and I have "
"no information on the file date")
times = ds["Times"].values
times = [
datetime.datetime.strptime(
str(b"".join(s), "utf-8"), "%Y-%m-%d_%H:%M:%S"
)
for s in times
]
hour = int((period[0] - times[0]).total_seconds() // 3600)
# Scale the diurnal cycle
if hasattr(tracer, "diurnal_factor"):
data = tracer.diurnal_factor * data \
+ (1 - tracer.diurnal_factor) * data.mean(axis=0)
trcr_flx.append(data[hour, ...])
# Building a xarray
xmod = xr.DataArray(
trcr_flx, coords={"time": np.array(dates)[:, 0]},
dims=("time", "lev", "lat", "lon")
)
return xmod