Skip to content

Commit

Permalink
fix(schedule): Add a method to extract all schedules from INP file
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey authored and Chris Mackey committed Jun 7, 2024
1 parent 32a2233 commit b68a951
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
36 changes: 35 additions & 1 deletion honeybee_doe2/schedule.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding=utf-8
"""honeybee-doe2 schedule translators."""
from __future__ import division
import re

from ladybug.dt import Date, MONTHNAMES
from ladybug.analysisperiod import AnalysisPeriod
Expand All @@ -12,7 +13,7 @@

from .config import RES_CHARS
from .util import generate_inp_string, generate_inp_string_list_format, \
parse_inp_string
clean_inp_file_contents, parse_inp_string


"""____________TRANSLATORS FROM HONEYBEE TO INP____________"""
Expand Down Expand Up @@ -559,6 +560,39 @@ def schedule_ruleset_from_inp(year_inp_string, week_inp_strings, day_inp_strings
return sched


def extract_all_schedule_ruleset_from_inp_file(inp_file):
"""Extract all ScheduleRuleset objects from a DOE-2 INP file.
Args:
inp_file: A path to an INP file containing objects for SCHEDULE
(or SCHEDULE-PD) and corresponding WEEK-SCHEDULE-PD and DAY-SCHEDULE
(or DAY-SCHEDULE-PD) objects. The SCHEDULE will be used to assemble
all of these into a ScheduleRuleset.
Returns:
schedules -- A list of all Schedule objects in the INP file as
honeybee_energy ScheduleRuleset objects.
"""
# read the file and remove lines of comments
file_contents = clean_inp_file_contents(inp_file)
# extract all of the DAY-SCHEDULE objects
day_pattern1 = re.compile(r'(?i)(".*=.*DAY-SCHEDULE\n[\s\S]*?\.\.)')
day_pattern2 = re.compile(r'(?i)(".*=.*DAY-SCHEDULE-PD\n[\s\S]*?\.\.)')
day_sch_str = day_pattern1.findall(file_contents) + \
day_pattern2.findall(file_contents)
day_schedule_dict = _inp_day_schedule_dictionary(day_sch_str)
# extract all of the WEEK-SCHEDULE-PD objects
week_pattern = re.compile(r'(?i)(".*=.*WEEK-SCHEDULE-PD\n[\s\S]*?\.\.)')
week_sch_str = week_pattern.findall(file_contents)
week_sch_dict, week_dd_dict = _inp_week_schedule_dictionary(
week_sch_str, day_schedule_dict)
# extract all of the SCHEDULE objects and convert to ScheduleRuleset
year_pattern1 = re.compile(r'(?i)(".*=.*SCHEDULE\n[\s\S]*?\.\.)')
year_pattern2 = re.compile(r'(?i)(".*=.*SCHEDULE-PD\n[\s\S]*?\.\.)')
year_sch_str = year_pattern1.findall(file_contents) + \
year_pattern2.findall(file_contents)


"""______EXTRA UTILITY FUNCTIONS RELATED TO SCHEDULES______"""


Expand Down
21 changes: 21 additions & 0 deletions honeybee_doe2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import division

import re
import os


def generate_inp_string(u_name, command, keywords, values):
Expand Down Expand Up @@ -79,6 +80,26 @@ def generate_inp_string_list_format(u_name, command, keywords, values):
return inp_str


def clean_inp_file_contents(inp_file):
"""Get the contents of an INP file without any commented lines.
These comment lines might interfere with regex parsing if they are present.
Args:
inp_file: A path to an IDF file containing objects to be parsed.
Returns:
A single string for the clean IDF file contents.
"""
assert os.path.isfile(inp_file), 'Cannot find an INP file at: {}'.format(inp_file)
file_lines = []
with open(inp_file, 'r') as ep_file:
for line in ep_file:
if not line.startswith('$'):
file_lines.append(line)
return ''.join(file_lines)


def parse_inp_string(inp_string):
"""Parse an INP string of a single DOE-2 object into a list of values.
Expand Down

0 comments on commit b68a951

Please sign in to comment.