예제 #1
0
def calc_av_per_season_fueltype(results_every_year, seasons, model_yeardays_daytype):
    """Calculate average demand per season and fueltype for every fueltype

    Arguments
    ---------
    results_every_year : dict
        Results for every year
    seasons : dict
        Seasons
    model_yeardays_daytype : list
        Daytype of modelled days

    Returns
    -------
    av_season_daytype_cy :
        Average demand per season and daytype
    season_daytype_cy :
        Demand per season and daytpe
    """
    av_season_daytype_cy = defaultdict(dict)
    season_daytype_cy = defaultdict(dict)

    for year, fueltypes_data in results_every_year.items():

        for fueltype, reg_fuels in enumerate(fueltypes_data):

            # Summarise across regions
            tot_all_reg_fueltype = np.sum(reg_fuels, axis=0)

            tot_all_reg_fueltype_reshape = tot_all_reg_fueltype.reshape((365, 24))

            calc_av, calc_lp = load_profile.calc_av_lp(
                tot_all_reg_fueltype_reshape,
                seasons,
                model_yeardays_daytype)

            av_season_daytype_cy[year][fueltype] = calc_av
            season_daytype_cy[year][fueltype] = calc_lp

    return dict(av_season_daytype_cy), dict(season_daytype_cy)
예제 #2
0
def spatio_temporal_val(ed_fueltype_national_yh, ed_fueltype_regs_yh,
                        result_paths, paths, regions, seasons,
                        model_yeardays_daytype, plot_crit):
    """Validate spatial and temporal energy demands

    Info
    -----
    Because the floor area is only availabe for LADs from 2001,
    the LADs are converted to 2015 LADs.
    """
    logging.info("... temporal validation")

    fueltypes = lookup_tables.basic_lookups()['fueltypes']
    # -------------------------------------------
    # Spatial validation after calculations
    # -------------------------------------------
    subnational_elec = data_loader.read_lad_demands(
        paths['val_subnational_elec'])
    subnational_gas = data_loader.read_lad_demands(
        paths['val_subnational_gas'])

    # Create fueltype secific dict
    fuel_elec_regs_yh = {}
    for region_array_nr, region in enumerate(regions):
        fuel_elec_regs_yh[region] = np.sum(
            ed_fueltype_regs_yh[fueltypes['electricity']][region_array_nr])

    # Create fueltype secific dict
    fuel_gas_regs_yh = {}
    for region_array_nr, region in enumerate(regions):
        fuel_gas_regs_yh[region] = np.sum(
            ed_fueltype_regs_yh[fueltypes['gas']][region_array_nr])

    # ----------------------------------------
    # Remap demands between 2011 and 2015 LADs
    # ----------------------------------------
    subnational_elec = map_LAD_2011_2015(subnational_elec)
    subnational_gas = map_LAD_2011_2015(subnational_gas)
    fuel_elec_regs_yh = map_LAD_2011_2015(fuel_elec_regs_yh)
    fuel_gas_regs_yh = map_LAD_2011_2015(fuel_gas_regs_yh)

    spatial_validation(fuel_elec_regs_yh,
                       subnational_elec,
                       regions,
                       'elec',
                       fig_name=os.path.join(
                           result_paths['data_results_validation'],
                           'validation_spatial_elec_post_calcualtion.pdf'),
                       label_points=False,
                       plotshow=plot_crit)

    spatial_validation(fuel_gas_regs_yh,
                       subnational_gas,
                       regions,
                       'gas',
                       fig_name=os.path.join(
                           result_paths['data_results_validation'],
                           'validation_spatial_gas_post_calcualtion.pdf'),
                       label_points=False,
                       plotshow=plot_crit)

    # -------------------------------------------
    # Temporal validation (hourly for national)
    # -------------------------------------------
    # Read validation data
    elec_2015_indo, elec_2015_itsdo = elec_national_data.read_raw_elec_2015(
        paths['val_nat_elec_data'])

    f_diff_elec = np.sum(ed_fueltype_national_yh[
        fueltypes['electricity']]) / np.sum(elec_2015_indo)
    logging.info("... ed diff modellend and real [p] %s: ",
                 (1 - f_diff_elec) * 100)

    elec_factored_yh = f_diff_elec * elec_2015_indo

    temporal_validation(result_paths,
                        ed_fueltype_national_yh[fueltypes['electricity']],
                        elec_factored_yh, plot_crit)

    # ---------------------------------------------------
    # Calculate average season and daytypes and plot
    # ---------------------------------------------------
    logging.info("...calculate average data and plot per season and fueltype")

    calc_av_lp_modelled, calc_lp_modelled = load_profile.calc_av_lp(
        ed_fueltype_national_yh[fueltypes['electricity']], seasons,
        model_yeardays_daytype)

    calc_av_lp_real, calc_lp_real = load_profile.calc_av_lp(
        elec_factored_yh, seasons, model_yeardays_daytype)

    # Plot average daily loads
    fig_load_profile_dh_multiple.run(
        path_fig_folder=result_paths['data_results_validation'],
        path_plot_fig=os.path.join(result_paths['data_results_validation'],
                                   'validation_all_season_daytypes.pdf'),
        calc_av_lp_modelled=calc_av_lp_modelled,
        calc_av_lp_real=calc_av_lp_real,
        calc_lp_modelled=calc_lp_modelled,
        calc_lp_real=calc_lp_real,
        plot_peak=True,
        plot_radar=False,
        plot_all_entries=False,
        plot_max_min_polygon=True,
        plotshow=False,
        max_y_to_plot=60,
        fueltype_str=False,
        year=False)

    # ---------------------------------------------------
    # Validation of national electrictiy demand for peak
    # ---------------------------------------------------
    logging.debug("...validation of peak data: compare peak with data")

    # Because the coldest day is not the same for every region,
    # the coldest day needs to be defined manually or defined
    # by getting the hours with maximum electricity demand

    # Peak across all fueltypes WARNING: Fueltype specific
    peak_day_all_fueltypes = enduse_func.get_peak_day_all_fueltypes(
        ed_fueltype_national_yh)
    logging.info("Peak day 'peak_day_all_fueltypes': " +
                 str(peak_day_all_fueltypes))

    fueltype = fueltypes['electricity']
    peak_day_electricity, _ = enduse_func.get_peak_day_single_fueltype(
        ed_fueltype_national_yh[fueltype])
    logging.info("Peak day 'peak_day_electricity': " +
                 str(peak_day_electricity))

    elec_national_data.compare_peak(
        "validation_peak_elec_day_all_fueltypes.pdf",
        result_paths['data_results_validation'],
        elec_2015_indo[peak_day_all_fueltypes], ed_fueltype_national_yh[
            fueltypes['electricity']][peak_day_all_fueltypes],
        peak_day_all_fueltypes)

    elec_national_data.compare_peak(
        "validation_peak_elec_day_only_electricity.pdf",
        result_paths['data_results_validation'],
        elec_2015_indo[peak_day_electricity], ed_fueltype_national_yh[
            fueltypes['electricity']][peak_day_electricity],
        peak_day_electricity)

    # Manual peak day
    peak_day = 19
    elec_national_data.compare_peak(
        "validation_elec_peak_day_{}.pdf".format(peak_day),
        result_paths['data_results_validation'], elec_factored_yh[peak_day],
        ed_fueltype_national_yh[fueltypes['electricity']][peak_day], peak_day)

    peak_day = 33
    elec_national_data.compare_peak(
        "validation_elec_peak_day_{}.pdf".format(peak_day),
        result_paths['data_results_validation'], elec_factored_yh[peak_day],
        ed_fueltype_national_yh[fueltypes['electricity']][peak_day], peak_day)

    peak_day_real_electricity, _ = enduse_func.get_peak_day_single_fueltype(
        elec_2015_indo)
    logging.info("Peak day 'peak_day_electricity': " +
                 str(peak_day_real_electricity))

    #raise Exception
    elec_national_data.compare_peak(
        "validation_elec_peak_day_{}.pdf".format(peak_day_real_electricity),
        result_paths['data_results_validation'], elec_factored_yh[peak_day],
        ed_fueltype_national_yh[fueltypes['electricity']]
        [peak_day_real_electricity], peak_day_real_electricity)

    # ---------------------------------------------------
    # Validate boxplots for every hour (temporal validation)
    # ---------------------------------------------------
    elec_national_data.compare_results_hour_boxplots(
        "validation_hourly_boxplots_electricity_01.pdf",
        result_paths['data_results_validation'], elec_2015_indo,
        ed_fueltype_national_yh[fueltypes['electricity']])

    return
