lib/openstudio-standards/standards/Standards.Model.rb in openstudio-standards-0.2.6 vs lib/openstudio-standards/standards/Standards.Model.rb in openstudio-standards-0.2.7
- old
+ new
@@ -1182,11 +1182,11 @@
# @param array_of_zones [Array] an array of Hashes for each zone, with the keys 'zone',
def model_eliminate_outlier_zones(model, array_of_zones, key_to_inspect, tolerance, field_name, units)
# Sort the zones by the desired key
begin
- array_of_zones = array_of_zones.sort_by {|hsh| hsh[key_to_inspect]}
+ array_of_zones = array_of_zones.sort_by { |hsh| hsh[key_to_inspect] }
rescue ArgumentError => e
OpenStudio.logFree(OpenStudio::Info, 'openstudio.standards.Model', "Unable to sort array_of_zones by #{key_to_inspect} due to #{e.message}, defaulting to order that was passed")
end
# Calculate the area-weighted average
@@ -1202,21 +1202,30 @@
total_area += area
all_vals << val.round(1)
all_areas << area.round
all_zn_names << zn['zone'].name.get.to_s
end
+
+ if total_area == 0
+ OpenStudio.logFree(OpenStudio::Error, 'openstudio.standards.Model', "Total area is zero for array_of_zones with key #{key_to_inspect}, unable to calculate area-weighted average.")
+ return false
+ end
+
avg = total / total_area
OpenStudio.logFree(OpenStudio::Debug, 'openstudio.standards.Model', "Values for #{field_name}, tol = #{tolerance} #{units}, area ft2:")
OpenStudio.logFree(OpenStudio::Debug, 'openstudio.standards.Model', "vals #{all_vals.join(', ')}")
OpenStudio.logFree(OpenStudio::Debug, 'openstudio.standards.Model', "areas #{all_areas.join(', ')}")
OpenStudio.logFree(OpenStudio::Debug, 'openstudio.standards.Model', "names #{all_zn_names.join(', ')}")
# Calculate the biggest delta and the index of the biggest delta
- biggest_delta_i = nil
+ biggest_delta_i = 0 # array at first item in case delta is 0
biggest_delta = 0.0
worst = nil
array_of_zones.each_with_index do |zn, i|
val = zn[key_to_inspect]
+ if worst.nil? # array at first item in case delta is 0
+ worst = val
+ end
delta = (val - avg).abs
if delta >= biggest_delta
biggest_delta = delta
biggest_delta_i = i
worst = val