Skip to content

Commit

Permalink
added weather station map
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann committed Oct 10, 2018
1 parent b870527 commit 1a4f686
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 69 deletions.
6 changes: 6 additions & 0 deletions energy_demand/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,12 @@ def energy_demand_model(
print("... Start writing results to file")
path_runs = data['result_paths']['data_results_model_runs']

write_data.write_residential_tot_demands(
sim_yr,
path_runs,
sim_obj.ed_residnetial_tot_reg_yh,
"ed_residnetial_tot_reg_yh")

write_data.write_supply_results(
sim_yr,
"result_tot_yh",
Expand Down
48 changes: 14 additions & 34 deletions energy_demand/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ def __init__(
# ----------------------------
# Create Weather Regions
# ----------------------------
print("KOK")
print(weather_stations)
# current weather_yr
weather_regions_weather_cy = {}
for weather_region in weather_stations[weather_yr]:
Expand Down Expand Up @@ -110,8 +108,8 @@ def __init__(

logging.info("... Simulate: region %s, simulation year: %s, weather_yr: %s, percent: (%s)",
region, assumptions.curr_yr, weather_yr, round((100/data['reg_nrs'])*reg_array_nr, 2))
print("... Simulate: region {}, simulation year: {}, weather_yr: {}, percent: ({})".format(
region, assumptions.curr_yr, weather_yr, round((100/data['reg_nrs'])*reg_array_nr, 2)), flush=True)
#rint("... Simulate: region {}, simulation year: {}, weather_yr: {}, percent: ({})".format(
#2 region, assumptions.curr_yr, weather_yr, round((100/data['reg_nrs'])*reg_array_nr, 2)), flush=True)

all_submodels = simulate_region(
region,
Expand All @@ -133,37 +131,7 @@ def __init__(
assumptions.enduse_space_heating,
data['technologies'])

#logging.info("Size " + str(getsizeof(all_submodels)))
del all_submodels
'''aggr_results = initialise_result_container(
data['lookups']['fueltypes_nr'],
data['reg_nrs'],
submodels_enduses=data['enduses'])
if reg_array_nr == 0:
sum1 = summary.summarize(muppy.get_objects())
if reg_array_nr == 1:
sum2 = summary.summarize(muppy.get_objects())
diff = summary.get_diff(sum1, sum2)
print(summary.print_(diff))
all_objects = muppy.get_objects()
print(all_objects)
for i in all_objects:
root = i
break
ib = refbrowser.InteractiveBrowser(root)
ib.main()
raise Exception'''

#if reg_array_nr == 1:
# break
#sum1 = summary.summarize(muppy.get_objects())
#sum2 = summary.summarize(muppy.get_objects())
#diff = summary.get_diff(sum1, sum2)
# print(summary.print_(diff))

# ------------------------------
# Plot generation to correlate HDD and energy demand
Expand Down Expand Up @@ -221,6 +189,18 @@ def aggregate_across_all_regs(
technologies : dict
Technologies per enduse
"""
# -----------------------------
# Aggregate residential demand [fueltype, region]
# -----------------------------
array_init = np.zeros((fueltypes_nr, reg_nrs)) #Annual
for submodel in aggr_results['ed_submodel_enduse_fueltype_regs_yh']:

if submodel == 0:
for enduse in aggr_results['ed_submodel_enduse_fueltype_regs_yh'][submodel]:
array_init += np.sum(aggr_results['ed_submodel_enduse_fueltype_regs_yh'][submodel][enduse][:, ])
else:
pass
aggr_results['ed_residnetial_tot_reg_yh'] = array_init

# ----------------------------------------------------
# Aggregate: [fueltype, region, fuel_yh_8760]
Expand Down
1 change: 0 additions & 1 deletion energy_demand/plotting/fig_p2_spatial_val.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ def plot_spatial_validation(
markeredgecolor='orange',
color='black',
label='model')'''


# Limit
plt.ylim(ymin=0)
Expand Down
124 changes: 124 additions & 0 deletions energy_demand/plotting/fig_p2_spatial_weather_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

"""
"""
from collections import defaultdict
import numpy as np
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
import matplotlib.pyplot as plt

from energy_demand.plotting import result_mapping
from energy_demand.plotting import fig_p2_weather_val
from energy_demand.read_write import data_loader
from energy_demand.read_write import read_weather_data
from energy_demand.plotting import basic_plot_functions

def run(
path_to_weather_data,
folder_path_weater_stations,
path_shapefile,
fig_path
):
"""
"""
# ----------------------------------------------------------
# Iterate all calculated weather stations
# ----------------------------------------------------------
weather_yrs_stations = read_weather_data.get_all_station_per_weather_yr(path_to_weather_data)

weather_station_coordinates = data_loader.read_weather_stations_raw(folder_path_weater_stations)
# ----------------------------------------------------------
# Station nr and how many times over simulation period
# ----------------------------------------------------------
station_counting_over_yr = {}

for weather_yr, stations in weather_yrs_stations.items():
for station in stations:
try:
station_counting_over_yr[station].append(weather_yr)
except:
station_counting_over_yr[station] = [weather_yr]

# Count number of years
for station in station_counting_over_yr:
station_counting_over_yr[station] = len(station_counting_over_yr[station])

# ----------------------------------------------------------
# Create dataframe
#{station_id: all_weather_stations[weather_yr][station_id]}
#df = pd.DataFrame({'src_id': [...], 'longitude': [...], 'latitude': [...]})
# ----------------------------------------------------------
stations_as_dict = {}
for station_id in station_counting_over_yr:
try:
stations_as_dict[station_id] = {
'longitude': weather_station_coordinates[station_id]['longitude'],
'latitude': weather_station_coordinates[station_id]['latitude'],
'nr_of_weather_yrs': station_counting_over_yr[station_id]}
except:
print("The station nr {} coul not be found".format(station_id))

df = pd.DataFrame.from_dict(stations_as_dict, orient='index')

df['Coordinates'] = list(zip(df.longitude, df.latitude))
df['Coordinates'] = df['Coordinates'].apply(Point)

# Load uk shapefile
uk_shapefile = gpd.read_file(path_shapefile)

# Assign correct projection
crs = {'init': 'epsg:27700'} #27700 == OSGB_1936_British_National_Grid
uk_gdf = gpd.GeoDataFrame(uk_shapefile, crs=crs)

# Plot
ax = uk_gdf.plot(
linewidth=0.3,
color='whitesmoke',
edgecolor='black',
figsize=basic_plot_functions.cm2inch(25, 20))

# plot coordinates
crs = {'init': 'epsg:4326'}
gdf = gpd.GeoDataFrame(df, geometry='Coordinates', crs=crs)
gdf = gdf.to_crs({'init' :'epsg:27700'})

field_to_plot = 'nr_of_weather_yrs'
nr_of_intervals = 6

bin_values = result_mapping.get_reasonable_bin_values_II(
data_to_plot=list(gdf[field_to_plot]),
nr_of_intervals=nr_of_intervals)

print("BINS " + str(bin_values))
gdf, cmap_rgb_colors, color_zero, min_value, max_value = fig_p2_weather_val.user_defined_bin_classification(
gdf,
field_to_plot,
bin_values=bin_values,
cmap_sequential='PuRd') #'Reds')

# plot with face color attribute
gdf.plot(
ax=ax,
markersize=20,
facecolor=gdf['bin_color'],
edgecolor='black',
linewidth=0.3)

legend_handles = result_mapping.add_simple_legend(
bin_values,
cmap_rgb_colors,
color_zero,
patch_form='circle')

plt.legend(
handles=legend_handles,
title=str(field_to_plot),
prop={'size': 8},
loc='center left', bbox_to_anchor=(1, 0.5),
frameon=False)

plt.show()
plt.savefig(fig_path)
raise Exception("HHH")
35 changes: 23 additions & 12 deletions energy_demand/plotting/fig_p2_weather_val.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ def user_defined_bin_classification(
input_df,
field_name,
bin_values,
cmap_diverging=None,
cmap_sequential=None

):
"""Classify values according to bins
Expand All @@ -279,7 +282,14 @@ def user_defined_bin_classification(
higher_as_bin : int
Bin value of > than last bin
cmap_sequential : str
'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds','YlOrBr',
'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu',
'PuBuGn', 'BuGn', 'YlGn'
cmap_diverging : str
'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu', 'RdYlGn',
'Spectral', 'coolwarm', 'bwr', 'seismic'
Info
-----
Include 0 in min_max_plot == False
Expand Down Expand Up @@ -310,22 +320,21 @@ def user_defined_bin_classification(
crit_append_val = False
raise Exception("The minimum user defined bin smaller is larger than minimum existing value")

# Sequential: 'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds','YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'
cmap, cmap_rgb_colors = norm_cmap(
bin_values[:1], #Remove 0 bin from colors
cmap='Purples') #'YlOrBr'
if not cmap_sequential:
cmap, cmap_rgb_colors = norm_cmap(bin_values[:1],cmap='Purples') #'YlOrBr'
else:
cmap, cmap_rgb_colors = norm_cmap(bin_values[:1],cmap=cmap_sequential) #'YlOrBr'

else: #only positive values
if max_real_value > bin_values[-1]:
# add "higher as bin"
bin_values.append(max_real_value)
elif bin_values[-1] > max_real_value:
raise Exception("The maximum user defined bin value is larger than maximum value")

# Sequential: 'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds','YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'
cmap, cmap_rgb_colors = norm_cmap(
bin_values[1:], #Remove 0 bin from colors
cmap='Purples') #'YlOrBr'
if not cmap_sequential:
cmap, cmap_rgb_colors = norm_cmap(bin_values[1:], cmap='Purples')
else:
cmap, cmap_rgb_colors = norm_cmap(bin_values[1:], cmap=cmap_sequential)

# e.g. [0, 3, 6] --> generates (0, 3], and (3, 6] bin
input_df['bin_color'] = pd.cut(input_df[field_name], bin_values, right=True, labels=cmap_rgb_colors)
Expand All @@ -344,8 +353,10 @@ def user_defined_bin_classification(
bin_values.append(max_real_value)
bin_values.insert(0, min_real_value)

# Diverging: 'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic'
cmap, cmap_rgb_colors = norm_cmap(bin_values, cmap='coolwarm')
if not cmap_diverging:
cmap, cmap_rgb_colors = norm_cmap(bin_values, cmap='coolwarm')
else:
cmap, cmap_rgb_colors = norm_cmap(bin_values, cmap=cmap_diverging)

# Reclassify zero value
positive_bin_colors = []
Expand Down
4 changes: 0 additions & 4 deletions energy_demand/plotting/fig_spatial_distribution_of_peak.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,6 @@ def run(
ax = uk_gdf.plot(
figsize=basic_plot_functions.cm2inch(25, 20))

# Field to plot
#field_to_plot = "std_deviation_p_demand_peak_h"
#field_to_plot = "std_deviation_abs_demand_peak_h"

nr_of_intervals = 6

bin_values = result_mapping.get_reasonable_bin_values_II(
Expand Down
33 changes: 18 additions & 15 deletions energy_demand/plotting/result_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import palettable
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.patches import Circle
from matplotlib.colors import LinearSegmentedColormap
from energy_demand.basic import basic_functions
from energy_demand.technologies import tech_related
Expand Down Expand Up @@ -269,10 +270,6 @@ def user_defined_classification(
def get_legend_handles(bins, color_list, color_zero, min_value, max_value):
"""
"""
print("bins fff")
print(bins)
print(color_list)

legend_handles = []
small_number = 0.0001 # Small number for plotting corrrect charts

Expand Down Expand Up @@ -304,6 +301,7 @@ def get_legend_handles(bins, color_list, color_zero, min_value, max_value):
patch = mpatches.Patch(
color=color_zero,
label=str("0"))

legend_handles.append(patch)
else:
pass
Expand All @@ -329,7 +327,7 @@ def get_legend_handles(bins, color_list, color_zero, min_value, max_value):

return legend_handles

def add_simple_legend(bins, color_list, color_zero):
def add_simple_legend(bins, color_list, color_zero, patch_form='rectangle'):
"""Add legend without giving the intervals
"""
legend_handles = []
Expand All @@ -347,21 +345,26 @@ def add_simple_legend(bins, color_list, color_zero):
else:
label = str(bin_value)

#if cnt == 0 and bin_value == 0: #ignore first element if zero
# pass
#else:
if 1 == 1:
if bin_value == 0:
color = color_zero
else:
color = color_list[bin_nr]
bin_nr += 1
if bin_value == 0:
color = color_zero
else:
color = color_list[bin_nr]
bin_nr += 1

if patch_form == 'rectangle':
patch = mpatches.Patch(
color=color,
label=label)
elif patch_form == 'circle':
patch = Circle(
xy=(0,0),
radius=1,
color=color,
label=label)
else:
raise Exception("wrong patch_form")

legend_handles.append(patch)
legend_handles.append(patch)

return legend_handles

Expand Down
13 changes: 10 additions & 3 deletions energy_demand/read_write/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,16 @@ def read_weather_stations_raw(path_to_csv):
weather_stations = {}

for _, row in df_stations.iterrows():
weather_stations[int(row['src_id'])] = {
'latitude' : float(row['Latitude']),
'longitude': float(row['Longitude'])}

# Filter out not fitting weather stations
if float(row['Longitude']) < -8:
pass
elif float(row['Latitude']) < 50:
pass
else:
weather_stations[int(row['src_id'])] = {
'latitude' : float(row['Latitude']),
'longitude': float(row['Longitude'])}

return weather_stations

Expand Down

0 comments on commit 1a4f686

Please sign in to comment.