import numpy as np
import xarray as xr
from pathlib import Path
import pandas as pd
from logging import debug, info
from .....utils.hdf5 import _hdf5_lock
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
tracer=None,
model=None,
ddi=None,
**kwargs
):
"""Get Carbon Monitor fluxes, apply hourly GNFR profiles, load into pyCIF.
Reads the hour-in-day GNFR time-profile CSV from ``tracer.dir_profils``,
maps each of the Carbon Monitor sectors selected in ``tracer.cat_select``
(indices 0-7) to its corresponding TNO GNFR category via the hardcoded
``TNO_cat`` mapping, applies the matching hourly coefficient to the raw
per-sector emission field, and sums the selected sectors together.
Args:
name (str): name of the component.
varnames (list[str] or str): variable name to read from the file.
dates (list): list of the date intervals to extract.
files (list): list of files matching ``dates``.
interpol_flx (bool): unused, kept for interface compatibility.
tracer: the tracer Plugin, giving access to ``domain``,
``dir_profils`` and ``cat_select``.
model: unused, kept for interface compatibility.
ddi: unused, kept for interface compatibility.
**kwargs: unused, kept for interface compatibility.
Returns:
xr.DataArray: the actual data with dimension:
time, levels, latitudes, longitudes
"""
# Get domain dimensions for random generation
domain = tracer.domain
nlon = domain.nlon
nlat = domain.nlat
nlevel = domain.nlev
# Time profiles
Pdir = tracer.dir_profils
f = Path(Pdir, 'timeprofiles-hour-in-day_GNFR.csv')
coef_dict = {}
key = 'hour'
coef = pd.read_csv(f, sep=';', comment='#')
l = list(coef.columns)
l2 = l[l.index('GNFR_Category_Name') + 1:]
coef_dict[key] = coef[l2].values
cat_list = tracer.cat_select
# corresponding CM : TNO categories
# 0: Agriculture;
# 1: Energy;
# 2: Industrial;
# 3: Transportation;
# 4: Residential, Commercial, Other;
# 5: Solvents production and application;
# 6: Waste;
# 7: International Shipping"
TNO_cat = {0: 15, 1: 1, 2: 2, 3: 6, 4: 3, 5: 5, 6: 13, 7: 10}
# Loop over dates/files and import data
data = []
outdates = []
nc = None
opened_file = ""
emis = 0
for dd, ff in zip(dates, files):
dd_CET = dd[0]
ddi = dd_CET.day
houri = dd_CET.hour
if ff != opened_file or nc is None:
info(f'Reading of {[varnames]} in {ff} for {dd}')
opened_file = ff
with _hdf5_lock:
nc = xr.open_dataset(ff, decode_times=False)
emis = nc[varnames].values
emis_array = np.zeros((nlevel, nlat, nlon))
for cat in cat_list:
local_hour_coef = coef_dict['hour'][TNO_cat[cat] - 1]
emis_array[0, :, :] += \
emis[ddi - 1, cat, :, :] * local_hour_coef[houri % 24]
data.append(emis_array)
outdates.append(dd_CET)
return xr.DataArray(
data,
coords={"time": outdates},
dims=("time", "lev", "lat", "lon"),
)