Пример #1
0
def test_get_season():
    """testing
    """
    result = date_prop.get_season(year_to_model=2018)

    assert result['summer'][
        0] == 151  #2018 regular year, first of june julianday - 1 for python
    assert result['autumn'][
        0] == 243  #2018 regular year, first of june julianday - 1 for python
    assert result['winter'][
        0] == 334  #2018 regular year, first of june julianday - 1 for python
    assert result['spring'][
        0] == 59  #2018 regular year, first of june julianday - 1 for python
Пример #2
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"))
Пример #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)
Пример #4
0
    def __init__(self,
                 lookup_enduses=None,
                 lookup_sector_enduses=None,
                 base_yr=None,
                 weather_by=None,
                 simulation_end_yr=None,
                 curr_yr=None,
                 sim_yrs=None,
                 paths=None,
                 enduses=None,
                 sectors=None,
                 reg_nrs=None):
        """Constructor
        """
        self.lookup_enduses = lookup_enduses
        self.lookup_sector_enduses = lookup_sector_enduses

        self.submodels_names = lookup_tables.basic_lookups()['submodels_names']
        self.nr_of_submodels = len(self.submodels_names)
        self.fueltypes = lookup_tables.basic_lookups()['fueltypes']
        self.fueltypes_nr = lookup_tables.basic_lookups()['fueltypes_nr']

        self.base_yr = base_yr
        self.weather_by = weather_by
        self.reg_nrs = reg_nrs
        self.simulation_end_yr = simulation_end_yr
        self.curr_yr = curr_yr
        self.sim_yrs = sim_yrs

        # ============================================================
        # Spatially modelled variables
        #
        # If spatial explicit diffusion is modelled, all parameters
        # or technologies having a spatial explicit diffusion need
        # to be defined.
        # ============================================================
        self.spatial_explicit_diffusion = 0  #0: False, 1: True

        # Define all variables which are affected by regional diffusion
        self.spatially_modelled_vars = []  # ['smart_meter_p']

        # Define technologies which are affected by spatial explicit diffusion
        self.techs_affected_spatial_f = ['heat_pumps_electricity']

        # Max penetration speed
        self.speed_con_max = 1  #1.5 # 1: uniform distribution >1: regional differences

        # ============================================================
        # Model calibration factors
        # ============================================================
        #
        #   These calibration factors are used to match the modelled
        #   electrictiy demand better with the validation data.
        #
        #   Weekend effects are used to distribut energy demands
        #   between working and weekend days. With help of these
        #   factors, the demand on weekends and holidays can be
        #   be lowered compared to working days.
        #   This factor can be applied either directly to an enduse
        #   or to the hdd or cdd calculations (to correct cooling
        #   or heating demand)
        #
        #       f_ss_cooling_weekend : float
        #           Weekend effect for cooling enduses
        #       f_ss_weekend : float
        #           WWeekend effect for service submodel enduses
        #       f_is_weekend : float
        #           Weekend effect for industry submodel enduses
        #       f_mixed_floorarea : float
        #           Share of floor_area which is assigned to either
        #           residential or non_residential floor area
        # ------------------------------------------------------------
        self.f_ss_cooling_weekend = 0.45  # Temporal calibration factor
        self.f_ss_weekend = 0.8  # Temporal calibration factor
        self.f_is_weekend = 0.45  # Temporal calibration factor

        # ============================================================
        #   Modelled day related factors
        # ============================================================
        #   model_yeardays_date : dict
        #     Contains for the base year for each days
        #     the information wheter this is a working or holiday
        # ------------------------------------------------------------
        self.model_yeardays = list(range(365))

        # Calculate dates
        self.model_yeardays_date = []
        for yearday in self.model_yeardays:
            self.model_yeardays_date.append(
                date_prop.yearday_to_date(base_yr, yearday))

        # ============================================================
        #   Dwelling stock related assumptions
        # ============================================================
        #
        #   Assumptions to generate a virtual dwelling stock
        #
        #       assump_diff_floorarea_pp : float
        #           Change in floor area per person (%, 1=100%)
        #       assump_diff_floorarea_pp_yr_until_changed : int
        #           Year until this change in floor area happens
        #       dwtype_distr_by : dict
        #           Housing Stock Distribution by Type
        #               Source: UK Housing Energy Fact File, Table 4c
        #       dwtype_distr_fy : dict
        #           welling type distribution end year
        #               Source: UK Housing Energy Fact File, Table 4c
        #       dwtype_floorarea_by : dict
        #           Floor area per dwelling type (Annex Table 3.1)
        #               Source: UK Housing Energy Fact File, Table 4c
        #       dwtype_floorarea_fy : dict
        #           Floor area per dwelling type
        #               Source: UK Housing Energy Fact File, Table 4c
        #       dwtype_age_distr : dict
        #           Floor area per dwelling type
        #               Source: Housing Energy Fact Sheet)
        #       yr_until_changed : int
        #           Year until change is realised
        #
        # https://www.gov.uk/government/statistics/english-housing-survey-2014-to-2015-housing-stock-report
        # ------------------------------------------------------------
        yr_until_changed_all_things = 2050

        self.dwtype_distr_by = {
            'semi_detached': 0.26,
            'terraced': 0.283,
            'flat': 0.203,
            'detached': 0.166,
            'bungalow': 0.088
        }

        self.dwtype_distr_fy = {
            'yr_until_changed': yr_until_changed_all_things,
            'semi_detached': 0.26,
            'terraced': 0.283,
            'flat': 0.203,
            'detached': 0.166,
            'bungalow': 0.088
        }

        self.dwtype_floorarea_by = {
            'semi_detached': 96,
            'terraced': 82.5,
            'flat': 61,
            'detached': 147,
            'bungalow': 77
        }

        self.dwtype_floorarea_fy = {
            'yr_until_changed': yr_until_changed_all_things,
            'semi_detached': 96,
            'terraced': 82.5,
            'flat': 61,
            'detached': 147,
            'bungalow': 77
        }

        # (Average builing age within age class, fraction)
        # The newest category of 2015 is added to implement change in refurbishing rate
        # For the base year, this is set to zero (if e.g. with future scenario set to 5%, then
        # proportionally to base year distribution number of houses are refurbished)
        self.dwtype_age_distr = {
            2015: {
                '1918': 0.21,
                '1941': 0.36,
                '1977.5': 0.3,
                '1996.5': 0.08,
                '2002': 0.05
            }
        }

        # ============================================================
        #  Scenario drivers
        # ============================================================
        #
        #   For every enduse the relevant factors which affect enduse
        #   consumption can be added in a list.
        #
        #   Note:   If e.g. floorarea and population are added, the
        #           effects will be overestimates (i.e. no multi-
        #           collinearity are considered).
        #
        #   scenario_drivers : dict
        #     Scenario drivers per enduse
        # ------------------------------------------------------------
        self.scenario_drivers = {

            # --Residential
            'rs_space_heating':
            ['floorarea',
             'hlc'],  # Do not use HDD or pop because otherweise double count
            'rs_water_heating': ['population'],
            'rs_lighting': ['population', 'floorarea'],
            'rs_cooking': ['population'],
            'rs_cold': ['population'],
            'rs_wet': ['population'],
            'rs_consumer_electronics': ['population', 'gva'],
            'rs_home_computing': ['population'],

            # --Service
            'ss_space_heating': ['floorarea'],
            'ss_water_heating': ['population'],
            'ss_lighting': ['floorarea'],
            'ss_catering': ['population'],
            'ss_ICT_equipment': ['population'],
            'ss_cooling_humidification': ['floorarea', 'population'],
            'ss_fans': ['floorarea', 'population'],
            'ss_small_power': ['population'],
            'ss_cooled_storage': ['population'],
            'ss_other_gas': ['population'],
            'ss_other_electricity': ['population'],

            # Industry
            'is_high_temp_process': ['gva'],
            'is_low_temp_process': ['gva'],
            'is_drying_separation': ['gva'],
            'is_motors': ['gva'],
            'is_compressed_air': ['gva'],
            'is_lighting': ['gva'],
            'is_space_heating': ['gva'],
            'is_other': ['gva'],
            'is_refrigeration': ['gva']
        }

        # ============================================================
        #   Cooling related assumptions
        # ============================================================
        #   assump_cooling_floorarea : int
        #       The percentage of cooled floor space in the base year
        #
        #   Literature
        #   ----------
        #   Abela, A. et al. (2016). Study on Energy Use by Air
        #   Conditioning. Bre, (June), 31. Retrieved from
        #   https://www.bre.co.uk/filelibrary/pdf/projects/aircon-energy-use/StudyOnEnergyUseByAirConditioningFinalReport.pdf
        # ------------------------------------------------------------

        # See Abela et al. (2016) & Carbon Trust. (2012). Air conditioning. Maximising comfort, minimising energy consumption
        self.cooled_ss_floorarea_by = 0.35

        # ============================================================
        # Smart meter related base year assumptions
        # ============================================================
        #   smart_meter_p_by : int
        #       The percentage of households with smart meters in by
        # ------------------------------------------------------------
        self.smart_meter_assump = {}

        # Currently in 2017 8.6 mio smart meter installed of 27.2 mio households --> 31.6%
        # https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/671930/Smart_Meters_2017_update.pdf)
        # In 2015, 5.8 % percent of all househods had one: https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/533060/2016_Q1_Smart_Meters_Report.pdf
        self.smart_meter_assump['smart_meter_p_by'] = 0.05

        # Long term smart meter induced general savings, purley as
        # a result of having a smart meter (e.g. 0.03 --> 3% savings)
        # DECC 2015: Smart Metering Early Learning Project: Synthesis report
        # https://www.gov.uk/government/publications/smart-metering-early-learning-project-and-small-scale-behaviour-trials
        # Reasonable assumption is between 0.03 and 0.01 (DECC 2015)
        self.smart_meter_assump['savings_smart_meter'] = {

            # Residential
            'rs_cold': 0.03,
            'rs_cooking': 0.03,
            'rs_lighting': 0.03,
            'rs_wet': 0.03,
            'rs_consumer_electronics': 0.03,
            'rs_home_computing': 0.03,
            'rs_space_heating': 0.03,
            'rs_water_heating': 0.03,

            # Service
            'ss_space_heating': 0.03,
            'ss_water_heating': 0.03,
            'ss_cooling_humidification': 0.03,
            'ss_fans': 0.03,
            'ss_lighting': 0.03,
            'ss_catering': 0.03,
            'ss_small_power': 0.03,
            'ss_ICT_equipment': 0.03,
            'ss_cooled_storage': 0.03,
            'ss_other_gas': 0.03,
            'ss_other_electricity': 0.03,

            # Industry submodule
            'is_high_temp_process': 0,
            'is_low_temp_process': 0,
            'is_drying_separation': 0,
            'is_motors': 0,
            'is_compressed_air': 0,
            'is_lighting': 0,
            'is_space_heating': 0,
            'is_other': 0,
            'is_refrigeration': 0
        }

        # ============================================================
        # Base temperature assumptions
        # ============================================================
        #
        #   Parameters related to smart metering
        #
        #   rs_t_heating : int
        #       Residential submodel base temp of heating of base year
        #   rs_t_cooling_by : int
        #       Residential submodel base temp of cooling of base year
        #   ...
        #
        #   Note
        #   ----
        #   Because demand for cooling cannot directly be linked to
        #   calculated cdd, the paramters 'ss_t_base_cooling' is used
        #   as a calibration factor. By artifiallcy lowering this
        #   parameter, the energy demand assignement over the days
        #   in a year is improved.
        # ------------------------------------------------------------
        t_bases = {
            'rs_t_heating': 15.5,
            'ss_t_heating': 15.5,
            'ss_t_cooling': 5,
            'is_t_heating': 15.5
        }

        self.t_bases = DummyClass(t_bases)

        # ============================================================
        # Enduses lists affed by hdd/cdd
        # ============================================================
        #
        #   These lists show for which enduses temperature related
        #   calculations are performed.
        #
        #   enduse_space_heating : list
        #       All enduses for which hdd are used for yd calculations
        #   ss_enduse_space_cooling : list
        #       All service submodel enduses for which cdd are used for
        #       yd calculations
        # ------------------------------------------------------------
        self.enduse_space_heating = [
            'rs_space_heating', 'ss_space_heating', 'is_space_heating'
        ]

        self.ss_enduse_space_cooling = ['ss_cooling_humidification']

        # ============================================================
        # Industry related
        #
        # High temperature processing (high_temp_ process) dominates
        # energy consumption in the iron and steel
        #
        # ---- Steel production - Enduse: is_high_temp_process, Sector: basic_metals
        # With industry service switch, the future shares of 'is_temp_high_process'
        # in sector 'basic_metals' can be set for 'basic_oxygen_furnace',
        # 'electric_arc_furnace', and 'SNG_furnace' can be specified
        #
        # ---- Cement production - Enduse: is_high_temp_process, Sector: non_metallic_mineral_products
        # Dry kilns, semidry kilns can be set
        # ============================================================

        # Share of cold rolling in steel manufacturing
        self.p_cold_rolling_steel_by = 0.2  # Estimated based on https://aceroplatea.es/docs/EuropeanSteelFigures_2015.pdf
        self.eff_cold_rolling_process = 1.8  # 80% more efficient than hot rolling Fruehan et al. (2002)
        self.eff_hot_rolling_process = 1.0  # 100% assumed efficiency

        # ============================================================
        # Assumption related to heat pump technologies
        # ============================================================
        #
        #   Assumptions related to technologies
        #
        #   gshp_fraction : list
        #       Fraction of installed gshp_fraction heat pumps in base year
        #       ASHP = 1 - gshp_fraction
        # ------------------------------------------------------------
        self.gshp_fraction = 0.1

        # Load defined technologies
        self.technologies, self.tech_list = read_data.read_technologies(
            paths['path_technologies'])

        self.installed_heat_pump_by = tech_related.generate_ashp_gshp_split(
            self.gshp_fraction)

        # Add heat pumps to technologies
        self.technologies, self.tech_list[
            'heating_non_const'], self.heat_pumps = tech_related.generate_heat_pump_from_split(
                self.technologies, self.installed_heat_pump_by, self.fueltypes)

        # ============================================================
        # Fuel Stock Definition
        # Provide for every fueltype of an enduse the share of fuel
        # which is used by technologies in the base year
        # ============================================================$
        fuel_tech_p_by = fuel_shares.assign_by_fuel_tech_p(
            enduses, sectors, self.fueltypes, self.fueltypes_nr)

        # ========================================
        # Get technologies of an enduse and sector
        # ========================================
        self.specified_tech_enduse_by = helpers.get_def_techs(fuel_tech_p_by)

        _specified_tech_enduse_by = helpers.add_undef_techs(
            self.heat_pumps, self.specified_tech_enduse_by,
            self.enduse_space_heating)
        self.specified_tech_enduse_by = _specified_tech_enduse_by

        # ========================================
        # General other info
        # ========================================
        self.seasons = date_prop.get_season(year_to_model=base_yr)
        self.model_yeardays_daytype, self.yeardays_month, self.yeardays_month_days = date_prop.get_yeardays_daytype(
            year_to_model=base_yr)

        # ========================================
        # Helper functions
        # ========================================
        self.fuel_tech_p_by, self.specified_tech_enduse_by, self.technologies = tech_related.insert_placholder_techs(
            self.technologies, fuel_tech_p_by, self.specified_tech_enduse_by)

        # ========================================
        # Calculations with assumptions
        # ========================================
        self.cdd_weekend_cfactors = hdd_cdd.calc_weekend_corr_f(
            self.model_yeardays_daytype, self.f_ss_cooling_weekend)

        self.ss_weekend_f = hdd_cdd.calc_weekend_corr_f(
            self.model_yeardays_daytype, self.f_ss_weekend)

        self.is_weekend_f = hdd_cdd.calc_weekend_corr_f(
            self.model_yeardays_daytype, self.f_is_weekend)

        # ========================================
        # Testing
        # ========================================
        testing_functions.testing_fuel_tech_shares(self.fuel_tech_p_by)

        testing_functions.testing_tech_defined(self.technologies,
                                               self.specified_tech_enduse_by)
