Example #1
0
def main(path_data_ed, path_shapefile_input, path_out_plots, plot_crit_dict):
    """Figure II plots
    """

    # ---------------------------------------------------------
    # Iterate folders and read out all weather years and stations
    # ---------------------------------------------------------
    all_result_folders = os.listdir(path_data_ed)
    paths_folders_result = []
    weather_yrs = []
    weather_station_per_y = {}
    all_calculated_yrs_paths = []
    for result_folder in all_result_folders:
        try:
            split_path_name = result_folder.split("__")
            weather_yr = int(split_path_name[0])
            weather_yrs.append(weather_yr)
            try:
                weather_station = int(split_path_name[1])
            except:
                weather_station = "all_stations"
            try:
                weather_station_per_y[weather_yr].append(weather_station)
            except:
                weather_station_per_y[weather_yr] = [weather_station]

            # Collect all paths to simulation result folders
            paths_folders_result.append(
                os.path.join(path_data_ed, result_folder))

            tupyle_yr_path = (weather_yr,
                              os.path.join(path_data_ed, result_folder))
            all_calculated_yrs_paths.append(tupyle_yr_path)

        except ValueError:
            pass

    # -----------
    # Used across different plots
    # -----------
    data = {}
    data['lookups'] = lookup_tables.basic_lookups()

    data['enduses'], data['assumptions'], data[
        'regions'] = data_loader.load_ini_param(os.path.join(path_data_ed))

    data['assumptions']['seasons'] = date_prop.get_season(year_to_model=2015)
    data['assumptions']['model_yeardays_daytype'], data['assumptions'][
        'yeardays_month'], data['assumptions'][
            'yeardays_month_days'] = date_prop.get_yeardays_daytype(
                year_to_model=2015)

    population_data = read_data.read_scenaric_population_data(
        os.path.join(path_data_ed, 'model_run_pop'))

    ####################################################################
    # Plotting weather variability results for all weather stations (Fig 2b)
    ####################################################################
    weather_yr_container = defaultdict(dict)

    for weather_yr, result_folder in all_calculated_yrs_paths:
        results_container = read_data.read_in_results(
            os.path.join(result_folder, 'model_run_results_txt'),
            data['assumptions']['seasons'],
            data['assumptions']['model_yeardays_daytype'])

        weather_yr_container['tot_fueltype_yh'][
            weather_yr] = results_container[
                'tot_fueltype_yh']  #tot_fueltype_yh
        weather_yr_container['results_enduse_every_year'][
            weather_yr] = results_container['ed_fueltype_regs_yh']

    # ####################################################################
    # Create plot with regional and non-regional plots for second paper
    # Compare hdd calculations and disaggregation of regional and local
    # ####################################################################
    if plot_crit_dict['plot_scenarios_sorted']:

        fig_p2_annual_hours_sorted.run(
            data_input=weather_yr_container['results_enduse_every_year'],
            regions=data['regions'],
            simulation_yrs_to_plot=[2015],  # Simulation year to plot
            fueltype_str='electricity',
            path_shapefile=path_shapefile_input,
            fig_name=os.path.join(path_out_plots,
                                  "fig_paper_IIb_weather_var_map.pdf"))
Example #2
0
def process_scenarios(path_to_scenarios, year_to_model=2015):
    """Iterate folder with scenario results and plot charts

    Arguments
    ----------
    path_to_scenarios : str
        Path to folders with stored results
    year_to_model : int, default=2015
        Year of base year
    """
    # -----------
    # Charts to plot
    # -----------
    heat_pump_range_plot = True        # Plot of changing scenario values stored in scenario name

    # Delete folder results if existing
    path_result_folder = os.path.join(
        path_to_scenarios, "__results_hp_chart")

    basic_functions.delete_folder(path_result_folder)

    seasons = date_prop.get_season(
        year_to_model=year_to_model)

    model_yeardays_daytype, _, _ = date_prop.get_yeardays_daytype(
        year_to_model=year_to_model)

    lookups = lookup_tables.basic_lookups()

    # Get all folders with scenario run results (name of folder is scenario)
    scenarios_hp = os.listdir(path_to_scenarios)

    scenario_data = {}

    for scenario_hp in scenarios_hp:
        print("HP SCENARIO " + str(scenario_hp))
        print(path_to_scenarios)
        scenario_data[scenario_hp] = {}

        # Simulation information is read in from .ini file for results
        path_fist_scenario = os.path.join(path_to_scenarios, scenario_hp)

        # -------------------------------
        # Iterate folders and get results
        # -------------------------------
        scenarios = os.listdir(path_fist_scenario)

        for scenario in scenarios:

            enduses, assumptions, reg_nrs, regions = data_loader.load_ini_param(
                os.path.join(path_fist_scenario, scenario))

            # Add scenario name to folder
            scenario_data[scenario_hp][scenario] = {}

            path_to_result_files = os.path.join(
                path_fist_scenario,
                scenario,
                'model_run_results_txt')

            scenario_data[scenario_hp][scenario] = read_data.read_in_results(
                path_result=path_to_result_files,
                seasons=seasons,
                model_yeardays_daytype=model_yeardays_daytype)

    # -----------------------
    # Generate result folder
    # -----------------------
    basic_functions.create_folder(path_result_folder)

    # -------------------------------
    # Generate plot with heat pump ranges
    # -------------------------------
    if heat_pump_range_plot:

        plotting_multiple_scenarios.plot_heat_pump_chart_multiple(
            lookups,
            regions,
            hp_scenario_data=scenario_data,
            fig_name=os.path.join(path_result_folder, "comparison_hp_share_peak_h.pdf"),
            txt_name=os.path.join(path_result_folder, "comparison_hp_share_peak_h.txt"),
            fueltype_str_input='electricity',
            plotshow=True)

    return
