Module: BTAP::FileIO
- Defined in:
- lib/openstudio-standards/btap/fileio.rb
Class Method Summary collapse
- .clean_osm_file(file_path:, output_path:) ⇒ Object
- .compare_osm_files(model_true, model_compare) ⇒ Object
-
.convert_idf_to_osm(filepath) ⇒ Object
This method will recursively translate all IDFs in a folder to OSMs, and save them to the OSM_-No_Space_Types folder.
-
.deep_copy(model, bool = true) ⇒ OpenStudio::Model::Model
This method will return a deep copy of the model.
-
.load_osm(filepath, name = "") ⇒ OpenStudio::Model::Model
This method loads an OpenStudio file into the model.
- .remove_special_characters(hash) ⇒ Object
- .rename_handles(osm_data) ⇒ Object
-
.safe_load_model(model_path_string) ⇒ Object
load a model into OS & version translates, exiting and erroring if a problem is found.
-
.save_idf(model, filename) ⇒ OpenStudio::Model::Model
This method will translate to an E+ IDF format and save the model to an idf file.
-
.save_osm(model, filename) ⇒ OpenStudio::Model::Model
This method will save the model to an osm file.
- .save_osm_file(osm_data, output_path) ⇒ Object
- .set_all_data_time(osm_data) ⇒ Object
- .set_all_epw_paths(osm_data) ⇒ Object
Instance Method Summary collapse
Class Method Details
.clean_osm_file(file_path:, output_path:) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 402 def self.clean_osm_file(file_path:, output_path:) osm_data = [] current_hash = {} File.foreach(file_path) do |line| line.strip! next if line.empty? if line.start_with?('OS:') current_hash = { type: line.split(/[;,]/).first } osm_data << current_hash elsif line.include?('!-') key, value = line.split('!-').map(&:strip) current_hash[value] = key.chomp(',;').strip elsif line.include?('! ') # for lines that have '! ' but not '!-' key, value = line.split('! ').map(&:strip) current_hash[value] = key.chomp(',;').strip else current_hash[:id] ||= line.chomp(',;').strip end end osm_data.sort_by! { |hash| [hash[:type], hash['Name'] || ''] } handles = osm_data.map { |hash| hash['Handle'] }.compact duplicates = handles.select { |e| handles.count(e) > 1 } raise "Duplicate handles found: #{duplicates.uniq}" if duplicates.any? osm_data.each { |hash| remove_special_characters(hash) } set_all_data_time(osm_data) set_all_epw_paths(osm_data) rename_handles(osm_data) File.open(output_path, 'w') do |file| osm_data.each do |hash| file.puts "#{hash["type"]}," hash.each do |key, value| next if key == "type" is_last_key = (hash.keys - [:type]).last == key if key == :id file.puts " #{value}," else file.puts " #{value}#{is_last_key ? ';' : ','}".ljust(42) + " !- #{key}" end end file.puts end end end |
.compare_osm_files(model_true, model_compare) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 150 def self.compare_osm_files(model_true, model_compare) only_model_true = [] # objects only found in the true model only_model_compare = [] # objects only found in the compare model both_models = [] # objects found in both models diffs = [] # differences between the two models num_ignored = 0 # objects not compared because they don't have names # Define types of objects to skip entirely during the comparison object_types_to_skip = [ 'OS:EnergyManagementSystem:Sensor', # Names are UIDs 'OS:EnergyManagementSystem:Program', # Names are UIDs 'OS:EnergyManagementSystem:Actuator', # Names are UIDs 'OS:Connection', # Names are UIDs 'OS:PortList', # Names are UIDs 'OS:Building', # Name includes timestamp of creation 'OS:ModelObjectList' # Names are UIDs ] # Find objects in the true model only or in both models model_true.getModelObjects.sort.each do |true_object| # Skip comparison of certain object types next if object_types_to_skip.include?(true_object.iddObject.name) # Skip comparison for objects with no name unless true_object.iddObject.hasNameField num_ignored += 1 next end # Find the object with the same name in the other model compare_object = model_compare.getObjectByTypeAndName(true_object.iddObject.type, true_object.name.to_s) if compare_object.empty? only_model_true << true_object else both_models << [true_object, compare_object.get] end end # Report a diff for each object found in only the true model only_model_true.each do |true_object| diffs << "A #{true_object.iddObject.name} called '#{true_object.name}' was found only in the before model" end # Find objects in compare model only model_compare.getModelObjects.sort.each do |compare_object| # Skip comparison of certain object types next if object_types_to_skip.include?(compare_object.iddObject.name) # Skip comparison for objects with no name unless compare_object.iddObject.hasNameField num_ignored += 1 next end # Find the object with the same name in the other model true_object = model_true.getObjectByTypeAndName(compare_object.iddObject.type, compare_object.name.to_s) if true_object.empty? only_model_compare << compare_object end end # Report a diff for each object found in only the compare model only_model_compare.each do |compare_object| #diffs << "An object called #{compare_object.name} of type #{compare_object.iddObject.name} was found only in the compare model" diffs << "A #{compare_object.iddObject.name} called '#{compare_object.name}' was found only in the after model" end # Compare objects found in both models field by field both_models.each do |b| true_object = b[0] compare_object = b[1] idd_object = true_object.iddObject true_object_num_fields = true_object.numFields compare_object_num_fields = compare_object.numFields # loop over fields skipping handle (1...[true_object_num_fields, compare_object_num_fields].max).each do |i| field_name = idd_object.getField(i).get.name # Don't compare node, branch, or port names because they are populated with IDs next if field_name.include?('Node Name') next if field_name.include?('Branch Name') next if field_name.include?('Inlet Port') next if field_name.include?('Outlet Port') next if field_name.include?('Inlet Node') next if field_name.include?('Outlet Node') next if field_name.include?('Port List') next if field_name.include?('Cooling Control Zone or Zone List Name') next if field_name.include?('Heating Control Zone or Zone List Name') next if field_name.include?('Heating Zone Fans Only Zone or Zone List Name') next if field_name.include?('Zone Terminal Unit List') # Don't compare the names of schedule type limits # because they appear to be created non-deteministically next if field_name.include?('Schedule Type Limits Name') # Get the value from the true object true_value = "" if i < true_object_num_fields true_value = true_object.getString(i).to_s end true_value = "-" if true_value.empty? # Get the same value from the compare object compare_value = "" if i < compare_object_num_fields compare_value = compare_object.getString(i).to_s end compare_value = "-" if compare_value.empty? # Round long numeric fields true_value = true_value.to_f.round(5) unless true_value.to_f.zero? compare_value = compare_value.to_f.round(5) unless compare_value.to_f.zero? # Move to the next field if no difference was found next if true_value == compare_value next if true_value.to_f.zero? && compare_value.to_f.zero? # Check numeric values if numeric if (compare_value.is_a? Numeric) && (true_value.is_a? Numeric) diff = true_value.to_f - compare_value.to_f unless true_value.zero? # next if absolute value is less than a tenth of a percent difference next if (diff / true_value.to_f).abs < 0.001 end end # Report the difference diffs << "For #{true_object.iddObject.name} called '#{true_object.name}' field '#{field_name}': before model = #{true_value}, after model = #{compare_value}" end end return diffs end |
.convert_idf_to_osm(filepath) ⇒ Object
This method will recursively translate all IDFs in a folder to OSMs, and save them to the OSM_-No_Space_Types folder
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 95 def self.convert_idf_to_osm(filepath) 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.("..\\OSM-No_Space_Types\\", filepath) << "\\" << Pathname.new(file).basename.to_s)[0..-5]) #puts # empty line break end } end |
.deep_copy(model, bool = true) ⇒ OpenStudio::Model::Model
This method will return a deep copy of the model. Simply because I don't trust the clone method yet.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 110 def self.deep_copy(model,bool = true) return model.clone(bool).to_Model # pull original weather file object over weather_file = new_model.getOptionalWeatherFile if not weather_file.empty? weather_file.get.remove BTAP::runner_register("Info", "Removed alternate model's weather file object.",runner) end original_weather_file = model.getOptionalWeatherFile if not original_weather_file.empty? original_weather_file.get.clone(new_model) end # pull original design days over new_model.getDesignDays.sort.each { |designDay| designDay.remove } 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 handles = OpenStudio::UUIDVector.new model.objects.each do |obj| handles << obj.handle end model.removeObjects(handles) # add new file to empty model model.addObjects( new_model.toIdfFile.objects ) BTAP::runner_register("Info", "Model name is now #{model.building.get.name}.", runner) end |
.load_osm(filepath, name = "") ⇒ OpenStudio::Model::Model
This method loads an OpenStudio file into the model.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 31 def self.load_osm(filepath, name = "") unless File.exist?(filepath) raise 'File does not exist: ' + filepath.to_s end 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" } version_translator.warnings.each { |warning| puts "Warning: #{warning.logMessage}\n" } if model.empty? raise "Could not load #{filepath}" end model = model.get if name != "" and not name.nil? self.set_name(model,name) end return model end |
.remove_special_characters(hash) ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 390 def self.remove_special_characters(hash) hash.keys.each do |key| new_key = key.to_s.sub(/[;,]$/, '') hash[new_key] = hash.delete(key) end hash.each do |key, value| next unless value.is_a?(String) hash[key] = value.gsub(/[;,]$/, '') end end |
.rename_handles(osm_data) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 331 def self.rename_handles(osm_data) hash = Digest::SHA1.hexdigest("fixed_seed") handle_map = {} new_handle_index = 0 object_type_index = 0 last_object_type = nil osm_data.each do |hash| if hash['Handle'] #puts hash #puts "#{hash['type']} == #{last_object_type}" if hash['type'] != last_object_type last_object_type = hash['type'] object_type_index += 1 new_handle_index = 1 else new_handle_index += 1 end # create a formatted string that is 12 charecters created with new_handle_index, but padded with 0s on the left. formatted_index = new_handle_index.to_s.rjust(12, '0') formatted_index_object_type = object_type_index.to_s.rjust(4, '0') new_handle = "{00000000-0000-0000-#{formatted_index_object_type}-#{formatted_index}}" handle_map[hash['Handle']] = new_handle hash['Handle'] = new_handle end end osm_data.each do |hash| hash.each do |key, value| if handle_map.key?(value) hash[key] = handle_map[value] end end end # Go through all hashes in osm_data and if the "Name" field value fits this pattern {9386c18d-e70a-447e-8b69-9a0a39fd8f06} replace it with an incremented value. name_map = {} new_name_index = 0 osm_data.each do |hash| if hash['Name'] && hash['Name'].match?(/\{[a-fA-F0-9\-]{36}\}/) new_name_index += 1 formatted_new_name_index = new_name_index.to_s.rjust(4, '0') new_name = "{00000000-0000-#{formatted_new_name_index}-0000-000000000000}" name_map[hash['Name']] = new_name hash['Name'] = new_name end end osm_data.each do |hash| hash.each do |key, value| if name_map.key?(value) hash[key] = name_map[value] end end end end |
.safe_load_model(model_path_string) ⇒ Object
load a model into OS & version translates, exiting and erroring if a problem is found
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 54 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? raise "Version translation failed for #{model_path_string}" else model = model.get end else raise "#{model_path_string} couldn't be found" end return model end |
.save_idf(model, filename) ⇒ OpenStudio::Model::Model
This method will translate to an E+ IDF format and save the model to an idf file.
87 88 89 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 87 def self.save_idf(model,filename) OpenStudio::EnergyPlus::ForwardTranslator.new().translateModel(model).toIdfFile().save(OpenStudio::Path.new(filename),true) end |
.save_osm(model, filename) ⇒ OpenStudio::Model::Model
This method will save the model to an osm file.
75 76 77 78 79 80 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 75 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." end |
.save_osm_file(osm_data, output_path) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 313 def self.save_osm_file(osm_data, output_path) File.open(output_path, 'w') do |file| osm_data.each do |hash| file.puts "#{hash["type"]}," hash.each do |key, value| next if key == "type" is_last_key = (hash.keys - [:type]).last == key if key == :id file.puts " #{value}," else file.puts " #{value}#{is_last_key ? ';' : ','}".ljust(42) + " !- #{key}" end end file.puts end end end |
.set_all_data_time(osm_data) ⇒ Object
302 303 304 305 306 307 308 309 310 311 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 302 def self.set_all_data_time(osm_data) osm_data.each do |hash| hash.each do |key, value| #Check if value is in a date-time format like this "2024-10-17 01:19:35 UTC" if value.match?(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} UTC/) hash[key] = "2024-01-01 01:00:00 UTC" end end end end |
.set_all_epw_paths(osm_data) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 290 def self.set_all_epw_paths(osm_data) osm_data.each do |hash| hash.each do |key, value| #Check if value is in a epw file path format like this "USA_CA_San.Francisco.Intl.AP.724940_TMY3.epw" if value.match?(/.*\.epw/) # Get name of file without path from value hash[key] = File.basename(value) end end end end |
Instance Method Details
#debug_puts(puts_text) ⇒ Object
144 145 146 147 148 |
# File 'lib/openstudio-standards/btap/fileio.rb', line 144 def debug_puts(puts_text) if Debug_Mode == true puts "#{puts_text}" end end |