Пример #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 = 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
Пример #6
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("===================================")
Пример #7
0
    def __init__(self,
                 base_yr=None,
                 curr_yr=None,
                 simulated_yrs=None,
                 paths=None,
                 enduses=None,
                 sectors=None,
                 fueltypes=None,
                 fueltypes_nr=None):

        yr_until_changed_all_things = 2050

        # Simulation parameters
        self.base_yr = base_yr
        self.curr_yr = curr_yr
        self.simulated_yrs = simulated_yrs

        # ============================================================
        # Spatially modelled variables
        # ============================================================
        # Define all variables which are affected by regional diffusion
        self.spatially_modelled_vars = ['smart_meter_improvement_p']

        # Define technologies which are affected by spatial explicit diffusion
        self.techs_affected_spatial_f = ['heat_pumps_electricity']

        # ============================================================
        # Model calibration factors
        # ============================================================
        #
        #   These calibration factors are used to match the modelled
        #   electrictiy demand better with the validation data.
        #
        #   Weekend effects are used to distribut energy demands
        #   between working and weekend days. With help of these
        #   factors, the demand on weekends and holidays can be
        #   be lowered compared to working days.
        #   This factor can be applied either directly to an enduse
        #   or to the hdd or cdd calculations (to correct cooling
        #   or heating demand)
        #
        #       f_ss_cooling_weekend : float
        #           Weekend effect for cooling enduses
        #       f_ss_weekend : float
        #           WWeekend effect for service submodel enduses
        #       f_is_weekend : float
        #           Weekend effect for industry submodel enduses
        #       f_mixed_floorarea : float
        #           Share of floor_area which is assigned to either
        #           residential or non_residential floor area
        # ------------------------------------------------------------

        # Temporal calibration factors
        self.f_ss_cooling_weekend = 0.45  # 0.55
        self.f_ss_weekend = 0.75  # 0.7
        self.f_is_weekend = 0.45  # 0.4

        # Spatial calibration factor
        self.f_mixed_floorarea = 0.5  # 0.5

        # ============================================================
        #   Modelled day related factors
        # ============================================================
        #
        #       model_yeardays_nrs : int
        #           Number of modelled yeardays (default=365)
        #       model_yearhours_nrs : int
        #           Number of modelled yearhours (default=8760)
        #        model_yeardays_date : dict
        #           Contains for the base year for each days
        #           the information wheter this is a working or holiday
        # ------------------------------------------------------------
        self.model_yeardays = list(range(365))

        # Calculate dates
        self.model_yeardays_date = []
        for yearday in self.model_yeardays:
            self.model_yeardays_date.append(
                date_prop.yearday_to_date(base_yr, yearday))

        self.model_yeardays_nrs = len(self.model_yeardays)
        self.model_yearhours_nrs = len(self.model_yeardays) * 24

        # ============================================================
        #   Dwelling stock related assumptions
        # ============================================================
        #
        #   Assumptions to generate a virtual dwelling stock
        #
        #       assump_diff_floorarea_pp : float
        #           Change in floor area per person (%, 1=100%)
        #       assump_diff_floorarea_pp_yr_until_changed : int
        #           Year until this change in floor area happens
        #       dwtype_distr_by : dict
        #           Housing Stock Distribution by Type
        #               Source: UK Housing Energy Fact File, Table 4c
        #       dwtype_distr_fy : dict
        #           welling type distribution end year
        #               Source: UK Housing Energy Fact File, Table 4c
        #       dwtype_floorarea_by : dict
        #           Floor area per dwelling type (Annex Table 3.1)
        #               Source: UK Housing Energy Fact File, Table 4c
        #       dwtype_floorarea_fy : dict
        #           Floor area per dwelling type
        #               Source: UK Housing Energy Fact File, Table 4c
        #       dwtype_age_distr : dict
        #           Floor area per dwelling type
        #               Source: Housing Energy Fact Sheet)
        #       yr_until_changed : int
        #           Year until change is realised
        #
        # https://www.gov.uk/government/statistics/english-housing-survey-2014-to-2015-housing-stock-report
        # ------------------------------------------------------------
        self.assump_diff_floorarea_pp = 1.0

        self.assump_diff_floorarea_pp_yr_until_changed = yr_until_changed_all_things

        self.dwtype_distr_by = {
            'semi_detached': 0.26,
            'terraced': 0.283,
            'flat': 0.203,
            'detached': 0.166,
            'bungalow': 0.088
        }

        self.dwtype_distr_fy = {
            'yr_until_changed': yr_until_changed_all_things,
            'semi_detached': 0.26,
            'terraced': 0.283,
            'flat': 0.203,
            'detached': 0.166,
            'bungalow': 0.088
        }

        self.dwtype_floorarea_by = {
            'semi_detached': 96,
            'terraced': 82.5,
            'flat': 61,
            'detached': 147,
            'bungalow': 77
        }

        self.dwtype_floorarea_fy = {
            'yr_until_changed': yr_until_changed_all_things,
            'semi_detached': 96,
            'terraced': 82.5,
            'flat': 61,
            'detached': 147,
            'bungalow': 77
        }

        # (Average builing age within age class, fraction)
        # The newest category of 2015 is added to implement change in refurbishing rate
        # For the base year, this is set to zero (if e.g. with future scenario set to 5%, then
        # proportionally to base year distribution number of houses are refurbished)
        self.dwtype_age_distr = {
            2015: {
                '1918': 0.21,
                '1941': 0.36,
                '1977.5': 0.3,
                '1996.5': 0.08,
                '2002': 0.05
            }
        }

        # ============================================================
        #   Scenario drivers
        # ============================================================
        #
        #   For every enduse the relevant factors which affect enduse
        #   consumption can be added in a list.
        #
        #   Note:   If e.g. floorarea and population are added, the
        #           effects will be overestimates (i.e. no multi-
        #           collinearity are considered).
        #
        #       scenario_drivers : dict
        #           Scenario drivers per enduse
        # ------------------------------------------------------------
        self.scenario_drivers = {}

        # --Residential SubModel
        self.scenario_drivers['rs_submodule'] = {
            'rs_space_heating':
            ['floorarea',
             'hlc'],  # Do not use HDD or pop because otherweise double count
            'rs_water_heating': ['population'],
            'rs_lighting': ['population', 'floorarea'],
            'rs_cooking': ['population'],
            'rs_cold': ['population'],
            'rs_wet': ['population'],
            'rs_consumer_electronics':
            ['population'
             ],  #GVA TODO. As soon as GVA is avaiable, drive it with GVA
            'rs_home_computing': ['population']
        }  #GVA

        # --Service Submodel (Table 5.5a)
        self.scenario_drivers['ss_submodule'] = {
            'ss_space_heating': ['floorarea'],
            'ss_water_heating': ['population'],
            'ss_lighting': ['floorarea'],
            'ss_catering': ['population'],
            'ss_ICT_equipment': ['population'],
            'ss_cooling_humidification': ['floorarea', 'population'],
            'ss_fans': ['floorarea', 'population'],
            'ss_small_power': ['population'],
            'ss_cooled_storage': ['population'],
            'ss_other_gas': ['population'],
            'ss_other_electricity': ['population']
        }

        # --Industry Submodel
        self.scenario_drivers['is_submodule'] = {
            'is_high_temp_process': ['gva'],
            'is_low_temp_process': ['gva'],
            'is_drying_separation': ['gva'],
            'is_motors': ['gva'],
            'is_compressed_air': ['gva'],
            'is_lighting': ['gva'],
            'is_space_heating': ['gva'],
            'is_other': ['gva'],
            'is_refrigeration': ['gva']
        }

        # ============================================================
        #   Cooling related assumptions
        # ============================================================
        #
        #   Parameters related to cooling enduses are defined here.
        #
        #   assump_cooling_floorarea : int
        #       The percentage of cooled floor space in the base year
        #
        #   Literature
        #   ----------
        #   Abela, A. et al. (2016). Study on Energy Use by Air
        #   Conditioning. Bre, (June), 31. Retrieved from
        #   https://www.bre.co.uk/filelibrary/pdf/projects/aircon-energy-use
        #   /StudyOnEnergyUseByAirConditioningFinalReport.pdf
        # ------------------------------------------------------------

        # See Abela et al. (2016)
        # Carbon Trust. (2012). Air conditioning. Maximising comfort, minimising energy consumption
        self.cooled_ss_floorarea_by = 0.35

        # ============================================================
        # Smart meter related base year assumptions
        # ============================================================
        #
        #   Parameters related to smart metering
        #
        #   smart_meter_p_by : int
        #       The percentage of households with smart meters in by
        #   smart_meter_diff_params : dict
        #       Sigmoid diffusion parameter of smater meters
        # ------------------------------------------------------------
        self.smart_meter_assump = {}
        self.smart_meter_assump['smart_meter_p_by'] = 0.1
        self.smart_meter_assump['smart_meter_diff_params'] = {
            'sig_midpoint': 0,
            'sig_steepness': 1
        }

        # ============================================================
        # Base temperature assumptions
        # ============================================================
        #
        #   Parameters related to smart metering
        #
        #   rs_t_heating_by : int
        #       Residential submodel base temp of heating of base year
        #   rs_t_cooling_by : int
        #       Residential submodel base temp of cooling of base year
        #   base_temp_diff_params : dict
        #       Sigmoid temperature diffusion parameters
        #   ...
        #
        #   Note
        #   ----
        #   Because demand for cooling cannot directly be linked to
        #   calculated cdd, the paramters 'ss_t_cooling_by' is used
        #   as a calibration factor. By artifiallcy lowering this
        #   parameter, the energy demand assignement over the days
        #   in a year is improved.
        # ------------------------------------------------------------
        t_bases = {}
        t_bases['rs_t_heating_by'] = 15.5  #
        #t_bases['rs_t_cooling_by'] = Not implemented

        t_bases['ss_t_heating_by'] = 15.5  #
        t_bases['ss_t_cooling_by'] = 5  # Orig: 5

        t_bases['is_t_heating_by'] = 15.5  #
        #self.t_bases['is_t_cooling_by'] = Not implemented

        self.t_bases = DummyClass(t_bases)

        self.base_temp_diff_params = {
            'sig_midpoint': 0,
            'sig_steepness': 1,
            'yr_until_changed': yr_until_changed_all_things
        }

        # ============================================================
        # Enduses lists affed by hdd/cdd
        # ============================================================
        #
        #   These lists show for which enduses temperature related
        #   calculations are performed.
        #
        #   enduse_space_heating : list
        #       All enduses for which hdd are used for yd calculations
        #   enduse_rs_space_cooling : list
        #       All residential enduses for which cdd are used for
        #       yd calculations
        #   ss_enduse_space_cooling : list
        #       All service submodel enduses for which cdd are used for
        #       yd calculations
        # ------------------------------------------------------------
        self.enduse_space_heating = [
            'rs_space_heating', 'ss_space_heating', 'is_space_heating'
        ]

        self.enduse_rs_space_cooling = []
        self.ss_enduse_space_cooling = ['ss_cooling_humidification']

        # ============================================================
        # Industry submodel related parameters
        # ============================================================
        #
        #   Assumptions related to industrial enduses
        #
        #   Overal changes in industry related enduse can be changed
        #   in 'enduse_overall_change_enduses'
        #
        # ------------------------------------------------------------

        # --------------------------------------------
        # heating
        # --------------------------------------------
        # --> No scenario drivers but driven by switches

        # --------------------------------------------
        # lighting
        #
        # No individual technologies are defined. Only
        # overall efficiency increase can be implemented
        #--------------------------------------------

        # --------------------------------------------
        # high_temp_ process
        #
        #       High temperature processing dominates energy consumption in the iron and steel,
        #       non-ferrous metal, bricks, cement, glass and potteries industries. This includes
        #          - coke ovens
        #          - blast furnaces and other furnaces
        #          - kilns and
        #          - glass tanks.
        # High consumption in Chemicals, Non_metallic mineral products, paper, food_production
        # Fuel use ratio - electric arc furnace over blast furnace steel making in cement sector
        # BAT - iron & steel - continous/Ingot casting 	Sectoral share - continuous %
        # --------------------------------------------

        # Share of cold rolling in steel manufacturing
        # *****************
        self.p_cold_rolling_steel_by = 0.2  # Estimated  based on https://aceroplatea.es/docs/EuropeanSteelFigures_2015.pdf
        self.eff_cold_rolling_process = 1.8  # 80% more efficient than hot rolling Fruehan et al. (2002)
        self.eff_hot_rolling_process = 1.0  # 100% assumed efficiency

        # Steel production - Enduse: is_high_temp_process, Sector: basic_metals
        # *****************
        # With industry service switch, the future shares
        # in 'basic_oxygen_furnace', 'electric_arc_furnace', and 'SNG_furnace'
        # can be specified

        #scrap-based production: electric arc furnace
        #direct reduction process: natrual gas based, electric arc furnace
        #BF-BOF (blast furnace - basix oxgen furnace)
        #       Example service switch:
        #           is_high_temp_process,SNG_furnace,1.0,2050,basic_metals

        # Cement production - Enduse: is_high_temp_process, Sector: non_metallic_mineral_products
        # *****************
        # technologies: Dry kilns, semidry kilns

        # CHEMICALs - Enduse: is_high_temp_process, Sector: CHEMICALS
        # *****************
        # technologies: Dry & wet kilns

        # ----------------
        # Efficiency of motors
        # ----------------
        #is_motors_eff_change = 0

        # ----------------
        # Efficiency of others
        # ----------------
        #is_others_eff_change = 0

        # ----------------
        # Efficiency of others
        # ----------------
        #is_refrigeration_eff_change = 0

        # ----------------
        # Efficiency of is_compressed_air
        # ----------------
        #is_compressed_air_eff_change =

        # ----------------
        # Efficiency of is_drying_separation
        # ----------------
        #is_drying_separation_eff_change =

        # ----------------
        # Efficiency of is_low_temp_process
        # ----------------
        #is_low_temp_process_eff_change =

        # ============================================================
        # Assumption related to heat pump technologies
        # ============================================================
        #
        #   Assumptions related to technologies
        #
        #   gshp_fraction : list
        #       Fraction of installed gshp_fraction heat pumps in base year
        #       ASHP = 1 - gshp_fraction
        # ------------------------------------------------------------
        self.gshp_fraction = 0.1

        self.technologies, self.tech_list = read_data.read_technologies(
            paths['path_technologies'], fueltypes)

        self.installed_heat_pump_by = tech_related.generate_ashp_gshp_split(
            self.gshp_fraction)

        # Add heat pumps to technologies
        self.technologies, self.tech_list[
            'heating_non_const'], self.heat_pumps = tech_related.generate_heat_pump_from_split(
                self.technologies, self.installed_heat_pump_by, fueltypes)

        # Collect all heating technologies
        # TODO: MAYBE ADD IN TECH DOC ANOTHER LIST SPECIFYING ALL HEATING TECHs
        self.heating_technologies = get_all_heating_techs(self.tech_list)

        # ============================================================
        # Enduse diffusion paramters
        # ============================================================
        #
        #   Assumptions related to general diffusion
        #
        #   This parameters are used to specify e.g. diffusion of
        #   an enduse which is not specified by technologies
        #   or the diffusion of a policy of changing a parameter
        #   over time.
        # ------------------------------------------------------------
        self.enduse_overall_change = {}
        self.enduse_overall_change['other_enduse_mode_info'] = {
            'diff_method': 'linear',
            'sigmoid': {
                'sig_midpoint': 0,
                'sig_steepness': 1
            }
        }

        # ============================================================
        # Fuel Stock Definition
        # Provide for every fueltype of an enduse
        # the share of fuel which is used by technologies for thebase year
        # ============================================================
        self.rs_fuel_tech_p_by, self.ss_fuel_tech_p_by, self.is_fuel_tech_p_by = assumptions_fuel_shares.assign_by_fuel_tech_p(
            enduses, sectors, fueltypes, fueltypes_nr)

        # ========================================
        # Get technologies of an enduse
        # ========================================
        self.rs_specified_tech_enduse_by = helpers.get_def_techs(
            self.rs_fuel_tech_p_by, sector_crit=False)

        self.ss_specified_tech_enduse_by = helpers.get_def_techs(
            self.ss_fuel_tech_p_by, sector_crit=True)

        self.is_specified_tech_enduse_by = helpers.get_def_techs(
            self.is_fuel_tech_p_by, sector_crit=True)

        rs_specified_tech_enduse_by_new = helpers.add_undef_techs(
            self.heat_pumps, self.rs_specified_tech_enduse_by,
            'rs_space_heating')
        self.rs_specified_tech_enduse_by = rs_specified_tech_enduse_by_new

        ss_specified_tech_enduse_by_new = helpers.add_undef_techs(
            self.heat_pumps, self.ss_specified_tech_enduse_by,
            'ss_space_heating')
        self.ss_specified_tech_enduse_by = ss_specified_tech_enduse_by_new

        is_specified_tech_enduse_by_new = helpers.add_undef_techs(
            self.heat_pumps, self.is_specified_tech_enduse_by,
            'is_space_heating')
        self.is_specified_tech_enduse_by = is_specified_tech_enduse_by_new

        # ============================================================
        # Read in switches
        # ============================================================

        # Read in scenaric fuel switches
        self.rs_fuel_switches = read_data.read_fuel_switches(
            paths['rs_path_fuel_switches'], enduses, fueltypes,
            self.technologies)
        self.ss_fuel_switches = read_data.read_fuel_switches(
            paths['ss_path_fuel_switches'], enduses, fueltypes,
            self.technologies)
        self.is_fuel_switches = read_data.read_fuel_switches(
            paths['is_path_fuel_switches'], enduses, fueltypes,
            self.technologies)

        # Read in scenaric service switches
        self.rs_service_switches = read_data.service_switch(
            paths['rs_path_service_switch'], self.technologies)
        self.ss_service_switches = read_data.service_switch(
            paths['ss_path_service_switch'], self.technologies)
        self.is_service_switches = read_data.service_switch(
            paths['is_path_industry_switch'], self.technologies)

        # Read in scenaric capacity switches
        self.rs_capacity_switches = read_data.read_capacity_switch(
            paths['rs_path_capacity_installation'])
        self.ss_capacity_switches = read_data.read_capacity_switch(
            paths['ss_path_capacity_installation'])
        self.is_capacity_switches = read_data.read_capacity_switch(
            paths['is_path_capacity_installation'])

        # Testing
        self.crit_switch_happening = testing_functions.switch_testing(
            fuel_switches=[
                self.rs_fuel_switches, self.ss_fuel_switches,
                self.is_fuel_switches
            ],
            service_switches=[
                self.rs_service_switches, self.ss_service_switches,
                self.is_service_switches
            ],
            capacity_switches=[
                self.rs_capacity_switches, self.ss_capacity_switches,
                self.is_capacity_switches
            ])

        # ========================================
        # General other assumptions
        # ========================================
        self.seasons = date_prop.get_season(year_to_model=base_yr)

        self.model_yeardays_daytype, self.yeardays_month, self.yeardays_month_days = date_prop.get_model_yeardays_daytype(
            year_to_model=base_yr)

        # ========================================
        # Helper functions
        # ========================================
        self.rs_fuel_tech_p_by, self.rs_specified_tech_enduse_by, self.technologies = tech_related.insert_placholder_techs(
            self.technologies,
            self.rs_fuel_tech_p_by,
            self.rs_specified_tech_enduse_by,
            sector_crit=False)

        self.ss_fuel_tech_p_by, self.ss_specified_tech_enduse_by, self.technologies = tech_related.insert_placholder_techs(
            self.technologies,
            self.ss_fuel_tech_p_by,
            self.ss_specified_tech_enduse_by,
            sector_crit=True)

        self.is_fuel_tech_p_by, self.is_specified_tech_enduse_by, self.technologies = tech_related.insert_placholder_techs(
            self.technologies,
            self.is_fuel_tech_p_by,
            self.is_specified_tech_enduse_by,
            sector_crit=True)

        # ========================================
        # Calculations with assumptions
        # ========================================
        self.cdd_weekend_cfactors = hdd_cdd.calc_weekend_corr_f(
            self.model_yeardays_daytype, self.f_ss_cooling_weekend)

        self.ss_weekend_f = hdd_cdd.calc_weekend_corr_f(
            self.model_yeardays_daytype, self.f_ss_weekend)

        self.is_weekend_f = hdd_cdd.calc_weekend_corr_f(
            self.model_yeardays_daytype, self.f_is_weekend)

        # ========================================
        # Testing
        # ========================================
        testing_functions.testing_fuel_tech_shares(self.rs_fuel_tech_p_by)
        for enduse in self.ss_fuel_tech_p_by:
            testing_functions.testing_fuel_tech_shares(
                self.ss_fuel_tech_p_by[enduse])
        for enduse in self.is_fuel_tech_p_by:
            testing_functions.testing_fuel_tech_shares(
                self.is_fuel_tech_p_by[enduse])

        testing_functions.testing_tech_defined(
            self.technologies, self.rs_specified_tech_enduse_by)
        testing_functions.testing_tech_defined(
            self.technologies, self.ss_specified_tech_enduse_by)
        testing_functions.testing_tech_defined(
            self.technologies, self.is_specified_tech_enduse_by)
Пример #8
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)
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("===================================")
Пример #11
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
Пример #12
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("===================================")
Пример #13
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("===================================")