lib/to_openstudio/model.rb in honeybee-openstudio-2.10.4 vs lib/to_openstudio/model.rb in honeybee-openstudio-2.11.0

- old
+ new

@@ -32,14 +32,43 @@ require 'honeybee/model' require 'openstudio' module Honeybee + + def self.write_schedule_csv(schedule_csv_dir, schedule_csv) + filename = schedule_csv[:filename] + columns = schedule_csv[:columns] + if !columns.empty? + n = columns[0].size + path = File.join(schedule_csv_dir, filename) + File.open(path, 'w') do |file| + (0...n).each do |i| + row = [] + columns.each do |column| + row << column[i] + end + file.puts row.join(',') + end + end + end + end + class Model attr_reader :openstudio_model + attr_reader :schedule_csv_dir, :include_datetimes, :schedule_csvs + # if a schedule csv dir is specified then ScheduleFixedIntervalAbridged objects + # will be translated to ScheduleFile objects instead of ScheduleFixedInterval + # the optional schedule_csv_include_datetimes argument controls whether schedule csv + # files include a first column of date times for verification + def set_schedule_csv_dir(schedule_csv_dir, include_datetimes = false) + @schedule_csv_dir = schedule_csv_dir + @include_datetimes = include_datetimes + end + # convert to openstudio model, clears errors and warnings def to_openstudio_model(openstudio_model=nil, log_report=true) @errors = [] @warnings = [] @@ -97,11 +126,11 @@ end if @hash[:properties][:energy][:schedule_type_limits] create_schedule_type_limits(@hash[:properties][:energy][:schedule_type_limits]) end if @hash[:properties][:energy][:schedules] - create_schedules(@hash[:properties][:energy][:schedules]) + create_schedules(@hash[:properties][:energy][:schedules], false, true) end if log_report puts 'Translating Materials' end @@ -281,11 +310,36 @@ schedule_type_limit_object.to_openstudio(@openstudio_model) end end end - def create_schedules(schedule_dicts, check_existing=false) + def create_schedules(schedule_dicts, check_existing=false, check_leap_year=true) + + # clear out schedule_csvs + @schedule_csvs = {} + + if check_leap_year + is_leap_year = :unknown + schedule_dicts.each do |schedule| + # set is leap year = true in case start date has 3 integers + this_leap_year = false + if schedule[:start_date] && schedule[:start_date][2] + this_leap_year = true + end + if is_leap_year == :unknown + is_leap_year = this_leap_year + elsif is_leap_year != this_leap_year + raise("Mixed leap year information.") + end + end + + if is_leap_year != :unknown + year_description = openstudio_model.getYearDescription + year_description.setIsLeapYear(is_leap_year) + end + end + schedule_dicts.each do |schedule| # check if there's already a schedule in the model with the identifier add_obj = true if check_existing object = @openstudio_model.getScheduleByName(schedule[:identifier]) @@ -303,13 +357,19 @@ when 'ScheduleFixedIntervalAbridged' schedule_object = ScheduleFixedIntervalAbridged.new(schedule) else raise("Unknown schedule type #{schedule_type}.") end - schedule_object.to_openstudio(@openstudio_model) + schedule_object.to_openstudio(@openstudio_model, @schedule_csv_dir, @include_datetimes, @schedule_csvs) end end + + # write schedule csvs + @schedule_csvs.each_value do |schedule_csv| + Honeybee.write_schedule_csv(@schedule_csv_dir, schedule_csv) + end + end def create_program_types(program_dicts, check_existing=false) program_dicts.each do |space_type| # check if there's already a space type in the model with the identifier @@ -341,10 +401,10 @@ create_schedule_type_limits([sch_type_limit], true) end end @@standards[:schedules].each do |schedule| if schedule[:identifier] == 'Always On' - create_schedules([schedule], true) + create_schedules([schedule], true, false) end end # set the default construction set to the building level of the Model construction_id = 'Default Generic Construction Set'