Example #3
0
def plot_fig_spatio_temporal_validation(path_regional_calculations,
                                        path_rolling_elec_demand,
                                        path_temporal_elec_validation,
                                        path_temporal_gas_validation,
                                        path_non_regional_elec_2015,
                                        path_out_plots,
                                        plot_show=False):
    """
    Create plot with regional and non-regional plots for second paper
    Compare hdd calculations and disaggregation of regional and local
    """
    # ---------------------------------------------------------
    # Iterate folders and read out all weather years and stations
    # ---------------------------------------------------------
    all_result_folders = os.listdir(path_regional_calculations)

    paths_folders_result = []
    data_container = defaultdict(dict)
    ed_fueltype_regs_yh = defaultdict(dict)
    weather_yr_station_tot_fueltype_yh = defaultdict(dict)
    residential_results = defaultdict(dict)
    for scenario_folder in all_result_folders:
        result_folders = os.listdir(
            os.path.join(path_regional_calculations, scenario_folder))
        for result_folder in result_folders:
            try:
                split_path_name = result_folder.split("__")
                weather_yr = int(split_path_name[0])

                try:
                    weather_station = int(split_path_name[1])
                except:
                    weather_station = "all_stations"

                paths_folders_result.append(
                    os.path.join(path_regional_calculations, result_folder))

                data = {}
                data['lookups'] = lookup_tables.basic_lookups()
                data['enduses'], data['assumptions'], data[
                    'regions'] = data_loader.load_ini_param(
                        os.path.join(
                            path_regional_calculations,
                            all_result_folders[0]))  # last result folder
                data['assumptions']['seasons'] = date_prop.get_season(
                    year_to_model=2015)
                data['assumptions']['model_yeardays_daytype'], data[
                    'assumptions']['yeardays_month'], data['assumptions'][
                        'yeardays_month_days'] = date_prop.get_yeardays_daytype(
                            year_to_model=2015)

                results_container = read_data.read_in_results(
                    os.path.join(path_regional_calculations, scenario_folder,
                                 "{}__{}".format(weather_yr, weather_station),
                                 'model_run_results_txt'),
                    data['assumptions']['seasons'],
                    data['assumptions']['model_yeardays_daytype'])

                weather_yr_station_tot_fueltype_yh[weather_yr][
                    weather_station] = results_container['tot_fueltype_yh']
                ed_fueltype_regs_yh[weather_yr][
                    weather_station] = results_container['ed_fueltype_regs_yh']
                residential_results[weather_yr][
                    weather_station] = results_container['residential_results']
            except ValueError:
                pass

    data_container['ed_fueltype_regs_yh'] = ed_fueltype_regs_yh
    data_container['tot_fueltype_yh'] = weather_yr_station_tot_fueltype_yh
    data_container['residential_results'] = residential_results
    data_container = dict(data_container)

    # -------------------------------------------------
    # Collect non regional 2015 elec data
    # Calculated with all regional weather stations
    # -------------------------------------------------
    year_non_regional = 2015
    path_with_txt = os.path.join(
        path_non_regional_elec_2015, "{}__{}".format(str(year_non_regional),
                                                     "all_stations"),
        'model_run_results_txt')

    demand_year_non_regional = read_data.read_in_results(
        path_with_txt, data['assumptions']['seasons'],
        data['assumptions']['model_yeardays_daytype'])
    tot_fueltype_yh = demand_year_non_regional['tot_fueltype_yh']

    fueltype_int = tech_related.get_fueltype_int('electricity')

    non_regional_elec_2015 = tot_fueltype_yh[year_non_regional][fueltype_int]

    # ---Collect real electricity data of year 2015
    elec_2015_indo, _ = elec_national_data.read_raw_elec_2015(
        path_rolling_elec_demand)

    # Factor data as total sum is not identical
    f_diff_elec = np.sum(non_regional_elec_2015) / np.sum(elec_2015_indo)
    elec_factored_yh = f_diff_elec * elec_2015_indo

    # *****************************************************************
    # Temporal validation
    # Compare regional and non regional and actual demand over time
    # *****************************************************************
    simulation_yr_to_plot = 2015

    winter_week, spring_week, summer_week, autumn_week = date_prop.get_seasonal_weeks(
    )

    # Peak day
    peak_day, _ = enduse_func.get_peak_day_single_fueltype(elec_factored_yh)

    # Convert days to hours
    period_to_plot = list(range(0, 400))
    period_to_plot = date_prop.get_8760_hrs_from_yeardays(winter_week)
    period_to_plot = date_prop.get_8760_hrs_from_yeardays([peak_day])
    period_to_plot_winter = date_prop.get_8760_hrs_from_yeardays(winter_week)
    period_to_plot_spring = date_prop.get_8760_hrs_from_yeardays(spring_week)

    fig_p2_temporal_validation.run_fig_p2_temporal_validation(
        data_input=data_container['tot_fueltype_yh'],
        weather_yr=2015,
        fueltype_str='electricity',
        simulation_yr_to_plot=simulation_yr_to_plot,  # Simulation year to plot
        period_h=period_to_plot,
        validation_elec_2015=elec_factored_yh,
        non_regional_elec_2015=non_regional_elec_2015,
        fig_name=os.path.join(path_out_plots, "temporal_validation_elec.pdf"),
        titel="yearday: {}".format(peak_day),
        y_lim_val=55,
        plot_validation=False,
        plot_show=plot_show)

    fueltype_gas = tech_related.get_fueltype_int('gas')
    fig_p2_temporal_validation.run_fig_p2_temporal_validation(
        data_input=data_container['tot_fueltype_yh'],
        weather_yr=2015,
        fueltype_str='gas',
        simulation_yr_to_plot=simulation_yr_to_plot,  # Simulation year to plot
        period_h=period_to_plot,
        validation_elec_2015=None,
        non_regional_elec_2015=tot_fueltype_yh[year_non_regional]
        [fueltype_gas],
        fig_name=os.path.join(path_out_plots, "temporal_validation_gas.pdf"),
        titel="yearday: {}".format(peak_day),
        y_lim_val=250,
        plot_validation=False,
        plot_show=plot_show)

    # -------------------
    # Spatial validation (not with maps)
    # -------------------
    # non_regional: All weather station, spatially disaggregated TODO Give BETTER NAMES
    # regional: Only one weather station for whole countr but still data for every region
    weather_yr = 2015
    fig_p2_spatial_val.run(
        simulation_yr_to_plot=simulation_yr_to_plot,
        demand_year_non_regional=demand_year_non_regional[
            'residential_results'][weather_yr],
        demand_year_regional=data_container['residential_results'][weather_yr],
        fueltypes=data['lookups']['fueltypes'],
        fig_path=path_out_plots,
        path_temporal_elec_validation=path_temporal_elec_validation,
        path_temporal_gas_validation=path_temporal_gas_validation,
        regions=data['regions'],
        plot_crit=plot_show)
