def modify_monthly_multiplier(locator, config, measured_building_names): ##create building input schedules to set monthly multiplier archetypes_mapper.archetypes_mapper( locator, update_architecture_dbf=False, update_air_conditioning_systems_dbf=False, update_indoor_comfort_dbf=False, update_internal_loads_dbf=False, update_supply_systems_dbf=False, update_schedule_operation_cea=True, buildings=[]) ##calculate monthly multiplier based on buildings real consumption for building_name in measured_building_names: monthly_measured_data = pd.read_csv(locator.get_monthly_measurements()) fields_to_extract = ['Name'] + MONTHS_IN_YEAR_NAMES monthly_measured_demand = monthly_measured_data[ fields_to_extract].set_index('Name') monthly_measured_demand = monthly_measured_demand.loc[building_name] monthly_measured_demand = pd.DataFrame({ 'Month': monthly_measured_demand.index.values, 'measurements': monthly_measured_demand.values }) monthly_measured_load = monthly_measured_demand.measurements / max( monthly_measured_demand.measurements) path_to_schedule = locator.get_building_weekly_schedules(building_name) data_schedule, data_metadata = read_cea_schedule(path_to_schedule) data_metadata["MONTHLY_MULTIPLIER"] = list( monthly_measured_load.round(2)) # save cea schedule format save_cea_schedule(data_schedule, data_metadata, path_to_schedule)
def calc_mixed_schedule(locator, building_typology_df, buildings, list_var_names=None, list_var_values=None): """ Builds the ``cea.inputlocator.InputLocator#get_building_weekly_schedules`` for each building in the zone, combining the occupancy types as indicated in the inputs. :param cea.inputlocator.InputLocator locator: the locator to use :param building_typology_df: ``occupancy.dbf``, with an added column "mainuse" :param buildings: the list of buildings to calculate the schedules for :type buildings: list[str] :param list_var_names: List of column names in building_typology_df that contain the names of use-types being caculated :type list_var_names: list[str] :param list_var_values: List of column names in building_typology_df that contain values of use-type ratio in respect to list_var_names :type list_var_values: list[str] :return: """ if list_var_names is None: list_var_names = ["1ST_USE", '2ND_USE', '3RD_USE'] if list_var_values is None: list_var_values = ["1ST_USE_R", '2ND_USE_R', '3RD_USE_R'] metadata = 'mixed-schedule' schedule_data_all_uses = ScheduleData(locator) building_typology_df = building_typology_df.loc[ building_typology_df['Name'].isin(buildings)] # get list of uses only with a valid value in building_occupancy_df list_uses = get_list_of_uses_in_case_study(building_typology_df) internal_loads = pd.read_excel(locator.get_database_use_types_properties(), 'INTERNAL_LOADS') building_typology_df.set_index('Name', inplace=True) internal_loads = internal_loads.set_index('code') occupant_densities = {} for use in list_uses: if internal_loads.loc[use, 'Occ_m2pax'] > 0.0: occupant_densities[use] = 1.0 / internal_loads.loc[use, 'Occ_m2pax'] else: occupant_densities[use] = 0.0 for building in buildings: schedule_new_data, schedule_complementary_data = calc_single_mixed_schedule( list_uses, occupant_densities, building_typology_df, internal_loads, building, schedule_data_all_uses, list_var_names, list_var_values, metadata) # save cea schedule format path_to_building_schedule = locator.get_building_weekly_schedules( building) save_cea_schedule(schedule_new_data, schedule_complementary_data, path_to_building_schedule)
def schedule_dict_to_file(schedule_dict, schedule_path): schedule_data = schedule_dict['SCHEDULES'] schedule_complementary_data = { 'MONTHLY_MULTIPLIER': schedule_dict['MONTHLY_MULTIPLIER'], 'METADATA': schedule_dict['METADATA'] } data = pandas.DataFrame() for day in ['WEEKDAY', 'SATURDAY', 'SUNDAY']: df = pandas.DataFrame({'HOUR': range(1, 25), 'DAY': [day] * 24}) for schedule_type, schedule in schedule_data.items(): df[schedule_type] = schedule[day] data = data.append(df, ignore_index=True) save_cea_schedule(data.to_dict('list'), schedule_complementary_data, schedule_path) print('Schedule file written to {}'.format(schedule_path))
def dict_to_schedule(locator, building, schedule_dict): schedule_path = locator.get_building_weekly_schedules(building) schedule_data = schedule_dict['SCHEDULES'] schedule_complementary_data = { 'MONTHLY_MULTIPLIER': schedule_dict['MONTHLY_MULTIPLIER'], 'METADATA': schedule_dict['METADATA'] } data = pandas.DataFrame() for day in ['WEEKDAY', 'SATURDAY', 'SUNDAY']: df = pandas.DataFrame({'HOUR': range(1, 25), 'DAY': [day] * 24}) for schedule_type, schedule in schedule_data.items(): df[schedule_type] = schedule[day] data = data.append(df, ignore_index=True) save_cea_schedule(data.to_dict('list'), schedule_complementary_data, schedule_path)
def create_mixed_use_type(locator, internal_loads_df, indoor_comfort_df, use_type_name, use_type_metadata, use_type_ratios_dict): """ Takes a list of use-types and their respective ratios to aggregate and create a new use-type with schedules, internal loads and indoor comfort data :param schedules_path: :param internal_loads_df: :param indoor_comfort_df: :param use_type_name: :param use_type_metadata: :param use_type_ratios_dict: :return: """ list_uses = use_type_ratios_dict.keys() list_var_names = [ COLUMN_VAR_NAME_TEMPLATE.format(i) for i in range(len(use_type_ratios_dict)) ] list_var_values = [ COLUMN_VAR_VAL_TEMPLATE.format(i) for i in range(len(use_type_ratios_dict)) ] # Creating required parameters properties_dict = {} for i, (k, v) in enumerate(use_type_ratios_dict.items()): properties_dict[list_var_names[i]] = k properties_dict[list_var_values[i]] = v properties_df = pd.DataFrame([properties_dict]) occupant_densities = calculate_occupant_density(list_uses, internal_loads_df) print("Calculating internal loads...") new_internal_loads_df = calculate_mixed_loads( properties_df, internal_loads_df, occupant_densities, list_uses, use_type_name, list_var_names, list_var_values) print("Calculating indoor comfort...") new_indoor_comfort_df = calculate_mixed_loads( properties_df, indoor_comfort_df, occupant_densities, list_uses, use_type_name, list_var_names, list_var_values) prop_df_c = properties_df.copy() prop_df_c[ 'Name'] = '0' # Set a `Name` column as index for function to work prop_df_c.set_index('Name', inplace=True) schedule_data_all_uses = ScheduleData(locator) internal_loads = internal_loads_df.set_index('code') print("Calculating schedules...") schedule_new_data, schedule_complementary_data = calc_single_mixed_schedule( list_uses, occupant_densities, prop_df_c, internal_loads, '0', schedule_data_all_uses, list_var_names, list_var_values, use_type_metadata) print("Writing to disk...") use_type_properties_path = locator.get_database_use_types_properties() with pd.ExcelWriter(use_type_properties_path) as writer: new_internal_loads_df.to_excel(writer, sheet_name='INTERNAL_LOADS', index=False) new_indoor_comfort_df.to_excel(writer, sheet_name='INDOOR_COMFORT', index=False) schedule_path = os.path.join(locator.get_database_use_types_folder(), '{}.csv'.format(use_type_name)) save_cea_schedule(schedule_new_data, schedule_complementary_data, schedule_path)
def put(self): form = api.payload config = current_app.cea_config locator = cea.inputlocator.InputLocator(config.scenario) tables = form['tables'] geojsons = form['geojsons'] crs = form['crs'] schedules = form['schedules'] out = {'tables': {}, 'geojsons': {}} # TODO: Maybe save the files to temp location in case something fails for db in INPUTS: db_info = INPUTS[db] location = getattr(locator, db_info['location'])() file_type = db_info['file_type'] if len(tables[db]) != 0: if file_type == 'shp': from cea.utilities.standardize_coordinates import get_geographic_coordinate_system table_df = geopandas.GeoDataFrame.from_features( geojsons[db]['features'], crs=get_geographic_coordinate_system()) out['geojsons'][db] = json.loads( table_df.to_json(show_bbox=True)) table_df = table_df.to_crs(crs[db]) table_df.to_file(location, driver='ESRI Shapefile', encoding='ISO-8859-1') table_df = pandas.DataFrame( table_df.drop(columns='geometry')) out['tables'][db] = json.loads( table_df.set_index('Name').to_json(orient='index')) elif file_type == 'dbf': table_df = pandas.read_json(json.dumps(tables[db]), orient='index') # Make sure index name is 'Name; table_df.index.name = 'Name' table_df = table_df.reset_index() cea.utilities.dbf.dataframe_to_dbf(table_df, location) out['tables'][db] = json.loads( table_df.set_index('Name').to_json(orient='index')) else: # delete file if empty out['tables'][db] = {} if os.path.isfile(location): if file_type == 'shp': import glob for filepath in glob.glob( os.path.join( locator.get_building_geometry_folder(), '%s.*' % db)): os.remove(filepath) elif file_type == 'dbf': os.remove(location) if file_type == 'shp': out['geojsons'][db] = {} if schedules: for building in schedules: schedule_dict = schedules[building] schedule_path = locator.get_building_weekly_schedules(building) schedule_data = schedule_dict['SCHEDULES'] schedule_complementary_data = { 'MONTHLY_MULTIPLIER': schedule_dict['MONTHLY_MULTIPLIER'], 'METADATA': schedule_dict['METADATA'] } data = pandas.DataFrame() for day in ['WEEKDAY', 'SATURDAY', 'SUNDAY']: df = pandas.DataFrame({ 'HOUR': range(1, 25), 'DAY': [day] * 24 }) for schedule_type, schedule in schedule_data.items(): df[schedule_type] = schedule[day] data = data.append(df, ignore_index=True) save_cea_schedule(data.to_dict('list'), schedule_complementary_data, schedule_path) print('Schedule file written to {}'.format(schedule_path)) return out
def calc_mixed_schedule(locator, building_occupancy_df, buildings): metadata = 'mixed-schedule' schedule_data_all_uses = ScheduleData(locator) building_occupancy_df = building_occupancy_df.set_index('Name') building_occupancy_df = building_occupancy_df.loc[buildings] # get list of uses only with a valid value in building_occupancy_df list_uses = get_short_list_of_uses_in_case_study(building_occupancy_df) internal_DB = pd.read_excel(locator.get_archetypes_properties(), 'INTERNAL_LOADS') internal_DB = internal_DB.set_index('Code') occupant_densities = {} for use in list_uses: if internal_DB.loc[use, 'Occ_m2pax'] > 0.0: occupant_densities[use] = 1 / internal_DB.loc[use, 'Occ_m2pax'] else: occupant_densities[use] = 0.0 for building in buildings: schedule_new_data = {} main_use_this_building = building_occupancy_df['mainuse'][building] monthly_multiplier = np.sum([np.array( schedule_data_all_uses.schedule_complementary_data[use]['MONTHLY_MULTIPLIER']) * building_occupancy_df.loc[ building, use] for use in list_uses], axis=0) for schedule_type in VARIABLE_CEA_SCHEDULE_RELATION.values(): current_schedule = np.zeros(len(schedule_data_all_uses.schedule_data['HOTEL'][schedule_type])) normalizing_value = 0.0 if schedule_type in ['HEATING', 'COOLING']: schedule_new_data[schedule_type] = schedule_data_all_uses.schedule_data[main_use_this_building][ schedule_type] else: for use in list_uses: if building_occupancy_df[use][building] > 0.0: current_share_of_use = building_occupancy_df[use][building] if schedule_type in ['OCCUPANCY'] and occupant_densities[use] > 0.0: # for variables that depend on the number of people, the schedule needs to be calculated by number # of people for each use at each time step, not the share of the occupancy for each share_time_occupancy_density = current_share_of_use * occupant_densities[use] normalizing_value += share_time_occupancy_density current_schedule = np.vectorize(calc_average)(current_schedule, schedule_data_all_uses.schedule_data[use][ schedule_type], share_time_occupancy_density) if schedule_type in ['WATER'] and occupant_densities[use] > 0.0 and ( internal_DB.loc[use, 'Vw_lpdpax'] + internal_DB.loc[use, 'Vw_lpdpax']) > 0.0: # for variables that depend on the number of people, the schedule needs to be calculated by number # of people for each use at each time step, not the share of the occupancy for each share_time_occupancy_density = current_share_of_use * occupant_densities[use] * ( internal_DB.loc[use, 'Vw_lpdpax'] + internal_DB.loc[use, 'Vw_lpdpax']) normalizing_value += share_time_occupancy_density current_schedule = np.vectorize(calc_average)(current_schedule, schedule_data_all_uses.schedule_data[use][ schedule_type], share_time_occupancy_density) elif schedule_type in ['APPLIANCES'] and internal_DB.loc[use, 'Ea_Wm2'] > 0.0: share_time_occupancy_density = current_share_of_use * internal_DB.loc[use, 'Ea_Wm2'] normalizing_value += share_time_occupancy_density current_schedule = np.vectorize(calc_average)(current_schedule, schedule_data_all_uses.schedule_data[use][ schedule_type], share_time_occupancy_density) elif schedule_type in ['LIGHTING'] and internal_DB.loc[use, 'El_Wm2'] > 0.0: share_time_occupancy_density = current_share_of_use * internal_DB.loc[use, 'El_Wm2'] normalizing_value += share_time_occupancy_density current_schedule = np.vectorize(calc_average)(current_schedule, schedule_data_all_uses.schedule_data[use][ schedule_type], share_time_occupancy_density) elif schedule_type in ['PROCESSES'] and internal_DB.loc[use, 'Epro_Wm2'] > 0.0: share_time_occupancy_density = current_share_of_use * internal_DB.loc[use, 'Epro_Wm2'] normalizing_value += share_time_occupancy_density current_schedule = np.vectorize(calc_average)(current_schedule, schedule_data_all_uses.schedule_data[use][ schedule_type], share_time_occupancy_density) elif schedule_type in ['SERVERS'] and internal_DB.loc[use, 'Ed_Wm2'] > 0.0: share_time_occupancy_density = current_share_of_use * internal_DB.loc[use, 'Ed_Wm2'] normalizing_value += share_time_occupancy_density current_schedule = np.vectorize(calc_average)(current_schedule, schedule_data_all_uses.schedule_data[use][ schedule_type], share_time_occupancy_density) if normalizing_value == 0.0: schedule_new_data[schedule_type] = current_schedule * 0.0 else: schedule_new_data[schedule_type] = np.round(current_schedule/normalizing_value, 2) # add hour and day of the week DAY = {'DAY': ['WEEKDAY'] * 24 + ['SATURDAY'] * 24 + ['SUNDAY'] * 24} HOUR = {'HOUR': range(1, 25) + range(1, 25) + range(1, 25)} schedule_new_data.update(DAY) schedule_new_data.update(HOUR) # calculate complementary_data schedule_complementary_data = {'METADATA': metadata, 'MONTHLY_MULTIPLIER': monthly_multiplier} # save cea schedule format path_to_building_schedule = locator.get_building_weekly_schedules(building) save_cea_schedule(schedule_new_data, schedule_complementary_data, path_to_building_schedule)