예제 #3
0
def tempo_spatial_validation(base_yr, model_yearhours_nrs, model_yeardays_nrs,
                             scenario_data, ed_fueltype_national_yh,
                             ed_fueltype_regs_yh, fueltypes, fueltypes_nr,
                             result_paths, paths, regions, reg_coord, seasons,
                             model_yeardays_daytype, plot_crit):
    """Validate national hourly demand for yearls fuel
    for all LADs. Test how the national disaggregation
    works.

    Info
    -----
    Because the floor area is only availabe for LADs from 2001,
    the LADs are converted to 2015 LADs.
    """
    logging.info("... spatial validation")

    # -------------------------------------------
    # Add electricity and gas for transportation sector
    # -------------------------------------------
    fueltype_elec = fueltypes['electricity']
    fuel_ktoe_transport_2015 = 385
    fuel_national_tranport = np.zeros((fueltypes_nr), dtype=float)

    # Elec demand from ECUK for transport sector
    fuel_national_tranport[fueltype_elec] = conversions.ktoe_to_gwh(
        fuel_ktoe_transport_2015)

    # Create transport model (add flat shapes)
    model_object_transport = generic_shapes.GenericFlatEnduse(
        fuel_national_tranport, model_yeardays_nrs)

    # Add national fuel
    ed_fueltype_national_yh = np.add(ed_fueltype_national_yh,
                                     model_object_transport.fuel_yh)

    # Add electricity of transportion to regional yh fuel proportionally to population
    for region_array_nr, region in enumerate(regions):

        # Disaggregation factor for transport electricity
        factor_transport_reg = scenario_data['population'][base_yr][
            region] / sum(scenario_data['population'][base_yr].values())
        ed_fueltype_regs_yh[fueltype_elec][
            region_array_nr] += model_object_transport.fuel_yh[
                fueltype_elec].reshape(
                    model_yearhours_nrs) * factor_transport_reg

    # -------------------------------------------
    # Spatial validation
    # -------------------------------------------
    subnational_elec = data_loader.read_national_real_elec_data(
        paths['path_val_subnational_elec'])
    subnational_gas = data_loader.read_national_real_gas_data(
        paths['path_val_subnational_gas'])

    # Create fueltype secific dict
    fuel_elec_regs_yh = {}
    for region_array_nr, region in enumerate(regions):
        gwh_modelled = np.sum(
            ed_fueltype_regs_yh[fueltypes['electricity']][region_array_nr])
        fuel_elec_regs_yh[region] = gwh_modelled

    # Create fueltype secific dict
    fuel_gas_regs_yh = {}
    for region_array_nr, region in enumerate(regions):
        gwh_modelled = np.sum(
            ed_fueltype_regs_yh[fueltypes['gas']][region_array_nr])
        fuel_gas_regs_yh[region] = gwh_modelled

    # ----------------------------------------
    # Remap demands between 2011 and 2015 LADs
    # ----------------------------------------
    subnational_elec = map_LAD_2011_2015(subnational_elec)
    subnational_gas = map_LAD_2011_2015(subnational_gas)
    fuel_elec_regs_yh = map_LAD_2011_2015(fuel_elec_regs_yh)
    fuel_gas_regs_yh = map_LAD_2011_2015(fuel_gas_regs_yh)

    logging.info("Validation of electricity")

    spatial_validation(reg_coord,
                       fuel_elec_regs_yh,
                       subnational_elec,
                       regions,
                       'elec',
                       os.path.join(result_paths['data_results_validation'],
                                    'validation_spatial_elec.pdf'),
                       label_points=False,
                       plotshow=plot_crit)

    logging.info("Validation of gas")
    spatial_validation(reg_coord,
                       fuel_gas_regs_yh,
                       subnational_gas,
                       regions,
                       'gas',
                       os.path.join(result_paths['data_results_validation'],
                                    'validation_spatial_gas.pdf'),
                       label_points=False,
                       plotshow=plot_crit)

    # -------------------------------------------
    # Temporal validation (hourly for national)
    # -------------------------------------------
    # Read validation data
    elec_2015_indo, elec_2015_itsdo = elec_national_data.read_raw_elec_2015(
        paths['path_val_nat_elec_data'])

    f_diff_elec = np.sum(ed_fueltype_national_yh[
        fueltypes['electricity']]) / np.sum(elec_2015_indo)
    logging.info("... ed diff modellend and real [p] %s: ",
                 (1 - f_diff_elec) * 100)

    elec_factored_yh = f_diff_elec * elec_2015_indo

    temporal_validation(result_paths,
                        ed_fueltype_national_yh[fueltypes['electricity']],
                        elec_factored_yh, elec_2015_indo, elec_2015_itsdo,
                        plot_crit)

    # ---------------------------------------------------
    # Calculate average season and daytypes and plot
    # ---------------------------------------------------
    logging.info("...calculate average data and plot per season and fueltype")

    calc_av_lp_modelled, calc_lp_modelled = load_profile.calc_av_lp(
        ed_fueltype_national_yh[fueltypes['electricity']], seasons,
        model_yeardays_daytype)

    calc_av_lp_real, calc_lp_real = load_profile.calc_av_lp(
        elec_factored_yh, seasons, model_yeardays_daytype)

    # Plot average daily loads
    plotting_results.plot_load_profile_dh_multiple(
        path_fig_folder=result_paths['data_results_validation'],
        path_plot_fig=os.path.join(result_paths['data_results_validation'],
                                   'validation_all_season_daytypes.pdf'),
        calc_av_lp_modelled=calc_av_lp_modelled,
        calc_av_lp_real=calc_av_lp_real,
        calc_lp_modelled=calc_lp_modelled,
        calc_lp_real=calc_lp_real,
        plot_peak=True,
        plot_radar=False,
        plot_all_entries=False,
        plot_max_min_polygon=True,
        plotshow=False,
        max_y_to_plot=60,
        fueltype_str=False,
        year=False)

    # ---------------------------------------------------
    # Validation of national electrictiy demand for peak
    # ---------------------------------------------------
    logging.debug("...validation of peak data: compare peak with data")

    # Peak across all fueltypes WARNING: Fueltype specific
    peak_day = enduse_func.get_peak_day_all_fueltypes(ed_fueltype_national_yh)

    elec_national_data.compare_peak(
        "validation_elec_peak_comparison_peakday_yh.pdf",
        result_paths['data_results_validation'], elec_2015_indo[peak_day],
        ed_fueltype_national_yh[fueltypes['electricity']][peak_day])

    # ---------------------------------------------------
    # Validate boxplots for every hour (temporal validation)
    # ---------------------------------------------------
    elec_national_data.compare_results_hour_boxplots(
        "validation_hourly_boxplots_electricity_01.pdf",
        result_paths['data_results_validation'], elec_2015_indo,
        ed_fueltype_national_yh[fueltypes['electricity']])

    return
