Skip to content

Commit

Permalink
error handling for non-US weatherfiles (#443)
Browse files Browse the repository at this point in the history
* error handling for non-US weatherfiles

* clarify error message when non-US weather file is used

* test error message when non-US weather file is used

* restore project-dir creation in tests
  • Loading branch information
vtnate authored Oct 19, 2023
1 parent 3a7c086 commit 0cdf088
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
20 changes: 18 additions & 2 deletions example_files/mappers/Baseline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,23 @@ def get_lookup_row(args, rows, template_vals)
def get_climate_zone_iecc(epw)
headers = CSV.open(epw, 'r', &:first)
wmo = headers[5]
zones_csv = File.join(File.dirname(__FILE__), '../resources/hpxml-measures/HPXMLtoOpenStudio/resources/data/climate_zones.csv')
zones_csv = Pathname(__FILE__).dirname.parent / 'resources' / 'hpxml-measures' / 'HPXMLtoOpenStudio' / 'resources' / 'data' / 'climate_zones.csv'

# Check if the CSV file is empty
if File.empty?(epw)
raise "Error: Your weather file #{epw} is empty."
end

CSV.foreach(zones_csv) do |row|
if row[0].to_s == wmo.to_s
return row[6].to_s
end
end

# If no match is found, raise an error
raise ("Error: No match found for WMO #{wmo} from your weather file #{Pathname(epw).expand_path} in our US WMO list.
This is known to happen when your weather file is from somewhere outside of the United States.
Please replace your weather file with one from an analagous weather location in the United States.")
end

# epw_state to subregions mapping methods
Expand Down Expand Up @@ -863,7 +874,12 @@ def create_osw(scenario, features, feature_names)
template_vals = template_vals.transform_keys(&:to_sym)

epw = File.join(File.dirname(__FILE__), '../weather', feature.weather_filename)
template_vals[:climate_zone] = get_climate_zone_iecc(epw)
begin
template_vals[:climate_zone] = get_climate_zone_iecc(epw)
rescue RuntimeError => e
# Non-US weather file can lead to abrupt exit
puts e.message
end

# ENCLOSURE

Expand Down
29 changes: 29 additions & 0 deletions spec/uo_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# See also https://github.com/urbanopt/urbanopt-cli/blob/develop/LICENSE.md
# *********************************************************************************

require 'csv'
require 'json'

RSpec.describe URBANopt::CLI do
Expand Down Expand Up @@ -33,6 +34,7 @@
test_feature_rnm = test_directory / 'example_project_with_streets.json'
test_validate_bounds = test_directory_res / 'out_of_bounds_validation.yaml'
test_reopt_scenario_assumptions_file = test_directory_pv / 'reopt' / 'multiPV_assumptions.json'
test_weather_file = test_directory_res / 'weather' / 'USA_NY_Buffalo-Greater.Buffalo.Intl.AP.725280_TMY3.epw'
call_cli = 'bundle exec uo'

# Ensure clean slate for testing
Expand Down Expand Up @@ -403,6 +405,33 @@ def select_measures(test_dir, measure_name_list, workflow = 'base_workflow.osw',
expect((test_directory_res / 'run' / 'two_building_res' / '16' / 'finished.job').exist?).to be true
end

it 'returns graceful error message when non-US weather file is provided' do
csv_data = CSV.read(test_weather_file)
existing_wmo = csv_data[0][5]
# Replace the WMO with a non-US WMO (Vancouver, BC)
csv_data[0][5] = 718920
CSV.open(test_weather_file, 'w') do |csv|
csv_data.each do |row|
csv << row
end
end

# Attempt to run the residential project
system("cp #{spec_dir / 'spec_files' / 'two_building_res.csv'} #{test_scenario_res}")
expect { system("#{call_cli} run --scenario #{test_scenario_res} --feature #{test_feature_res}") }
.to output(a_string_including('This is known to happen when your weather file is from somewhere outside of the United States.'))
.to_stdout_from_any_process

csv_data = CSV.read(test_weather_file)
# Restore the original WMO
csv_data[0][5] = existing_wmo
CSV.open(test_weather_file, 'w') do |csv|
csv_data.each do |row|
csv << row
end
end
end

it 'runs a 2 building scenario using create bar geometry method' do
# Copy create bar specific files
system("cp #{example_dir / 'mappers' / 'CreateBar.rb'} #{test_directory / 'mappers' / 'CreateBar.rb'}")
Expand Down

0 comments on commit 0cdf088

Please sign in to comment.