def archetypes_mapper(locator, update_architecture_dbf,
                      update_air_conditioning_systems_dbf,
                      update_indoor_comfort_dbf, update_internal_loads_dbf,
                      update_supply_systems_dbf, update_schedule_operation_cea,
                      buildings):
    """
    algorithm to query building properties from statistical database
    Archetypes_HVAC_properties.csv. for more info check the integrated demand
    model of Fonseca et al. 2015. Appl. energy.

    :param InputLocator locator: an InputLocator instance set to the scenario to work on
    :param boolean update_architecture_dbf: if True, update the construction and architecture properties.
    :param boolean update_indoor_comfort_dbf: if True, get properties about thermal comfort.
    :param boolean update_air_conditioning_systems_dbf: if True, get properties about types of HVAC systems, otherwise False.
    :param boolean update_internal_loads_dbf: if True, get properties about internal loads, otherwise False.

    The following files are created by this script, depending on which flags were set:

    - building_HVAC: .dbf
        describes the queried properties of HVAC systems.

    - architecture.dbf
        describes the queried properties of architectural features

    - building_thermal: .shp
        describes the queried thermal properties of buildings

    - indoor_comfort.shp
        describes the queried thermal properties of buildings
    """
    # get occupancy and age files
    building_typology_df = dbf_to_dataframe(locator.get_building_typology())

    # validate list of uses in case study
    list_uses = get_list_of_uses_in_case_study(building_typology_df)

    # get occupant densities from archetypes schedules
    occupant_densities = {}
    occ_densities = pd.read_excel(locator.get_database_use_types_properties(),
                                  'INTERNAL_LOADS').set_index('code')
    for use in list_uses:
        if occ_densities.loc[use, 'Occ_m2pax'] > 0.0:
            occupant_densities[use] = 1 / occ_densities.loc[use, 'Occ_m2pax']
        else:
            occupant_densities[use] = 0.0

    # get properties about the construction and architecture
    if update_architecture_dbf:
        architecture_mapper(locator, building_typology_df)

    # get properties about types of HVAC systems
    if update_air_conditioning_systems_dbf:
        aircon_mapper(locator, building_typology_df)

    if update_indoor_comfort_dbf:
        indoor_comfort_mapper(list_uses, locator, occupant_densities,
                              building_typology_df)

    if update_internal_loads_dbf:
        internal_loads_mapper(list_uses, locator, occupant_densities,
                              building_typology_df)

    if update_schedule_operation_cea:
        calc_mixed_schedule(locator, building_typology_df, buildings)

    if update_supply_systems_dbf:
        supply_mapper(locator, building_typology_df)
