import datetime
from logging import debug
import numpy as np
import xarray as xr
from netCDF4 import Dataset
from .....utils.netcdf import readnc
[docs]
def read(
self, name, tracdir, tracfile, varnames, dates, comp_type=None, model=None, **kwargs
):
"""Get prescribed concentrations from pre-computed fields and load them
into a pyCIF
variables
Args:
self: the prescribed concentrations Plugin
name: the name of the component
tracdir, tracfile: raw data directory and file format
dates: list of dates to extract
"""
# # Prescribed species are specified in the chemical scheme
# TO DO: give varname in each prescribed species
# print('aaaaaaaa',varnames)
# if len(varnames) > 0:
# raise Exception(
# "Prescribed species are specified in the chemical scheme,
# not in the yaml file"
# )
# df_prescr = pd.read_csv(
# "{}/PRESCRIBED_SPECIES.{}".format(
# model.chemistry.dirchem_ref, model.chemistry.schemeid),
# index_col=False,
# header=None,
# comment="#",
# engine="python",
# sep="\s+",
# )
# varnames = df_prescr.iloc[:,1].values
# print('vvvv',varnames,len(varnames))
# Keep in memory data from previous date in case they are from the same file
concs, time, lat = 0, 0, 0
ref_file = ""
dates_file = []
data = []
for dd, file_prescr in zip(dates, tracfile):
date_ref = datetime.datetime(year=dd.year, month=dd.month, day=1)
if file_prescr != ref_file:
debug("Reading file {}".format(file_prescr))
# Read time manually as time_counter:calendar = "360d"
# not dealt with by decode_times
# latitude for N-S orientation
concs, time, lat = readnc(file_prescr, [varnames, 'time_counter', "lat"])
ref_file = file_prescr
dates_file = [date_ref + datetime.timedelta(hours=t / 3600) for t in time]
d = dates_file.index(dd)
data.append(concs[d, :, :, :])
if lat[1] < lat[0]:
xmod = xr.DataArray(
np.array(data)[:, :, ::-1, :],
coords={"time": dates},
dims=("time", "lev", "lat", "lon"),
)
else:
xmod = xr.DataArray(
np.array(data)[:, :, :, :],
coords={"time": dates},
dims=("time", "lev", "lat", "lon"),
)
return xmod