Skip to content

Commit

Permalink
finished refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann committed Dec 18, 2017
1 parent 2552b6c commit 65e6f1d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 47 deletions.
10 changes: 3 additions & 7 deletions energy_demand/enduse_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"""
import logging
import numpy as np

from energy_demand.initalisations import helpers
from energy_demand.profiles import load_profile as lp
from energy_demand.profiles import load_factors as lf
Expand Down Expand Up @@ -56,12 +55,8 @@ class Enduse(object):
Distribution of fuel within year to days (yd) (directly correlates with HDD)
cooling_factor_y : array
Distribution of fuel within year to days (yd) (directly correlates with CDD)
fuel_switches : list
Fuel switches
service_switches : list
Service switches
capacity_switches : list
Capacity switches
fuel_tech_p_by : dict
Fuel tech assumtions in base year
tech_increased_service : dict
Expand All @@ -70,8 +65,6 @@ class Enduse(object):
Technologies per enduse with decreased service due to scenarios
tech_constant_share : dict
Technologies per enduse with constat service
installed_tech : dict
Installed technologes for this enduse
sig_param_tech : dict
Sigmoid parameters
enduse_overall_change : dict
Expand Down Expand Up @@ -417,6 +410,9 @@ def assign_lp_no_techs(enduse, sector, load_profiles, fuel_new_y):
Returns
-------
fuel_yh
fuel_peak_dh
fuel_peak_h
"""
_fuel = fuel_new_y[:, np.newaxis, np.newaxis]

Expand Down
2 changes: 1 addition & 1 deletion energy_demand/geography/spatial_diffusion.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This file calculates spatial diffusion index
"""
import numpy as np
from collections import defaultdict
import numpy as np

def load_spatial_diff_values(regions, enduses):
"""Load or calculate spatial diffusion values
Expand Down
2 changes: 1 addition & 1 deletion energy_demand/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def energy_demand_model(data, fuel_in=0, fuel_in_elec=0):
data['criterias']['mode_constrained'] = False
data['criterias']['plot_HDD_chart'] = False
data['criterias']['virtual_building_stock_criteria'] = virtual_building_stock_criteria
data['criterias']['spatial_exliclit_diffusion'] = False
data['criterias']['spatial_exliclit_diffusion'] = True

data['paths'] = data_loader.load_paths(path_main)
data['local_paths'] = data_loader.load_local_paths(local_data_path)
Expand Down
41 changes: 15 additions & 26 deletions energy_demand/scripts/init_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
import os
import logging
from collections import defaultdict
import numpy as np
from energy_demand.read_write import data_loader
from energy_demand.scripts import s_fuel_to_service
Expand Down Expand Up @@ -32,6 +31,7 @@ def scenario_initalisation(path_data_ed, data=False):
sgs_cont = {}
fts_cont = {}
sd_cont = {}
switches_cont = {}

logger_setup.set_up_logger(os.path.join(path_data_ed, "scenario_init.log"))

Expand All @@ -43,7 +43,6 @@ def scenario_initalisation(path_data_ed, data=False):
data['local_paths']['data_processed'],
data['local_paths']['path_post_installation_data'])
basic_functions.del_previous_setup(data['local_paths']['data_results'])

basic_functions.create_folder(data['local_paths']['data_results'])
basic_functions.create_folder(data['local_paths']['dir_services'])
basic_functions.create_folder(data['local_paths']['path_sigmoid_data'])
Expand Down Expand Up @@ -88,35 +87,34 @@ def scenario_initalisation(path_data_ed, data=False):
data['assumptions']['technologies'])

