Skip to content

Commit

Permalink
before fixing file to write out YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann authored and Sven Eggimann committed May 16, 2018
1 parent 03deb19 commit 1773650
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 161 deletions.
161 changes: 161 additions & 0 deletions energy_demand/config_data/population_from_andrew.py
@@ -0,0 +1,161 @@
"""Download population projections from https://github.com/nismod/population/blob/master/README.md
Info
-----
https://github.com/virgesmith/UKCensusAPI
https://www.nomisweb.co.uk/myaccount/webservice.asp
https://github.com/nismod/population
https://github.com/virgesmith/UKCensusAPI
Steps
------
1. optain nomis key
2. in cmd line: set NOMIS_API_KEY=XXX
3. ./setup.py install
4. run python script from command line
https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/populationprojections/bulletins/nationalpopulationprojections/2015-10-29
Potential variants
-------------------
hhh: High population,
hpp: High fertility,
lll: Low population,
lpp: Low fertility,
php: High life expectancy,
pjp: Moderately high life expectancy,
pkp: Moderately low life expectancy,
plp: Low life expectancy,
pph: High migration,
ppl: Low migration,
ppp: Principal,
ppq: 0% future EU migration (non-ONS),
ppr: 50% future EU migration (non-ONS),
pps: 150% future EU migration (non-ONS),
ppz: Zero net migration
Select for paper I
-------------------
Prinicpal projection ppp
Low mnigration ppl
High migration pph
"""
import pandas as pd
import population.nppdata as NPPData
import population.snppdata as SNPPData
import population.utils as utils

reg_pop = True
extrapolate_pop = False
extrapolate_specific_scenario = True

if extrapolate_specific_scenario:
'''
Get extrapolated data for full time range for different ONS scenarios from 2015 - 2050
# https://github.com/nismod/population/blob/master/doc/example_variant_ex.py
'''

# initialise the population modules
npp = NPPData.NPPData()
snpp = SNPPData.SNPPData()

# 50 years, roughly half is extrapolated
years = range(2015, 2051)

# start with an empty data frame
result_ppp = pd.DataFrame()
result_ppl = pd.DataFrame()
result_pph = pd.DataFrame()

# loop over all the UK LAD (or LAD-equivalents)
for lad in snpp.data.GEOGRAPHY_CODE.unique():
print("Collection population for region {}".format(lad))

# calculate the the variants
region_ppp = snpp.create_variant("ppp", npp, lad, years)
region_ppl = snpp.create_variant("ppl", npp, lad, years)
region_pph = snpp.create_variant("pph", npp, lad, years)

# aggregate the calculated variants by age and gender
region_ppp = utils.aggregate(region_ppp, ["GENDER", "C_AGE"])
region_ppl = utils.aggregate(region_ppl, ["GENDER", "C_AGE"])
region_pph = utils.aggregate(region_pph, ["GENDER", "C_AGE"])

result_ppp.append(region_ppp, ignore_index=True)
result_ppl.append(region_ppl, ignore_index=True)
result_pph.append(region_pph, ignore_index=True)

break
print("tt")