Example #4
0
def main(path_data_energy_demand, path_shapefile_input):
    """Read in all results and plot PDFs

    Arguments
    ----------
    path_data_energy_demand : str
        Path to results
    path_shapefile_input : str
        Path to shapefile
    """
    print("Start processing")
    # ---------
    # Criterias
    # ---------
    write_shapefiles = False  # Write shapefiles
    spatial_results = True  # Spatial geopanda maps

    # Set up logger
    logger_setup.set_up_logger(
        os.path.join(path_data_energy_demand, "plotting.log"))

    # ------------------
    # Load necessary inputs for read in
    # ------------------
    data = {}
    data['local_paths'] = data_loader.load_local_paths(path_data_energy_demand)
    data['result_paths'] = data_loader.load_result_paths(
        os.path.join(path_data_energy_demand, '_result_data'))

    data['lookups'] = lookup_tables.basic_lookups()

    # ---------------
    # Folder cleaning
    # ---------------
    basic_functions.del_previous_setup(
        data['result_paths']['data_results_PDF'])
    basic_functions.del_previous_setup(
        data['result_paths']['data_results_shapefiles'])
    basic_functions.create_folder(data['result_paths']['data_results_PDF'])
    basic_functions.create_folder(
        data['result_paths']['data_results_shapefiles'])

    # Simulation information is read in from .ini file for results
    data['enduses'], data['assumptions'], data['reg_nrs'], data[
        'regions'] = data_loader.load_ini_param(
            os.path.join(path_data_energy_demand, '_result_data'))

    # Other information is read in
    data['assumptions']['seasons'] = date_prop.get_season(year_to_model=2015)
    data['assumptions']['model_yeardays_daytype'], data['assumptions'][
        'yeardays_month'], data['assumptions'][
            'yeardays_month_days'] = date_prop.get_model_yeardays_daytype(
                year_to_model=2015)

    # Read scenario data
    data['scenario_data'] = {}

    data['scenario_data'][
        'population'] = read_data.read_scenaric_population_data(
            data['result_paths']['model_run_pop'])

    # --------------------------------------------
    # Reading in results from different model runs
    # Read in and plot in same step if memory is a problem
    # --------------------------------------------
    results_container = read_data.read_in_results(
        data['result_paths']['data_results_model_runs'],
        data['assumptions']['seasons'],
        data['assumptions']['model_yeardays_daytype'])

    # ----------------
    # Write results to CSV files and merge with shapefile
    # ----------------
    if write_shapefiles:
        write_data.create_shp_results(data, results_container,
                                      data['local_paths'], data['lookups'],
                                      data['regions'])

    # ------------------------------
    # Plotting other results
    # ------------------------------
    plotting_results.run_all_plot_functions(results_container, data['reg_nrs'],
                                            data['regions'], data['lookups'],
                                            data['result_paths'],
                                            data['assumptions'],
                                            data['enduses'])

    # ------------------------------
    # Plotting spatial results
    # ------------------------------
    print("... plotting spatial results")
    if spatial_results:
        logging.info("Create spatial geopanda files")
        result_mapping.create_geopanda_files(
            data, results_container,
            data['result_paths']['data_results_shapefiles'], data['regions'],
            data['lookups']['fueltypes_nr'], data['lookups']['fueltypes'],
            path_shapefile_input)

    print("===================================")
    print("... finished reading and plotting results")
    print("===================================")
