def test_get_fueltype_str(): """Testing function """ fueltypes = {'gas': 1} in_value = 1 expected = 'gas' # call function out_value = tech_related.get_fueltype_str(fueltypes, in_value) assert out_value == expected
def read_in_results( path_result, seasons, model_yeardays_daytype ): """Read and post calculate results from txt files and store into container Arguments --------- path_result : str Paths seasons : dict seasons model_yeardays_daytype : dict Daytype of modelled yeardays """ logging.info("... Reading in results") lookups = lookup_tables.basic_lookups() results_container = {} # ----------------- # Read in demands # ----------------- try: results_container['results_enduse_every_year'] = read_enduse_specific_results( path_result) except: pass try: print("path_result " + str(path_result)) results_container['ed_fueltype_regs_yh'] = read_results_yh( path_result, 'ed_fueltype_regs_yh') except: pass # Read in residential demands try: results_container['residential_results'] = read_results_yh( path_result, 'residential_results') except: pass # Calculate total demand per fueltype for every hour try: tot_fueltype_yh = {} for year in results_container['ed_fueltype_regs_yh']: nr_of_fueltypes = results_container['ed_fueltype_regs_yh'][year].shape[0] tot_fueltype_yh[year] = np.zeros((nr_of_fueltypes, 8760)) for year, ed_regs_yh in results_container['ed_fueltype_regs_yh'].items(): fuel_yh = np.sum(ed_regs_yh, axis=1) #Sum across all regions tot_fueltype_yh[year] += fuel_yh results_container['tot_fueltype_yh'] = tot_fueltype_yh except: pass # ----------------- # Peak calculations # ----------------- try: results_container['ed_peak_h'] = {} results_container['ed_peak_regs_h'] = {} for year, ed_fueltype_reg_yh in results_container['ed_fueltype_regs_yh'].items(): results_container['ed_peak_h'][year] = {} results_container['ed_peak_regs_h'][year] = {} for fueltype_int, ed_reg_yh in enumerate(ed_fueltype_reg_yh): fueltype_str = tech_related.get_fueltype_str(lookups['fueltypes'], fueltype_int) # Calculate peak per fueltype for all regions (ed_reg_yh= np.array(fueltype, reg, yh)) all_regs_yh = np.sum(ed_reg_yh, axis=0) # sum regs peak_h = np.max(all_regs_yh) # select max of 8760 h results_container['ed_peak_h'][year][fueltype_str] = peak_h results_container['ed_peak_regs_h'][year][fueltype_str] = np.max(ed_reg_yh, axis=1) # ------------- # Load factors # ------------- results_container['reg_load_factor_y'] = read_lf_y( os.path.join(path_result, "result_reg_load_factor_y")) results_container['reg_load_factor_yd'] = read_lf_y( os.path.join(path_result, "result_reg_load_factor_yd")) # ------------- # Post-calculations # ------------- # Calculate average per season and fueltype for every fueltype results_container['av_season_daytype_cy'], results_container['season_daytype_cy'] = calc_av_per_season_fueltype( results_container['ed_fueltype_regs_yh'], seasons, model_yeardays_daytype) '''results_container['load_factor_seasons'] = {} results_container['load_factor_seasons']['winter'] = read_lf_y( os.path.join(path_result, "result_reg_load_factor_winter")) results_container['load_factor_seasons']['spring'] = read_lf_y( os.path.join(path_result, "result_reg_load_factor_spring")) results_container['load_factor_seasons']['summer'] = read_lf_y( os.path.join(path_result, "result_reg_load_factor_summer")) results_container['load_factor_seasons']['autumn'] = read_lf_y( os.path.join(path_result, "result_reg_load_factor_autumn"))''' except: pass logging.info("... Reading in results finished") return results_container
def create_geopanda_files(data, results_container, path_data_results_shapefiles, regions, fueltypes_nr, fueltypes, path_shapefile_input): """Create map related files (png) from results. Arguments --------- results_container : dict Data container paths : dict Paths regions : list Region in a list with order how they are stored in result array fueltypes_nr : int Number of fueltypes """ logging.info("... create spatial maps of results") # -------- # Read LAD shapefile and create geopanda # -------- # Single scenario run lad_geopanda_shp = gpd.read_file(path_shapefile_input) # Attribute merge unique Key unique_merge_id = 'name' #'geo_code' # ====================================== # Spatial maps of difference in load factors # ====================================== simulated_yrs = list(results_container['load_factors_y'].keys()) final_yr = simulated_yrs[-1] base_yr = simulated_yrs[0] for fueltype in range(fueltypes_nr): logging.info("progress.. {}".format(fueltype)) fueltype_str = tech_related.get_fueltype_str(fueltypes, fueltype) field_name = 'lf_diff_{}-{}_{}_'.format(base_yr, final_yr, fueltype_str) lf_end_yr = basic_functions.array_to_dict( results_container['load_factors_y'][final_yr][fueltype], regions) lf_base_yr = basic_functions.array_to_dict( results_container['load_factors_y'][base_yr][fueltype], regions) # Calculate load factor difference base and final year (100 = 100%) diff_lf = {} for reg in regions: diff_lf[reg] = lf_end_yr[reg] - lf_base_yr[reg] # Both need to be lists merge_data = { str(field_name): list(diff_lf.values()), str(unique_merge_id): list(regions) } # Merge to shapefile lad_geopanda_shp = merge_data_to_shp(lad_geopanda_shp, merge_data, unique_merge_id) # If user classified, defined bins [x for x in range(0, 1000000, 200000)] #bins = [-4, -2, 0, 2, 4] # must be of uneven length containing zero bins = [-15, -10, -5, 0, 5, 10, 15] # must be of uneven length containing zero color_list, color_prop, user_classification, color_zero = colors_plus_minus_map( bins=bins, color_prop='qualitative', color_order=True, color_zero='#ffffff') #8a2be2 plot_lad_national(lad_geopanda_shp=lad_geopanda_shp, legend_unit="%", field_to_plot=field_name, fig_name_part="lf_max_y", result_path=path_data_results_shapefiles, color_palette='Purples_9', color_prop=color_prop, user_classification=user_classification, color_list=color_list, color_zero=color_zero, bins=bins) # ====================================== # Population # ====================================== for year in results_container['results_every_year'].keys(): field_name = 'pop_{}'.format(year) # Both need to be lists merge_data = { str(field_name): data['scenario_data']['population'][year].flatten().tolist(), str(unique_merge_id): list(regions) } # Merge to shapefile lad_geopanda_shp = merge_data_to_shp(lad_geopanda_shp, merge_data, unique_merge_id) # If user classified, defined bins bins = [50000, 300000] plot_lad_national(lad_geopanda_shp=lad_geopanda_shp, legend_unit="people", field_to_plot=field_name, fig_name_part="pop_", result_path=path_data_results_shapefiles, color_palette='Dark2_7', color_prop='qualitative', user_classification=True, bins=bins) # ====================================== # Total fuel (y) all enduses # ====================================== base_yr = list(results_container['results_every_year'].keys())[0] for year in results_container['results_every_year'].keys(): for fueltype in range(fueltypes_nr): logging.info(" progress.. {}".format(fueltype)) fueltype_str = tech_related.get_fueltype_str(fueltypes, fueltype) # --------- # Sum per enduse and year (y) # --------- field_name = 'y_{}_{}'.format(year, fueltype_str) # Calculate yearly sum yearly_sum_gwh = np.sum( results_container['results_every_year'][year][fueltype], axis=1) data_to_plot = basic_functions.array_to_dict( yearly_sum_gwh, regions) # Both need to be lists merge_data = { str(field_name): list(data_to_plot.values()), str(unique_merge_id): list(regions) } # Merge to shapefile lad_geopanda_shp = merge_data_to_shp(lad_geopanda_shp, merge_data, unique_merge_id) # If user classified, defined bins print("... plot {}".format(lad_geopanda_shp)) plot_lad_national(lad_geopanda_shp=lad_geopanda_shp, legend_unit="GWh", field_to_plot=field_name, fig_name_part="tot_all_enduses_y_", result_path=path_data_results_shapefiles, color_palette='Dark2_7', color_prop='qualitative', user_classification=False) # =============================================== # Differences in percent per enduse and year (y) # =============================================== field_name = 'y_diff_p_{}-{}_{}'.format(base_yr, year, fueltype_str) # Calculate yearly sums yearly_sum_gwh_by = np.sum( results_container['results_every_year'][base_yr][fueltype], axis=1) yearly_sum_gwh_cy = np.sum( results_container['results_every_year'][year][fueltype], axis=1) # Calculate percentual difference p_diff = ((yearly_sum_gwh_cy / yearly_sum_gwh_by) * 100) - 100 data_to_plot = basic_functions.array_to_dict(p_diff, regions) # Both need to be lists merge_data = { str(field_name): list(data_to_plot.values()), str(unique_merge_id): list(regions) } # Merge to shapefile lad_geopanda_shp = merge_data_to_shp(lad_geopanda_shp, merge_data, unique_merge_id) # If user classified, defined bins [x for x in range(0, 1000000, 200000)] #bins = get_reasonable_bin_values(list(data_to_plot.values())) #bins = [-4, -2, 0, 2, 4] # must be of uneven length containing zero bins = [-30, -20, -10, 0, 10, 20, 30] # must be of uneven length containing zero color_list, color_prop, user_classification, color_zero = colors_plus_minus_map( bins=bins, color_prop='qualitative', color_order=True, color_zero='#ffffff') #8a2be2 # Plot difference in % per fueltype of total fuel (y) plot_lad_national(lad_geopanda_shp=lad_geopanda_shp, legend_unit="GWh", field_to_plot=field_name, fig_name_part="tot_all_enduses_y_", result_path=path_data_results_shapefiles, color_palette='Purples_9', color_prop=color_prop, user_classification=user_classification, color_list=color_list, color_zero=color_zero, bins=bins) # ====================================== # Load factors # ====================================== for year in results_container['load_factors_y'].keys(): for fueltype in range(fueltypes_nr): fueltype_str = tech_related.get_fueltype_str(fueltypes, fueltype) field_name = 'lf_{}_{}'.format(year, fueltype_str) results = basic_functions.array_to_dict( results_container['load_factors_y'][year][fueltype], regions) # Both need to be lists merge_data = { str(field_name): list(results.values()), str(unique_merge_id): list(regions) } # Merge to shapefile lad_geopanda_shp = merge_data_to_shp(lad_geopanda_shp, merge_data, unique_merge_id) # If user classified, defined bins plot_lad_national(lad_geopanda_shp=lad_geopanda_shp, legend_unit="%", field_to_plot=field_name, fig_name_part="lf_max_y", result_path=path_data_results_shapefiles, color_palette='Dark2_7', color_prop='qualitative', user_classification=False)
def plot_radar_plots_average_peak_day( scenario_data, year_to_plot, fig_name, plotshow ): """Compare averaged dh profile overall regions for peak day for future year and base year MAYBE: SO FAR ONLY FOR ONE SCENARIO """ lookups = lookup_tables.basic_lookups() # Scenarios all_scenarios = list(scenario_data.keys()) first_scenario = str(all_scenarios[:1][0]) # ---------------- # Create base year peak load profile # ----------------- all_regs_fueltypes_yh_by = {} for fueltype, fuels_regs in enumerate(scenario_data[first_scenario]['results_every_year'][2015]): for region_array_nr, fuel_reg in enumerate(fuels_regs): try: all_regs_fueltypes_yh_by[fueltype] += fuel_reg except KeyError: all_regs_fueltypes_yh_by[fueltype] = fuel_reg try: all_fuels_by += fuel_reg except: all_fuels_by = fuel_reg # ------------------------ # Future year load profile # ------------------------ all_regs_fueltypes_yh_ey = {} for fueltype, fuels_regs in enumerate(scenario_data[first_scenario]['results_every_year'][year_to_plot]): for region_array_nr, fuel_reg in enumerate(fuels_regs): try: all_regs_fueltypes_yh_ey[fueltype] += fuel_reg except KeyError: all_regs_fueltypes_yh_ey[fueltype] = fuel_reg # ----------- # get peak day across all fueltypes # ----------- for fueltype in all_regs_fueltypes_yh_by.keys(): all_regs_fueltypes_yh_by[fueltype] = all_regs_fueltypes_yh_by[fueltype].reshape((365, 24)) for fueltype in all_regs_fueltypes_yh_ey.keys(): all_regs_fueltypes_yh_ey[fueltype] = all_regs_fueltypes_yh_ey[fueltype].reshape((365, 24)) all_fuels_by_d = all_fuels_by.reshape((365, 24)) peak_day_nr = np.argmax(np.sum(all_fuels_by_d, axis=1)) for fueltype in all_regs_fueltypes_yh_by.keys(): fueltype_str = tech_related.get_fueltype_str(lookups['fueltypes'], fueltype) name_spider_plot = os.path.join(fig_name, "spider_{}.pdf".format(fueltype_str)) # --------------------------- # Calculate load factors # --------------------------- max_load_h_by = max(all_regs_fueltypes_yh_by[fueltype][peak_day_nr]) average_load_y_by = np.average(all_regs_fueltypes_yh_by[fueltype]) load_factor_fueltype_y_by = (average_load_y_by / max_load_h_by) * 100 #convert to percentage max_load_h_cy = max(all_regs_fueltypes_yh_ey[fueltype][peak_day_nr]) average_load_y_cy = np.average(all_regs_fueltypes_yh_ey[fueltype]) load_factor_fueltype_y_cy = (average_load_y_cy / max_load_h_cy) * 100 #convert to percentage # ---------------------------------- # Plot dh for peak day for base year # ---------------------------------- individ_radars_to_plot_dh = [ list(all_regs_fueltypes_yh_by[fueltype][peak_day_nr]), list(all_regs_fueltypes_yh_ey[fueltype][peak_day_nr])] plotting_results.plot_radar_plot_multiple_lines( individ_radars_to_plot_dh, name_spider_plot, plot_steps=50, plotshow=False, lf_y_by=load_factor_fueltype_y_by, lf_y_cy=load_factor_fueltype_y_cy)
def run_all_plot_functions(results_container, reg_nrs, regions, lookups, result_paths, assumptions, enduses, plot_crit, base_yr, comparison_year): """Summary function to plot all results comparison_year : int Year to generate comparison plots """ if plot_crit['plot_lad_cross_graphs']: try: # Plot cross graph where very region is a dot fig_cross_graphs.plot_cross_graphs( base_yr=base_yr, comparison_year=comparison_year, regions=regions, ed_year_fueltype_regs_yh=results_container[ 'ed_fueltype_regs_yh'], reg_load_factor_y=results_container['reg_load_factor_y'], fueltype_int=lookups['fueltypes']['electricity'], fueltype_str='electricity', fig_name=os.path.join( result_paths['data_results_PDF'], "comparions_LAD_cross_graph_electricity_by_cy.pdf"), label_points=False, plotshow=False) fig_cross_graphs.plot_cross_graphs( base_yr=base_yr, comparison_year=comparison_year, regions=regions, ed_year_fueltype_regs_yh=results_container[ 'ed_fueltype_regs_yh'], reg_load_factor_y=results_container['reg_load_factor_y'], fueltype_int=lookups['fueltypes']['gas'], fueltype_str='gas', fig_name=os.path.join( result_paths['data_results_PDF'], "comparions_LAD_cross_graph_gas_by_cy.pdf"), label_points=False, plotshow=False) except KeyError: logging.info( "Check if correct comparison year is provided, i.e. really data exists for this year" ) # ---------- # Plot LAD differences for first and last year # ---------- try: fig_lad_related.plot_lad_comparison( base_yr=2015, comparison_year=2050, regions=regions, ed_year_fueltype_regs_yh=results_container['ed_fueltype_regs_yh'], fueltype_int=lookups['fueltypes']['electricity'], fueltype_str='electricity', fig_name=os.path.join( result_paths['data_results_PDF'], "comparions_LAD_modelled_electricity_by_cy.pdf"), label_points=False, plotshow=False) print("... plotted by-cy LAD energy demand compariosn") # Plot peak h for every hour fig_lad_related.lad_comparison_peak( base_yr=2015, comparison_year=2050, regions=regions, ed_year_fueltype_regs_yh=results_container['ed_fueltype_regs_yh'], fueltype_int=lookups['fueltypes']['electricity'], fueltype_str='electricity', fig_name=os.path.join( result_paths['data_results_PDF'], "comparions_LAD_modelled_electricity_peakh_by_cy.pdf"), label_points=False, plotshow=False) print("... plotted by-cy LAD energy demand compariosn") except: pass # ---------------- # Plot demand for every region over time # ------------------- if plot_crit['plot_line_for_every_region_of_peak_demand']: logging.info( "... plot fuel per fueltype for every region over annual teimsteps" ) fig_one_fueltype_multiple_regions_peak_h.plt_regions_peak_h( results_container['ed_fueltype_regs_yh'], lookups, regions, os.path.join(result_paths['data_results_PDF'], 'peak_h_total_electricity.pdf'), fueltype_str_to_plot="electricity") if plot_crit['plot_fuels_enduses_y']: #... Plot total fuel (y) per fueltype as line chart" fig_fuels_enduses_y.run( results_container['ed_fueltype_regs_yh'], lookups, os.path.join(result_paths['data_results_PDF'], 'y_fueltypes_all_enduses.pdf')) # ------------ # Plot stacked annual enduses # ------------ if plot_crit['plot_stacked_enduses']: rs_enduses_sorted = [ 'rs_space_heating', 'rs_water_heating', 'rs_lighting', 'rs_cold', 'rs_wet', 'rs_consumer_electronics', 'rs_home_computing', 'rs_cooking' ] ss_enduses_sorted = [ 'ss_space_heating', 'ss_water_heating', 'ss_lighting', 'ss_catering', 'ss_small_power', 'ss_fans', 'ss_cooling_humidification', 'ss_ICT_equipment', 'ss_other_gas', 'ss_other_electricity', 'ss_cooled_storage' ] is_enduses_sorted = [ 'is_space_heating', 'is_lighting', 'is_refrigeration', 'is_motors', 'is_compressed_air', 'is_high_temp_process', 'is_low_temp_process', 'is_other', 'is_drying_separation' ] rs_color_list = plotting_styles.rs_color_list_selection() ss_color_list = plotting_styles.ss_color_list_selection() is_color_list = plotting_styles.is_color_list_selection() # Residential fig_stacked_enduse.run(assumptions['sim_yrs'], results_container['results_enduse_every_year'], rs_enduses_sorted, rs_color_list, os.path.join(result_paths['data_results_PDF'], "stacked_rs_country.pdf"), plot_legend=True) # Service fig_stacked_enduse.run(assumptions['sim_yrs'], results_container['results_enduse_every_year'], ss_enduses_sorted, ss_color_list, os.path.join(result_paths['data_results_PDF'], "stacked_ss_country.pdf"), plot_legend=True) # Industry fig_stacked_enduse.run(assumptions['sim_yrs'], results_container['results_enduse_every_year'], is_enduses_sorted, is_color_list, os.path.join(result_paths['data_results_PDF'], "stacked_is_country_.pdf"), plot_legend=True) # ------------------------------ # Plot annual demand for enduses for all submodels # ------------------------------ if plot_crit['plot_y_all_enduses']: fig_stacked_enduse_sectors.run( lookups, assumptions['sim_yrs'], results_container['results_enduse_every_year'], enduses['residential'], enduses['service'], enduses['industry'], os.path.join(result_paths['data_results_PDF'], "stacked_all_enduses_country.pdf")) # -------------- # Fuel per fueltype for whole country over annual timesteps # ---------------- if plot_crit['plot_fuels_enduses_y']: logging.info( "... plot fuel per fueltype for whole country over annual timesteps" ) #... Plot total fuel (y) per fueltype as line chart" fig_fuels_enduses_y.run( results_container['ed_fueltype_regs_yh'], lookups, os.path.join(result_paths['data_results_PDF'], 'y_fueltypes_all_enduses.pdf')) # ---------- # Plot seasonal typical load profiles # Averaged load profile per daytpe for a region # ---------- # ------------------------------------ # Load factors per fueltype and region # ------------------------------------ if plot_crit['plot_lf']: for fueltype_str, fueltype_int in lookups['fueltypes'].items(): '''fig_lf.plot_seasonal_lf( fueltype_int, fueltype_str, results_container['load_factor_seasons'], reg_nrs, os.path.join( result_paths['data_results_PDF'], 'lf_seasonal_{}.pdf'.format(fueltype_str)))''' '''fig_lf.plot_lf_y( fueltype_int, fueltype_str, results_container['reg_load_factor_yd'], reg_nrs, os.path.join( result_paths['data_results_PDF'], 'lf_yd_{}.pdf'.format(fueltype_str)))''' # reg_load_factor_yd = max daily value / average annual daily value fig_lf.plot_lf_y( fueltype_int, fueltype_str, results_container['reg_load_factor_y'], reg_nrs, os.path.join(result_paths['data_results_PDF'], 'lf_y_{}.pdf'.format(fueltype_str))) # -------------- # Fuel week of base year # ---------------- if plot_crit['plot_week_h']: fig_fuels_enduses_week.run( results_resid=results_container['ed_fueltype_regs_yh'], lookups=lookups, hours_to_plot=range(7 * 24), year_to_plot=2015, fig_name=os.path.join(result_paths['data_results_PDF'], "tot_all_enduse03.pdf")) # ------------------------------------ # Plot averaged per season and fueltype # ------------------------------------ if plot_crit['plot_averaged_season_fueltype']: for year in results_container['av_season_daytype_cy'].keys(): for fueltype_int in results_container['av_season_daytype_cy'][ year].keys(): fueltype_str = tech_related.get_fueltype_str( lookups['fueltypes'], fueltype_int) fig_load_profile_dh_multiple.run( path_fig_folder=result_paths['data_results_PDF'], path_plot_fig=os.path.join( result_paths['data_results_PDF'], 'season_daytypes_by_cy_comparison__{}__{}.pdf'.format( year, fueltype_str)), calc_av_lp_modelled=results_container[ 'av_season_daytype_cy'][year] [fueltype_int], # current year calc_av_lp_real=results_container['av_season_daytype_cy'] [base_yr][fueltype_int], # base year calc_lp_modelled=results_container['season_daytype_cy'] [year][fueltype_int], # current year calc_lp_real=results_container['season_daytype_cy'] [base_yr][fueltype_int], # base year plot_peak=True, plot_all_entries=False, plot_max_min_polygon=True, plotshow=False, plot_radar=plot_crit['plot_radar_seasonal'], max_y_to_plot=120, fueltype_str=fueltype_str, year=year) # --------------------------------- # Plot hourly peak loads over time for different fueltypes # -------------------------------- if plot_crit['plot_h_peak_fueltypes']: fig_fuels_peak_h.run( results_container['ed_fueltype_regs_yh'], lookups, os.path.join(result_paths['data_results_PDF'], 'fuel_fueltypes_peak_h.pdf')) print("finisthed plotting") return