# Service
ss_fuels_aggregated_across_sectors = s_fuel_to_service.sum_fuel_enduse_sectors(
ss_aggr_sector_fuels = s_fuel_to_service.sum_fuel_enduse_sectors(
data['fuels']['ss_fuel_raw_data_enduses'],
data['enduses']['ss_all_enduses'],
data['lookups']['fueltypes_nr'])
fts_cont['ss_service_tech_by_p'], fts_cont['ss_service_fueltype_tech_by_p'], fts_cont['ss_service_fueltype_by_p'] = s_fuel_to_service.get_service_fueltype_tech(
data['assumptions']['tech_list'],
data['lookups']['fueltype'],
data['assumptions']['ss_fuel_tech_p_by'],
ss_fuels_aggregated_across_sectors,
ss_aggr_sector_fuels,
data['assumptions']['technologies'])

# Industry
is_fuels_aggregated_across_sectors = s_fuel_to_service.sum_fuel_enduse_sectors(
is_aggr_sector_fuels = s_fuel_to_service.sum_fuel_enduse_sectors(
data['fuels']['is_fuel_raw_data_enduses'],
data['enduses']['is_all_enduses'],
data['lookups']['fueltypes_nr'])
fts_cont['is_service_tech_by_p'], fts_cont['is_service_fueltype_tech_by_p'], fts_cont['is_service_fueltype_by_p'] = s_fuel_to_service.get_service_fueltype_tech(
data['assumptions']['tech_list'],
data['lookups']['fueltype'],
data['assumptions']['is_fuel_tech_p_by'],
is_fuels_aggregated_across_sectors,
is_aggr_sector_fuels,
data['assumptions']['technologies'])

# ------------------------------------
# Autocomplement defined service switches
# with technologies not explicityl specified in switch
# on a national scale
# ------------------------------------
switches_cont = {}
switches_cont['rs_service_switches'] = fuel_service_switch.autocomplete_switches(
data['assumptions']['rs_service_switches'],
data['assumptions']['rs_specified_tech_enduse_by'],
Expand Down Expand Up @@ -149,7 +147,7 @@ def scenario_initalisation(path_data_ed, data=False):
data['assumptions']['capacity_switches']['ss_capacity_switches'],
data['assumptions']['technologies'],
data['assumptions']['enduse_overall_change']['other_enduse_mode_info'],
ss_fuels_aggregated_across_sectors,
ss_aggr_sector_fuels,
data['assumptions']['ss_fuel_tech_p_by'],
data['sim_param']['base_yr'])

Expand All @@ -158,7 +156,7 @@ def scenario_initalisation(path_data_ed, data=False):
data['assumptions']['capacity_switches']['is_capacity_switches'],
data['assumptions']['technologies'],
data['assumptions']['enduse_overall_change']['other_enduse_mode_info'],
is_fuels_aggregated_across_sectors,
is_aggr_sector_fuels,
data['assumptions']['is_fuel_tech_p_by'],
data['sim_param']['base_yr'])

Expand All @@ -180,23 +178,14 @@ def scenario_initalisation(path_data_ed, data=False):
# Calculate sigmoid diffusion parameters
# (either for every region or aggregated for all regions)
# -------------------------------
sgs_cont['rs_sig_param_tech'] = {}
sgs_cont['rs_tech_increased_service'] = {}
sgs_cont['rs_tech_decreased_share'] = {}
sgs_cont['rs_tech_constant_share'] = {}
sgs_cont['rs_service_switch'] = {}

sgs_cont['ss_sig_param_tech'] = {}
sgs_cont['ss_tech_increased_service'] = {}
sgs_cont['ss_tech_decreased_share'] = {}
sgs_cont['ss_tech_constant_share'] = {}
sgs_cont['ss_service_switch'] = {}

sgs_cont['is_sig_param_tech'] = {}
sgs_cont['is_tech_increased_service'] = {}
sgs_cont['is_tech_decreased_share'] = {}
sgs_cont['is_tech_constant_share'] = {}
sgs_cont['is_service_switch'] = {}
key_to_init = [
'rs_sig_param_tech', 'rs_tech_increased_service', 'rs_tech_decreased_share', 'rs_tech_constant_share',
'rs_service_switch', 'ss_sig_param_tech', 'ss_tech_increased_service', 'ss_tech_decreased_share',
'ss_tech_constant_share', 'ss_service_switch', 'is_sig_param_tech', 'is_tech_increased_service',
'is_tech_decreased_share', 'is_tech_constant_share', 'is_service_switch']

for key_name in key_to_init:
sgs_cont[key_name] = {}

if data['criterias']['spatial_exliclit_diffusion']:

Expand Down
20 changes: 17 additions & 3 deletions energy_demand/scripts/s_fuel_to_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,30 @@ def sum_2_level_dict(two_level_dict):

return tot_sum

def sum_fuel_enduse_sectors(ss_fuel_raw_data_enduses, ss_enduses, nr_fueltypes):
def sum_fuel_enduse_sectors(data_enduses, enduses, nr_fueltypes):
"""Aggregated fuel for all sectors according to enduse
Arguments
--------
data_enduses : dict
Fuel per enduse
enduses : list
Enduses
nr_fueltypes : int
Number of fuetlypes
Returns
-------
aggregated_fuel_enduse : dict
Arregated fuel per enduse ({enduse: np.array(fueltype)})
"""
aggregated_fuel_enduse = {}

for enduse in ss_enduses:
for enduse in enduses:
aggregated_fuel_enduse[str(enduse)] = np.zeros((nr_fueltypes), dtype=float)

# Iterate and sum fuel per enduse
for fuels_sector in ss_fuel_raw_data_enduses.values():
for fuels_sector in data_enduses.values():
for enduse, fuels_enduse in fuels_sector.items():
aggregated_fuel_enduse[enduse] += fuels_enduse

Expand Down
16 changes: 9 additions & 7 deletions energy_demand/scripts/s_generate_sigmoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ def calc_service_fuel_switched(
):
"""Calculate energy service demand percentages after fuel switches
TODO CLEAN AN IMPROVE
Arguments
----------
enduse_fuel_switches : dict
Expand Down Expand Up @@ -330,8 +333,7 @@ def calc_service_fuel_switched(

# Substract service demand for replaced technologies
for tech in replaced_tech_fueltype:
fueltype_of_tech_replacing = technologies[tech].fuel_type_int
service_tech_switched_p[tech] = service_tech_by_p[tech] - (change_service_fueltype_by_p * fuel_tech_p_by[fueltype_of_tech_replacing][tech])
service_tech_switched_p[tech] = service_tech_by_p[tech] - (change_service_fueltype_by_p * fuel_tech_p_by[fueltype_tech_replace][tech])

# -----------------------
# Calculate service fraction of all technologies in enduse not affected
Expand All @@ -342,19 +344,19 @@ def calc_service_fuel_switched(
remaining_service_to_distr_p = 1 - affected_service_p_ey

# Calculate service fraction of remaining technologies
all_fractions_not_affected_by_switch = {}
all_fractions_unaffected_switch = {}
for tech in service_tech_by_p:
if tech not in service_tech_switched_p.keys():
all_fractions_not_affected_by_switch[tech] = service_tech_by_p[tech]
all_fractions_unaffected_switch[tech] = service_tech_by_p[tech]

# Iterate all technologies of enduse_by
service_tot_remaining = sum(all_fractions_not_affected_by_switch.values())
service_tot_remaining = sum(all_fractions_unaffected_switch.values())

# Get relative distribution of all not affected techs
for tech in all_fractions_not_affected_by_switch:
for tech in all_fractions_unaffected_switch:

# Relative share
rel_service_fraction_p = all_fractions_not_affected_by_switch[tech] / service_tot_remaining
rel_service_fraction_p = all_fractions_unaffected_switch[tech] / service_tot_remaining
service_tech_switched_p[tech] = rel_service_fraction_p * remaining_service_to_distr_p

return dict(service_tech_switched_p)
Expand Down
3 changes: 1 addition & 2 deletions energy_demand/technologies/fuel_service_switch.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Function related to service or fuel switch
"""
from collections import defaultdict
from energy_demand.technologies import tech_related
from energy_demand.read_write import read_data
from collections import defaultdict

def get_share_service_tech_ey(service_switches, specified_tech_enduse_by):
"""Get fraction of service for each technology
Expand Down Expand Up @@ -372,7 +372,6 @@ def capacity_assumption_to_service(
# Add to switch of technology_install
# -------------------------------------------
service_switches_enduse = []

for tech, service_tech_p in service_enduse_tech.items():

# WARNING: MUST BE THE SAME YEAR FOR ALL CAPACITY SWITCHES
Expand Down

0 comments on commit 65e6f1d

Please sign in to comment.