def model_occ_schedules(model_json, threshold, period, output_file): """Translate a Model's occupancy schedules into a JSON of 0/1 values. \b Args: model_json: Full path to a Model JSON file. """ try: # re-serialize the Model with open(model_json) as json_file: data = json.load(json_file) model = Model.from_dict(data) # loop through the rooms and collect all unique occupancy schedules scheds, room_occupancy = [], {} for room in model.rooms: people = room.properties.energy.people if people is not None: model.properties.energy._check_and_add_schedule( people.occupancy_schedule, scheds) room_occupancy[room.identifier] = people.occupancy_schedule.identifier else: room_occupancy[room.identifier] = None # process the run period if it is supplied if period is not None and period != '' and period != 'None': a_per = AnalysisPeriod.from_string(period) start = Date(a_per.st_month, a_per.st_day) end = Date(a_per.end_month, a_per.end_day) a_period = AnalysisPeriod(start.month, start.day, 0, end.month, end.day, 23) timestep = a_per.timestep else: a_per = a_period = AnalysisPeriod() start, end, timestep = Date(1, 1), Date(12, 31), 1 # convert occupancy schedules to lists of 0/1 values schedules = {} for sch in scheds: values = [] for val in sch.values(timestep, start, end): is_occ = 0 if val < threshold else 1 values.append(is_occ) header = Header(Fraction(), 'fraction', a_period) schedules[sch.identifier] = HourlyContinuousCollection(header, values) if a_per.st_hour != 0 or a_per.end_hour != 23: schedules = {key: data.filter_by_analysis_period(a_per) for key, data in schedules.items()} schedules = {key: data.values for key, data in schedules.items()} # write out the JSON file occ_dict = {'schedules': schedules, 'room_occupancy': room_occupancy} output_file.write(json.dumps(occ_dict)) except Exception as e: _logger.exception('Model translation failed.\n{}'.format(e)) sys.exit(1) else: sys.exit(0)
def _load_analysis_period_str(analysis_period_str): """Load an AnalysisPeriod from a string. Args: analysis_period_str: A string of an AnalysisPeriod to be loaded. """ if analysis_period_str is not None and analysis_period_str != '' \ and analysis_period_str != 'None': return AnalysisPeriod.from_string(analysis_period_str)
def test_from_string_leap_year(): """Test creating analysis priod from a string.""" ap_string = '2/21 to 3/22 between 5 to 17 @1*' ap = AnalysisPeriod.from_string(ap_string) assert ap.st_hour == 5 assert ap.end_hour == 17 assert ap.st_month == 2 assert ap.end_month == 3 assert ap.st_day == 21 assert ap.end_day == 22 assert ap.timestep == 1
def test_from_string(self): """Test creating analysis priod from a string.""" ap_string = '2/21 to 3/22 between 5 to 17 @1' ap = AnalysisPeriod.from_string(ap_string) self.assertEqual(ap.st_hour, 5) self.assertEqual(ap.end_hour, 17) self.assertEqual(ap.st_month, 2) self.assertEqual(ap.end_month, 3) self.assertEqual(ap.st_day, 21) self.assertEqual(ap.end_day, 22) self.assertEqual(ap.timestep, 1)
def test_from_string_leap_year(self): """Test creating analysis priod from a string.""" ap_string = '2/21 to 3/22 between 5 to 17 @1*' ap = AnalysisPeriod.from_string(ap_string) assert ap.st_hour == 5 assert ap.end_hour == 17 assert ap.st_month == 2 assert ap.end_month == 3 assert ap.st_day == 21 assert ap.end_day == 22 assert ap.timestep == 1
def _load_run_period_str(run_period_str): """Load a RunPeriod from a string of a run period or analysis period. Args: run_period_str: A string of a RunPeriod or AnalysisPeriod to be loaded. """ if run_period_str is not None and run_period_str != '' \ and run_period_str != 'None': if run_period_str.startswith('RunPeriod'): return RunPeriod.from_string(run_period_str) else: return RunPeriod.from_analysis_period( AnalysisPeriod.from_string(run_period_str))