Example #5
0
def process_scenarios(path_to_scenarios, year_to_model=2015):
    """Iterate folder with scenario results and plot charts

    Arguments
    ----------
    path_to_scenarios : str
        Path to folders with stored results
    year_to_model : int, default=2015
        Year of base year
    """
    # -----------
    # Charts to plot
    # -----------
    heat_pump_range_plot = False        # Plot of changing scenario values stored in scenario name
    plot_multiple_cross_charts = True   # Compare cross charts of different scenario
    comparison_year = 2050
    year_to_plot = 2050

    # Delete folder results if existing
    path_result_folder = os.path.join(
        path_to_scenarios, "__results_multiple_scenarios")

    basic_functions.delete_folder(path_result_folder)

    seasons = date_prop.get_season(
        year_to_model=year_to_model)

    model_yeardays_daytype, _, _ = date_prop.get_yeardays_daytype(
        year_to_model=year_to_model)

    lookups = lookup_tables.basic_lookups()

    # Get all folders with scenario run results (name of folder is scenario)
    scenarios = os.listdir(path_to_scenarios)

    # Simulation information is read in from .ini file for results
    path_fist_scenario = os.path.join(path_to_scenarios, scenarios[0])
    enduses, assumptions, regions = data_loader.load_ini_param(
        path_fist_scenario)

    # -------------------------------
    # Iterate folders and get results
    # -------------------------------
    scenario_data = {}
    for scenario in scenarios:
        scenario_data[scenario] = {}

        all_stations = os.listdir(
            os.path.join(path_to_scenarios, scenario))

        _to_igore = [
            'infoparam.txt',
            'model_run_pop',
            'PDF_validation', 'model_run_sim_param.ini']

        for station in all_stations:
            if station not in _to_igore:

                path_to_result_files = os.path.join(
                    path_to_scenarios,
                    scenario,
                    station,
                    'model_run_results_txt')

                scenario_data[scenario] = read_data.read_in_results(
                    path_result=path_to_result_files,
                    seasons=seasons,
                    model_yeardays_daytype=model_yeardays_daytype)
            else:
                pass

    # Create result folder
    basic_functions.create_folder(path_result_folder)

    # -------------------------------
    # Generate plot with heat pump ranges
    # -------------------------------
    if heat_pump_range_plot:

        plotting_multiple_scenarios.plot_heat_pump_chart(
            lookups,
            regions,
            scenario_data,
            fig_name=os.path.join(path_result_folder, "comparison_hp_service_switch_and_lf.pdf"),
            fueltype_str_input='electricity',
            plotshow=True)

    # -------------------------------
    # Compare cross charts for different scenario
    # Ideally only compare two scenario
    # -------------------------------
    if plot_multiple_cross_charts:
        fig_cross_graphs.plot_cross_graphs_scenarios(
            base_yr=2015,
            comparison_year=comparison_year,
            regions=regions,
            scenario_data=scenario_data,
            fueltype_int=lookups['fueltypes']['electricity'],
            fueltype_str='electricity',
            fig_name=os.path.join(path_result_folder, "cross_chart_electricity.pdf"),
            label_points=False,
            plotshow=False)

        fig_cross_graphs.plot_cross_graphs_scenarios(
            base_yr=2015,
            comparison_year=comparison_year,
            regions=regions,
            scenario_data=scenario_data,
            fueltype_int=lookups['fueltypes']['gas'],
            fueltype_str='gas',
            fig_name=os.path.join(path_result_folder, "cross_chart_gas.pdf"),
            label_points=False,
            plotshow=False)

    # -------------------------------
    # Plot total demand for every year in line plot
    # -------------------------------
    plotting_multiple_scenarios.plot_tot_fueltype_y_over_time(
        scenario_data,
        lookups['fueltypes'],
        fueltypes_to_plot=['electricity', 'gas'],
        fig_name=os.path.join(path_result_folder, "tot_y_multiple_fueltypes.pdf"),
        txt_name=os.path.join(path_result_folder, "tot_y_multiple_fueltypes.txt"),
        plotshow=False)

    plotting_multiple_scenarios.plot_tot_y_over_time(
        scenario_data,
        fig_name=os.path.join(path_result_folder, "tot_y_multiple.pdf"),
        plotshow=False)

    # -------------------------------
    # Plot for all regions demand for every year in line plot
    # -------------------------------
    plotting_multiple_scenarios.plot_reg_y_over_time(
        scenario_data,
        fig_name=os.path.join(path_result_folder, "reg_y_multiple.pdf"),
        plotshow=False)

    # -------------------------------
    # Plot comparison of total demand for a year for all LADs (scatter plot)
    # -------------------------------
    plotting_multiple_scenarios.plot_LAD_comparison_scenarios(
        scenario_data,
        year_to_plot=year_to_plot,
        fig_name=os.path.join(path_result_folder, "LAD_multiple.pdf"),
        plotshow=False)

    # -------------------------------
    # Plot different profiles in radar plot (spider plot)
    # -------------------------------
    plotting_multiple_scenarios.plot_radar_plots_average_peak_day(
        scenario_data,
        fueltype_to_model='electricity',
        fueltypes=lookups['fueltypes'],
        year_to_plot=year_to_plot,
        fig_name=os.path.join(path_result_folder))

    plotting_multiple_scenarios.plot_radar_plots_average_peak_day(
        scenario_data,
        fueltype_to_model='gas',
        fueltypes=lookups['fueltypes'],
        year_to_plot=year_to_plot,
        fig_name=os.path.join(path_result_folder))

    # ----------------------
    # Plot peak hour of all fueltypes for different scenario
    # ----------------------
    plotting_multiple_scenarios.plot_tot_y_peak_hour(
        scenario_data,
        fig_name=os.path.join(path_result_folder, "tot_y_peak_h_electricity.pdf"),
        fueltype_str_input='electricity',
        plotshow=False)

    plotting_multiple_scenarios.plot_tot_y_peak_hour(
        scenario_data,
        fig_name=os.path.join(path_result_folder, "tot_y_peak_h_gas.pdf"),
        fueltype_str_input='gas',
        plotshow=False)
    print("Finished processing multiple scenario")
    return
