Source code for pycif.plugins.datastreams.fields.iconart_icbc.read
import pandas as pd
import numpy as np
import xarray as xr
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
comp_type=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
# Check the type of limit condition to check
if comp_type is None:
raise Exception(
"Trying to read limit conditions for ICON-ART, "
"but did not specify the type"
)
# Read initial conditions
if comp_type == "inicond":
ic_file = files[0]
ds = xr.open_dataset(ic_file)
data = ds[var2extract][:]
# -- Add fake lat dimensions at the end
data = data.values[:, :, np.newaxis, :]
xmod = xr.DataArray(
data,
coords={"time": [np.min(dates)]},
dims=("time", "lev", "lat", "lon"),
)
# Read Lateral boundary conditions
elif comp_type in ["lbc", "background"]:
trcr_lbc = []
out_dates = []
for dd, ff in zip(dates, files):
# Getting the data
ds = xr.open_dataset(ff)
da = ds[var2extract]
if dd[0] in pd.to_datetime(da.time):
data = da.sel(time=dd[0])
else:
raise KeyError("Could not find the correct data in the lbc file")
# Appending
trcr_lbc.append(data)
out_dates.append(dd[0])
xout = np.array(trcr_lbc)[:, :, np.newaxis, :]
# Putting the data into an xarray
xmod = xr.DataArray(
xout, coords={"time": out_dates}, dims=("time", "lev", "lat", "lon")
)
else:
raise Exception(
"Could not recognize the type of file "
"to read in ICONART: {}".format(comp_type)
)
return xmod