# write out results
result_ppp.to_csv("C:/Users/cenv0553/mistral_population/__RESULTS/prinicpal_extrap_2050.csv", index=False)
result_ppl.to_csv("C:/Users/cenv0553/mistral_population/__RESULTS/lowmigration_extrap_2050.csv", index=False)
result_pph.to_csv("C:/Users/cenv0553/mistral_population/__RESULTS/highmigration_extrap_2050.csv", index=False)
print("FINISHED")
'''
if extrapolate_pop:
# ===================
# Extrapolating SNPP for 2040 - 2050
#https://github.com/nismod/population
# ===================
# initialise the population modules
npp = NPPData.NPPData()
snpp = SNPPData.SNPPData()
# get the first year where extrapolation is necessary
ex_start = snpp.max_year() + 1
# we extrapolate to 2050
ex_end = 2050
# start with an empty data frame
result = pd.DataFrame()
# loop over all the UK LAD (or LAD-equivalents)
for lad in snpp.data.GEOGRAPHY_CODE.unique():
# extrapolate
lad_ex = snpp.extrapolagg("GEOGRAPHY_CODE", npp, lad, range(ex_start, ex_end + 1))
# append to data
result = result.append(lad_ex, ignore_index=True)
# write out results
result.to_csv("C:/Users/cenv0553/mistral_population/population/__RESULTS/snpp_extrap_2050.csv")
if reg_pop:
# initialise the population modules
npp = NPPData.NPPData()
snpp = SNPPData.SNPPData()
# get the first year where extrapolation is necessary
ex_start = snpp.max_year() + 1
# we extrapolate to 2050
ex_end = 2050
# start with an empty data frame
result = pd.DataFrame()
# loop over all the UK LAD (or LAD-equivalents)
for lad in snpp.data.GEOGRAPHY_CODE.unique():
# get the total projected population for newcastle up to the SNPP horizon (2039)
lad_non_ex = snpp.aggregate("GEOGRAPHY_CODE", lad, range(2015, ex_start))
# extrapolate for another 25 years
lad_ex = snpp.extrapolagg("GEOGRAPHY_CODE", npp, lad, range(ex_end))
# append to data
result = result.append(lad_non_ex, ignore_index=True)
result = result.append(lad_ex, ignore_index=True)
# write out results
result.to_csv("C:/Users/cenv0553/mistral_population/population/__RESULTS/snpp_extrap_2015_2050_ppp.csv")
'''
42 changes: 35 additions & 7 deletions energy_demand/plotting/plotting_multiple_scenarios.py
Expand Up @@ -637,7 +637,6 @@ def plot_tot_fueltype_y_over_time(
# Set figure size
fig = plt.figure(figsize=plotting_program.cm2inch(9, 8))


ax = fig.add_subplot(1, 1, 1)

y_scenario = {}
Expand Down Expand Up @@ -845,9 +844,8 @@ def plot_tot_y_over_time(

if plotshow:
plt.show()
plt.close()
else:
plt.close()

plt.close()

def plot_radar_plots_average_peak_day(
scenario_data,
Expand All @@ -864,6 +862,8 @@ def plot_radar_plots_average_peak_day(
name_spider_plot = os.path.join(
fig_name, "spider_scenarios_{}.pdf".format(fueltype_to_model))

fueltype_int = fueltypes[fueltype_to_model]

# ----------------
# Create base year peak load profile
# Aggregate load profiles of all regions
Expand All @@ -877,11 +877,12 @@ def plot_radar_plots_average_peak_day(
print("-------Scenario: {} {}".format(scenario, fueltype_to_model))
base_yr = 2015

# ------------------------
# Future year load profile
# ------------------------
all_regs_fueltypes_yh_by = np.sum(scenario_data[scenario]['results_every_year'][base_yr], axis=1)
all_regs_fueltypes_yh_cy = np.sum(scenario_data[scenario]['results_every_year'][year_to_plot], axis=1)

fueltype_int = fueltypes[fueltype_to_model]

# ---------------------------
# Calculate load factors
Expand All @@ -897,6 +898,30 @@ def plot_radar_plots_average_peak_day(
scen_load_factor_fueltype_y_cy = load_factors.calc_lf_y(all_regs_fueltypes_yh_cy)
load_factor_fueltype_y_cy.append(round(scen_load_factor_fueltype_y_cy[fueltype_int], fueltype_int))

# ------------------------
# Calculate share or space and water heating of total
# electrictiy demand in 2050
# ------------------------
enduses_to_agg = ['rs_space_heating', 'rs_water_heating', 'ss_space_heating', 'ss_water_heating', 'is_space_heating']

aggregated_enduse_fueltype = np.zeros((365, 24))
aggregated_enduse_fueltype_by = np.zeros((365, 24))
total_demand = np.zeros((365, 24))
for enduse in scenario_data[scenario]['results_enduse_every_year'][2050].keys():
if enduse in enduses_to_agg:
aggregated_enduse_fueltype += scenario_data[scenario]['results_enduse_every_year'][2050][enduse][fueltype_int]
aggregated_enduse_fueltype_by += scenario_data[scenario]['results_enduse_every_year'][2015][enduse][fueltype_int]
total_demand += scenario_data[scenario]['results_enduse_every_year'][2050][enduse][fueltype_int]
else:
total_demand += scenario_data[scenario]['results_enduse_every_year'][2050][enduse][fueltype_int]

# Total demand
selected_enduses_total_peak_day = round(np.max(aggregated_enduse_fueltype[peak_day_nr_cy]), 1)
all_enduses_total_peak_day = round(np.max(total_demand[peak_day_nr_cy]), 1)
selected_enduses_peak_by = round(np.max(aggregated_enduse_fueltype_by), 1)
print("p_all_enduses_total_peak_day " + str(all_enduses_total_peak_day))
print("p_selected_enduses_total_peak_day " + str(selected_enduses_total_peak_day))

# ----------
# Calculate change in peak
# ----------
Expand All @@ -905,11 +930,14 @@ def plot_radar_plots_average_peak_day(

diff_max_h = round(((100 / by_max_h) * cy_max_h) - 100, 1)

label_max_h = " by: {} cy: {} d: {}, scen: {}".format(
label_max_h = " by: {} cy: {} d: {}, scen: {} ed_peakh_heating: {} ed_tot_peah_ {} by_heating: {}".format(
round(by_max_h, 2),
round(cy_max_h, 1),
round(diff_max_h, 1),
scenario,)
scenario,
selected_enduses_total_peak_day,
all_enduses_total_peak_day,
selected_enduses_peak_by)

list_diff_max_h.append(label_max_h)

Expand Down
26 changes: 9 additions & 17 deletions energy_demand/plotting/plotting_results.py
Expand Up @@ -2219,34 +2219,26 @@ def plot_radar_plot_multiple_lines(
color_line,
alpha=0.05)

font_additional_info = plotting_styles.font_info(size=5)
# ------------
# Write outs
# ------------
font_additional_info = plotting_styles.font_info(size=4)

for cnt, entry in enumerate(list_diff_max_h):
plt.text(
0.25,
0.15,
0 + cnt/50,
entry,
fontdict=font_additional_info,
transform=plt.gcf().transFigure)

# ------------
# Title
# ------------
font_additional_info = plotting_styles.font_info(size=4)
#plt.title(
# title_info,
# loc='left',
# fontdict=font_additional_info)

# ------------
# Legend
# ------------
plt.legend(
ncol=1,
bbox_to_anchor=(0.5, -0.1),
prop={
'family': 'arial',
'size': 4},
bbox_to_anchor=(0.2, -0.1),
prop={'size': 4},
frameon=False)

plt.savefig(fig_name)
Expand All @@ -2267,8 +2259,8 @@ def plt_one_fueltype_multiple_regions_peak_h(
Arguments
---------
"""
# Set figure size
fig = plt.figure(figsize=plotting_program.cm2inch(14, 8))
fig = plt.figure(
figsize=plotting_program.cm2inch(14, 8))

ax = fig.add_subplot(1, 1, 1)

Expand Down

0 comments on commit 1773650

Please sign in to comment.