コード例 #1
0
ファイル: run.py プロジェクト: nismod/nismod2
    def create_folders_rename_folders(config):
        """Create scenario name and get paths

        Arguments
        ---------
        config : dict
            Configuration dictionary containing all the file paths
        """
        path_new_scenario = config['PATHS']['path_new_scenario']

        # ------------------------------
        # Delete previous model results and create result folders
        # ------------------------------
        result_paths = config['PATHS']['path_result_data']
        basic_functions.del_previous_setup(result_paths)
        basic_functions.create_folder(path_new_scenario)

        folders_to_create = [
            os.path.join(result_paths, 'model_run_pop'),
            os.path.join(result_paths, 'validation')
        ]

        for folder in folders_to_create:
            basic_functions.create_folder(folder)
コード例 #2
0
    path_strategy_vars = os.path.join(config['DATA_PATHS']['path_strategy_vars'], name_config_path)
    name_region_set = os.path.join(config['PATHS']['path_local_data'], 'region_definitions', "lad_2016_uk_simplified.shp")
    name_population_dataset = os.path.join(os.path.join(config['PATHS']['path_local_data'], ".."), 'scenarios', 'MISTRAL_pop_gva', 'data', '{}/population__lad.csv'.format(local_scenario))
    name_gva_dataset = os.path.join(os.path.join(config['PATHS']['path_local_data'], ".."),'scenarios', 'MISTRAL_pop_gva', 'data', '{}/gva_per_head__lad_sector.csv'.format(local_scenario))
    name_gva_dataset_per_head = os.path.join(os.path.join(config['PATHS']['path_local_data'], ".."), 'scenarios', 'MISTRAL_pop_gva', 'data', '{}/gva_per_head__lad.csv'.format(local_scenario))

    # --------------------
    # Create scenario path
    # --------------------
    name_scenario_run = "{}_result_local_{}".format(scenario_name, str(time.ctime()).replace(":", "_").replace(" ", "_"))
    path_new_scenario = os.path.join(config['PATHS']['path_result_data'], name_scenario_run)

    # -----------------------------------------------------------------------
    # Create new folders
    # -----------------------------------------------------------------------
    basic_functions.del_previous_setup(path_new_scenario)
    basic_functions.create_folder(path_new_scenario)

    # --------------------
    # Load all other paths
    # --------------------
    data['paths'] = config['CONFIG_DATA']
    data['local_paths'] = config['DATA_PATHS']
    data['result_paths'] = basic_functions.get_result_paths(path_new_scenario)

    for folder, folder_path in data['result_paths'].items():
        basic_functions.create_folder(folder_path)

    # ----------------------------------------------------------------------
    # Load data
    # ----------------------------------------------------------------------
コード例 #3
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("===================================")
コード例 #4
0
def post_install_setup(args):
    """Run this function after installing the energy_demand
    model with smif and putting the data folder with all necessary
    data into a local drive. This scripts only needs to be
    executed once after the energy_demand model has been installed

    Arguments
    ----------
    args : object
        Arguments defined in ``./cli/__init__.py``
    """
    print("... start running initialisation scripts")

    # Paths
    path_main = resource_filename(Requirement.parse("energy_demand"), "config_data")
    path_results = resource_filename(Requirement.parse("energy_demand"), "results")
    local_data_path = args.local_data

    # Initialise logger
    logger_setup.set_up_logger(
        os.path.join(local_data_path, "logging_post_install_setup.log"))
    logging.info("... start local energy demand calculations")

    # Load data
    base_yr = 2015

    data = {}
    data['paths'] = data_loader.load_paths(path_main)
    data['local_paths'] = data_loader.load_local_paths(local_data_path)
    data['result_paths'] = data_loader.load_result_paths(path_results)
    data['lookups'] = lookup_tables.basic_lookups()
    data['enduses'], data['sectors'], data['fuels'] = data_loader.load_fuels(
        data['paths'], data['lookups'])

    # Assumptions
    data['assumptions'] = non_param_assumptions.Assumptions(
        base_yr=base_yr,
        paths=data['paths'],
        enduses=data['enduses'],
        sectors=data['sectors'],
        fueltypes=data['lookups']['fueltypes'],
        fueltypes_nr=data['lookups']['fueltypes_nr'])

    # Delete all previous data from previous model runs
    basic_functions.del_previous_setup(data['local_paths']['data_processed'])
    basic_functions.del_previous_setup(data['result_paths']['data_results'])

    # Create folders and subfolder for data_processed
    basic_functions.create_folder(data['local_paths']['data_processed'])
    basic_functions.create_folder(data['local_paths']['path_post_installation_data'])
    basic_functions.create_folder(data['local_paths']['dir_raw_weather_data'])
    basic_functions.create_folder(data['local_paths']['dir_changed_weather_station_data'])
    basic_functions.create_folder(data['local_paths']['load_profiles'])
    basic_functions.create_folder(data['local_paths']['rs_load_profile_txt'])
    basic_functions.create_folder(data['local_paths']['ss_load_profile_txt'])
    basic_functions.create_folder(data['local_paths']['dir_disaggregated'])

    print("... Read in temperature data from raw files")
    s_raw_weather_data.run(
        data['local_paths'])

    print("... Read in service submodel load profiles")
    s_ss_raw_shapes.run(
        data['paths'],
        data['local_paths'],
        data['lookups'])

    print("... Read in residential submodel load profiles")
    s_rs_raw_shapes.run(
        data['paths'],
        data['local_paths'],
        base_yr)

    print("... successfully finished setup")
    return
コード例 #5
0
def post_install_setup(args):
    """Run this function after installing the energy_demand
    model with smif and putting the data folder with all necessary
    data into a local drive. This scripts only needs to be
    executed once after the energy_demand model has been installed

    Arguments
    ----------
    args : object
        Arguments defined in ``./cli/__init__.py``
    """
    print("... start running initialisation scripts", flush=True)

    path_config_file = args.local_data

    config = data_loader.read_config_file(path_config_file)
    local_data_path = config['PATHS']['path_local_data']

    path_results = resource_filename(Requirement.parse("energy_demand"),
                                     "results")
    local_data_path = args.local_data

    path_config = config['PATHS']['path_energy_demand_config']
    base_yr = config['CONFIG']['base_yr']

    data = {}
    data['paths'] = config['CONFIG_DATA']
    data['local_paths'] = config['DATA_PATHS']
    data['result_paths'] = config['RESULT_DATA']
    data['lookups'] = lookup_tables.basic_lookups()
    data['enduses'], data['sectors'], data['fuels'], lookup_enduses, \
        lookup_sector_enduses = data_loader.load_fuels(data['paths'])

    data['assumptions'] = general_assumptions.Assumptions(
        lookup_enduses=lookup_enduses,
        lookup_sector_enduses=lookup_sector_enduses,
        base_yr=base_yr,
        paths=data['paths'],
        enduses=data['enduses'],
        sectors=data['sectors'])

    # Delete all previous data from previous model runs
    basic_functions.del_previous_setup(data['local_paths']['data_processed'])
    basic_functions.del_previous_setup(data['result_paths']['data_results'])
    basic_functions.del_previous_setup(
        data['local_paths']['path_post_installation_data'])

    # Create folders and subfolder for data_processed
    folders_to_create = [
        data['local_paths']['data_processed'],
        data['local_paths']['path_post_installation_data'],
        data['local_paths']['load_profiles'],
        data['local_paths']['rs_load_profile_txt'],
        data['local_paths']['ss_load_profile_txt']
    ]

    for folder in folders_to_create:
        basic_functions.create_folder(folder)

    print("... Read in residential submodel load profiles", flush=True)
    s_rs_raw_shapes.run(data['paths'], data['local_paths'], base_yr)

    print("... Read in service submodel load profiles", flush=True)
    s_ss_raw_shapes.run(data['paths'], data['local_paths'], data['lookups'])

    # Input data preparation
    print("Generate additional data", flush=True)

    # Extract NISMOD population data
    path_to_zip_file = os.path.join(
        local_data_path, "population-economic-smif-csv-from-nismod-db.zip")
    path_extraction = os.path.join(local_data_path, 'scenarios',
                                   "MISTRAL_pop_gva")
    zip_ref = zipfile.ZipFile(path_to_zip_file, 'r')
    zip_ref.extractall(path_extraction)
    zip_ref.close()

    # Complete gva and pop data for every sector
    data_pop = os.path.join(local_data_path, "scenarios", "MISTRAL_pop_gva",
                            "data")
    path_geography = os.path.join(
        local_data_path, "scenarios",
        "uk_pop_principal_2015_2050_MSOA_england.csv")
    geography_name = "region"  # "lad_uk_2016"

    script_data_preparation_MISTRAL_pop_gva.run(
        path_to_folder=data_pop,
        path_MSOA_baseline=path_geography,
        MSOA_calculations=False,
        geography_name="region")  # "lad_uk_2016"

    print("... successfully finished setup")
    return
