lib/openstudio-standards/btap/fileio.rb in openstudio-standards-0.1.15 vs lib/openstudio-standards/btap/fileio.rb in openstudio-standards-0.2.0.rc1

- old
+ new

@@ -19,10 +19,11 @@ require "#{File.dirname(__FILE__)}/btap" require 'fileutils' require 'csv' +require 'securerandom' #require 'rubygems' module BTAP module FileIO @@ -79,11 +80,11 @@ end def self.delete_files_in_folder_by_extention(folder,ext) BTAP::FileIO::get_find_files_from_folder_by_extension(folder, ext).each do |file| FileUtils.rm(file) - puts "#{file} deleted." + #puts "#{file} deleted." end end def self.find_file_in_folder_by_filename(folder,filename) Dir.glob("#{folder}/**/*#{filename}") @@ -106,11 +107,11 @@ def self.load_idf(filepath, name = "") #load file unless File.exist?(filepath) raise 'File does not exist: ' + filepath.to_s end - puts "loading file #{filepath}..." + #puts "loading file #{filepath}..." model_path = OpenStudio::Path.new(filepath.to_s) #Upgrade version if required. version_translator = OpenStudio::OSVersion::VersionTranslator.new model = OpenStudio::EnergyPlus::loadAndTranslateIdf(model_path) version_translator.errors.each {|error| puts "Error: #{error.logMessage}\n\n"} @@ -121,11 +122,11 @@ end model = model.get if name != "" self.set_name(model,name) end - puts "File #{filepath} loaded." + #puts "File #{filepath} loaded." return model end def self.replace_model(model,new_model,runner = nil) # pull original weather file object over @@ -138,14 +139,14 @@ if not original_weather_file.empty? original_weather_file.get.clone(new_model) end # pull original design days over - new_model.getDesignDays.each { |designDay| + new_model.getDesignDays.sort.each { |designDay| designDay.remove } - model.getDesignDays.each { |designDay| + model.getDesignDays.sort.each { |designDay| designDay.clone(new_model) } # swap underlying data in model with underlying data in new_model # remove existing objects from model @@ -172,11 +173,11 @@ #load file unless File.exist?(filepath) raise 'File does not exist: ' + filepath.to_s end - puts "loading file #{filepath}..." + #puts "loading file #{filepath}..." model_path = OpenStudio::Path.new(filepath.to_s) #Upgrade version if required. version_translator = OpenStudio::OSVersion::VersionTranslator.new model = version_translator.loadModel(model_path) version_translator.errors.each {|error| puts "Error: #{error.logMessage}\n\n"} @@ -187,11 +188,11 @@ end model = model.get if name != "" and not name.nil? self.set_name(model,name) end - puts "File #{filepath} loaded." + #puts "File #{filepath} loaded." return model end # This method loads an *Quest file into the model. @@ -201,11 +202,11 @@ def self.load_e_quest(filepath) #load file unless File.exist?(filepath) raise 'File does not exist: ' + filepath.to_s end - puts "loading equest file #{filepath}. This will only convert geometry." + #puts "loading equest file #{filepath}. This will only convert geometry." #Create an instancse of a DOE model doe_model = BTAP::EQuest::DOEBuilding.new() #Load the inp data into the DOE model. doe_model.load_inp(filepath) @@ -242,14 +243,14 @@ if not original_weather_file.empty? original_weather_file.get.clone(new_model) end # pull original design days over - new_model.getDesignDays.each { |designDay| + new_model.getDesignDays.sort.each { |designDay| designDay.remove } - model.getDesignDays.each { |designDay| + model.getDesignDays.sort.each { |designDay| designDay.clone(new_model) } # swap underlying data in model with underlying data in new_model # remove existing objects from model @@ -274,11 +275,11 @@ # @return [OpenStudio::Model::Model] a copy of the OpenStudio model object. def self.save_osm(model,filename) FileUtils.mkdir_p(File.dirname(filename)) File.delete(filename) if File.exist?(filename) model.save(OpenStudio::Path.new(filename)) - puts "File #{filename} saved." + #puts "File #{filename} saved." end # This method will translate to an E+ IDF format and save the model to an idf file. # @author Phylroy A. Lopez # @param model @@ -296,11 +297,11 @@ Find.find(filepath) { |file| if file[-4..-1] == ".idf" model = FileIO.load_idf(file) # this is a bit ugly but it works properly when called on a recursive folder structure FileIO.save_osm(model, (File.expand_path("..\\OSM-No_Space_Types\\", filepath) << "\\" << Pathname.new(file).basename.to_s)[0..-5]) - puts # empty line break + #puts # empty line break end } end @@ -319,11 +320,11 @@ osmfiles.each do |eso_file_path| #Run ESO Vars command must be run in folder. root_folder = Dir.getwd() - puts File.dirname(eso_file_path) + #puts File.dirname(eso_file_path) Dir.chdir(File.dirname(eso_file_path)) if File.exist?("eplustbl.htm") File.open("dummy.rvi", 'w') {|f| f.write("") } @@ -331,22 +332,22 @@ #get name of run from html file. runname = "" f = File.open("eplustbl.htm") f.each_line do |line| if line =~ /<p>Building: <b>(.*)<\/b><\/p>/ - puts "Found name: #{$1}" + #puts "Found name: #{$1}" runname = $1 break end end f.close #copy files over with distinct names - puts "copy hourly results to #{out_folder}/#{runname}_eplusout.csv" + #puts "copy hourly results to #{out_folder}/#{runname}_eplusout.csv" FileUtils.cp("eplusout.csv","#{out_folder}/#{runname}_eplusout.csv") - puts "copy html results to #{out_folder}/#{runname}_eplustbl.htm" + #puts "copy html results to #{out_folder}/#{runname}_eplustbl.htm" FileUtils.cp("eplustbl.htm","#{out_folder}/#{runname}_eplustbl.htm") - puts "copy sql results to #{out_folder}/#{runname}_eplusout.sql" + #puts "copy sql results to #{out_folder}/#{runname}_eplusout.sql" FileUtils.cp("eplusout.sql","#{out_folder}/#{runname}_eplusout.sql") list_of_csv_files << "#{out_folder}/#{runname}_eplusout.csv" end @@ -407,20 +408,20 @@ headers.each { |header| total = total + row[header] } return total end def self.terminus_hourly_output(csv_file) - puts "Starting Terminus output processing." - puts "reading #{csv_file} being processed" + #puts "Starting Terminus output processing." + #puts "reading #{csv_file} being processed" #reads csv file into memory. original = CSV.read(csv_file, { :headers => true, #This flag tell the parser that there are headers. :converters => :numeric #This tell it to convert string data into numeric when possible. } ) - puts "done reading #{csv_file} being processed" + #puts "done reading #{csv_file} being processed" # We are going to collect the header names that fit a pattern. But first we need to # create array containers to save the header name. In ruby we can use the string header names # as the array index. #Create arrays to store the header names for each type. @@ -459,24 +460,24 @@ facility_total_electrical_demand_headers << header if stripped_header =~/^.*:Facility Total Electric Demand Power \[W\]\(Hourly\)$/ boiler_gas_rate_headers << header if stripped_header =~/^.*:Boiler Gas Rate \[W\]\(Hourly\)/ end #Debug printout stuff. Make sure the output it captures the headers you want otherwise modify the regex above - puts waterheater_gas_rate_headers - puts waterheater_electric_rate_headers - puts waterheater_heating_rate_headers + #puts waterheater_gas_rate_headers + #puts waterheater_electric_rate_headers + #puts waterheater_heating_rate_headers - puts cooling_coil_electric_power_headers - puts cooling_coil_total_cooling_rate_headers + #puts cooling_coil_electric_power_headers + #puts cooling_coil_total_cooling_rate_headers - puts heating_coil_air_heating_rate_headers - puts heating_coil_gas_rate_headers + #puts heating_coil_air_heating_rate_headers + #puts heating_coil_gas_rate_headers - puts plant_supply_heating_demand_rate_headers - puts facility_total_electrical_demand_headers - puts boiler_gas_rate_headers - puts heating_coil_electric_power_headers + #puts plant_supply_heating_demand_rate_headers + #puts facility_total_electrical_demand_headers + #puts boiler_gas_rate_headers + #puts heating_coil_electric_power_headers #open up a new file to save the file to..Note: This will fail it the file is open in EXCEL. CSV.open("#{csv_file}.terminus_hourly.csv", 'w') do |csv| #Create header row for new terminus hourly file. @@ -529,11 +530,11 @@ facility_total_electrical_demand_total, boiler_gas_rate_headers_total ] end end - puts "Ending Terminus output processing." + #puts "Ending Terminus output processing." end def self.remove_rows_from_csv_table(start_index,stop_index,table) total_rows_to_remove = stop_index - start_index (0..total_rows_to_remove-1).each do |counter| @@ -542,11 +543,11 @@ return table end #load a model into OS & version translates, exiting and erroring if a problem is found - def safe_load_model(model_path_string) + def self.safe_load_model(model_path_string) model_path = OpenStudio::Path.new(model_path_string) if OpenStudio::exists(model_path) versionTranslator = OpenStudio::OSVersion::VersionTranslator.new model = versionTranslator.loadModel(model_path) if model.empty? @@ -564,11 +565,11 @@ def safe_load_sql(sql_path_string) sql_path = OpenStudio::Path.new(sql_path_string) if OpenStudio::exists(sql_path) sql = OpenStudio::SqlFile.new(sql_path) else - puts "#{sql_path} couldn't be found" + puts "Error: #{sql_path} couldn't be found" exit end return sql end @@ -583,11 +584,11 @@ returnArray = Array.new() variable_name_array.each do |variable_name| possible_key_values = openstudio_sql_file.availableKeyValues(env_period,timestep,variable_name) possible_variable_names = openstudio_sql_file.availableVariableNames(env_period,timestep).include?(variable_name) if not possible_variable_names.nil? and possible_variable_names.include?(variable_name) and not possible_key_values.nil? - possible_key_values.get.each do |key_value| + possible_key_values.get.sort.each do |key_value| unless regex_name_filter.match(key_value).nil? returnArray << get_timeseries_array(openstudio_sql_file, timestep, variable_name, key_value) end end end @@ -617,11 +618,11 @@ #we'll use this to check if the query will work before we send it. puts "*#{env_period}*#{timestep}*#{variable_name}" time_series_array = [] puts env_period.class if env_period.nil? - puts "here" + time_series_array = [nil] return time_series_array end possible_env_periods = openstudio_sql_file.availableEnvPeriods() if possible_env_periods.nil? @@ -691,11 +692,29 @@ si_quantity = OpenStudio::convert(ip_quantity, si_unit).get #puts "#{ip_quantity} = #{si_quantity}" return si_quantity.value end - - + def self.compile_qaqc_results(output_folder) + full_json = [] + Dir.foreach("#{output_folder}") do |folder| + next if folder == '.' or folder == '..' + Dir.glob("#{output_folder}/#{folder}/qaqc.json") { |item| + puts "Reading #{output_folder}/#{folder}/qaqc.json" + json = JSON.parse(File.read(item)) + json['eplusout_err']['warnings'] = json['eplusout_err']['warnings'].size + json['eplusout_err']['severe'] = json['eplusout_err']['warnings'].size + json['eplusout_err']['fatal'] = json['eplusout_err']['warnings'].size + json['run_uuid'] = SecureRandom.uuid + bldg = json['building']['name'].split('-') + json['building_type'] = bldg[1] + json['template'] = bldg[0] + full_json << json + } + end + File.open("#{output_folder}/../RESULTS-#{Time.now.strftime("%m-%d-%Y")}.json", 'w') {|f| f.write(JSON.pretty_generate(full_json)) } + end + # This is a simple example which uses rubyzip to # recursively generate a zip file from the contents of # a specified directory. The directory itself is not # included in the archive, rather just its contents. # \ No newline at end of file