Commit 68a74bd0 authored by Woude, Auke van der's avatar Woude, Auke van der
Browse files

write fluxes to files

parents 36119604 e79032dc
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ctdas_light_refactor_new</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?>
<pydev_project>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/ctdas_light_refactor_new</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
</pydev_project>
......@@ -59,7 +59,7 @@ categories = {
'CO2.uncertainty': 'n',
'CO': -4.32,
'CO.uncertainty': 'l'},
'cars middle road': {'name': 'cars middle road',
'heavy duty': {'name': 'heavy duty',
'model': 1,
'spatial': 'Road transport',
'temporal': 't_carmr',
......
......@@ -20,6 +20,7 @@ import numpy as np
from numpy import array, logical_and
import da.tools.io4 as io
import math
import pytz
import da.tools.rc as rc
from da.tools.general import create_dirs, to_datetime
......@@ -63,13 +64,27 @@ class EmisModel(object):
self.countries = [country.strip() for country in dacycle.dasystem['countries'].split(';')]
areafile = dacycle.dasystem['area.file']
self.area = nc.Dataset(areafile)['Area'][:]
self.time_prof_file = dacycle.dasystem['file.timeprofs']
self.energy_use_per_country = energy_use_per_country
self.categories = categories
self.paramdict = rc.read(dacycle.dasystem['paramdict'])
self.country_mask_file = dacycle.dasystem['country.mask']
logging.debug('Emismodel has been set-up')
self.countries_dict = { # From ISO3166-1 alpha-3 to alpha-2
'AUS': 'at',
'BEL': 'be',
'CZE': 'cz',
'FRA': 'fr',
'DEU': 'de',
'LUX': 'lu',
'NED': 'nl',
'POL': 'pl',
'CHE': 'cz',
'GBR': 'gb'}
logging.debug('Emismodel has been set-up')
def find_in_state(self, params, station, cat, name=None, return_index=False):
"""Function that finds the index in the state vector"""
......@@ -77,7 +92,6 @@ class EmisModel(object):
key = station + '.' + cat
else:
key = station + '.' + cat + '.' + name
#print('Looking for {}'.format(key))
if key in self.paramdict:
i_in_state = int(self.paramdict[key])
......@@ -88,12 +102,13 @@ class EmisModel(object):
elif return_index: return False
else: return 1
def get_emis(self, dacycle, samples, indices, do_pseudo):
def get_emis(self, dacycle, indices):
"""set up emission information for pseudo-obs (do_pseudo=1) and ensemble runs (do_pseudo=0)"""
self.timestartkey = self.dacycle['time.sample.start']
self.timefinishkey = self.dacycle['time.sample.end']
time_profiles = self.make_time_profiles(indices=indices)
self.time_profiles= time_profiles
pool = Pool(self.nmembers)
# Create the function that calculates the conentration
......@@ -112,7 +127,12 @@ class EmisModel(object):
emission_factor = values['emission_factors']
emission_factor *= self.find_in_state(params, country, cat, 'emission_factors')
fraction_of_total = values['fraction_of_total']
if cat == 'Public power gas':
fraction_of_total = energy_use_per_country[country]['Public power ratio gas']
elif cat == 'Public power coal':
fraction_of_total = 1 - energy_use_per_country[country]['Public power ratio gas']
else: fraction_of_total = values['fraction_of_total']
fraction_of_total *= self.find_in_state(params, country, cat, 'fraction_of_total')
e_use = self.energy_use_per_country[country][values['spatial']]
......@@ -128,10 +148,19 @@ class EmisModel(object):
def get_emissions(self, dacycle, time_profiles, member):
"""read in proxy data used for spatial distribution of the gridded emissions, disaggregate yearly totals for the area"""
prmfile=os.path.join(dacycle['dir.input'],'parameters.%03d.nc'%member)
f = io.ct_read(prmfile, 'read')
params = f.get_variable('parametervalues')
f.close()
if isinstance(member, int) or member == 'optimised':
if isinstance(member, int):
prmfile = os.path.join(dacycle['dir.input'],'parameters.%03d.nc'%member)
prmname = 'parametervalues'
elif member == 'optimised':
prmfile = os.path.join(dacycle['dir.output'], 'optimizer.%s.nc' % dacycle['time.start'].strftime('%Y%m%d'))
prmname = 'statevectormean_optimized'
f = io.ct_read(prmfile, 'read')
params = f.get_variable(prmname)
f.close()
elif member == 'true':
params = np.ones(100)
yremis = self.get_yearly_emissions(params)
# Create a recalculation factor from kg/km2/yr to umol/m2/sec
M_mass = np.array([44e-9, 28e-9][:self.nrspc])
......@@ -162,38 +191,82 @@ class EmisModel(object):
emissions = []
for i, category in enumerate(self.categories):
spatial_name = self.categories[category]['spatial']
cat_index = proxy_category_names.index(spatial_name)
temporal_name = self.categories[category]['temporal']
temporal_profile = time_profiles[temporal_name]
emissions.append(spatial_emissions[cat_index, :, :, :] * temporal_profile[None, :, :, :])
emissions.append(spatial_emissions[i, :, :, :] * temporal_profile[None, :, :, :])
self.emissions = emissions
emissions = np.array(emissions)
emissions = np.swapaxes(emissions, 0,1) # Indices: species, category, time, lat, lon
emissions = np.array(emissions) # [cat, species, time, lat, lon]
emissions = np.swapaxes(emissions, 0, 2) # [time, cat, species, lat, lon]
emissions = np.swapaxes(emissions, 1, 2) # [time, species, cat, lat, lon]
# Recalculate spatial emissions to umol/sec/m2
emissions = emissions / kgperkmperyr2umolperm2pers[:, None, None, :, :]
emissions = emissions / kgperkmperyr2umolperm2pers[None, :, None, :, :]
## create output file
prior_file = os.path.join(self.inputdir, 'prior_spatial_{0:03d}.nc'.format(member))
f = io.CT_CDF(prior_file, method='create')
dimid = f.add_dim('ncat', self.nrcat)
dimid2 = f.add_dim('ops',2 )
dimtime= f.add_dim('time', emissions.shape[2])
dimlat = f.add_dim('lat', self.area.shape[0])
dimlon = f.add_dim('lon', self.area.shape[1])
#loop over all tracers
for i, species in enumerate(self.species):
savedict = io.std_savedict.copy()
savedict['name'] = species
savedict['long_name'] = "Spatially distributed emissions"
savedict['units'] = "micromole/m2/s"
savedict['dims'] = dimid + dimtime + dimlat + dimlon
savedict['values'] = emissions[i, :, :, :]
savedict['dtype'] = 'float'
f.add_data(savedict)
f.close()
if not isinstance(member, str):
prior_file = os.path.join(self.inputdir, 'prior_spatial_{0:03d}.nc'.format(member))
f = io.CT_CDF(prior_file, method='create')
if self.dacycle.dasystem['cat.sum_emissions']:
emissions = emissions.sum(axis=2) # [time, species, lat, lon]
logging.debug('Summed emissions')
else:
dimid = f.add_dim('ncat', self.nrcat)
dimid2 = f.add_dim('ops',2 )
dimtime= f.add_dim('time', emissions.shape[0])
dimlat = f.add_dim('lat', self.area.shape[0])
dimlon = f.add_dim('lon', self.area.shape[1])
#loop over all tracers
for i, species in enumerate(self.species):
savedict = io.std_savedict.copy()
savedict['name'] = species
savedict['long_name'] = "Spatially distributed emissions"
savedict['units'] = "micromole/m2/s"
if self.dacycle.dasystem['cat.sum_emissions']:
dims = dimtime + dimlat + dimlon
else: dims = dimtime + dimid + dimlat + dimlon
savedict['dims'] = dims
if self.dacycle.dasystem['cat.sum_emissions']:
savedict['values'] = emissions[:, i]
else: savedict['values'] = emissions[:, :, i]
savedict['dtype'] = 'float'
f.add_data(savedict)
f.close()
if member == 0 or isinstance(member, str):
if member == 0: qual = 'prior'
elif member == 'optimised': qual = 'optimised'
elif member == 'true': qual = 'true'
name = 'ff_emissions_{}_{}.nc'.format(qual, self.dacycle['time.sample.start'].strftime('%Y%m%d'))
emisfile = os.path.join(dacycle['dir.output'], name)
f = io.CT_CDF(emisfile, method='create')
if self.dacycle.dasystem['cat.sum_emissions']:
emissions = emissions.sum(axis=2) # [time, species, lat, lon]
else:
dimid = f.add_dim('ncat', self.nrcat)
dimid2 = f.add_dim('ops',2 )
backtime = int(dacycle.dasystem['run.backtime'])
dimtime= f.add_dim('time', emissions.shape[0]-backtime)
dimlat = f.add_dim('lat', self.area.shape[0])
dimlon = f.add_dim('lon', self.area.shape[1])
#loop over all tracers
for i, species in enumerate(self.species):
savedict = io.std_savedict.copy()
savedict['name'] = species
savedict['long_name'] = "Spatially distributed emissions"
savedict['units'] = "micromole/m2/s"
if self.dacycle.dasystem['cat.sum_emissions']:
dims = dimtime + dimlat + dimlon
else: dims = dimtime + dimid + dimlat + dimlon
savedict['dims'] = dims
if self.dacycle.dasystem['cat.sum_emissions']:
savedict['values'] = emissions[backtime:, i]
else: savedict['values'] = emissions[backtime:, :, i]
savedict['dtype'] = 'float'
f.add_data(savedict)
f.close()
def make_time_profiles(self, indices):
"""Function that calculates the time profiles based on pre-specified
monthly, daily and hourly profiles. Temperature and radiation affect
......@@ -205,21 +278,26 @@ class EmisModel(object):
The temporal profiles (one for each hour) for each gridcel and timestep """
# --- Settings
year = self.startdate.year
ndays = (dtm.datetime(year, 12, 31) - dtm.datetime(year - 1, 12, 31)).days
self.ndays = ndays
times = np.array([dtm.datetime(year, 1, 1, 0, 0, 0) + dtm.timedelta(hours = i) for i in range(ndays*24)])
ndays_year = (dtm.datetime(year, 12, 31) - dtm.datetime(year - 1, 12, 31)).days
self.ndays = ndays_year
times = np.array([dtm.datetime(year, 1, 1, 0, 0, 0) + dtm.timedelta(hours = i) for i in range(ndays_year*24)])
times_add1 = times[indices.start: indices.stop + 1]
times = times[indices]
self.times = times
numdays = (times[-1] - times[0]).days + 1
day_start = times[0].timetuple().tm_yday
datapath = '/projects/0/ctdas/RINGO/EmissionInventories/DynEmMod_TimeProfiles'
infilename = '{}/RINGO_ECMWF_DailyMeteo{}.nc'.format(datapath, year)
with nc.Dataset(infilename) as infile:
T2myr = infile.variables['T2m' ][:ndays] - 273.15 # T2m K --> oC
U10 = infile.variables['U10m' ][:ndays] # u m/s
Rinyr = infile.variables['Rsin' ][:ndays] / (24. * 1.0e4) # Radiation (?) J/m2/day --> J/cm2/hr
T2myr_av = infile.variables['T2m_avg' ][:ndays] - 273.15 #
U10_av = infile.variables['U10m_avg'][:ndays]
Rinyr_av = infile.variables['Rsin_avg'][:ndays]
T2myr = infile.variables['T2m' ][:ndays_year] - 273.15 # T2m K --> oC
U10 = infile.variables['U10m' ][:ndays_year] # u m/s
Rinyr = infile.variables['Rsin' ][:ndays_year] / (24. * 1.0e4) # Radiation (?) J/m2/day --> J/cm2/hr
T2myr_av = infile.variables['T2m_avg' ][:ndays_year] - 273.15 #
U10_av = infile.variables['U10m_avg'][:ndays_year]
Rinyr_av = infile.variables['Rsin_avg'][:ndays_year]
ndays, nlat, nlon = T2myr.shape
# --- calculate degree day sum and corresponding time profiles
fr_cons = 0.2 # constant emission for consumers (cooking, warm water)
fr_gls = 0. # no constant emission for glasshouses
......@@ -237,164 +315,139 @@ class EmisModel(object):
HDC_coal = np.empty((ndays, nlat, nlon)) # Heating Demand Category 3 (Power plants)
HDC_gas = np.empty((ndays, nlat, nlon)) # Heating Demand Category 4 (Renewable activity)
for i in range(ndays):
HDC_cons[i] = np.fmax(T0_cons - T2myr[i, :, :], 0) # Daily demand for consumers / household heating
HDC_gls [i] = np.fmax(T0_gls - T2myr[i, :, :], 0) # Daily demand for glasshouse heating
HDC_coal[i] = np.fmax(T0_coal - T2myr[i, :, :], 0) # Daily demand for coal-fired powerplant productvity
dum1 = np.fmax(U0_gas - U10[i, :, :], 0) # Wind energy
dum2 = np.fmax(R0_gas - Rinyr[i, :, :], 0) # Solar energy
HDC_gas [i] = dum1 * dum2
for day in range(ndays): #
HDC_cons[day] = np.fmax(T0_cons - T2myr[day, :, :], 0) # Daily demand for consumers / household heating
HDC_gls [day] = np.fmax(T0_gls - T2myr[day, :, :], 0) # Daily demand for glasshouse heating
HDC_coal[day] = np.fmax(T0_coal - T2myr[day, :, :], 0) # Daily demand for coal-fired powerplant productvity
wind_engy = np.fmax(U0_gas - U10[day, :, :], 0) # Wind energy
solar_engy = np.fmax(R0_gas - Rinyr[day, :, :], 0) # Solar energy
HDC_gas [day] = wind_engy * solar_engy
HC_cons = HDC_cons.mean(axis=0)
HC_gls = HDC_gls.mean(axis=0)
HC_coal = HDC_coal.mean(axis=0)
HC_gas = HDC_gas.mean(axis=0)
t_consd = (HDC_cons + fr_cons * HC_cons) / ((1 + fr_cons) * HC_cons) # daily time profile for consumer/household heating
t_glsd = (HDC_gls + fr_gls * HC_gls) / ((1 + fr_gls ) * HC_gls) # glasshouse
t_coald = (HDC_coal + fr_coal * HC_coal) / ((1 + fr_coal) * HC_coal) # coal-fired powerplant
t_gasd = (HDC_gas + fr_gas * HC_gas) / ((1 + fr_gas ) * HC_gas) # gas-fired powerplant
# Hourly time profiles for energy use and consumers
engy_hr = np.array([0.79, 0.72, 0.72, 0.71, 0.74, 0.80,
0.92, 1.08, 1.19, 1.22, 1.21, 1.21,
1.17, 1.15, 1.14, 1.13, 1.10, 1.07,
1.04, 1.02, 1.02, 1.01, 0.96, 0.88])
cons_hr = np.array([0.38, 0.36, 0.36, 0.36, 0.37, 0.50,
1.19, 1.53, 1.57, 1.56, 1.35, 1.16,
1.07, 1.06, 1.00, 0.98, 0.99, 1.12,
1.41, 1.52, 1.39, 1.35, 1.00, 0.42])
engy_hrm = np.tile(engy_hr, (nlat, nlon, 1)).transpose(2, 0, 1) #Repeat the daily time profile for all days
cons_hrm = np.tile(cons_hr, (nlat, nlon, 1)).transpose(2, 0, 1)
nt = len(self.times)
t_gas = np.empty((nt, nlat, nlon))
t_coal = np.empty((nt, nlat, nlon))
t_cons = np.empty((nt, nlat, nlon))
t_gls = np.empty((nt, nlat, nlon))
t_consd = ((HDC_cons + fr_cons * HC_cons) / ((1 + fr_cons) * HC_cons))[day_start-1:]# daily time profile for consumer/household heating, starting from the day of the inversion
t_glsd = ((HDC_gls + fr_gls * HC_gls) / ((1 + fr_gls ) * HC_gls))[day_start-1:] # glasshouse
t_coald = ((HDC_coal + fr_coal * HC_coal) / ((1 + fr_coal) * HC_coal))[day_start-1:] # coal-fired powerplant
t_gasd = ((HDC_gas + fr_gas * HC_gas) / ((1 + fr_gas ) * HC_gas))[day_start-1:] # gas-fired powerplant
#### Get the time profiles for all sectors:
with nc.Dataset(self.time_prof_file) as ds:
public_power_monthly= ds['FM_A'][:]
public_power_weekly = ds['FW_A'][:]
public_power_hourly = ds['FH_A'][:]
#Repeat the daily time profile for all days
for iday in range(ndays):
t_gas [iday * 24:(iday + 1) * 24, :, :] = np.tile(t_gasd [iday], (24, 1, 1)) * engy_hrm # hourly time profile = daily * hourly
t_coal[iday * 24:(iday + 1) * 24, :, :] = np.tile(t_coald[iday], (24, 1, 1)) * engy_hrm
t_cons[iday * 24:(iday + 1) * 24, :, :] = np.tile(t_consd[iday], (24, 1, 1)) * cons_hrm
t_gls [iday * 24:(iday + 1) * 24, :, :] = np.tile(t_glsd [iday], (24, 1, 1)) * cons_hrm
industry_monthly = ds['FM_B'][:]
industry_weekly = np.array([1.02] * 6 + [0.88]) # TNO https://atmosphere.copernicus.eu/sites/default/files/2019-07/MACC_TNO_del_1_3_v2.pdf
industry_hourly = 1
other_stationary_monthly = ds['FM_C'][:]
other_stationary_hourly = ds['FH_C'][:]
road_transport_monthly = ds['FM_F'][:]
road_transport_weekly = ds['FW_F'][:]
road_transport_hourly_wkd = ds['FHwd_F'][:]
road_transport_hourly_sat = ds['FHst_F'][:]
road_transport_hourly_sun = ds['FHsn_F'][:]
#######
# road traffic time profiles
#######
carhw_mnt = [1.04, 1.05, 0.98, 1.00, 1.00, 1.04, 1.01, 0.93, 0.86, 1.01, 1.07, 1.02] # monthly time profiles car highway
carmr_mnt = [1.05, 1.06, 0.93, 1.01, 0.99, 1.01, 1.00, 1.00, 0.86, 1.02, 1.08, 1.03] # car middle road
carur_mnt = [1.06, 1.10, 0.97, 1.00, 1.01, 1.00, 1.00, 0.98, 0.85, 1.01, 1.07, 1.01] # car urban road
hdvhw_mnt = [0.92, 0.89, 1.03, 1.02, 1.07, 1.05, 1.03, 1.02, 0.90, 0.97, 0.98, 0.94]
hdvmr_mnt = [1.06, 1.09, 0.93, 0.98, 0.99, 1.01, 1.02, 1.02, 0.78, 1.09, 1.08, 1.03]
hdvur_mnt = [1.10, 1.17, 0.91, 0.94, 0.76, 1.02, 1.04, 1.04, 0.82, 1.13, 1.14, 1.03]
carhw_wk = [1.00, 1.09, 1.10, 1.10, 1.11, 0.87, 0.73] # weekly time profile (mon-sun)
carmr_wk = [0.99, 1.07, 1.08, 1.07, 1.09, 0.93, 0.76]
carur_wk = [0.98, 1.06, 1.07, 1.07, 1.09, 0.92, 0.80]
hdvhw_wk = [1.23, 1.30, 1.27, 1.27, 1.33, 0.42, 0.19]
hdvmr_wk = [1.27, 1.25, 1.26, 1.29, 1.29, 0.45, 0.19]
hdvur_wk = [1.07, 1.24, 1.23, 1.21, 1.23, 0.63, 0.38]
carhw_hrw = [0.17, 0.09, 0.05, 0.05, 0.09, 0.42, 1.38, 1.87, # hourly time profile weekdays
1.81, 1.31, 1.11, 1.13, 1.26, 1.30, 1.39, 1.63,
1.95, 1.97, 1.46, 1.05, 0.78, 0.69, 0.62, 0.42]
carmr_hrw = [0.20, 0.09, 0.05, 0.05, 0.11, 0.36, 1.04, 1.42,
1.49, 1.26, 1.20, 1.25, 1.37, 1.42, 1.49, 1.62,
1.83, 1.92, 1.56, 1.23, 0.92, 0.81, 0.77, 0.56]
carur_hrw = [0.30, 0.20, 0.12, 0.09, 0.10, 0.25, 0.63, 1.22,
1.53, 1.17, 1.10, 1.21, 1.37, 1.41, 1.52, 1.71,
1.94, 2.03, 1.59, 1.18, 0.98, 0.91, 0.83, 0.61]
hdvhw_hrw = [0.13, 0.12, 0.12, 0.15, 0.28, 0.68, 1.59, 1.50,
1.46, 1.67, 1.72, 1.77, 1.70, 1.77, 1.82, 1.85,
1.60, 1.18, 0.97, 0.73, 0.49, 0.33, 0.22, 0.16]
hdvmr_hrw = [0.13, 0.09, 0.11, 0.10, 0.21, 0.52, 1.58, 1.75,
1.61, 1.74, 1.70, 1.69, 1.58, 1.70, 1.79, 1.96,
1.82, 1.23, 0.93, 0.61, 0.43, 0.30, 0.23, 0.19]
hdvur_hrw = [0.14, 0.08, 0.06, 0.08, 0.15, 0.36, 0.95, 1.61,
1.86, 1.77, 1.76, 1.79, 1.74, 1.75, 1.83, 2.16,
1.76, 1.27, 0.92, 0.65, 0.47, 0.37, 0.29, 0.21]
carhw_hrd = [0.57, 0.34, 0.20, 0.13, 0.11, 0.17, 0.29, 0.38, # hourly time profile weekends
0.65, 1.03, 1.30, 1.54, 1.75, 1.95, 1.94, 1.83,
1.81, 1.72, 1.32, 1.27, 1.18, 1.00, 0.84, 0.68]
carmr_hrd = [0.58, 0.35, 0.22, 0.14, 0.14, 0.25, 0.32, 0.38,
0.61, 1.01, 1.29, 1.50, 1.73, 1.90, 1.96, 1.88,
1.84, 1.76, 1.33, 1.18, 1.10, 0.93, 0.88, 0.72]
carur_hrd = [0.79, 0.58, 0.45, 0.33, 0.28, 0.27, 0.28, 0.32,
0.48, 0.79, 1.04, 1.29, 1.60, 1.77, 1.85, 1.87,
1.86, 1.76, 1.38, 1.18, 1.10, 0.99, 0.93, 0.81]
hdvhw_hrd = [0.44, 0.38, 0.36, 0.38, 0.42, 0.67, 0.99, 1.19,
1.51, 1.69, 1.77, 1.68, 1.63, 1.53, 1.39, 1.34,
1.33, 1.29, 1.12, 0.86, 0.69, 0.55, 0.44, 0.35]
hdvmr_hrd = [0.49, 0.33, 0.35, 0.23, 0.36, 0.56, 0.93, 1.21,
1.31, 1.47, 1.58, 1.76, 1.63, 1.52, 1.52, 1.28,
1.24, 1.09, 0.89, 0.67, 0.61, 0.58, 0.55, 0.52]
hdvur_hrd = [0.47, 0.41, 0.33, 0.33, 0.34, 0.42, 0.61, 0.85,
1.04, 1.27, 1.43, 1.55, 1.72, 1.72, 1.69, 1.69,
1.66, 1.56, 1.34, 1.03, 0.83, 0.68, 0.58, 0.46]
shipping_monthly = np.array([0.88, 0.92, 0.98, 1.03, 1.05, 1.06, 1.01, 1.02, 1.06, 1.05, 1.01, 0.93]) # TNO
shipping_weekly = 1
shipping_daily = 1
t_carhw = np.empty((nt, nlat, nlon))
t_carmr = np.empty((nt, nlat, nlon))
t_carur = np.empty((nt, nlat, nlon))
t_hdvhw = np.empty((nt, nlat, nlon))
t_hdvmr = np.empty((nt, nlat, nlon))
t_hdvur = np.empty((nt, nlat, nlon))
t_ship = np.empty((nt, nlat, nlon))
t_public_power_coal = np.zeros((len(times_add1), *self.area.shape))
t_public_power_gas = np.zeros_like(t_public_power_coal)
t_industry = np.zeros_like(t_public_power_coal)
t_other_stat_cons = np.zeros_like(t_public_power_coal)
t_other_stat_gls = np.zeros_like(t_public_power_coal)
t_road = np.zeros_like(t_public_power_coal)
t_ship = np.zeros_like(t_public_power_coal)
for i, t in enumerate(times_add1):
month = t.month -1 # Make index
day = (t.day -1) % 7 # Make weekly index
day_ind = t.timetuple().tm_yday - day_start
hour = t.hour # Hours start at 0
weekday = t.weekday() < 6
saturday = t.weekday() == 6
sunday = t.weekday() == 7
self.t = t
# Monthly time profiles
public_power_month_mul = public_power_monthly[month, :, :]
industry_month_mul = industry_monthly[month, :, :]
other_stationary_month_mul = other_stationary_monthly[month, :, :]
road_transport_month_mul = road_transport_monthly[month, :, :]
shipping_month_mul = shipping_monthly[month]
# Weekly ('daily') profiles:
public_power_day_mul_coal = t_coald[day_ind, :, :]
public_power_day_mul_gas = t_gasd[day_ind, :, :]
industry_day_mul = industry_weekly[day]
other_stat_day_mul_cons = t_consd[day_ind, :, :] # Index should start at startday, check!
other_stat_day_mul_gls = t_glsd[day_ind, :, :]
road_transport_day_mul = road_transport_weekly[day, :, :]
shipping_day_mul = shipping_weekly
# Hourly profiles:
public_power_hour_mul = public_power_hourly[hour, :, :]
industry_hour_mul = industry_hourly
other_stationary_hour_mul = other_stationary_hourly[hour, :, :]
if weekday: road_hour_mul = road_transport_hourly_wkd[hour, :, :]
elif saturday: road_hour_mul = road_transport_hourly_sat[hour, :, :]
elif sunday: road_hour_mul = road_transport_hourly_sun[hour, :, :]
shipping_hour_mul = shipping_daily
public_power_coal_tprof = public_power_month_mul * public_power_day_mul_coal * public_power_hour_mul
public_power_gas_tprof = public_power_month_mul * public_power_day_mul_gas * public_power_hour_mul
industry_tprof = industry_month_mul * industry_day_mul * industry_hour_mul
other_stat_cons_tprof = other_stationary_month_mul * other_stat_day_mul_cons * other_stationary_hour_mul
other_stat_gls_tprof = other_stationary_month_mul * other_stat_day_mul_gls * other_stationary_hour_mul
road_tprof = road_transport_month_mul * road_transport_day_mul * road_hour_mul
shipping_tprof = shipping_month_mul * shipping_day_mul * shipping_hour_mul
t_public_power_coal[i, :, :] = public_power_coal_tprof
t_public_power_gas[i, :, :] = public_power_gas_tprof
t_industry[i, :, :] = industry_tprof
t_other_stat_cons[i, :, :] = other_stat_cons_tprof
t_other_stat_gls[i, :, :] = other_stat_gls_tprof
t_road[i, :, :] = road_tprof
t_ship[i, :, :] = shipping_tprof
time_profiles = {
't_gas': t_public_power_gas,
't_coal': t_public_power_coal,
't_ind': t_industry,
't_cons': t_other_stat_cons,
't_gls': t_other_stat_gls,
't_road': t_road,
't_ship': t_ship
}
# Roll the time profiles to be consisten with time zones
with nc.Dataset(self.country_mask_file) as ds:
masks = ds['country_mask'][:]
country_names = [b''.join(c).decode() for c in ds['country_names'][:]]
for sect, profile in time_profiles.items():
new_profile = np.zeros((len(times), *profile.shape[1:]))
for country in self.countries:
mask = masks[country_names.index(country)]
country_times = mask * profile
timezone = pytz.timezone(pytz.country_timezones[self.countries_dict[country]][0])
offset = timezone.utcoffset(times[0]).seconds//3600
rolled = np.roll(country_times, -offset, 0)[:len(times)]
new_profile += rolled
new_profile[new_profile == 0] = 1
time_profiles[sect] = new_profile.astype(np.float32)
# Multiply the hourly, daily and monthly time profiles to create new profile
# Only for the times in which we are interested
enum_times = list(enumerate(self.times))[indices]
for i, t in enum_times:
month_idx = t.month - 1
day_idx = t.weekday()
hour_idx = t.hour
if day_idx < 5: # weekdays
t_carhw[i, None, None] = carhw_mnt[month_idx] * carhw_wk[day_idx] * carhw_hrw[hour_idx]
t_carmr[i, None, None] = carmr_mnt[month_idx] * carmr_wk[day_idx] * carmr_hrw[hour_idx]
t_carur[i, None, None] = carur_mnt[month_idx] * carur_wk[day_idx] * carur_hrw[hour_idx]
t_hdvhw[i, None, None] = hdvhw_mnt[month_idx] * hdvhw_wk[day_idx] * hdvhw_hrw[hour_idx]
t_hdvmr[i, None, None] = hdvmr_mnt[month_idx] * hdvmr_wk[day_idx] * hdvmr_hrw[hour_idx]
t_hdvur[i, None, None] = hdvur_mnt[month_idx] * hdvur_wk[day_idx] * hdvur_hrw[hour_idx]
t_ship [i, None, None] = hdvhw_mnt[month_idx] * hdvhw_wk[day_idx] * hdvhw_hrw[hour_idx]
else: # weekends
t_carhw[i, None, None] = carhw_mnt[month_idx] * carhw_wk[day_idx] * carhw_hrd[hour_idx]
t_carmr[i, None, None] = carmr_mnt[month_idx] * carmr_wk[day_idx] * carmr_hrd[hour_idx]
t_carur[i, None, None] = carur_mnt[month_idx] * carur_wk[day_idx] * carur_hrd[hour_idx]
t_hdvhw[i, None, None] = hdvhw_mnt[month_idx] * hdvhw_wk[day_idx] * hdvhw_hrd[hour_idx]
t_hdvmr[i, None, None] = hdvmr_mnt[month_idx] * hdvmr_wk[day_idx] * hdvmr_hrd[hour_idx]
t_hdvur[i, None, None] = hdvur_mnt[month_idx] * hdvur_wk[day_idx] * hdvur_hrd[hour_idx]
t_ship [i, None, None] = hdvhw_mnt[month_idx] * hdvhw_wk[day_idx] * hdvhw_hrd[hour_idx]
# --- normalize time profiles
t_gas /= nt
t_coal /= nt
t_cons /= nt
t_gls /= nt
t_carhw /= nt
t_carmr /= nt
t_carur /= nt
t_hdvhw /= nt
t_hdvmr /= nt
t_hdvur /= nt
t_ship /= nt
datepoint = self.startdate
enddate = self.enddate
t_ind = np.ones((indices.stop, nlat, nlon), dtype=np.float32)
# Make container (dict) containing all time profiles
time_profiles = {'t_gas': t_gas[indices].astype(np.float32),
't_coal': t_coal[indices].astype(np.float32),
't_ind': t_ind[indices].astype(np.float32),
't_cons': t_cons[indices].astype(np.float32),
't_gls': t_gls[indices].astype(np.float32),
't_carhw': t_carhw[indices].astype(np.float32),
't_carmr': t_carmr[indices].astype(np.float32),
't_carur': t_carur[indices].astype(np.float32),
't_hdvhw': t_hdvhw[indices].astype(np.float32),
't_hdvmr': t_hdvmr[indices].astype(np.float32),
't_hdvur': t_hdvur[indices].astype(np.float32),
't_ship': t_ship[indices].astype(np.float32)}
logging.debug('Time profiles created')
return time_profiles
################### End Class Emission model ###################
......
......@@ -30,8 +30,6 @@ from numpy import array, logical_and
import glob
import subprocess