Skip to content

Commit

Permalink
Merge pull request #152 from nismod/prepare_cardif_runs
Browse files Browse the repository at this point in the history
Prepare cardif runs
  • Loading branch information
eggimasv committed Jan 26, 2019
2 parents 0a0c938 + 7de0402 commit ee55b84
Show file tree
Hide file tree
Showing 19 changed files with 332 additions and 340 deletions.
42 changes: 8 additions & 34 deletions energy_demand/basic/basic_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import numpy as np
from pyproj import Proj, transform

def get_result_paths(scenario_run_results):
"""Create a dict with sub_folder path for user defined
results generation
def get_result_paths(folder_path, container=1):
"""Joins results subfolders to ``folder_path`` and returns a dict
Arguments
---------
scenario_run_results : str
folder_path : str
Path of scenario run results
Returns
Expand All @@ -20,39 +19,14 @@ def get_result_paths(scenario_run_results):
Dict with all sub_folder paths
"""
path_dict = {
'data_results': scenario_run_results,
'data_results_model_run_pop': os.path.join(scenario_run_results, 'model_run_pop'),
'data_results_validation': os.path.join(scenario_run_results, 'PDF_validation'),
'model_run_pop': os.path.join(scenario_run_results, 'model_run_pop'),
#'data_results_model_runs': os.path.join(scenario_run_results, 'model_run_results_txt'),
#'data_results_PDF': os.path.join(scenario_run_results, 'PDF_results'),
#'data_results_shapefiles': os.path.join(scenario_run_results, 'spatial_results'),
#'individual_enduse_lp': os.path.join(scenario_run_results, 'individual_enduse_lp'),
}
'data_results': folder_path,
'data_results_model_run_pop': os.path.join(folder_path, 'model_run_pop'),
'data_results_validation': os.path.join(folder_path, 'PDF_validation'),
'data_results_model_run_results_txt': os.path.join(folder_path, 'model_run_results_txt'),
'data_results_PDF': os.path.join(folder_path, 'PDF_results')}

return path_dict

def get_weather_result_paths(weather_path):
"""Joins results subfolders to ``weather_path`` and returns a dict
Arguments
---------
weather_path : str
Path to the weather results simulations
Returns
-------
dict
"""
paths = {
'data_results':weather_path,
'data_results_PDF': os.path.join(weather_path, 'PDF_results'),
'data_results_validation': os.path.join(weather_path, 'PDF_validation'),
'data_results_model_runs': os.path.join(weather_path, 'model_run_results_txt'),
}

return paths

def dict_depth(dictionary):
"""Get depth of nested dict
"""
Expand Down
84 changes: 6 additions & 78 deletions energy_demand/enduse_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def __init__(
scenario_data,
assumptions,
load_profiles,
f_weather_correction,
base_yr,
curr_yr,
enduse,
Expand Down Expand Up @@ -140,8 +139,7 @@ def __init__(
cooling_factor_y,
heating_factor_y,
assumptions.enduse_space_heating,
assumptions.ss_enduse_space_cooling,
f_weather_correction)
assumptions.ss_enduse_space_cooling)
self.fuel_y = _fuel_new_y
#print("FUEL TRAIN B0: " + str(np.sum(self.fuel_y)))

Expand Down Expand Up @@ -397,10 +395,6 @@ def load_shifting_multiple_tech(
mode_constrained=True,
param_lf_improved_cy=param_lf_improved_cy,
make_all_flat=False)
#plt.plot(sum_all_techs.reshape(8760), label="test taase")
#plt.plot(sum_all_techs_shifted.reshape(8760), label="test after")
#plt.legend()
#plt.show()

# Shift demand of peak (removed demand)
removed_demand = sum_all_techs - sum_all_techs_shifted
Expand All @@ -424,87 +418,34 @@ def load_shifting_multiple_tech(

for tech in technologies_fueltype:

#fuel_tech_8760 = fuel_yh[tech].reshape(8760)
#TODO Probably does not need to be copied but just assigned
fuel_tech_8760 = copy.copy(fuel_yh[tech].reshape(8760))
sum_all_techs_8760 = sum_all_techs.reshape(8760)
#print("-------------FUEL {} {}".format(np.sum(fuel_tech_8760), tech))
#print("shape_added: " + str(shape_added[:24]))
#print("ff " + str(np.sum(shape_added)))
#print("param_lf_improved_cy: " + str(param_lf_improved_cy))
#print("sum_all_techs_8760 {} {}".format(np.sum(sum_all_techs_8760), tech))

# Calculate share of technology specific demand to overall enduse demand per hour before shift
h_contribution_before = (1 / sum_all_techs_8760) * fuel_tech_8760
h_contribution_before[np.isnan(h_contribution_before)] = 0
#print("0 A " + str(np.sum(fuel_yh[tech])))
#print(np.sum(fuel_yh[tech]))
#print("A " + str(h_contribution_before))

# Get absolute demand in peak hours
tech_peak_shifted_demand = 0
for peak_hour_pos in peak_hour_positions:
tech_peak_shifted_demand += h_contribution_before[peak_hour_pos] * removed_demand[peak_hour_pos]

#print("peak_hour_positions: " + str(len(peak_hour_positions)))
#print("tech_peak_shifted_demand: " + str(tech_peak_shifted_demand))
#print("tot_removed_demand: " + str(tot_removed_demand))
# Percentage of total shifted demand of technology
p_abs_tech_shifted = (1.0 / tot_removed_demand) * tech_peak_shifted_demand

if np.isnan(p_abs_tech_shifted):
p_abs_tech_shifted = 0
#print("p_abs_tech_shifted: " + str(p_abs_tech_shifted))
#print(np.sum(fuel_yh[tech]))
#print(np.sum(fuel_tech_8760))
#print("--------")
#print(tot_removed_demand)
#print(p_abs_tech_shifted)
#print(p_abs_tech_shifted * tot_removed_demand)
#print(p_abs_tech_shifted * tot_removed_demand * shape_added)
#_a = p_abs_tech_shifted * tot_removed_demand * shape_added
#print("TEST davor" + str(np.sum(_a)))
#print(np.sum(fuel_tech_8760))

# Add shifted demand to total demand
fuel_tech_8760 += (p_abs_tech_shifted * tot_removed_demand) * shape_added
#plt.plot(fuel_tech_8760, label="intermediar")
#print("ZZZ")
#_a = shape_removed * tot_removed_demand
#print(shape_removed[:24])
#print(_a[:24])
##print(np.sum(fuel_tech_8760))

# Substract peak from total demand
fuel_tech_8760 -= (p_abs_tech_shifted * tot_removed_demand) * shape_removed

#print("KROKODIL")
#_max_before = np.max(fuel_yh[tech].reshape(8760))
#_max_after = np.max(fuel_tech_8760)
#print(_max_before)
#print(_max_after)
#print("PERCENT NEW " + str(((1/_max_before) * _max_after)))
#print(np.sum(fuel_tech_8760))
#print(np.sum(fuel_yh[tech]))
#plt.plot(fuel_yh[tech].reshape(8760), linewidth=4, label="before taase {}".format(tech))
#plt.plot(fuel_tech_8760, label="after")
#plt.legend()
#plt.show()
##print(np.sum(fuel_tech_8760))
#print(np.sum(fuel_yh[tech]))
assert round(np.sum(fuel_tech_8760), 6) == round(np.sum(fuel_yh[tech]), 6)

fuel_tech_shifted[tech] = fuel_tech_8760.reshape(365, 24)

'''if enduse == 'rs_space_heating':
plt.plot(sum_all_techs.reshape(8760), label="COMBINED BEFORE SHFITING")

# Sum across al technologies
sum_all_techs = np.zeros((365, 24))
for tech in technologies_fueltype:
sum_all_techs += fuel_tech_shifted[tech]
plt.plot(sum_all_techs.reshape(8760), label="COMBINED AFTER SHFITING")
#plt.plot(sum_all_techs_shifted.reshape(8760), label="test after")
plt.legend()
plt.show()'''
return fuel_tech_shifted

def load_shifting(
Expand Down Expand Up @@ -1420,8 +1361,7 @@ def apply_weather_correction(
cooling_factor_y,
heating_factor_y,
enduse_space_heating,
enduse_space_cooling,
f_weather_correction
enduse_space_cooling
):
"""Change fuel demand for heat and cooling service
depending on changes in HDD and CDD within a region
Expand All @@ -1442,10 +1382,6 @@ def apply_weather_correction(
Enduses defined as space heating
enduse_space_cooling : list
Enduses defined as space cooling
f_weather_correction : dict
Correction factors to calculate hdd and cdd
related extra demands in case a different
weather station is used
Return
------
Expand All @@ -1459,16 +1395,8 @@ def apply_weather_correction(
directly with HDD or CDD.
"""
if enduse in enduse_space_heating:

# Apply correction factor for different weather year
# which has different wheather stations and associated temperatures
fuel_y = fuel_y * f_weather_correction['hdd']

# Apply correction factor for change in climate over simulation period
fuel_y = fuel_y * heating_factor_y

elif enduse in enduse_space_cooling:
fuel_y = fuel_y * f_weather_correction['cdd']
fuel_y = fuel_y * cooling_factor_y

return fuel_y
Expand Down
54 changes: 2 additions & 52 deletions energy_demand/geography/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ class Region(object):
Note
----
* The closest weather station is calculated
The closest weather station is calculated
"""
def __init__(
self,
name,
longitude,
latitude,
region_fuel_disagg,
weather_reg_cy,
weather_reg_by
region_fuel_disagg
):
"""Constructor
"""
Expand All @@ -48,54 +46,6 @@ def __init__(
self.latitude = latitude
self.fuels = region_fuel_disagg

# =================
# Calculate Weather Correction factor in relation to base year
#
# Because the number of HDD and CDD may be different for different
# weather years, a linear relationship between energy demand
# related to heating and cooling is assumed and a weather
# correction factor is calculated based on HDD and CDD calculations
# for the weather base year and the actual weather data depending
# on the closest weather region
# =================

# Climate correction factor (hdd)

# Residential
factor = np.sum(weather_reg_cy.rs_hdd_by) / np.sum(weather_reg_by.rs_hdd_by)
if np.isnan(factor):
f_climate_hdd_rs = 1
else:
f_climate_hdd_rs = factor

# Service
factor = np.sum(weather_reg_cy.ss_hdd_by) / np.sum(weather_reg_by.ss_hdd_by)
if np.isnan(factor):
f_climate_hdd_ss = 1
else:
f_climate_hdd_ss = factor

# Industry
factor = np.sum(weather_reg_cy.is_hdd_by) / np.sum(weather_reg_by.is_hdd_by)

if np.isnan(factor):
f_climate_hdd_is = 1
else:
f_climate_hdd_is = factor

# Climate correction factor (cdd)
factor = np.sum(weather_reg_cy.ss_cdd_by) / np.sum(weather_reg_by.ss_cdd_by)
#f_climate_cdd_is = np.sum(weather_reg_cy.is_cdd_by) / np.sum(weather_reg_by.is_cdd_by)
if np.isnan(factor):
f_climate_cdd_ss = 1
else:
f_climate_cdd_ss = factor

self.f_weather_correction = {
'residential': {'hdd': f_climate_hdd_rs, 'cdd': None},
'service': {'hdd': f_climate_hdd_ss, 'cdd': f_climate_cdd_ss},
'industry': {'hdd': f_climate_hdd_is, 'cdd': None}}

# Visual testing purposes
#from energy_demand.read_write import data_loader
#data_loader.print_closest_and_region(
Expand Down

0 comments on commit ee55b84

Please sign in to comment.