diff --git a/example_files/mappers/Baseline.rb b/example_files/mappers/Baseline.rb index 3c7dc2bb..d21521a7 100644 --- a/example_files/mappers/Baseline.rb +++ b/example_files/mappers/Baseline.rb @@ -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 @@ -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 diff --git a/spec/uo_cli_spec.rb b/spec/uo_cli_spec.rb index 23d0a0d7..621793a1 100644 --- a/spec/uo_cli_spec.rb +++ b/spec/uo_cli_spec.rb @@ -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 @@ -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 @@ -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'}")