def run_fig_spatial_distribution_of_peak(
        scenarios,
        path_to_folder_with_scenarios,
        path_shapefile,
        sim_yrs,
        field_to_plot,
        unit,
        fig_path,
        fueltype_str='electricity'
    ):
    """
    """
    weather_yrs = []
    calculated_yrs_paths = []
    fueltype_int = tech_related.get_fueltype_int(fueltype_str)

    for scenario in scenarios:
        path_scenario = os.path.join(path_to_folder_with_scenarios, scenario)
        all_result_folders = os.listdir(path_scenario)

        for result_folder in all_result_folders:
            try:
                split_path_name = result_folder.split("__")
                weather_yr = int(split_path_name[0])
                weather_yrs.append(weather_yr)
                tupyle_yr_path = (weather_yr, os.path.join(path_scenario))
                calculated_yrs_paths.append(tupyle_yr_path)
            except ValueError:
                pass

    for simulation_yr in sim_yrs:
        container = {}
        container['abs_demand_in_peak_h_pp'] = {}
        container['abs_demand_in_peak_h'] = {}
        container['p_demand_in_peak_h'] = {}

        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, "{}__{}".format(weather_yr, 'all_stations'))

            data = {}
            data['lookups'] = lookup_tables.basic_lookups()
            data['enduses'], data['assumptions'], regions = data_loader.load_ini_param(os.path.join(path_data_ed))
            data['assumptions']['seasons'] = date_prop.get_season(year_to_model=2015)
            data['assumptions']['model_yeardays_daytype'], data['assumptions']['yeardays_month'], data['assumptions']['yeardays_month_days'] = date_prop.get_yeardays_daytype(year_to_model=2015)

            # Population 
            population_data = read_data.read_scenaric_population_data(os.path.join(path_data_ed, 'model_run_pop'))

            results_container = read_data.read_in_results(
                os.path.join(path_to_weather_yr, 'model_run_results_txt'),
                data['assumptions']['seasons'],
                data['assumptions']['model_yeardays_daytype'])

            # ---------------------------------------------------
            # Calculate hour with national peak demand
            # This may be different depending on the weather yr
            # ---------------------------------------------------
            ele_regions_8760 = results_container['ed_fueltype_regs_yh'][simulation_yr][fueltype_int]
            sum_all_regs_fueltype_8760 = np.sum(ele_regions_8760, axis=0) # Sum for every hour

            max_day = int(basic_functions.round_down((np.argmax(sum_all_regs_fueltype_8760) / 24), 1))
            max_h = np.argmax(sum_all_regs_fueltype_8760)
            max_demand = np.max(sum_all_regs_fueltype_8760)

            # Calculate the national peak demand in GW
            national_peak_GW = np.max(sum_all_regs_fueltype_8760)

            # ------------------------------------------------------
            # Calculate the contribution of the regional peak demand
            # ------------------------------------------------------
            demand_in_peak_h = ele_regions_8760[:, max_h]

            if unit == 'GW':
                container['abs_demand_in_peak_h'][weather_yr] = demand_in_peak_h
            elif unit == 'kW':
                container['abs_demand_in_peak_h'][weather_yr] = demand_in_peak_h * 1000000 # Convert to KWh
            else:
                # Use GW as default
                container['abs_demand_in_peak_h'][weather_yr] = demand_in_peak_h

            container['abs_demand_in_peak_h_pp'][weather_yr] = demand_in_peak_h / population_data[simulation_yr]
    
            # Relative fraction of regional demand in relation to peak
            container['p_demand_in_peak_h'][weather_yr] = (demand_in_peak_h / national_peak_GW ) * 100 # given as percent

            print("=================================")
            print("{}  {}  {}  {}".format(
                simulation_yr,
                weather_yr,
                np.sum(ele_regions_8760),
                national_peak_GW))

        # --------------
        # Create dataframe with all weather yrs calculatiosn for every region
            
        #               region1, region2, region3
        # weather yr1
        # weather yr2
        # --------------
        # Convert regional data to dataframe
        abs_demand_in_peak_h_pp = np.array(list(container['abs_demand_in_peak_h_pp'].values()))
        abs_demand_peak_h = np.array(list(container['abs_demand_in_peak_h'].values()))
        p_demand_peak_h = np.array(list(container['p_demand_in_peak_h'].values()))

        # Absolute demand
        df_abs_peak_demand = pd.DataFrame(
            abs_demand_peak_h,
            columns=regions,
            index=list(container['abs_demand_in_peak_h'].keys()))

        # Relative demand
        df_p_peak_demand = pd.DataFrame(
            p_demand_peak_h,
            columns=regions,
            index=list(container['p_demand_in_peak_h'].keys()))
        
        df_abs_demand_in_peak_h_pp = pd.DataFrame(
            abs_demand_in_peak_h_pp,
            columns=regions,
            index=list(container['abs_demand_in_peak_h_pp'].keys()))

        # Absolute peak value - mean
        max_peak_h_across_weather_yrs = df_abs_peak_demand.max()
        average_across_weather_yrs = df_abs_peak_demand.mean()
        diff_peak_h_minus_mean = max_peak_h_across_weather_yrs - average_across_weather_yrs

        for index, row in df_p_peak_demand.iterrows():
            print("Weather yr: {} Total p: {}".format(index, np.sum(row)))
            assert round(np.sum(row), 4) == 100.0

        # ----------------------------
        # Calculate standard deviation
        # ----------------------------
        std_deviation_df_abs_demand_in_peak_h_pp = df_abs_demand_in_peak_h_pp.std()
        std_deviation_abs_demand_peak_h = df_abs_peak_demand.std()
        std_deviation_p_demand_peak_h = df_p_peak_demand.std()

        print("=========")
        print("National stats")
        print("=========")
        print("Sum of std of absolut peak demand:  " + str(np.sum(std_deviation_abs_demand_peak_h)))

        # --------------------
        # Create map
        # --------------------
        regional_statistics_columns = [
            'name',
            'std_deviation_p_demand_peak_h',
            'std_deviation_abs_demand_peak_h',
            'std_deviation_df_abs_demand_in_peak_h_pp',
            'diff_peak_h_minus_mean']

        df_stats = pd.DataFrame(columns=regional_statistics_columns)

        for region_name in regions:

            # 'name', 'absolute_GW', 'p_GW_peak'
            line_entry = [[
                region_name,
                std_deviation_p_demand_peak_h[region_name],
                std_deviation_abs_demand_peak_h[region_name],
                std_deviation_df_abs_demand_in_peak_h_pp[region_name],
                diff_peak_h_minus_mean[region_name],
                ]]

            line_df = pd.DataFrame(line_entry, columns=regional_statistics_columns)
            df_stats = df_stats.append(line_df)

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

        # Merge stats to geopanda
        shp_gdp_merged = uk_shapefile.merge(
            df_stats,
            on='name')

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

        ax = uk_gdf.plot(
            figsize=basic_plot_functions.cm2inch(25, 20))

        nr_of_intervals = 6

        bin_values = result_mapping.get_reasonable_bin_values_II(
            data_to_plot=list(uk_gdf[field_to_plot]),
            nr_of_intervals=nr_of_intervals)
        # Maual bins
        #bin_values = [0, 0.005, 0.01, 0.015, 0.02, 0.025, 0.03]
        print(float(uk_gdf[field_to_plot].max()))
        print("BINS " + str(bin_values))

        uk_gdf, cmap_rgb_colors, color_zero, min_value, max_value = fig_p2_weather_val.user_defined_bin_classification(
            uk_gdf,
            field_to_plot,
            bin_values=bin_values)

        # plot with face color attribute
        uk_gdf.plot(
            ax=ax,
            facecolor=uk_gdf['bin_color'],
            edgecolor='black',
            linewidth=0.5)

        legend_handles = result_mapping.add_simple_legend(
            bin_values,
            cmap_rgb_colors,
            color_zero)

        plt.legend(
            handles=legend_handles,
            title="{}  [{}]".format(field_to_plot, unit),
            prop={'size': 8},
            #loc='upper center', bbox_to_anchor=(0.5, -0.05),
            loc='center left', bbox_to_anchor=(1, 0.5),
            frameon=False)

        # PLot bins on plot
        '''plt.text(
            -20,
            -20,
            bin_values[:-1], #leave away maximum value
            fontsize=8)'''

        plt.tight_layout()

        fig_out_path = os.path.join(fig_path, str(field_to_plot) + "__" + str(simulation_yr) + ".pdf")
        plt.savefig(fig_out_path)