예제 #4
0
def test_calc_av_lp():

    model_yeardays_daytype = [
        'holiday',
        'workday',
        'workday',
        'workday',
        'holiday',
        'workday',
        'workday',
        'workday',
        'holiday',
        'workday',
        'workday',
        'workday',
        'holiday',
        'workday',
        'workday',
        'workday',
    ]  #

    # Example with 16 days
    seasons = {}
    seasons['winter'] = [0, 1, 2, 3]
    seasons['spring'] = [4, 5, 6, 7]
    seasons['summer'] = [8, 9, 10, 11]
    seasons['autumn'] = [12, 13, 14, 15]

    demand_yh = np.zeros((16, 24)) + 10

    for i in range(4):
        demand_yh[i] = 12

    for i in range(4, 8):
        demand_yh[i] = 10

    # weekend
    demand_yh[0] = 100
    demand_yh[4] = 200

    result_av, _ = load_profile.calc_av_lp(demand_yh, seasons,
                                           model_yeardays_daytype)

    expected = {
        'winter': {
            'workday': np.full((24), 12),
            'holiday': np.full((24), 100)
        },
        'spring': {
            'workday': np.full((24), 10),
            'holiday': np.full((24), 200)
        },
        'summer': {
            'workday': np.full((24), 12),
            'holiday': np.full((24), 100)
        },
        'autmn': {
            'workday': np.full((24), 10),
            'holiday': np.full((24), 10)
        }
    }

    np.testing.assert_array_equal(expected['winter']['workday'],
                                  result_av['winter']['workday'])
    np.testing.assert_array_equal(expected['winter']['holiday'],
                                  result_av['winter']['holiday'])
    np.testing.assert_array_equal(expected['spring']['workday'],
                                  result_av['spring']['workday'])
    np.testing.assert_array_equal(expected['spring']['holiday'],
                                  result_av['spring']['holiday'])