Source code for pycif.plugins.datastreams.fields.lmdz_outfields_nc.read
import numpy as np
import xarray as xr
import os
import datetime
import tracemalloc
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
tracer=None,
model=None,
**kwargs
):
# Reading required lmdz files
trcr_lmdz = []
trcr_dates = []
data = None
file_ref = ""
for dd, file_concs in zip(dates, files):
if file_concs != file_ref:
file_ref = file_concs
# Reading file
with xr.open_dataset(file_concs) as ds:
data = ds[varnames if len(varnames) > 0 else name].load()
# Flip upside down
# TODO: check once for all whether to flip North-South...
# data = data[:, :, ::-1]
# Get correct data index
# as the month/year may be different
# in the file than the one required
times = ds["time"].to_pandas().dt.to_pydatetime()
ind = np.where(times == dd[0])[0][0]
# Appending
trcr_lmdz.append(data[ind, ...].values)
trcr_dates.append(dd[0])
# Putting the data into an xarray
xmod = xr.DataArray(
trcr_lmdz, coords={"time": trcr_dates}, dims=("time", "lev", "lat", "lon")
)
return xmod