summer_course_2024/imports/schedules/doe_idf.py

135 lines
5.5 KiB
Python
Raw Normal View History

"""
Building test
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Project Author Guille Gutierrez Morote Guillermo.GutierrezMorote@concordia.ca
Contributor Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
"""
import pandas as pd
import parseidf
2021-09-01 09:39:27 -04:00
import xmltodict
2021-09-16 13:45:27 -04:00
from imports.schedules.helpers.schedules_helper import SchedulesHelper
from city_model_structure.attributes.schedule import Schedule
import helpers.constants as cte
class DoeIdf:
"""
Idf factory to import schedules into the data model
"""
# todo: shouldn't this be in the schedule helper class????
idf_schedule_to_standard_schedule = {'BLDG_LIGHT_SCH': cte.LIGHTING,
'BLDG_OCC_SCH_wo_SB': cte.OCCUPANCY,
'BLDG_EQUIP_SCH': cte.EQUIPMENT,
'ACTIVITY_SCH': cte.ACTIVITY,
'INFIL_QUARTER_ON_SCH': cte.INFILTRATION}
# todo: @Guille -> in idf the types can be written in capital letters or low case, but both should be accepted.
# How is that solved?? Of course not like this
idf_data_type_to_standard_data_type = {'Fraction': cte.FRACTION,
'fraction': cte.FRACTION,
'Any Number': cte.ANY_NUMBER,
'ON/OFF': cte.ON_OFF,
'On/Off': cte.ON_OFF,
'Temperature': cte.TEMPERATURE,
'Humidity': cte.HUMIDITY,
'Control Type': cte.CONTROL_TYPE}
_SCHEDULE_COMPACT_TYPE = 'SCHEDULE:COMPACT'
_SCHEDULE_TYPE_NAME = 1
_SCHEDULE_TYPE_DATA_TYPE = 2
2021-09-16 13:45:27 -04:00
def __init__(self, city, base_path, doe_idf_file):
self._hours = []
panda_hours = pd.timedelta_range(0, periods=24, freq='H')
2021-08-27 12:51:30 -04:00
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)
2021-09-16 13:45:27 -04:00
print(path)
with open(path) as xml:
2021-09-16 13:45:27 -04:00
self._schedule_library = xmltodict.parse(xml.read())
2021-09-16 13:45:27 -04:00
for building in self._city.buildings:
for usage_zone in building.usage_zones:
for schedule_archetype in self._schedule_library['archetypes']['archetypes']:
function = schedule_archetype['@building_type']
if SchedulesHelper.usage_from_function(function) == usage_zone.usage:
self._idf_schedules_path = (base_path / schedule_archetype['idf']['path']).resolve()
with open(self._idf_schedules_path, 'r') as file:
idf = parseidf.parse(file.read())
self._load_schedule(idf, usage_zone)
break
2021-09-16 13:45:27 -04:00
def _load_schedule(self, idf, usage_zone):
schedules_day = {}
schedules = []
for compact_schedule in idf[self._SCHEDULE_COMPACT_TYPE]:
schedule = Schedule()
schedule.time_step = cte.HOUR
schedule.time_range = cte.DAY
if compact_schedule[self._SCHEDULE_TYPE_NAME] in self.idf_schedule_to_standard_schedule:
schedule.type = self.idf_schedule_to_standard_schedule[compact_schedule[self._SCHEDULE_TYPE_NAME]]
schedule.data_type = self.idf_data_type_to_standard_data_type[compact_schedule[self._SCHEDULE_TYPE_DATA_TYPE]]
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))
# save schedule
2022-02-17 12:48:23 -05:00
values = []
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
2021-08-27 12:51:30 -04:00
entry = days_schedules[hours_values + 1]
schedules_day[f'{days_schedules[day_index]}'].append(entry)
for entry in schedules_day[f'{days_schedules[day_index]}']:
values.append(entry)
2022-02-17 12:48:23 -05:00
print(values)
schedule.values = values
2021-08-25 14:29:30 -04:00
if 'Weekdays' in days_schedules[day_index]:
schedule.day_types = [cte.MONDAY, cte.TUESDAY, cte.WEDNESDAY, cte.THURSDAY, cte.FRIDAY]
2021-08-25 14:29:30 -04:00
elif 'Alldays' in days_schedules[day_index]:
schedule.day_types = [cte.MONDAY, cte.TUESDAY, cte.WEDNESDAY, cte.THURSDAY, cte.FRIDAY, cte.SATURDAY,
cte.SUNDAY]
2021-08-25 14:29:30 -04:00
elif 'Weekends' in days_schedules[day_index]:
# Weekends schedule present so let's re set the values
schedule.day_types = [cte.SATURDAY, cte.SUNDAY]
2021-08-25 14:29:30 -04:00
elif 'Saturday' in days_schedules[day_index]:
schedule.day_types = [cte.SATURDAY]
2021-08-25 14:29:30 -04:00
elif 'Sunday' in days_schedules[day_index]:
schedule.day_types = [cte.SUNDAY]
2021-08-25 14:29:30 -04:00
else:
continue
schedules.append(schedule)
usage_zone.schedules = schedules