Example #7
0
def process_scenarios(path_to_scenarios, year_to_model=2015):
    """Iterate folder with scenario results and plot charts

    Arguments
    ----------
    path_to_scenarios : str
        Path to folders with stored results
    year_to_model : int, default=2015
        Year of base year
    """
    # Delete folder results if existing
    path_result_folder = os.path.join(
        path_to_scenarios, "_results_multiple_scenarios")

    basic_functions.delete_folder(path_result_folder)

    seasons = date_prop.get_season(
        year_to_model=year_to_model)

    model_yeardays_daytype, _, _ = date_prop.get_model_yeardays_daytype(
        year_to_model=year_to_model)

    # Get all folders with scenario run results (name of folder is scenario)
    scenarios = os.listdir(path_to_scenarios)

    # -------------------------------
    # Iterate folders and get results
    # -------------------------------
    scenario_data = {}
    for scenario in scenarios:

        # Add scenario name to folder
        scenario_data[scenario] = {}

        path_to_result_files = os.path.join(
            path_to_scenarios,
            scenario,
            '_result_data',
            'model_run_results_txt')

        scenario_data[scenario] = read_data.read_in_results(
            path_runs=path_to_result_files,
            seasons=seasons,
            model_yeardays_daytype=model_yeardays_daytype)

    # -----------------------
    # Generate result folder
    # -----------------------
    basic_functions.create_folder(path_result_folder)

    # ------------
    # Create plots
    # ------------
    # Plot total demand for every year in line plot
    plotting_multiple_scenarios.plot_tot_y_over_time(
        scenario_data,
        fig_name=os.path.join(path_result_folder, "tot_y_multiple.pdf"),
        plotshow=False)

    # Plot for all regions demand for every year in line plot
    plotting_multiple_scenarios.plot_reg_y_over_time(
        scenario_data,
        fig_name=os.path.join(path_result_folder, "reg_y_multiple.pdf"),
        plotshow=False)

    # Plot comparison of total demand for a year for all LADs (scatter plot)
    plotting_multiple_scenarios.plot_LAD_comparison_scenarios(
        scenario_data,
        year_to_plot=2050,
        fig_name=os.path.join(path_result_folder, "LAD_multiple.pdf"),
        plotshow=False)

    # Plot different profiels in radar plot
    plotting_multiple_scenarios.plot_radar_plots_average_peak_day(
        scenario_data,
        year_to_plot=2050,
        fig_name=os.path.join(path_result_folder),
        plotshow=False)

    logging.info("Finished processing multiple scenario")
    return
