示例#1
0
def temporal_validation_msoa_lad(ed_fueltype_national_yh, ed_fueltype_regs_yh,
                                 fueltypes, 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("... temporal validation")

    # -------------------------------------------
    # Electrictiy demands
    # -------------------------------------------

    # LAD level
    subnational_elec_lad = data_loader.read_lad_demands(
        paths['val_subnational_elec'])

    # MSOA level
    subnational_elec_msoa = data_loader.read_elec_data_msoa(
        paths['val_subnational_msoa_elec'])

    # 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_lad)
    fuel_elec_regs_yh = map_LAD_2011_2015(fuel_elec_regs_yh)

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

    return
示例#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 disaggr_demand(data, crit_temp_min_max, spatial_calibration=False):
    """Disaggregated demand

    Arguments
    ----------
    data : dict
        Data

    Returns
    --------
    disagg : dict
        Disaggregated energy demand
    spatial_calibration : bool
        Criteria wheter base year data should be used to
        calibrate spatial disaggregation (non-residential demand)
    """
    disagg = {}

    # ===========================================
    # I. Disaggregation
    # ===========================================
    base_yr = data['assumptions'].base_yr

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

    # Disaggregate fuel for all regions
    disagg['residential'], disagg['service'], disagg['industry'] = disaggregate_base_demand(
        data['pop_for_disag'],
        data['regions'],
        data['fuels'],
        data['scenario_data'],
        data['assumptions'],
        data['reg_coord'],
        data['weather_stations'], # Base year data used to disaggregate demand
        data['temp_data'][base_yr], # Base year data used to disaggregate demand
        data['sectors'],
        data['enduses'],
        data['service_building_count'],
        crit_temp_min_max)

    if spatial_calibration:
        '''The spatial disaggregation of non-residential demand
        can be calibrated for gas and electricity based on actual
        measured demand data

        Note: All other fueltypes are not scaled
        '''
        calibrate_residential = False # Calibrate residential demands
        calibrate_non_residential = True # Calibrate non residential demands

        # Non-residential electricity regional demands of base year for electrictiy and gas
        fueltype_elec = tech_related.get_fueltype_int('electricity')
        fueltype_gas = tech_related.get_fueltype_int('gas')

        if calibrate_non_residential:

            valid_non_resid_elec = data_loader.read_lad_demands(
                data['paths']['val_subnational_elec_non_residential'])

            valid_non_resid_gas = data_loader.read_lad_demands(
                data['paths']['val_subnational_gas_non_residential'])

            # Calculate and apply regional calibration factor
            for region in data['regions']:

                # Total modeleld non_residential
                service_demand_elec = 0
                service_demand_gas = 0
                for enduse_data in disagg['service'][region].values():
                    for sector_data in enduse_data.values():
                        service_demand_elec += np.sum(sector_data[fueltype_elec])
                        service_demand_gas += np.sum(sector_data[fueltype_gas])

                industry_demand_elec = 0
                industry_demand_gas = 0
                for enduse_data in disagg['industry'][region].values():
                    for sector_data in enduse_data.values():
                        industry_demand_elec += np.sum(sector_data[fueltype_elec])
                        industry_demand_gas += np.sum(sector_data[fueltype_gas])

                modelled_elec = service_demand_elec + industry_demand_elec
                modelled_gas = service_demand_gas + industry_demand_gas

                # Calculate calibration factor
                try:
                    f_spatial_calibration_elec = valid_non_resid_elec[region] / modelled_elec
                except KeyError: # No real data available
                    f_spatial_calibration_elec = 1

                try:
                    f_spatial_calibration_gas = valid_non_resid_gas[region] / modelled_gas
                except KeyError: # No real data available
                    f_spatial_calibration_gas = 1

                # Apply calibration factor to spatial disaggregation
                for enduse in disagg['service'][region].keys():
                    for sector in disagg['service'][region][enduse]:
                        disagg['service'][region][enduse][sector][fueltype_elec] *= f_spatial_calibration_elec
                        disagg['service'][region][enduse][sector][fueltype_gas] *= f_spatial_calibration_gas
                for enduse in disagg['industry'][region].keys():
                    for sector in disagg['industry'][region][enduse]:
                        disagg['industry'][region][enduse][sector][fueltype_elec] *= f_spatial_calibration_elec
                        disagg['industry'][region][enduse][sector][fueltype_gas] *= f_spatial_calibration_gas

        if calibrate_residential:

            valid_resid_elec = data_loader.read_lad_demands(
                data['paths']['val_subnational_elec_residential'])
            valid_resid_gas = data_loader.read_lad_demands(
                data['paths']['val_subnational_gas_residential'])

            # Calculate and apply regional calibration factor
            for region in data['regions']:

                # Total modeleld residential
                modelled_elec = 0
                modelled_gas = 0
                for enduse_data in disagg['residential'][region].values():
                    modelled_elec += np.sum(enduse_data[fueltype_elec])
                    modelled_gas += np.sum(enduse_data[fueltype_gas])

                # Calculate calibration factor
                try:
                    real_elec = valid_resid_elec[region]
                    f_spatial_calibration_elec = real_elec / modelled_elec
                    real_gas = valid_resid_gas[region]
                    f_spatial_calibration_gas = real_gas / modelled_gas
                except KeyError:
                    # No real data available
                    f_spatial_calibration_elec = 1
                    f_spatial_calibration_gas = 1

                # Apply calibration factor to spatial disaggregation
                for enduse in disagg['residential'][region].keys():
                    disagg['residential'][region][enduse][fueltype_elec] *= f_spatial_calibration_elec
                    disagg['residential'][region][enduse][fueltype_gas] *= f_spatial_calibration_gas
    else:
        pass

    # Sum demand across all sectors for every region
    disagg['ss_fuel_disagg_sum_all_sectors'] = init_scripts.sum_across_sectors_all_regs(
        disagg['service'])

    disagg['is_aggr_fuel_sum_all_sectors'] = init_scripts.sum_across_sectors_all_regs(
        disagg['industry'])

    # Sum demand across all submodels and sectors for every region
    disagg['tot_disaggregated_regs'] = init_scripts.sum_across_all_submodels_regs(
        data['regions'],
        [disagg['residential'], disagg['service'], disagg['industry']])

    disagg['tot_disaggregated_regs_residenital'] = init_scripts.sum_across_all_submodels_regs(
        data['regions'],
        [disagg['residential']])

    disagg['tot_disaggregated_regs_non_residential'] = init_scripts.sum_across_all_submodels_regs(
        data['regions'],
        [disagg['service'], disagg['industry']])

    return disagg
示例#4
0
def spatial_validation_lad_level(disaggregated_fuel, data_results_validation,
                                 paths, regions, reg_coord, plot_crit):
    """Spatial validation
    """
    fuel_elec_regs_yh = {}
    fuel_gas_regs_yh = {}
    fuel_gas_residential_regs_yh = {}
    fuel_gas_non_residential_regs_yh = {}
    fuel_elec_residential_regs_yh = {}
    fuel_elec_non_residential_regs_yh = {}

    lookups = lookup_tables.basic_lookups()
    # -------------------------------------------
    # Spatial validation
    # -------------------------------------------
    subnational_elec = data_loader.read_lad_demands(
        paths['val_subnational_elec'])
    subnational_elec_residential = data_loader.read_lad_demands(
        paths['val_subnational_elec_residential'])
    subnational_elec_non_residential = data_loader.read_lad_demands(
        paths['val_subnational_elec_non_residential'])
    subnational_gas = data_loader.read_lad_demands(
        paths['val_subnational_gas'])
    subnational_gas_residential = data_loader.read_lad_demands(
        paths['val_subnational_gas_residential'])
    subnational_gas_non_residential = data_loader.read_lad_demands(
        paths['val_subnational_gas_non_residential'])
    logging.info("compare total II {}  {}".format(
        sum(subnational_gas.values()),
        sum(subnational_gas_residential.values())))

    # Create fueltype secific dict
    for region in regions:
        fuel_elec_regs_yh[region] = disaggregated_fuel[
            'tot_disaggregated_regs'][region][lookups['fueltypes']
                                              ['electricity']]
        fuel_elec_residential_regs_yh[region] = disaggregated_fuel[
            'tot_disaggregated_regs_residenital'][region][lookups['fueltypes']
                                                          ['electricity']]
        fuel_elec_non_residential_regs_yh[region] = disaggregated_fuel[
            'tot_disaggregated_regs_non_residential'][region][
                lookups['fueltypes']['electricity']]
        fuel_gas_regs_yh[region] = disaggregated_fuel[
            'tot_disaggregated_regs'][region][lookups['fueltypes']['gas']]
        fuel_gas_residential_regs_yh[region] = disaggregated_fuel[
            'tot_disaggregated_regs_residenital'][region][lookups['fueltypes']
                                                          ['gas']]
        fuel_gas_non_residential_regs_yh[region] = disaggregated_fuel[
            'tot_disaggregated_regs_non_residential'][region][
                lookups['fueltypes']['gas']]

    # ----------------------------------------
    # Remap demands between 2011 and 2015 LADs
    # ----------------------------------------
    subnational_elec = map_LAD_2011_2015(subnational_elec)
    subnational_elec_residential = map_LAD_2011_2015(
        subnational_elec_residential)
    subnational_elec_non_residential = map_LAD_2011_2015(
        subnational_elec_non_residential)

    subnational_gas = map_LAD_2011_2015(subnational_gas)
    subnational_gas_residential = map_LAD_2011_2015(
        subnational_gas_residential)
    subnational_gas_non_residential = map_LAD_2011_2015(
        subnational_gas_non_residential)

    fuel_elec_regs_yh = map_LAD_2011_2015(fuel_elec_regs_yh)
    fuel_elec_residential_regs_yh = map_LAD_2011_2015(
        fuel_elec_residential_regs_yh)
    fuel_elec_non_residential_regs_yh = map_LAD_2011_2015(
        fuel_elec_non_residential_regs_yh)

    fuel_gas_regs_yh = map_LAD_2011_2015(fuel_gas_regs_yh)
    fuel_gas_residential_regs_yh = map_LAD_2011_2015(
        fuel_gas_residential_regs_yh)
    fuel_gas_non_residential_regs_yh = map_LAD_2011_2015(
        fuel_gas_non_residential_regs_yh)

    logging.info("compare total {}  {}".format(
        sum(fuel_gas_residential_regs_yh.values()),
        sum(fuel_gas_regs_yh.values())))

    # --------------------------------------------
    # Correct REAL Values that sum is the same
    # ----------------------------------------------
    data_inputlist = [
        (fuel_elec_residential_regs_yh,
         subnational_elec_residential),  # domestic
        (fuel_elec_non_residential_regs_yh, subnational_elec_non_residential)
    ]  # nondomestics

    spatial_validation_multiple(reg_coord=reg_coord,
                                input_data=data_inputlist,
                                regions=regions,
                                fueltype_str='elec',
                                fig_name=os.path.join(
                                    data_results_validation,
                                    'validation_multiple_elec.pdf'),
                                label_points=False,
                                plotshow=plot_crit)

    data_inputlist = [
        (fuel_gas_residential_regs_yh,
         subnational_gas_residential),  # domestic
        (fuel_gas_non_residential_regs_yh, subnational_gas_non_residential)
    ]  # nondomestics

    spatial_validation_multiple(reg_coord=reg_coord,
                                input_data=data_inputlist,
                                regions=regions,
                                fueltype_str='gas',
                                fig_name=os.path.join(
                                    data_results_validation,
                                    'validation_multiple_gas.pdf'),
                                label_points=False,
                                plotshow=plot_crit)

    logging.info("... Validation of electricity")
    spatial_validation(fuel_elec_regs_yh,
                       subnational_elec,
                       regions,
                       'elec',
                       os.path.join(data_results_validation,
                                    'validation_spatial_elec.pdf'),
                       label_points=True,
                       plotshow=plot_crit)

    logging.info("... Validation of residential electricity")
    spatial_validation(fuel_elec_residential_regs_yh,
                       subnational_elec_residential,
                       regions,
                       'elec',
                       os.path.join(data_results_validation,
                                    'validation_spatial_residential_elec.pdf'),
                       label_points=True,
                       plotshow=plot_crit)

    logging.info("... Validation of non-residential electricity")
    spatial_validation(fuel_elec_non_residential_regs_yh,
                       subnational_elec_non_residential,
                       regions,
                       'elec',
                       os.path.join(
                           data_results_validation,
                           'validation_spatial_non_residential_elec.pdf'),
                       label_points=True,
                       plotshow=plot_crit)

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

    logging.info("... Validation of residential gas")
    spatial_validation(fuel_gas_residential_regs_yh,
                       subnational_gas_residential,
                       regions,
                       'gas',
                       os.path.join(data_results_validation,
                                    'validation_spatial_residential_gas.pdf'),
                       label_points=True,
                       plotshow=plot_crit)

    logging.info("... Validation of non residential gas")
    spatial_validation(fuel_gas_non_residential_regs_yh,
                       subnational_gas_non_residential,
                       regions,
                       'gas',
                       os.path.join(
                           data_results_validation,
                           'validation_spatial_non_residential_gas.pdf'),
                       label_points=True,
                       plotshow=plot_crit)

    return
def run(simulation_yr_to_plot, demand_year_non_regional, demand_year_regional,
        fueltypes, fig_path, path_temporal_elec_validation,
        path_temporal_gas_validation, regions, 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")

    ######################################
    # Data preparation
    ######################################
    subnational_elec = data_loader.read_lad_demands(
        path_temporal_elec_validation)  # Read only domestic heating
    subnational_gas = data_loader.read_lad_demands(
        path_temporal_gas_validation)  # Read only domestic heating

    # ----------------------------------------
    # Remap demands between 2011 and 2015 LADs
    # ----------------------------------------
    subnational_elec = lad_validation.map_LAD_2011_2015(subnational_elec)
    subnational_gas = lad_validation.map_LAD_2011_2015(subnational_gas)

    # Create fueltype secific dict electricity
    fuel_elec_regs_yh_non_regional = {}
    for region_array_nr, region in enumerate(regions):
        gwh_modelled_tot = demand_year_non_regional[
            fueltypes['electricity']][region_array_nr]
        fuel_elec_regs_yh_non_regional[region] = gwh_modelled_tot

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

    fuel_elec_regs_yh_non_regional = lad_validation.map_LAD_2011_2015(
        fuel_elec_regs_yh_non_regional)
    fuel_gas_regs_yh = lad_validation.map_LAD_2011_2015(fuel_gas_regs_yh)

    ######################################
    # Plotting
    ######################################
    # Electrcity
    plot_spatial_validation(simulation_yr_to_plot,
                            fuel_elec_regs_yh_non_regional,
                            demand_year_regional,
                            subnational_elec,
                            regions,
                            'electricity',
                            fig_path=os.path.join(
                                fig_path,
                                "spatial_validation_electricity.pdf"),
                            label_points=False,
                            plotshow=plot_crit)

    # Gas
    plot_spatial_validation(simulation_yr_to_plot,
                            fuel_gas_regs_yh,
                            demand_year_regional,
                            subnational_gas,
                            regions,
                            'gas',
                            fig_path=os.path.join(
                                fig_path, "spatial_validation_gas.pdf"),
                            label_points=False,
                            plotshow=plot_crit)