""" MyClass module SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2020 Project """ import pandas as pd import parseidf class DoeIdf: """ Idf factory to import schedules into the data model """ idf_schedule_to_commet_schedule = {'BLDG_LIGHT_SCH': 'Lights', 'BLDG_OCC_SCH_wo_SB': 'Occupancy', 'BLDG_EQUIP_SCH': 'Equipment', 'ACTIVITY_SCH': 'Activity', 'INFIL_QUARTER_ON_SCH': 'Infiltration'} _SCHEDULE_COMPACT_TYPE = 'SCHEDULE:COMPACT' _SCHEDULE_TYPE_NAME = 1 def __init__(self, city, base_path): self._hours = [] panda_hours = pd.timedelta_range(0, periods=24, freq='H') for _, hour in enumerate(panda_hours): self._hours.append(str(hour).replace('0 days ', '').replace(':00:00', ':00')) self._city = city path = str(base_path / doe_idf_file) with open(path) as xml: self._library = xmltodict.parse(xml.read()) for archetype in self._library['archetypes']['archetypes']: self._archetype_keys = {} for key, value in archetype.items(): if key[0] == '@': archetype_keys[key] = value self._idf_schedules_path = archetype['idf']['path'] with open(self._idf_schedules_path, 'r') as file: idf = parseidf.parse(file.read()) self._load_schedule(idf, archetype_keys['@building_type']) def _load_schedule(self, idf, building_usage): schedules_day = {} for compact_schedule in idf[self._SCHEDULE_COMPACT_TYPE]: if compact_schedule[self._SCHEDULE_TYPE_NAME] in self.idf_schedule_to_commet_schedule: schedule_type = self.idf_schedule_to_commet_schedule[compact_schedule[self._SCHEDULE_TYPE_NAME]] else: continue days_index = [] days_schedules = [] for position, elements in enumerate(compact_schedule): element_title = elements.title().replace('For: ', '') if elements.title() != element_title: days_index.append(position) # store a cleaned version of the compact schedule days_schedules.append(element_title) days_index.append(len(days_schedules)) # create panda data = {'WD': [], 'Sat': [], 'Sun': []} rows = [] for j in range(1, 13): rows.append(f'{j}am') for j in range(1, 13): rows.append(f'{j}pm') for i, day_index in enumerate(days_index): if day_index == len(days_schedules): break schedules_day[f'{days_schedules[day_index]}'] = [] hour_index = 0 for hours_values in range(day_index + 1, days_index[i + 1] - 1, 2): # Create 24h sequence for index, hour in enumerate(self._hours[hour_index:]): hour_formatted = days_schedules[hours_values].replace("Until: ", "") if len(hour_formatted) == 4: hour_formatted = f'0{hour_formatted}' if hour == hour_formatted: hour_index += index break entry = days_schedules[hours_values + 1] schedules_day[f'{days_schedules[day_index]}'].append(entry) if 'Weekdays' in days_schedules[day_index]: data['WD'] = [] for entry in schedules_day[f'{days_schedules[day_index]}']: data['WD'].append(entry) elif 'Alldays' in days_schedules[day_index]: data['WD'] = [] data['Sat'] = [] data['Sun'] = [] for entry in schedules_day[f'{days_schedules[day_index]}']: data['WD'].append(entry) data['Sat'].append(entry) data['Sun'].append(entry) elif 'Weekends' in days_schedules[day_index]: # Weekends schedule present so let's re set the values data['Sat'] = [] data['Sun'] = [] for entry in schedules_day[f'{days_schedules[day_index]}']: data['Sat'].append(entry) data['Sun'].append(entry) elif 'Saturday' in days_schedules[day_index]: data['Sat'] = [] for entry in schedules_day[f'{days_schedules[day_index]}']: data['Sat'].append(entry) elif 'Sunday' in days_schedules[day_index]: data['Sun'] = [] for entry in schedules_day[f'{days_schedules[day_index]}']: data['Sun'].append(entry) else: continue df = pd.DataFrame(data, index=rows) for building in self._city.buildings: for usage_zone in building.usage_zones: if usage_zone.usage == building_usage: if usage_zone.schedules is None: usage_zone.schedules = {} usage_zone.schedules[schedule_type] = df