Source code for pycif.plugins.datastreams.fluxes.CarbonMonitor.read
import numpy as np
import xarray as xr
from pathlib import Path
import pandas as pd
from logging import debug, info
[docs]
def read(
self,
name,
varnames,
dates,
files,
interpol_flx=False,
tracer=None,
model=None,
ddi=None,
**kwargs
):
"""Get fluxes from raw files and load them into a pyCIF
Return:
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('Reading of {} in {} for {}'.format([varnames], ff, dd))
opened_file = ff
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"),
)