def data_helper(locator, region, overwrite_technology_folder,
                update_architecture_dbf, update_HVAC_systems_dbf,
                update_indoor_comfort_dbf, update_internal_loads_dbf,
                update_supply_systems_dbf, update_schedule_operation_cea,
                buildings):
    """
    algorithm to query building properties from statistical database
    Archetypes_HVAC_properties.csv. for more info check the integrated demand
    model of Fonseca et al. 2015. Appl. energy.

    :param InputLocator locator: an InputLocator instance set to the scenario to work on
    :param boolean update_architecture_dbf: if True, update the construction and architecture properties.
    :param boolean update_indoor_comfort_dbf: if True, get properties about thermal comfort.
    :param boolean update_HVAC_systems_dbf: if True, get properties about types of HVAC systems, otherwise False.
    :param boolean update_internal_loads_dbf: if True, get properties about internal loads, otherwise False.

    The following files are created by this script, depending on which flags were set:

    - building_HVAC: .dbf
        describes the queried properties of HVAC systems.

    - architecture.dbf
        describes the queried properties of architectural features

    - building_thermal: .shp
        describes the queried thermal properties of buildings

    - indoor_comfort.shp
        describes the queried thermal properties of buildings
    """
    # get technology database
    if overwrite_technology_folder:
        # copy all the region-specific archetypes to the scenario's technology folder
        get_technology_related_databases(locator, region)

    # get occupancy and age files
    building_occupancy_df = dbf_to_dataframe(locator.get_building_occupancy())
    building_age_df = dbf_to_dataframe(locator.get_building_age())

    # validate list of uses in case study
    list_uses = get_list_of_uses_in_case_study(building_occupancy_df)

    # get occupant densities from archetypes schedules
    occupant_densities = {}
    occ_densities = pd.read_excel(locator.get_archetypes_properties(),
                                  'INTERNAL_LOADS').set_index('Code')
    for use in list_uses:
        if occ_densities.loc[use, 'Occ_m2pax'] > 0.0:
            occupant_densities[use] = 1 / occ_densities.loc[use, 'Occ_m2pax']
        else:
            occupant_densities[use] = 0.0

    # prepare shapefile to store results (a shapefile with only names of buildings
    names_df = building_age_df[['Name']]

    # define main use:
    building_occupancy_df['mainuse'] = calc_mainuse(building_occupancy_df,
                                                    list_uses)

    # dataframe with joined data for categories
    categories_df = building_occupancy_df.merge(building_age_df, on='Name')

    # get properties about the construction and architecture
    if update_architecture_dbf:
        architecture_DB = pd.read_excel(locator.get_archetypes_properties(),
                                        'ARCHITECTURE')
        architecture_DB['Code'] = architecture_DB.apply(lambda x: calc_code(
            x['building_use'], x['year_start'], x['year_end'], x['standard']),
                                                        axis=1)
        categories_df['cat_built'] = calc_category(architecture_DB,
                                                   categories_df, 'built', 'C')
        retrofit_category = ['envelope', 'roof', 'windows']
        for category in retrofit_category:
            categories_df['cat_' + category] = calc_category(
                architecture_DB, categories_df, category, 'R')

        prop_architecture_df = get_prop_architecture(categories_df,
                                                     architecture_DB,
                                                     list_uses)

        # write to dbf file
        prop_architecture_df_merged = names_df.merge(prop_architecture_df,
                                                     on="Name")

        fields = [
            'Name', 'Hs_ag', 'Hs_bg', 'Ns', 'Es', 'void_deck', 'wwr_north',
            'wwr_west', 'wwr_east', 'wwr_south', 'type_cons', 'type_leak',
            'type_roof', 'type_wall', 'type_win', 'type_shade'
        ]

        dataframe_to_dbf(prop_architecture_df_merged[fields],
                         locator.get_building_architecture())

    # get properties about types of HVAC systems
    if update_HVAC_systems_dbf:
        construction_properties_hvac = pd.read_excel(
            locator.get_archetypes_properties(), 'HVAC')
        construction_properties_hvac[
            'Code'] = construction_properties_hvac.apply(
                lambda x: calc_code(x['building_use'], x['year_start'], x[
                    'year_end'], x['standard']),
                axis=1)

        categories_df['cat_HVAC'] = calc_category(construction_properties_hvac,
                                                  categories_df, 'HVAC', 'R')

        # define HVAC systems types
        prop_HVAC_df = categories_df.merge(construction_properties_hvac,
                                           left_on='cat_HVAC',
                                           right_on='Code')

        # write to shapefile
        fields = [
            'Name', 'type_cs', 'type_hs', 'type_dhw', 'type_ctrl', 'type_vent',
            'heat_starts', 'heat_ends', 'cool_starts', 'cool_ends'
        ]
        prop_HVAC_df_merged = names_df.merge(prop_HVAC_df, on="Name")
        dataframe_to_dbf(prop_HVAC_df_merged[fields],
                         locator.get_building_air_conditioning())

    if update_indoor_comfort_dbf:
        comfort_DB = pd.read_excel(locator.get_archetypes_properties(),
                                   'INDOOR_COMFORT')

        # define comfort
        prop_comfort_df = categories_df.merge(comfort_DB,
                                              left_on='mainuse',
                                              right_on='Code')

        # write to shapefile
        fields = [
            'Name', 'Tcs_set_C', 'Ths_set_C', 'Tcs_setb_C', 'Ths_setb_C',
            'Ve_lpspax', 'RH_min_pc', 'RH_max_pc'
        ]
        prop_comfort_df_merged = names_df.merge(prop_comfort_df, on="Name")
        prop_comfort_df_merged = calculate_average_multiuse(
            fields, prop_comfort_df_merged, occupant_densities, list_uses,
            comfort_DB)

        dataframe_to_dbf(prop_comfort_df_merged[fields],
                         locator.get_building_comfort())

    if update_internal_loads_dbf:
        internal_DB = pd.read_excel(locator.get_archetypes_properties(),
                                    'INTERNAL_LOADS')

        # define comfort
        prop_internal_df = categories_df.merge(internal_DB,
                                               left_on='mainuse',
                                               right_on='Code')

        # write to shapefile
        fields = [
            'Name', 'Occ_m2pax', 'Qs_Wpax', 'X_ghpax', 'Ea_Wm2', 'El_Wm2',
            'Ed_Wm2', 'Qcre_Wm2', 'Vww_lpdpax', 'Vw_lpdpax', 'Qhpro_Wm2',
            'Qcpro_Wm2', 'Epro_Wm2'
        ]
        prop_internal_df_merged = names_df.merge(prop_internal_df, on="Name")
        prop_internal_df_merged = calculate_average_multiuse(
            fields, prop_internal_df_merged, occupant_densities, list_uses,
            internal_DB)

        dataframe_to_dbf(prop_internal_df_merged[fields],
                         locator.get_building_internal())

    if update_schedule_operation_cea:
        if buildings == []:
            buildings = locator.get_zone_building_names()
        calc_mixed_schedule(locator, building_occupancy_df, buildings)

    if update_supply_systems_dbf:
        supply_DB = pd.read_excel(locator.get_archetypes_properties(),
                                  'SUPPLY')
        supply_DB['Code'] = supply_DB.apply(lambda x: calc_code(
            x['building_use'], x['year_start'], x['year_end'], x['standard']),
                                            axis=1)

        categories_df['cat_supply'] = calc_category(supply_DB, categories_df,
                                                    'HVAC', 'R')

        # define HVAC systems types
        prop_supply_df = categories_df.merge(supply_DB,
                                             left_on='cat_supply',
                                             right_on='Code')

        # write to shapefile
        prop_supply_df_merged = names_df.merge(prop_supply_df, on="Name")
        fields = ['Name', 'type_cs', 'type_hs', 'type_dhw', 'type_el']
        dataframe_to_dbf(prop_supply_df_merged[fields],
                         locator.get_building_supply())