コード例 #6
0
def main(scenarios_path, path_shapefile_input, base_yr,
         simulation_yrs_to_plot):
    """Read in all results and plot PDFs

    Arguments
    ----------
    scenarios_path : 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")

    # -------------------
    # Create result folder
    # -------------------
    result_path = os.path.join(scenarios_path, '_results_weather_plots')
    basic_functions.del_previous_setup(result_path)
    basic_functions.create_folder(result_path)

    for simulation_yr_to_plot in simulation_yrs_to_plot:
        print("-----------")
        print("...simulation_yr_to_plot: " + str(simulation_yr_to_plot))
        print("-----------")
        data = {}

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

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

        all_scenarios_incl_ignored = os.listdir(scenarios_path)
        all_scenarios = []
        for scenario in all_scenarios_incl_ignored:
            if scenario not in to_ignores:
                all_scenarios.append(scenario)

        scenario_result_container = []
        for scenario_nr, scenario_name in enumerate(all_scenarios):
            print(" ")
            print("Scenario: {}".format(scenario_name))
            print(" ")
            scenario_path = os.path.join(scenarios_path, scenario_name)
            all_result_folders = os.listdir(scenario_path)

            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(scenario_path, result_folder))

            fueltype_str_to_create_maps = ['electricity']

            fueltype_str = 'electricity'
            fueltype_int = tech_related.get_fueltype_int(fueltype_str)

            ####################################################################
            # Collect regional simulation data for every realisation
            ####################################################################
            total_regional_demand_electricity = pd.DataFrame()
            peak_hour_demand = pd.DataFrame()
            national_peak = pd.DataFrame()
            regional_share_national_peak = pd.DataFrame()
            national_electricity = pd.DataFrame()
            national_gas = pd.DataFrame()
            national_hydrogen = pd.DataFrame()

            for path_result_folder in paths_folders_result:

                data = {}

                # 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_result_folder))
                pop_data = read_data.read_scenaric_population_data(
                    os.path.join(path_result_folder, 'model_run_pop'))
                path_result_folder = os.path.join(path_result_folder,
                                                  'simulation_results')
                path_result_folder_model_runs = os.path.join(
                    path_result_folder, 'model_run_results_txt')
                data['lookups'] = lookup_tables.basic_lookups()

                # 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)

                # --------------------------------------------
                # Reading in results from different model runs
                # --------------------------------------------
                results_container = read_weather_results.read_in_weather_results(
                    path_result_folder_model_runs,
                    data['assumptions']['seasons'],
                    data['assumptions']['model_yeardays_daytype'],
                    fueltype_str='electricity')

                # --Total demand (dataframe with row: realisation, column=region)
                realisation_data = pd.DataFrame([
                    results_container['ed_reg_tot_y'][simulation_yr_to_plot]
                    [fueltype_int]
                ],
                                                columns=data['regions'])
                total_regional_demand_electricity = total_regional_demand_electricity.append(
                    realisation_data)

                # National per fueltype electricity
                fueltype_elec_int = tech_related.get_fueltype_int(
                    'electricity')
                simulation_yrs_result = [
                    results_container['national_all_fueltypes'][year]
                    [fueltype_elec_int] for year in
                    results_container['national_all_fueltypes'].keys()
                ]

                realisation_data = pd.DataFrame(
                    [simulation_yrs_result],
                    columns=data['assumptions']['sim_yrs'])
                national_electricity = national_electricity.append(
                    realisation_data)

                # National per fueltype gas
                fueltype_elec_int = tech_related.get_fueltype_int('gas')
                simulation_yrs_result = [
                    results_container['national_all_fueltypes'][year]
                    [fueltype_elec_int] for year in
                    results_container['national_all_fueltypes'].keys()
                ]

                realisation_data = pd.DataFrame(
                    [simulation_yrs_result],
                    columns=data['assumptions']['sim_yrs'])
                national_gas = national_gas.append(realisation_data)

                # National per fueltype hydrogen
                fueltype_elec_int = tech_related.get_fueltype_int('hydrogen')
                simulation_yrs_result = [
                    results_container['national_all_fueltypes'][year]
                    [fueltype_elec_int] for year in
                    results_container['national_all_fueltypes'].keys()
                ]

                realisation_data = pd.DataFrame(
                    [simulation_yrs_result],
                    columns=data['assumptions']['sim_yrs'])
                national_hydrogen = national_hydrogen.append(realisation_data)

                # --Peak day demand (dataframe with row: realisation, column=region)
                realisation_data = pd.DataFrame([
                    results_container['ed_reg_peakday_peak_hour']
                    [simulation_yr_to_plot][fueltype_int]
                ],
                                                columns=data['regions'])

                peak_hour_demand = peak_hour_demand.append(realisation_data)

                # --National peak
                simulation_yrs_result = [
                    results_container['national_peak'][year][fueltype_int]
                    for year in results_container['national_peak'].keys()
                ]

                realisation_data = pd.DataFrame(
                    [simulation_yrs_result],
                    columns=data['assumptions']['sim_yrs'])
                national_peak = national_peak.append(realisation_data)

                # --Regional percentage of national peak demand
                realisation_data = pd.DataFrame([
                    results_container['regional_share_national_peak']
                    [simulation_yr_to_plot]
                ],
                                                columns=data['regions'])

                regional_share_national_peak = regional_share_national_peak.append(
                    realisation_data)

            # Add to scenario container
            scenario_result_container.append({
                'scenario_name':
                scenario_name,
                'peak_hour_demand':
                peak_hour_demand,
                'national_peak':
                national_peak,
                'regional_share_national_peak':
                regional_share_national_peak,
                'total_regional_demand_electricity':
                total_regional_demand_electricity,
                'national_electricity':
                national_electricity,
                'national_gas':
                national_gas,
                'national_hydrogen':
                national_hydrogen,
            })

        # ------------------------------
        # Plot national sum over time per fueltype and scenario
        # ------------------------------
        print("... plotting national sum of fueltype over time ")
        fig_3_plot_over_time.fueltypes_over_time(
            scenario_result_container=scenario_result_container,
            sim_yrs=data['assumptions']['sim_yrs'],
            fig_name="fueltypes_over_time__{}__{}.pdf".format(
                simulation_yr_to_plot, fueltype_str),
            fueltypes=['electricity', 'gas', 'hydrogen'],
            result_path=result_path,
            unit='TWh',
            plot_points=True,
            crit_smooth_line=True,
            seperate_legend=False)

        # ------------------------------
        # Plot national peak change over time for each scenario including weather variability
        # ------------------------------
        fig_3_plot_over_time.scenario_over_time(
            scenario_result_container=scenario_result_container,
            sim_yrs=data['assumptions']['sim_yrs'],
            fig_name="scenarios_peak_over_time__{}__{}.pdf".format(
                simulation_yr_to_plot, fueltype_str),
            plot_points=True,
            result_path=result_path,
            crit_smooth_line=True,
            seperate_legend=False)

        # ------------------------------
        # Plotting spatial results for electricity
        # ------------------------------
        for i in scenario_result_container:
            scenario_name = i['scenario_name']
            total_regional_demand_electricity = i[
                'total_regional_demand_electricity']
            peak_hour_demand = i['peak_hour_demand']
            regional_share_national_peak = i['regional_share_national_peak']

            print("... plot spatial map of total annual demand")
            field_to_plot = 'std_dev'
            fig_3_weather_map.total_annual_demand(
                total_regional_demand_electricity,
                path_shapefile_input,
                data['regions'],
                pop_data=pop_data,
                simulation_yr_to_plot=simulation_yr_to_plot,
                result_path=result_path,
                fig_name="{}__tot_demand__{}_{}_{}.pdf".format(
                    scenario_name, field_to_plot, fueltype_str,
                    simulation_yr_to_plot),
                field_to_plot=field_to_plot,
                unit='GW',
                seperate_legend=False)

            print("... plot spatial map of peak hour demand")
            field_to_plot = 'std_dev'
            fig_3_weather_map.total_annual_demand(
                peak_hour_demand,
                path_shapefile_input,
                data['regions'],
                pop_data=pop_data,
                simulation_yr_to_plot=simulation_yr_to_plot,
                result_path=result_path,
                fig_name="{}__peak_h_demand_{}_{}_{}.pdf".format(
                    scenario_name, field_to_plot, fueltype_str,
                    simulation_yr_to_plot),
                field_to_plot=field_to_plot,
                unit='GW',
                seperate_legend=False)

            print(
                "... plot spatial map of percentage of regional peak hour demand"
            )
            field_to_plot = 'mean'
            fig_3_weather_map.total_annual_demand(
                regional_share_national_peak,
                path_shapefile_input,
                data['regions'],
                pop_data=pop_data,
                simulation_yr_to_plot=simulation_yr_to_plot,
                result_path=result_path,
                fig_name="{}__regional_share_national_peak_{}_{}_{}.pdf".
                format(scenario_name, field_to_plot, fueltype_str,
                       simulation_yr_to_plot),
                field_to_plot=field_to_plot,
                unit='percentage',
                seperate_legend=False,
                bins=[0.000001, 0.25, 0.5, 0.75, 1, 1.25, 1.5])

            field_to_plot = 'std_dev'
            fig_3_weather_map.total_annual_demand(
                regional_share_national_peak,
                path_shapefile_input,
                data['regions'],
                pop_data=pop_data,
                simulation_yr_to_plot=simulation_yr_to_plot,
                result_path=result_path,
                fig_name="{}__regional_share_national_peak_{}_{}_{}.pdf".
                format(scenario_name, field_to_plot, fueltype_str,
                       simulation_yr_to_plot),
                field_to_plot=field_to_plot,
                unit='percentage',
                seperate_legend=False)

    print("===================================")
    print("... finished reading and plotting results")
    print("===================================")
コード例 #7
0
ファイル: init_scripts.py プロジェクト: RoaldL/energy_demand
def scenario_initalisation(path_data_ed, data=False):
    """Scripts which need to be run for every different scenario.
    Only needs to be executed once for each scenario (not for every
    simulation year).

    The following calculations are performed:

        I.      Disaggregation of fuel for every region
        II.     Switches calculations
        III.    Spatial explicit diffusion modelling

    Arguments
    ----------
    path_data_ed : str
        Path to the energy demand data folder
    data : dict
        Data container

    Info
    -----
        # Non spatiall differentiated modelling of
    # technology diffusion (same diffusion pattern for
    # the whole UK) or spatially differentiated (every region)
    """
    logging.info("... Start initialisation scripts")

    init_cont = defaultdict(dict)
    fuel_disagg = {}

    logger_setup.set_up_logger(os.path.join(path_data_ed, "scenario_init.log"))

    # --------------------------------------------
    # Delete results from previous model runs and initialise folders
    # --------------------------------------------
    basic_functions.del_previous_results(
        data['local_paths']['data_processed'],
        data['local_paths']['path_post_installation_data'])

    basic_functions.del_previous_setup(data['result_paths']['data_results'])

    folders_to_create = [
        data['local_paths']['dir_services'],
        data['local_paths']['path_sigmoid_data'],
        data['result_paths']['data_results'],
        data['result_paths']['data_results_PDF'],
        data['result_paths']['data_results_model_run_pop'],
        data['result_paths']['data_results_validation'],
        data['result_paths']['data_results_model_runs']
    ]

    for folder in folders_to_create:
        basic_functions.create_folder(folder)

    # ===========================================
    # I. Disaggregation
    # ===========================================

    # Load data for disaggregateion
    data['scenario_data'][
        'employment_stats'] = data_loader.read_employment_stats(
            data['paths']['path_employment_statistics'])

    # Disaggregate fuel for all regions
    fuel_disagg['rs_fuel_disagg'], fuel_disagg['ss_fuel_disagg'], fuel_disagg[
        'is_fuel_disagg'] = s_disaggregation.disaggregate_base_demand(
            data['regions'], data['assumptions'].base_yr,
            data['assumptions'].curr_yr, data['fuels'], data['scenario_data'],
            data['assumptions'], data['reg_coord'], data['weather_stations'],
            data['temp_data'], data['sectors'], data['sectors']['all_sectors'],
            data['enduses'])

    # Sum demand across all sectors for every region
    fuel_disagg[
        'ss_fuel_disagg_sum_all_sectors'] = sum_across_sectors_all_regs(
            fuel_disagg['ss_fuel_disagg'])

    fuel_disagg['is_aggr_fuel_sum_all_sectors'] = sum_across_sectors_all_regs(
        fuel_disagg['is_fuel_disagg'])

    # ---------------------------------------
    # Convert base year fuel input assumptions to energy service
    # ---------------------------------------

    # Residential
    rs_s_tech_by_p, _, rs_s_fueltype_by_p = s_fuel_to_service.get_s_fueltype_tech(
        data['enduses']['rs_enduses'], data['assumptions'].tech_list,
        data['lookups']['fueltypes'], data['assumptions'].rs_fuel_tech_p_by,
        data['fuels']['rs_fuel_raw'], data['technologies'])

    # Service
    ss_s_tech_by_p = {}
    ss_s_fueltype_by_p = {}
    for sector in data['sectors']['ss_sectors']:
        ss_s_tech_by_p[sector], _, ss_s_fueltype_by_p[
            sector] = s_fuel_to_service.get_s_fueltype_tech(
                data['enduses']['ss_enduses'], data['assumptions'].tech_list,
                data['lookups']['fueltypes'],
                data['assumptions'].ss_fuel_tech_p_by,
                data['fuels']['ss_fuel_raw'], data['technologies'], sector)

    # Industry
    is_s_tech_by_p = {}
    is_s_fueltype_by_p = {}
    for sector in data['sectors']['is_sectors']:
        is_s_tech_by_p[sector], _, is_s_fueltype_by_p[
            sector] = s_fuel_to_service.get_s_fueltype_tech(
                data['enduses']['is_enduses'], data['assumptions'].tech_list,
                data['lookups']['fueltypes'],
                data['assumptions'].is_fuel_tech_p_by,
                data['fuels']['is_fuel_raw'], data['technologies'], sector)

    # ===========================================
    # SPATIAL CALCULATIONS factors
    #
    # Calculate spatial diffusion factors
    # ===========================================
    if data['criterias']['spatial_exliclit_diffusion']:

        f_reg, f_reg_norm, f_reg_norm_abs = spatial_diffusion.calc_spatially_diffusion_factors(
            regions=data['regions'],
            fuel_disagg=fuel_disagg,
            real_values=data['pop_density'],  # Real value to select
            speed_con_max=1.0)  # diffusion speed differences

        # ---------------------
        # Plot figure for paper
        # ---------------------
        plot_fig_paper = True  #FALSE
        plot_fig_paper = False  #FALSE
        if plot_fig_paper:

            # Global value to distribute
            global_value = 50

            # Select spatial diffusion factor
            #diffusion_vals = f_reg                                 # not weighted
            diffusion_vals = f_reg_norm[
                'rs_space_heating']  # Weighted with enduse
            #diffusion_vals = f_reg_norm_abs['rs_space_heating']    # Absolute distribution (only for capacity installements)

            path_shapefile_input = os.path.abspath(
                'C:/Users/cenv0553/ED/data/_raw_data/C_LAD_geography/same_as_pop_scenario/lad_2016_uk_simplified.shp'
            )

            result_mapping.plot_spatial_mapping_example(
                diffusion_vals=diffusion_vals,
                global_value=global_value,
                paths=data['result_paths'],
                regions=data['regions'],
                path_shapefile_input=path_shapefile_input)
    else:
        f_reg = False
        f_reg_norm = False
        f_reg_norm_abs = False
        init_cont['regional_strategy_variables'] = None

    # ===========================================
    # II. Switches
    # ===========================================

    # ========================================================================================
    # Capacity switches
    #
    # Calculate service shares considering potential capacity installations
    # ========================================================================================
    # Service
    ss_aggr_sector_fuels = s_fuel_to_service.sum_fuel_enduse_sectors(
        data['fuels']['ss_fuel_raw'], data['enduses']['ss_enduses'])

    # Industry
    is_aggr_sector_fuels = s_fuel_to_service.sum_fuel_enduse_sectors(
        data['fuels']['is_fuel_raw'], data['enduses']['is_enduses'])

    if data['criterias']['spatial_exliclit_diffusion']:

        # Select diffusion value
        f_diffusion = f_reg_norm_abs

        # Convert globally defined switches to regional switches
        reg_capacity_switches_rs = global_to_reg_capacity_switch(
            data['regions'], data['assumptions'].rs_capacity_switches,
            f_diffusion)
        reg_capacity_switches_ss = global_to_reg_capacity_switch(
            data['regions'], data['assumptions'].ss_capacity_switches,
            f_diffusion)
        reg_capacity_switches_is = global_to_reg_capacity_switch(
            data['regions'], data['assumptions'].is_capacity_switches,
            f_diffusion)

        rs_service_switches_incl_cap = {}
        ss_service_switches_inlc_cap = {}
        is_service_switches_incl_cap = {}

        for region in data['regions']:

            # Residential
            rs_service_switches_incl_cap[
                region] = fuel_service_switch.capacity_switch(
                    reg_capacity_switches_rs[region], data['technologies'],
                    data['assumptions'].
                    enduse_overall_change['other_enduse_mode_info'],
                    data['fuels']['rs_fuel_raw'],
                    data['assumptions'].rs_fuel_tech_p_by,
                    data['assumptions'].base_yr)

            ss_service_switches_inlc_cap[
                region] = fuel_service_switch.capacity_switch(
                    reg_capacity_switches_ss[region], data['technologies'],
                    data['assumptions'].
                    enduse_overall_change['other_enduse_mode_info'],
                    ss_aggr_sector_fuels,
                    data['assumptions'].ss_fuel_tech_p_by,
                    data['assumptions'].base_yr)

            is_service_switches_incl_cap[
                region] = fuel_service_switch.capacity_switch(
                    reg_capacity_switches_is[region], data['technologies'],
                    data['assumptions'].
                    enduse_overall_change['other_enduse_mode_info'],
                    is_aggr_sector_fuels,
                    data['assumptions'].is_fuel_tech_p_by,
                    data['assumptions'].base_yr)
    else:  #Not spatial explicit

        rs_service_switches_incl_cap = fuel_service_switch.capacity_switch(
            data['assumptions'].rs_capacity_switches, data['technologies'],
            data['assumptions'].
            enduse_overall_change['other_enduse_mode_info'],
            data['fuels']['rs_fuel_raw'],
            data['assumptions'].rs_fuel_tech_p_by, data['assumptions'].base_yr)

        ss_service_switches_inlc_cap = fuel_service_switch.capacity_switch(
            data['assumptions'].ss_capacity_switches, data['technologies'],
            data['assumptions'].
            enduse_overall_change['other_enduse_mode_info'],
            ss_aggr_sector_fuels, data['assumptions'].ss_fuel_tech_p_by,
            data['assumptions'].base_yr)

        is_service_switches_incl_cap = fuel_service_switch.capacity_switch(
            data['assumptions'].is_capacity_switches, data['technologies'],
            data['assumptions'].
            enduse_overall_change['other_enduse_mode_info'],
            is_aggr_sector_fuels, data['assumptions'].is_fuel_tech_p_by,
            data['assumptions'].base_yr)

    # ========================================================================================
    # Service switches
    #
    # Get service shares of technologies for future year by considering
    # service switches. Potential capacity switches are used as inputs.
    #
    # Autocomplement defined service switches with technologies not
    # explicitly specified in switch on a global scale and distribute
    # spatially.
    # Autocomplete and regional diffusion levels calculations
    # ========================================================================================

    # Select spatial diffusion
    f_diffusion = f_reg_norm

    # Residential
    rs_share_s_tech_ey_p, rs_switches_autocompleted = fuel_service_switch.autocomplete_switches(
        data['assumptions'].rs_service_switches,
        data['assumptions'].rs_specified_tech_enduse_by,
        rs_s_tech_by_p,
        spatial_exliclit_diffusion=data['criterias']
        ['spatial_exliclit_diffusion'],
        regions=data['regions'],
        f_diffusion=f_diffusion,
        techs_affected_spatial_f=data['assumptions'].techs_affected_spatial_f,
        service_switches_from_capacity=rs_service_switches_incl_cap)

    # Service
    ss_switches_autocompleted = {}
    ss_share_s_tech_ey_p = {}
    for sector in data['sectors']['ss_sectors']:

        # Get all switches of a sector
        sector_switches = get_sector_switches(
            sector, data['assumptions'].ss_service_switches)

        ss_share_s_tech_ey_p[sector], ss_switches_autocompleted[
            sector] = fuel_service_switch.autocomplete_switches(
                sector_switches,
                data['assumptions'].ss_specified_tech_enduse_by,
                ss_s_tech_by_p[sector],
                sector=sector,
                spatial_exliclit_diffusion=data['criterias']
                ['spatial_exliclit_diffusion'],
                regions=data['regions'],
                f_diffusion=f_diffusion,
                techs_affected_spatial_f=data['assumptions'].
                techs_affected_spatial_f,
                service_switches_from_capacity=ss_service_switches_inlc_cap)

    # Industry
    is_switches_autocompleted = {}
    is_share_s_tech_ey_p = {}

    for sector in data['sectors']['is_sectors']:

        # Get all switches of a sector
        sector_switches = get_sector_switches(
            sector, data['assumptions'].is_service_switches)

        is_share_s_tech_ey_p[sector], is_switches_autocompleted[
            sector] = fuel_service_switch.autocomplete_switches(
                sector_switches,
                data['assumptions'].is_specified_tech_enduse_by,
                is_s_tech_by_p[sector],
                sector=sector,
                spatial_exliclit_diffusion=data['criterias']
                ['spatial_exliclit_diffusion'],
                regions=data['regions'],
                f_diffusion=f_diffusion,
                techs_affected_spatial_f=data['assumptions'].
                techs_affected_spatial_f,
                service_switches_from_capacity=is_service_switches_incl_cap)

    # ========================================================================================
    # Fuel switches
    #
    # Calculate sigmoid diffusion considering fuel switches
    # and service switches. As inputs, service (and thus also capacity switches) are used
    # ========================================================================================

    # Residential
    for enduse in data['enduses']['rs_enduses']:
        init_cont['rs_sig_param_tech'][
            enduse] = sig_param_calc_incl_fuel_switch(
                data['assumptions'].base_yr,
                data['assumptions'].crit_switch_happening,
                data['technologies'],
                enduse=enduse,
                fuel_switches=data['assumptions'].rs_fuel_switches,
                service_switches=rs_switches_autocompleted,
                s_tech_by_p=rs_s_tech_by_p[enduse],
                s_fueltype_by_p=rs_s_fueltype_by_p[enduse],
                share_s_tech_ey_p=rs_share_s_tech_ey_p[enduse],
                fuel_tech_p_by=data['assumptions'].rs_fuel_tech_p_by[enduse],
                regions=data['regions'],
                regional_specific=data['criterias']
                ['spatial_exliclit_diffusion'])

    # Service
    for enduse in data['enduses']['ss_enduses']:
        init_cont['ss_sig_param_tech'][enduse] = {}
        for sector in data['sectors']['ss_sectors']:
            init_cont['ss_sig_param_tech'][enduse][
                sector] = sig_param_calc_incl_fuel_switch(
                    data['assumptions'].base_yr,
                    data['assumptions'].crit_switch_happening,
                    data['technologies'],
                    enduse=enduse,
                    fuel_switches=data['assumptions'].ss_fuel_switches,
                    service_switches=ss_switches_autocompleted[sector],
                    s_tech_by_p=ss_s_tech_by_p[sector][enduse],
                    s_fueltype_by_p=ss_s_fueltype_by_p[sector][enduse],
                    share_s_tech_ey_p=ss_share_s_tech_ey_p[sector][enduse],
                    fuel_tech_p_by=data['assumptions'].
                    ss_fuel_tech_p_by[enduse][sector],
                    regions=data['regions'],
                    sector=sector,
                    regional_specific=data['criterias']
                    ['spatial_exliclit_diffusion'])

    # Industry
    for enduse in data['enduses']['is_enduses']:
        init_cont['is_sig_param_tech'][enduse] = {}
        for sector in data['sectors']['is_sectors']:
            init_cont['is_sig_param_tech'][enduse][
                sector] = sig_param_calc_incl_fuel_switch(
                    data['assumptions'].base_yr,
                    data['assumptions'].crit_switch_happening,
                    data['technologies'],
                    enduse=enduse,
                    fuel_switches=data['assumptions'].is_fuel_switches,
                    service_switches=is_switches_autocompleted[sector],
                    s_tech_by_p=is_s_tech_by_p[sector][enduse],
                    s_fueltype_by_p=is_s_fueltype_by_p[sector][enduse],
                    share_s_tech_ey_p=is_share_s_tech_ey_p[sector][enduse],
                    fuel_tech_p_by=data['assumptions'].
                    is_fuel_tech_p_by[enduse][sector],
                    regions=data['regions'],
                    sector=sector,
                    regional_specific=data['criterias']
                    ['spatial_exliclit_diffusion'])

    # ===========================================
    # III. Spatial explicit modelling of scenario variables
    #
    # From UK factors to regional specific factors
    # Convert strategy variables to regional variables
    # ===========================================
    if data['criterias']['spatial_exliclit_diffusion']:
        init_cont['regional_strategy_variables'] = defaultdict(dict)

        # Iterate strategy variables and calculate regional variable
        for var_name, strategy_var in data[
                'assumptions'].strategy_variables.items():

            logging.info("Spatially explicit diffusion modelling %s", var_name)
            logging.info(data['assumptions'].spatially_modelled_vars)

            # Check whether scenario varaible is regionally modelled
            if var_name not in data['assumptions'].spatially_modelled_vars:

                # Variable is not spatially modelled
                for region in data['regions']:
                    init_cont['regional_strategy_variables'][region][
                        var_name] = {
                            'scenario_value':
                            float(strategy_var['scenario_value']),
                            'affected_enduse':
                            data['assumptions'].strategy_variables[var_name]
                            ['affected_enduse']
                        }
            else:

                if strategy_var['affected_enduse'] == []:
                    logging.info(
                        "For scenario var %s no affected enduse is defined. Thus speed is used for diffusion",
                        var_name)
                else:
                    pass

                # Get enduse specific fuel for each region
                fuels_reg = spatial_diffusion.get_enduse_regs(
                    enduse=strategy_var['affected_enduse'],
                    fuels_disagg=[
                        fuel_disagg['rs_fuel_disagg'],
                        fuel_disagg['ss_fuel_disagg'],
                        fuel_disagg['is_fuel_disagg']
                    ])

                # Calculate regional specific strategy variables values
                reg_specific_variables = spatial_diffusion.factor_improvements_single(
                    factor_uk=strategy_var['scenario_value'],
                    regions=data['regions'],
                    f_reg=f_reg,
                    f_reg_norm=f_reg_norm,
                    f_reg_norm_abs=f_reg_norm_abs,
                    fuel_regs_enduse=fuels_reg)

                # Add regional specific strategy variables values
                for region in data['regions']:
                    init_cont['regional_strategy_variables'][region][
                        var_name] = {
                            'scenario_value':
                            float(reg_specific_variables[region]),
                            'affected_enduse': strategy_var['affected_enduse']
                        }

        init_cont['regional_strategy_variables'] = dict(
            init_cont['regional_strategy_variables'])

    logging.info("... finished scenario initialisation")
    return dict(init_cont), fuel_disagg
コード例 #8
0
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("===================================")
コード例 #9
0
ファイル: main.py プロジェクト: RoaldL/energy_demand
        }
    }

    print("Start Energy Demand Model with python version: " + str(sys.version))
    print("Info model run")
    print("Nr of Regions " + str(data['reg_nrs']))

    # In order to load these data, the initialisation scripts need to be run
    print("... Load data from script calculations")
    data = read_data.load_script_data(data)  #SCENARIO INITIALISATION

    #-------------------
    # Folder cleaning
    #--------------------
    print("... delete previous model run results")
    basic_functions.del_previous_setup(data['result_paths']['data_results'])
    basic_functions.create_folder(data['result_paths']['data_results'])
    basic_functions.create_folder(data['result_paths']['data_results_PDF'])
    basic_functions.create_folder(
        data['result_paths']['data_results_model_run_pop'])

    # Create .ini file with simulation information
    write_data.write_simulation_inifile(data['result_paths']['data_results'],
                                        data['enduses'], data['assumptions'],
                                        data['reg_nrs'], data['regions'])

    for sim_yr in data['assumptions'].simulated_yrs:
        setattr(data['assumptions'], 'curr_yr', sim_yr)

        print("Simulation for year --------------:  " + str(sim_yr))
        fuel_in, fuel_in_biomass, fuel_in_elec, fuel_in_gas, fuel_in_heat, fuel_in_hydro, fuel_in_solid_fuel, fuel_in_oil, tot_heating = testing.test_function_fuel_sum(
コード例 #10
0
ファイル: paper_II.py プロジェクト: edoabraham/energy_demand
def paper_II_plots(
    #/soge-home/staff/cenv0553/ED
    #scenario_path_all_stations_single_weather_yr="C:/Users/cenv0553/ed/results/_multiple_TEST_two_scenarios",
    #scenario_path_all_stations_single_weather_yr="C:/Users/cenv0553/ed/results/_Fig2_multiple_all_yrs_all_stations",
    ed_path='C:/Users/cenv0553/ED',
    scenario_path_single_stations='C:/Users/cenv0553/ED/results/_Fig2_multiple_2015_weather_stations',
    scenario_path_all_stations_single_weather_yr="C:/Users/cenv0553/ED/results/_Fig2_multiple_all_yrs_all_stations",
    path_shapefile_input="C:/Users/cenv0553/ED/data/region_definitions/lad_2016_uk_simplified.shp"
):
    """Iterate the folders with scenario
    runs and generate PDF results of individual
    simulation runs

    """
    # Chose which plots should be generated
    plot_crit_dict = {

        # Scenario plots
        'scenario_plots': False,  #True,

        #Plot weather map
        'weather_map': False,  #True,

        # Spatio temporal validation
        'spatio_temporal_validation': False,  #True,

        # Spatial map of distribution of peak and contribution in terms of overall variability
        "plot_spatial_distribution_of_peak": True,

        # Needs to have weatehryr_stationid files in folder
        "plot_national_regional_hdd_disaggregation": False,

        # Plot weather variability for every weather station
        # of demand generated with regional HDD calculation
        "plot_weather_day_year": False,

        # Plot spatial distribution of differences in
        # variability of demand by weather variability
        # normed by population
        "plot_spatial_weather_var_peak": False,

        # Plot scenario years sorted with weather variability
        "plot_scenarios_sorted": True,
    }

    ####################################################################
    # Plot scenarios
    ####################################################################
    if plot_crit_dict['scenario_plots']:
        sim_yrs = [2015, 2020, 2030, 2040, 2050]
        sim_yrs = [2015, 2050]

        scenario_result_paths = [
            "C:/Users/cenv0553/ed/results/_scenario_low_elec",
            "C:/Users/cenv0553/ed/results/_scenario_high_elec"
        ]

        scenario_result_paths = [
            "C:/Users/cenv0553/ed/results/_Fig2_multiple_all_yrs_all_stations"
        ]

        # Create result folder
        path_out_plots = os.path.join(
            "C:/Users/cenv0553/ed/results/_results_nationalFIGS")
        basic_functions.del_previous_setup(path_out_plots)
        basic_functions.create_folder(path_out_plots)

        fig_p2_total_demand_national_scenarios.total_demand_national_scenarios(
            scenario_result_paths,
            sim_yrs=sim_yrs,
            fueltype_str='electricity',
            path_out_plots=os.path.join(path_out_plots,
                                        "PDF_national_scenarios.pdf"))

    ####################################################################
    # Plot weather station availability map
    ####################################################################
    if plot_crit_dict['weather_map']:

        # Wheather station distribution map
        path_to_weather_data = os.path.join(
            ed_path,
            "data/_raw_data/A-temperature_data/cleaned_weather_stations_data")
        folder_path_weater_stations = os.path.join(
            ed_path,
            "data/_raw_data/A-temperature_data/cleaned_weather_stations.csv")
        fig_path = os.path.join(ed_path, scenario_path_single_stations,
                                "weather_station_maps.pdf")
        fig_p2_spatial_weather_map.run(path_to_weather_data,
                                       folder_path_weater_stations,
                                       path_shapefile=path_shapefile_input,
                                       fig_path=fig_path)

        print("... plotted weather station distribution map")

    ####################################################################
    # Plot regional vs national spatio-temporal validation
    ####################################################################
    if plot_crit_dict['spatio_temporal_validation']:

        path_regional_calculations = os.path.join(
            scenario_path_single_stations, "_regional_calculations")
        path_non_regional_elec_2015 = os.path.abspath(
            os.path.join(path_regional_calculations, '..',
                         "_all_stations_wy_2015"))
        path_rolling_elec_demand = os.path.join(ed_path, "energy_demand",
                                                "energy_demand", "config_data",
                                                '01-validation_datasets',
                                                '01_national_elec_2015',
                                                'elec_demand_2015.csv')
        path_temporal_elec_validation = os.path.join(
            ed_path, "energy_demand", "energy_demand", "config_data",
            '01-validation_datasets', '02_subnational_elec',
            'data_2015_elec_domestic.csv')
        path_temporal_gas_validation = os.path.join(
            ed_path, "energy_demand", "energy_demand", "config_data",
            '01-validation_datasets', '03_subnational_gas',
            'data_2015_gas_domestic.csv')

        path_results = os.path.abspath(
            os.path.join(path_regional_calculations, '..'))
        path_out_plots = os.path.join(path_results, '_results_PDF_figs')
        basic_functions.del_previous_setup(path_out_plots)
        basic_functions.create_folder(path_out_plots)

        # 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,
            path_out_plots=path_out_plots)

        print("... plotted spatio-temporal validation")

    ####################################################################
    # 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']:

        # Create result folder
        path_out_plots = os.path.join(
            scenario_path_all_stations_single_weather_yr, '_results_PDF_figs')
        basic_functions.del_previous_setup(path_out_plots)
        basic_functions.create_folder(path_out_plots)

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

        scenario_names_ignored = [
            '__results_multiple_scenarios', '_FigII_non_regional_2015',
            '_results_PDF_figs'
        ]

        only_scenarios = []
        for scenario in scenarios:
            if scenario in scenario_names_ignored:
                pass
            else:
                only_scenarios.append(scenario)

        # Select simulation years
        sim_yrs = [2015, 2050]
        unit = 'kW'  #'GW'

        # Plot standard deviation of demand per person in peak hour
        fig_spatial_distribution_of_peak.run_fig_spatial_distribution_of_peak(
            only_scenarios,
            scenario_path_all_stations_single_weather_yr,
            path_shapefile_input,
            sim_yrs=sim_yrs,
            field_to_plot="std_deviation_df_abs_demand_in_peak_h_pp",
            unit=unit,
            fig_path=path_out_plots)

        # Plot standard deviation of percentage of national peak
        fig_spatial_distribution_of_peak.run_fig_spatial_distribution_of_peak(
            only_scenarios,
            scenario_path_all_stations_single_weather_yr,
            path_shapefile_input,
            sim_yrs=sim_yrs,
            field_to_plot='std_deviation_p_demand_peak_h',
            unit='percent',
            fig_path=path_out_plots)

        # Plot standard deviation of absolute demand of national peak
        fig_spatial_distribution_of_peak.run_fig_spatial_distribution_of_peak(
            only_scenarios,
            scenario_path_all_stations_single_weather_yr,
            path_shapefile_input,
            sim_yrs=sim_yrs,
            field_to_plot='std_deviation_abs_demand_peak_h',
            unit=unit,
            fig_path=path_out_plots)

        # Plot maximum peak hour - mean peak hour demand
        fig_spatial_distribution_of_peak.run_fig_spatial_distribution_of_peak(
            only_scenarios,
            scenario_path_all_stations_single_weather_yr,
            path_shapefile_input,
            sim_yrs=sim_yrs,
            field_to_plot='diff_peak_h_minus_mean',
            unit='GW',
            fig_path=path_out_plots)

        print("... plotted variability maps")

    print("Finihsed")
    raise Exception("Finished")

    ####################################################################
    # left overs
    ####################################################################
    for scenario in only_scenarios:

        # -----------
        # Other plots
        # -----------
        figs_p2.main(
            os.path.join(scenario_path_all_stations_single_weather_yr,
                         scenario), path_shapefile_input, path_out_plots,
            plot_crit_dict)

    # ####################################################################
    # Plot demand over time and peak over time (for modassar paper)
    # ####################################################################
    '''if plot_crit_dict['plot_weather_day_year']:

        # --------------------------------------------
        # Plot peak demand and total demand per fueltype
        # --------------------------------------------
        for fueltype_str in data['lookups']['fueltypes'].keys():

            fig_total_demand_peak.run(
                data_input=weather_yr_container['tot_fueltype_yh'],
                fueltype_str=fueltype_str,
                fig_name=os.path.join(
                    path_out_plots, "tot_{}_h.pdf".format(fueltype_str)))

        # plot over period of time across all weather scenario
        fig_weather_variability_priod.run(
            data_input=weather_yr_container['tot_fueltype_yh'],
            fueltype_str='electricity',
            simulation_yr_to_plot=2015, # Simulation year to plot
            period_h=list(range(200,500)), #period to plot
            fig_name=os.path.join(
                path_out_plots, "weather_var_period.pdf"))'''

    return
コード例 #11
0
def main(
        scenarios_path,
        path_shapefile_input,
        base_yr,
        simulation_yrs_to_plot
    ):
    """Read in all results and plot PDFs

    Arguments
    ----------
    scenarios_path : 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")

    # -------------------
    # Create temperatuere figur plot
    # -------------------
    plot_weather_chart = False
    if plot_weather_chart:
        from energy_demand.plotting import fig3_weather_at_home_plot
        path_weather_data = "//linux-filestore.ouce.ox.ac.uk/mistral/nismod/data/energy_demand/J-MARIUS_data/_weather_realisation"
        fig3_weather_at_home_plot.plotting_weather_data(path_weather_data)

    # -------------------
    # Create result folder
    # -------------------
    result_path = os.path.join(scenarios_path, '_results_weather_plots')
    basic_functions.del_previous_setup(result_path)
    basic_functions.create_folder(result_path)

    x_chart_yrs_storage = {}

    for simulation_yr_to_plot in simulation_yrs_to_plot:
        print("=================")
        print("...simulation_yr_to_plot: " + str(simulation_yr_to_plot))
        print("=================")
        data = {}
        x_chart_yrs_storage[simulation_yr_to_plot] = {}

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

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

        all_scenarios_incl_ignored = os.listdir(scenarios_path)
        all_scenarios = []
        for scenario in all_scenarios_incl_ignored:
            if scenario not in to_ignores:
                all_scenarios.append(scenario)

        scenario_result_container = []
        for scenario_nr, scenario_name in enumerate(all_scenarios):
            print(" ")
            print("Scenario: {}".format(scenario_name))
            print(" ")
            scenario_path = os.path.join(scenarios_path, scenario_name)
            all_result_folders = os.listdir(scenario_path)
            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(scenario_path, result_folder))

            fueltype_str_to_create_maps = ['electricity']

            fueltype_str ='electricity'
            fueltype_elec_int = tech_related.get_fueltype_int('electricity')

            ####################################################################
            # Collect regional simulation data for every realisation
            ####################################################################
            total_regional_demand_electricity = pd.DataFrame()
            peak_hour_demand = pd.DataFrame()
            peak_hour_demand_per_person = pd.DataFrame()
            national_peak = pd.DataFrame()
            regional_share_national_peak = pd.DataFrame()
            regional_share_national_peak_pp = pd.DataFrame()
            national_electricity = pd.DataFrame()
            national_gas = pd.DataFrame()
            national_hydrogen = pd.DataFrame()
            national_heating_peak = pd.DataFrame()
            daily_mean_peak_day = pd.DataFrame()

            for path_result_folder in paths_folders_result:
                print("... path_result_folder: {}".format(path_result_folder))
                data = {}
                ed_national_heating_peak = {}

                try:
                    # ================================
                    # Loading in only heating peak demands (seperate calculations)
                    # ================================
                    
                    # 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_result_folder))
                    pop_data = read_data.read_scenaric_population_data(os.path.join(path_result_folder, 'model_run_pop'))
                    path_result_folder_heating = os.path.join(path_result_folder, 'simulation_results')
                    path_result_folder_model_runs = os.path.join(path_result_folder_heating, 'model_run_results_txt')
                    data['lookups'] = lookup_tables.basic_lookups()

                    # Read in heating deamnds
                    path_heating_demands = os.path.join(path_result_folder_model_runs, 'enduse_specific_results')
                    all_files = os.listdir(path_heating_demands)
                    for file_name in all_files:
                        
                        ending  = file_name[-4:]
                        if ending == ".npy":
                            year = int(file_name.split("__")[2][:-4])
                            file_path = os.path.join(path_heating_demands, file_name)
                            heating_demand = np.load(file_path)
                            maximum_hour_of_peak_day = heating_demand[fueltype_elec_int].argmax() #get maxim hour of peak day
                            ed_national_heating_peak[year] = heating_demand[fueltype_elec_int][maximum_hour_of_peak_day]

                    simulation_yrs_result = [ed_national_heating_peak[year] for year in simulation_yrs_to_plot]
                    realisation_data = pd.DataFrame(
                        [simulation_yrs_result],
                        columns=data['assumptions']['sim_yrs'])
                    national_heating_peak = national_heating_peak.append(realisation_data)

                except:
                    raise Exception("... no heating peak data available " + str(path_result_folder))

                try:
                    # 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_result_folder))
                    pop_data = read_data.read_scenaric_population_data(os.path.join(path_result_folder, 'model_run_pop'))
                    path_result_folder = os.path.join(path_result_folder, 'simulation_results')
                    path_result_folder_model_runs = os.path.join(path_result_folder, 'model_run_results_txt')
                    data['lookups'] = lookup_tables.basic_lookups()

                    # 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)

                    # --------------------------------------------
                    # Reading in results from different model runs
                    # --------------------------------------------
                    results_container = read_weather_results.read_in_weather_results(
                        path_result_folder_model_runs,
                        data['assumptions']['seasons'],
                        data['assumptions']['model_yeardays_daytype'],
                        pop_data,
                        fueltype_str='electricity')

                    # --Total demand (dataframe with row: realisation, column=region)
                    realisation_data = pd.DataFrame(
                        [results_container['ed_reg_tot_y'][simulation_yr_to_plot][fueltype_elec_int]],
                        columns=data['regions'])
                    total_regional_demand_electricity = total_regional_demand_electricity.append(realisation_data)

                    # National per fueltype electricity
                    simulation_yrs_result = [results_container['national_all_fueltypes'][year][fueltype_elec_int] for year in simulation_yrs_to_plot]

                    realisation_data = pd.DataFrame(
                        [simulation_yrs_result],
                        columns=data['assumptions']['sim_yrs'])
                    national_electricity = national_electricity.append(realisation_data)

                    # National per fueltype gas
                    fueltype_gas_int = tech_related.get_fueltype_int('gas')
                    simulation_yrs_result = [results_container['national_all_fueltypes'][year][fueltype_gas_int] for year in simulation_yrs_to_plot]

                    realisation_data = pd.DataFrame(
                        [simulation_yrs_result],
                        columns=data['assumptions']['sim_yrs'])
                    national_gas = national_gas.append(realisation_data)

                    # National per fueltype hydrogen
                    fueltype_hydrogen_int = tech_related.get_fueltype_int('hydrogen')
                    simulation_yrs_result = [results_container['national_all_fueltypes'][year][fueltype_hydrogen_int] for year in simulation_yrs_to_plot]

                    realisation_data = pd.DataFrame(
                        [simulation_yrs_result],
                        columns=data['assumptions']['sim_yrs'])
                    national_hydrogen = national_hydrogen.append(realisation_data)

                    # --Peak hour demand per region (dataframe with row: realisation, column=region)
                    realisation_data = pd.DataFrame(
                        [results_container['ed_reg_peakday_peak_hour'][simulation_yr_to_plot][fueltype_elec_int]],
                        columns=data['regions'])

                    peak_hour_demand = peak_hour_demand.append(realisation_data)

                    # --Peak hour demand / pop per region (dataframe with row: realisation, column=region)
                    realisation_data = pd.DataFrame(
                        [results_container['ed_reg_peakday_peak_hour_per_pop'][simulation_yr_to_plot][fueltype_elec_int]],
                        columns=data['regions'])

                    peak_hour_demand_per_person = peak_hour_demand_per_person.append(realisation_data)

                    # --National peak
                    simulation_yrs_result = [results_container['national_peak'][year][fueltype_elec_int] for year in simulation_yrs_to_plot]

                    realisation_data = pd.DataFrame(
                        [simulation_yrs_result],
                        columns=data['assumptions']['sim_yrs'])
                    national_peak = national_peak.append(realisation_data)

                    # --Regional percentage of national peak demand
                    realisation_data = pd.DataFrame(
                        [results_container['regional_share_national_peak'][simulation_yr_to_plot]],
                        columns=data['regions'])
                    regional_share_national_peak = regional_share_national_peak.append(realisation_data)

                    # --Regional percentage of national peak demand per person
                    realisation_data = pd.DataFrame(
                        [results_container['regional_share_national_peak_pp'][simulation_yr_to_plot]],
                        columns=data['regions'])

                    regional_share_national_peak_pp = regional_share_national_peak_pp.append(realisation_data)

                    # Mean demand of peak day
                    simulation_yrs_result = [results_container['mean_peak_day_demand'][year][fueltype_elec_int] for year in simulation_yrs_to_plot]
                    realisation_data = pd.DataFrame(
                        [simulation_yrs_result],
                        columns=data['assumptions']['sim_yrs'])
                    daily_mean_peak_day = daily_mean_peak_day.append(realisation_data)
                except:
                    raise Exception("The run '{}' is corrupted".format(path_result_folder))

            # Add to scenario container
            result_entry = {
                'national_heating_peak': national_heating_peak,
                'scenario_name': scenario_name,
                'peak_hour_demand': peak_hour_demand,
                'peak_hour_demand_per_person': peak_hour_demand_per_person,
                'national_peak': national_peak,
                'regional_share_national_peak': regional_share_national_peak,
                'regional_share_national_peak_pp': regional_share_national_peak_pp,
                'total_regional_demand_electricity': total_regional_demand_electricity,
                'national_electricity': national_electricity,
                'national_gas': national_gas,
                'national_hydrogen': national_hydrogen,
                'daily_mean_peak_day': daily_mean_peak_day}

            scenario_result_container.append(result_entry)

            # ---------------------------------------------------------------
            # TEST PLOT X-axis: Contribution to peak y-axis: Std: deviation
            # ---------------------------------------------------------------
            x_chart_yrs_storage[simulation_yr_to_plot][scenario_name] = result_entry

        # ------------------------------
        # Plot national sum over time per fueltype and scenario
        # ------------------------------
        crit_smooth_line = False
        seperate_legend = True

        try:
            print("... plotting national sum of fueltype over time ")
            fig_3_plot_over_time.fueltypes_over_time(
                scenario_result_container=scenario_result_container,
                sim_yrs=data['assumptions']['sim_yrs'],
                fig_name="fueltypes_over_time__{}__{}.pdf".format(simulation_yr_to_plot, fueltype_str),
                fueltypes=['electricity', 'gas', 'hydrogen'],
                result_path=result_path,
                unit='TWh',
                plot_points=True,
                crit_smooth_line=crit_smooth_line,
                seperate_legend=seperate_legend)
        except:
            raise Exception("FAILS national sum")

        # ------------------------------
        # Plot national peak change over time for each scenario including weather variability
        # ------------------------------
        try:
            fig_3_plot_over_time.scenario_over_time(
                scenario_result_container=scenario_result_container,
                field_name='national_peak',
                sim_yrs=data['assumptions']['sim_yrs'],
                fig_name="scenarios_peak_over_time__{}__{}.pdf".format(simulation_yr_to_plot, fueltype_str),
                plot_points=True,
                result_path=result_path,
                crit_smooth_line=crit_smooth_line,
                seperate_legend=seperate_legend)
        except:
            raise Exception("FAILED")
            pass

        # ------------------------------
        # Plot heating peak change over time for each scenario including weather variability
        # ------------------------------
        try:
            fig_3_plot_over_time.scenario_over_time(
                scenario_result_container=scenario_result_container,
                field_name='national_heating_peak',
                sim_yrs=data['assumptions']['sim_yrs'],
                fig_name="scenarios_heating_peak_over_time__{}__{}.pdf".format(simulation_yr_to_plot, fueltype_str),
                plot_points=True,
                result_path=result_path,
                crit_smooth_line=crit_smooth_line,
                seperate_legend=seperate_legend)
        except:
            raise Exception("FAILED")
            pass

        # ------------------------------
        # plot PEAK DAY mean
        # ------------------------------
        try:
            fig_3_plot_over_time.scenario_over_time(
                scenario_result_container=scenario_result_container,
                field_name='daily_mean_peak_day',
                sim_yrs=data['assumptions']['sim_yrs'],
                fig_name="mean_demand_of_peak_day{}__{}.pdf".format(simulation_yr_to_plot, fueltype_str),
                plot_points=True,
                result_path=result_path,
                crit_smooth_line=crit_smooth_line,
                seperate_legend=seperate_legend)
        except:
            raise Exception("FAILED")
            pass

    ## ------------------------------
    ## Plotting x-chart
    ## ------------------------------
    fig_3_plot_over_time.plot_std_dev_vs_contribution(
        scenario_result_container=x_chart_yrs_storage,
        sim_yrs=data['assumptions']['sim_yrs'],
        fig_name="_scenarios_4_chart_absolute.pdf",
        fueltypes=['electricity'],
        result_path=result_path,
        path_shapefile_input=path_shapefile_input,
        unit='TWh',
        plot_points=True)

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