import datetime
from logging import debug
import numpy as np
import pandas as pd
import xarray as xr
from netCDF4 import Dataset
from .....utils.netcdf import readnc
[docs]
def read(
self,
name,
tracdir,
tracfile,
varnames,
dates,
interpol_j=False,
comp_type=None,
model=None,
**kwargs,
):
"""Get photolysis kinetic constants from pre-computed Js and load them
into a pyCIF
variables
Args:
self: the photochem Plugin
name: the name of the component
tracdir, tracfile: kinetic directory and file format
dates: list of dates to extract
interpol_j (bool): if True, interpolates Js at time t from
values of surrounding available files
"""
# Js are specified in the reactions, in the chemical scheme
# if len(varnames) > 0:
# raise Exception(
# "Js are specified in the reactions, in the chemical scheme, "
# "not in the yaml file"
# )
# df_j = pd.read_csv(
# "{}/PHOTO_RATES.{}".format(model.chemistry.dirchem_ref,
# model.chemistry.schemeid),
# index_col=False,
# header=None,
# comment="#",
# engine="python",
# sep="\s+",
# )
# varnames = df_j.iloc[:, 1].values[0]
# varnames = [ varnames , 'temp', 'pmid']
# CE QUI PRECEDE VA AILLEURS EN FAIT?
# , read est appele pour chaque variable voulue?
concs, time, lat = 0, 0, 0
ref_file = ""
dates_file = []
data = []
for (
dd,
file_photochem,
) in zip(dates, tracfile):
date_ref = datetime.datetime(year=dd.year, month=dd.month, day=1)
if file_photochem != ref_file:
debug("Reading file {}".format(file_photochem))
# Read time manually as time_counter:calendar = "360d"
# not dealt with by decode_times
# latitude for N-S orientation
Js, time, lat = readnc(
file_photochem, np.append(name, ['time_counter', "lat"])
)
ref_file = file_photochem
dates_file = [date_ref + datetime.timedelta(hours=t / 3600) for t in time]
d = dates_file.index(dd)
data.append(Js[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