Skip to content

Commit

Permalink
refactored that only runs for a single weater station and weather yr
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann committed Oct 10, 2018
1 parent cbb702b commit 9433480
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 323 deletions.
14 changes: 4 additions & 10 deletions energy_demand/geography/weather_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,7 @@ def insert_peak_dh_shape(
def get_weather_station_selection(
all_weather_stations,
counter,
weather_yr,
weather_by
weather_yr
):
"""Select weather stations based on
position in list
Expand All @@ -753,23 +752,18 @@ def get_weather_station_selection(
"""
all_stations_of_weather_yr = list(all_weather_stations[weather_yr].keys())

# Add base year
all_weather_stations_out = {}

# Sort
all_stations_of_weather_yr.sort()

try:
# Select station according ot position in list
station_id = all_stations_of_weather_yr[counter]

all_weather_stations_out[weather_yr] = {station_id: all_weather_stations[weather_yr][station_id]}
continue_calculation = True
all_weather_stations_out = {station_id: all_weather_stations[weather_yr][station_id]}
except:
# Not enough stations to select position in list
continue_calculation = False
station_id = False
all_weather_stations_out[weather_yr] = []
all_weather_stations_out = []
print("... no weather station found")

return all_weather_stations_out, continue_calculation, station_id
return all_weather_stations_out, station_id
427 changes: 212 additions & 215 deletions energy_demand/main.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions energy_demand/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ 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
2 changes: 1 addition & 1 deletion energy_demand/plotting/fig_p2_weather_val.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def run(
bin_values = result_mapping.get_reasonable_bin_values_II(
data_to_plot=list(uk_gdf[field_to_plot]),
nr_of_intervals=nr_of_intervals)
print(float(uk_gdf[field_to_plot].max()))
print(float(uk_gdf[field_to_plot]))
print("BINS " + str(bin_values))

uk_gdf, cmap_rgb_colors, color_zero, min_value, max_value = user_defined_bin_classification(
Expand Down
24 changes: 3 additions & 21 deletions energy_demand/plotting/fig_spatial_distribution_of_peak.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ def run(
"""
"""
weather_yrs = []
calculated_yrs_paths = []

all_calculated_yrs_paths = []
for scenario in scenarios:

path_scenario = os.path.join(path_to_folder_with_scenarios, scenario)
all_result_folders = os.listdir(path_scenario)
calculated_yrs_paths = []

for result_folder in all_result_folders:
try:
Expand All @@ -47,29 +46,12 @@ def run(
# weather_station = int(split_path_name[1])
#except:
# weather_station = "all_station"

tupyle_yr_path = (weather_yr, os.path.join(path_scenario))
calculated_yrs_paths.append(tupyle_yr_path)

except ValueError:
pass

# delete 2015 if more than two becuase only base year for every calcuatio
only_weather_yr_paths = []
if len(calculated_yrs_paths) == 2:

for weather_yr, path in calculated_yrs_paths:
if weather_yr == 2015:
pass
else:
only_weather_yr_paths.append((weather_yr, path))
else:
only_weather_yr_paths = calculated_yrs_paths

# Add only weather yr not 2015
for entry in only_weather_yr_paths:
all_calculated_yrs_paths.append(entry)

for simulation_yr in simulation_yrs:
# -----------
# Used across different plots
Expand All @@ -78,13 +60,13 @@ def run(
#Fueltype to consider
fueltype_str = 'electricity'
fueltype_int = tech_related.get_fueltype_int(fueltype_str)

#results_container['ed_weatheryr_fueltype_regs_yh']
container = {}
container['abs_demand_in_peak_h'] = {}
container['p_demand_in_peak_h'] = {}

for weather_yr, path_data_ed in all_calculated_yrs_paths:
for weather_yr, path_data_ed in calculated_yrs_paths:
print("... prepare data {} {}".format(weather_yr, path_data_ed))

path_to_weather_yr = os.path.join(path_data_ed, str(weather_yr))
Expand Down
104 changes: 49 additions & 55 deletions energy_demand/read_write/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,50 +64,53 @@ def create_weather_station_map(
'longitude': [...],
'latitude': [...]})
"""
# Convert dict to dataframe
df = pd.DataFrame.from_dict(stations_as_dict, orient='index')
if stations_as_dict == {}:
print("No stations available to plot")
else:
# Convert dict to dataframe
df = pd.DataFrame.from_dict(stations_as_dict, orient='index')

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

if path_shapefile is False:
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
ax = world[world.name == "United Kingdom"].plot(
color='white', edgecolor='black')
else:
# Load uk shapefile
uk_shapefile = gpd.read_file(path_shapefile)
if path_shapefile is False:
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
ax = world[world.name == "United Kingdom"].plot(
color='white', edgecolor='black')
else:
# 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)
# Assign correct projection
crs = {'init': 'epsg:27700'} #27700 == OSGB_1936_British_National_Grid
uk_gdf = gpd.GeoDataFrame(uk_shapefile, crs=crs)

# Transform
uk_gdf = uk_gdf.to_crs({'init' :'epsg:4326'})
# Transform
uk_gdf = uk_gdf.to_crs({'init' :'epsg:4326'})

# Plot
ax = uk_gdf.plot(color='white', edgecolor='black')
# Plot
ax = uk_gdf.plot(color='white', edgecolor='black')

# print coordinates
crs = {'init': 'epsg:4326'}
gdf = gpd.GeoDataFrame(df, geometry='Coordinates') # crs=crs,
gdf.plot(ax=ax, color='red')
# print coordinates
crs = {'init': 'epsg:4326'}
gdf = gpd.GeoDataFrame(df, geometry='Coordinates') # crs=crs,
gdf.plot(ax=ax, color='red')

plt.savefig(fig_path)
plt.savefig(fig_path)

# ---------------------------------
# Save coordinates as file
# ---------------------------------
station_infos = ["name, latitue, longitude"]
fig_path = fig_path[:-4] + ".txt"
for station in stations_as_dict:
station_info = "{}, {}, {}".format(
station,
stations_as_dict[station]['latitude'],
stations_as_dict[station]['longitude'])
station_infos.append(station_info)
# ---------------------------------
# Save coordinates as file
# ---------------------------------
station_infos = ["name, latitue, longitude"]
fig_path = fig_path[:-4] + ".txt"
for station in stations_as_dict:
station_info = "{}, {}, {}".format(
station,
stations_as_dict[station]['latitude'],
stations_as_dict[station]['longitude'])
station_infos.append(station_info)

write_data.write_list_to_txt(fig_path, station_infos)
write_data.write_list_to_txt(fig_path, station_infos)

def read_weather_stations_raw(path_to_csv):
"""Read in weather stations from csv file
Expand Down Expand Up @@ -870,7 +873,7 @@ def load_temp_data(local_paths, weather_yrs_scenario, save_fig=False):
----------
local_paths : dict
Local local_paths
weather_yrs_scenario : list
weather_yr_scenario : list
Years to use temperatures
Returns
Expand All @@ -883,13 +886,12 @@ def load_temp_data(local_paths, weather_yrs_scenario, save_fig=False):
temp_data_short = defaultdict(dict)
weather_stations_with_data = defaultdict(dict)

for year in weather_yrs_scenario:

weather_stations = read_weather_stations_raw(
local_paths['folder_path_weater_stations'])
weather_stations = read_weather_stations_raw(
local_paths['folder_path_weater_stations'])

for weather_yr_scenario in weather_yrs_scenario:
temp_data = read_weather_data.read_weather_data_script_data(
local_paths['weather_data'], year)
local_paths['weather_data'], weather_yr_scenario)

for station in weather_stations:
try:
Expand All @@ -899,27 +901,19 @@ def load_temp_data(local_paths, weather_yrs_scenario, save_fig=False):
if weather_stations[station]['longitude'] > 2 or weather_stations[station]['longitude'] < -8.5:
pass
else:
temp_data_short[year][station] = temp_data[station]
temp_data_short[weather_yr_scenario][station] = temp_data[station]
except:
logging.debug("no data for weather station " + str(station))

for station_id in temp_data_short[year].keys():
for station_id in temp_data_short[weather_yr_scenario].keys():
try:
weather_stations_with_data[year][station_id] = weather_stations[station_id]
weather_stations_with_data[weather_yr_scenario][station_id] = weather_stations[station_id]
except:
del temp_data_short[year][station_id]
del temp_data_short[weather_yr_scenario][station_id]

logging.info(
"Info: Number of weather stations: {} year: Number of temp data: {}, year: {}".format(
len(weather_stations_with_data), len(temp_data_short[year]), weather_yrs_scenario))

if not save_fig:
pass
else:
create_weather_station_map(
weather_stations_with_data[year],
os.path.join(save_fig, 'weather_station_distribution_{}.pdf'.format(year)),
path_shapefile=local_paths['lad_shapefile'])
"Info: Number of weather stations: {} weather_yr_scenario: Number of temp data: {}, weather_yr_scenario: {}".format(
len(weather_stations_with_data), len(temp_data_short[weather_yr_scenario]), weather_yr_scenario))

return dict(weather_stations_with_data), dict(temp_data_short)

Expand Down
14 changes: 14 additions & 0 deletions energy_demand/read_write/read_weather_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
import numpy as np
from collections import defaultdict

def get_all_weather_yrs(path_to_csvs):
"""Get all weather yrs
TODO
"""
weather_yrs = set([])

all_txt_files_in_folder = os.listdir(path_to_csvs)

for file_path in all_txt_files_in_folder:
year = int(file_path.split("__")[0])
weather_yrs.add(year)

return list(weather_yrs)

def read_weather_data_script_data(path_to_csv, weather_yrs_scenario):
"""Read in weather data from script data
Expand Down
40 changes: 39 additions & 1 deletion energy_demand/result_processing/paper_II.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,21 @@ def paper_II_plots(
basic_functions.del_previous_setup(path_out_plots)
basic_functions.create_folder(path_out_plots)

####################################################################
# Plot weather station availability map
####################################################################


####################################################################
# Plot spatial distribution of variability depending on weather year
# Plot the variability of the contribution of regional peak demand
# to national peak demand
####################################################################
if plot_crit_dict['plot_spatial_distribution_of_peak']:

# Select simulation years
simulation_yrs = [2015, 2050]

# Select field to plot
field_to_plot = "std_deviation_p_demand_peak_h"
#field_to_plot = "std_deviation_abs_demand_peak_h"
Expand All @@ -76,11 +86,39 @@ def paper_II_plots(
only_scenarios,
path_to_folder_with_scenarios,
path_shapefile_input,
simulation_yrs=[2015, 2050],
simulation_yrs=simulation_yrs,
field_to_plot=field_to_plot,
fig_path=path_out_plots)

####################################################################
# Plot regional vs national spatio-temporal validation
####################################################################
'''path_regional_calculations = "C:/Users/cenv0553/ed/results/_for_FIG2a/_regional_calculations"
path_rolling_elec_demand = os.path.join(
"C:/Users/cenv0553/ed/energy_demand/energy_demand/config_data",
'01-validation_datasets', '01_national_elec_2015', 'elec_demand_2015.csv')
path_temporal_elec_validation = os.path.join(
"C:/Users/cenv0553/ed/energy_demand/energy_demand/config_data",
'01-validation_datasets', '02_subnational_elec', 'data_2015_elec_domestic.csv')
path_temporal_gas_validation = os.path.join(
"C:/Users/cenv0553/ed/energy_demand/energy_demand/config_data",
'01-validation_datasets', '03_subnational_gas', 'data_2015_gas_domestic.csv')
path_non_regional_elec_2015 = os.path.abspath(
os.path.join(path_regional_calculations, '..', "_non_regional_calculations_2015"))'''

# Plot figure national an regional validation comparison
'''figs_p2.plot_fig_spatio_temporal_validation(
path_regional_calculations=path_regional_calculations,
path_rolling_elec_demand=path_rolling_elec_demand,
path_temporal_elec_validation=path_temporal_elec_validation,
path_temporal_gas_validation=path_temporal_gas_validation,
path_non_regional_elec_2015=path_non_regional_elec_2015)'''

####################################################################
#
####################################################################
for scenario in only_scenarios:

# -----------
# plot where spatio-temporal comparison between national and regional
# -----------
Expand Down

0 comments on commit 9433480

Please sign in to comment.