hub/imports/schedules/doe_idf.py

150 lines
6.3 KiB
Python

"""
Building test
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Guille Gutierrez Morote Guillermo.GutierrezMorote@concordia.ca
Code contributors: Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
"""
import pandas as pd
import parseidf
import xmltodict
from imports.schedules.helpers.schedules_helper import SchedulesHelper
from city_model_structure.attributes.schedule import Schedule
from city_model_structure.building_demand.occupancy import Occupancy
from city_model_structure.building_demand.lighting import Lighting
from city_model_structure.building_demand.thermal_control import ThermalControl
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
def __init__(self, city, base_path, doe_idf_file):
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._schedule_library = xmltodict.parse(xml.read())
for building in self._city.buildings:
for internal_zone in building.internal_zones:
for usage_zone in internal_zone.usage_zones:
for schedule_archetype in self._schedule_library['archetypes']['archetypes']:
function = schedule_archetype['@building_type']
if SchedulesHelper.usage_from_function(function) == usage_zone.name:
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
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
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
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)
schedule.values = values
if 'Weekdays' in days_schedules[day_index]:
schedule.day_types = [cte.MONDAY, cte.TUESDAY, cte.WEDNESDAY, cte.THURSDAY, cte.FRIDAY]
elif 'Alldays' in days_schedules[day_index]:
schedule.day_types = [cte.MONDAY, cte.TUESDAY, cte.WEDNESDAY, cte.THURSDAY, cte.FRIDAY, cte.SATURDAY,
cte.SUNDAY]
elif 'Weekends' in days_schedules[day_index]:
# Weekends schedule present so let's re set the values
schedule.day_types = [cte.SATURDAY, cte.SUNDAY]
elif 'Saturday' in days_schedules[day_index]:
schedule.day_types = [cte.SATURDAY]
elif 'Sunday' in days_schedules[day_index]:
schedule.day_types = [cte.SUNDAY]
else:
continue
schedules.append(schedule)
for schedule in schedules:
if schedule.type == cte.OCCUPANCY:
if usage_zone.occupancy is None:
usage_zone.occupancy = Occupancy()
usage_zone.occupancy.occupancy_schedules = [schedule]
elif schedule.type == cte.LIGHTING:
if usage_zone.lighting is None:
usage_zone.lighting = Lighting()
usage_zone.lighting.schedules = [schedule]
elif schedule.type == cte.HVAC_AVAILABILITY:
if usage_zone.thermal_control is None:
usage_zone.thermal_control = ThermalControl()
usage_zone.thermal_control.hvac_availability_schedules = [schedule]