Source code for pycif.plugins.datastreams.backgrounds.tm5_background.read
import xarray as xr
import numpy as np
from netCDF4 import Dataset
import datetime
import pandas as pd
from .....utils.datastores.empty import init_empty
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
tracer=None,
model=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
"""
trcr_bc = []
out_dates = []
ref_file = ""
for dd, ff in zip(dates, files):
if ff != ref_file:
ref_file = ff
with Dataset(ff) as bkg:
# Station ids
ids = [''.join([c.decode().lower() for c in name])
for name in bkg[tracer.id_varname][:]]
ids = pd.Series(ids).str.split("_", expand=True)
stations = ids[0].values
network = ids[1].values
agl = ids[2].values
# Time steps
ts = np.array([
datetime.datetime(*t) for t in bkg[tracer.time_varname][:]
])
# Simulations from TM5
data = bkg.variables[self.varname][:]
mesh_stat, mesh_ts = np.meshgrid(stations, ts)
mesh_network, mesh_ts = np.meshgrid(network, ts)
mesh_agl, mesh_ts = np.meshgrid(agl, ts)
ds = init_empty()
ds[("metadata", "date")] = mesh_ts.flatten()
ds[("metadata", "station")] = mesh_stat.flatten()
ds[("metadata", "network")] = mesh_network.flatten()
ds[("metadata", "parameter")] = name
ds[("metadata", "duration")] = 1
ds[("maindata", "obserror")] = 0
ds[("maindata", "spec")] = \
data.flatten() * getattr(tracer, "numscale", 1)
return ds