Source code for pycif.plugins.datastreams.fluxes.GCP_1x1_N2O.read
import datetime
import glob
import os
import numpy as np
import xarray as xr
from logging import info
from .....utils.netcdf import readnc
[docs]
def read(
self,
name,
tracdir,
tracfile,
varnames,
dates,
interpol_flx=False,
tracer=None,
model=None,
**kwargs
):
# tracfile can be a list of same length as dates
if type(tracfile) == str:
tracfile = [tracfile[:]]
if len(tracfile) != len(dates) \
and len(tracfile) > 1:
raise Exception(
"Try read GCP files from a list of dates and a "
"list of files, but not of same length:\n{}\n{}".format(
tracfile, dates
)
)
elif len(tracfile) == 1:
list_files = len(dates) * tracfile
else:
list_files = tracfile[:]
# Reading fluxes for periods within the simulation window
trcr_flx = []
for dd, dd_file in zip(dates, list_files):
file_flx = dd.strftime(dd_file)
fluxes = readnc(file_flx, [varnames])
#print('FFFFFFFFFFFFFFFFFF',fluxes.shape)
time = readnc(file_flx, ['TIME'])
lat = readnc(file_flx,['LAT'])
year_beg_file = int((dd_file.split('-')[0]).split('.')[-1])
index_dd = (dd.year - year_beg_file) * 12 + dd.month -1
trcr_flx.append(fluxes[index_dd, :, :])
if lat[1] < lat[0]:
xmod = xr.DataArray(
np.array(trcr_flx)[:, np.newaxis, ::-1,:],
coords={"time": dates},
dims=("time", "lev", "lat", "lon"),
)
else:
xmod = xr.DataArray(
np.array(trcr_flx)[:, np.newaxis, :,:],
coords={"time": dates},
dims=("time", "lev", "lat", "lon"),
)
return xmod