def main(path_data_ed, path_shapefile_input, plot_crit_dict, base_yr,
         comparison_year):
    """Read in all results and plot PDFs

    Arguments
    ----------
    path_data_ed : str
        Path to results
    path_shapefile_input : str
        Path to shapefile
    plot_crit_dict : dict
        Criteria to select plots to plot
    base_yr : int
        Base year
    comparison_year : int
        Year to generate comparison plots
    """
    print("...Start creating plots")
    data = {}

    # ---------------------------------------------------------
    # Iterate folders and read out all weather years and stations
    # ---------------------------------------------------------
    to_ignores = ['model_run_pop', 'PDF_validation']

    endings_to_ignore = ['.pdf', '.txt', '.ini']

    all_result_folders = os.listdir(path_data_ed)
    paths_folders_result = []

    for result_folder in all_result_folders:
        if result_folder not in to_ignores and result_folder[
                -4:] not in endings_to_ignore:
            paths_folders_result.append(
                os.path.join(path_data_ed, result_folder))

    ####################################################################
    # Calculate results for every weather year
    ####################################################################
    for path_result_folder in paths_folders_result:

        print("-----------------------")
        print("path_result_folder: " + str(path_result_folder))
        print("-----------------------")
        # Simulation information is read in from .ini file for results
        data['enduses'], data['assumptions'], data[
            'regions'] = data_loader.load_ini_param(os.path.join(path_data_ed))

        # ------------------
        # Load necessary inputs for read in
        # ------------------
        data = {}
        data['local_paths'] = data_loader.get_local_paths(path_result_folder)
        data['result_paths'] = basic_functions.get_result_paths(
            os.path.join(path_result_folder))
        data['lookups'] = lookup_tables.basic_lookups()

        # ---------------
        # Folder cleaning
        # ---------------
        basic_functions.del_previous_setup(
            data['result_paths']['data_results_PDF'])
        basic_functions.del_previous_setup(
            data['result_paths']['data_results_shapefiles'])
        basic_functions.create_folder(data['result_paths']['data_results_PDF'])
        basic_functions.create_folder(
            data['result_paths']['data_results_shapefiles'])
        basic_functions.create_folder(
            data['result_paths']['individual_enduse_lp'])

        # Simulation information is read in from .ini file for results
        data['enduses'], data['assumptions'], data[
            'regions'] = data_loader.load_ini_param(os.path.join(path_data_ed))

        # Other information is read in
        data['assumptions']['seasons'] = date_prop.get_season(
            year_to_model=2015)
        data['assumptions']['model_yeardays_daytype'], data['assumptions'][
            'yeardays_month'], data['assumptions'][
                'yeardays_month_days'] = date_prop.get_yeardays_daytype(
                    year_to_model=2015)

        data['scenario_data'] = {}

        data['scenario_data'][
            'population'] = read_data.read_scenaric_population_data(
                os.path.join(path_data_ed, 'model_run_pop'))

        # --------------------------------------------
        # Reading in results from different model runs
        # --------------------------------------------
        results_container = read_data.read_in_results(
            data['result_paths']['data_results_model_run_results_txt'],
            data['assumptions']['seasons'],
            data['assumptions']['model_yeardays_daytype'])

        # ------------------------------
        # Plotting other results
        # ------------------------------
        plotting_results.run_all_plot_functions(
            results_container,
            data['assumptions']['reg_nrs'],
            data['regions'],
            data['lookups'],
            data['result_paths'],
            data['assumptions'],
            data['enduses'],
            plot_crit=plot_crit_dict,
            base_yr=base_yr,
            comparison_year=comparison_year)

        # ------------------------------
        # Plotting spatial results
        # ------------------------------
        if plot_crit_dict['spatial_results']:
            result_mapping.spatial_maps(
                data,
                results_container,
                data['result_paths']['data_results_shapefiles'],
                data['regions'],
                data['lookups']['fueltypes_nr'],
                data['lookups']['fueltypes'],
                path_shapefile_input,
                plot_crit_dict,
                base_yr=base_yr)

    print("===================================")
    print("... finished reading and plotting results")
    print("===================================")