From ef1351812f0ebe41170f59afbefce00bbd8a23f6 Mon Sep 17 00:00:00 2001 From: mrigesh gupta Date: Fri, 28 Jun 2019 18:55:56 +0530 Subject: [PATCH] download the design day file. --- lib/buildingsync/get_bcl_weather_file.rb | 143 +++++++++++------- .../model_articulation/building.rb | 2 +- .../weather_file_download_spec.rb | 19 ++- .../translator_baseline_generation_spec.rb | 6 +- 4 files changed, 107 insertions(+), 63 deletions(-) diff --git a/lib/buildingsync/get_bcl_weather_file.rb b/lib/buildingsync/get_bcl_weather_file.rb index 42954c42..0e318444 100644 --- a/lib/buildingsync/get_bcl_weather_file.rb +++ b/lib/buildingsync/get_bcl_weather_file.rb @@ -36,77 +36,35 @@ module BuildingSync class GetBCLWeatherFile def download_weather_file_from_city_name(state, city) - + wmo_no = 0 remote = OpenStudio::RemoteBCL.new # Search for weather files responses = remote.searchComponentLibrary(city, 'Weather File') choices = OpenStudio::StringVector.new - responses.each do |response| - p "response name: #{response.name}" - if response.name.include? 'TMY3' - response.attributes.each do |attribute| - if attribute.name == 'State' - next if attribute.valueAsString != state - choices << response.uid - end + filter_response = find_response_from_given_state(responses, state) + if !filter_response.nil? + choices << filter_response.uid + filter_response.attributes.each do |attribute| + if attribute.name == 'WMO' + wmo_no = attribute.valueAsDouble end end end - if choices.count == 0 - if responses.count > 0 - responses.each do |response| - response.attributes.each do |attribute| - if attribute.valueType.value == 1 - p "attribute name: #{attribute.name} and value: #{attribute.valueAsDouble}" - elsif attribute.valueType.value == 6 - p "attribute name: #{attribute.name} and value: #{attribute.valueAsString}" - else - p "attribute name: #{attribute.name} and value type id: #{ attribute.valueType.value}" - end - if attribute.name == 'State' - next if attribute.valueAsString != state - end - end - end - end - end - - if choices.count == 0 && !latitude.nil? && !longitude.nil? - hash = {} - # let's try to find the closest EPW file via latitude and longitude - responses.each do |response| - latitudeAttrValue = nil - longitudeAttrValue = nil - response.attributes.each do |attribute| - if attribute.name == 'Latitude' - latitudeAttrValue = attribute.valueAsDouble - elsif attribute.name == 'Longitude' - longitudeAttrValue = attribute.valueAsDouble - end - end - next if latitudeAttrValue.nil? - next if longitudeAttrValue.nil? - - diff = (latitudeAttrValue - latitude) ^ 2 + (longitudeAttrValue - longitude) ^ 2 - hash[diff] = response - end - - if(hash.count > 0) - choices << hash.sort_by { |key | key }.to_h[0] - end - end if choices.count == 0 p "Error, could not find uid for state #{state} and city #{city}. Initial count of weather files: #{responses.count}. Please try a different weather file." return false end - return download_weather_file(remote, choices) + epw_path = download_weather_file(remote, choices) + download_design_day_file(wmo_no, epw_path) + return epw_path end def download_weather_file_from_weather_id(weather_id) + wmo_no = 0 remote = OpenStudio::RemoteBCL.new # Search for weather files @@ -122,6 +80,12 @@ def download_weather_file_from_weather_id(weather_id) if response.name.include? 'TMY3' choices << response.uid name_to_uid[response.name] = response.uid + + response.attributes.each do |attribute| + if attribute.name == 'WMO' + wmo_no = attribute.valueAsDouble + end + end end end @@ -130,7 +94,9 @@ def download_weather_file_from_weather_id(weather_id) return false end - return download_weather_file(remote, choices) + epw_path = download_weather_file(remote, choices) + download_design_day_file(wmo_no, epw_path) + return epw_path end def download_weather_file(remote, choices) @@ -164,5 +130,74 @@ def download_weather_file(remote, choices) return epw_path end + + def download_design_day_file(wmo_no, epw_path) + remote = OpenStudio::RemoteBCL.new + responses = remote.searchComponentLibrary(wmo_no.to_s[0, 6], 'Design Day') + choices = OpenStudio::StringVector.new + + idf_path_collection = [] + + responses.each do |response| + choices << response.uid + end + + choices.each do |choice| + uid = choice + + remote.downloadComponent(uid) + component = remote.waitForComponentDownload + + if !component.empty? + + component = component.get + + files = component.files('idf') + + if !files.empty? + idf_path_collection.push(component.files('idf')[0]) + else + p 'No idf file found' + end + else + p "Cannot find local component for #{choice}" + end + end + create_ddy_file(idf_path_collection, epw_path) + end + + def create_ddy_file(idf_path_collection, epw_path) + idf_file_lines = [] + + idf_path_collection.each do |idf_file_path| + idf_file = File.open(idf_file_path) + idf_file_lines.push(idf_file.readlines) + end + + design_day_path = File.dirname(epw_path) + weather_file_name = File.basename(epw_path, '.*') + design_day_file = File.new("#{design_day_path}/#{weather_file_name}.ddy", 'w') + + idf_file_lines.each do |line| + design_day_file.puts(line) + end + + design_day_file.close + end + + def find_response_from_given_state(responses,state) + responses.each do |response| + if response.name.include? 'TMY3' + response.attributes.each do |attribute| + if attribute.name == 'State' + if attribute.valueAsString == state + return response + end + end + end + end + end + return nil + end end end diff --git a/lib/buildingsync/model_articulation/building.rb b/lib/buildingsync/model_articulation/building.rb index 2f3c707d..a69f5ba2 100644 --- a/lib/buildingsync/model_articulation/building.rb +++ b/lib/buildingsync/model_articulation/building.rb @@ -317,7 +317,7 @@ def set_weather_and_climate_zone(climate_zone, epw_file_path, standard_to_be_use if !weather_station_id.nil? epw_file_path = BuildingSync::GetBCLWeatherFile.new.download_weather_file_from_weather_id(weather_station_id) elsif !city_name.nil? && !state_name.nil? - epw_file_path = BuildingSync::GetBCLWeatherFile.new.download_weather_file_from_city_name(state_name, city_name,) + epw_file_path = BuildingSync::GetBCLWeatherFile.new.download_weather_file_from_city_name(state_name, city_name) end set_weather_and_climate_zone_from_epw(climate_zone, epw_file_path, standard_to_be_used, latitude, longitude) diff --git a/spec/tests/model_articulation_test/weather_file_download_spec.rb b/spec/tests/model_articulation_test/weather_file_download_spec.rb index 1c366536..f666830f 100644 --- a/spec/tests/model_articulation_test/weather_file_download_spec.rb +++ b/spec/tests/model_articulation_test/weather_file_download_spec.rb @@ -34,18 +34,31 @@ # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ******************************************************************************* - require_relative '../../../lib/buildingsync/get_bcl_weather_file' RSpec.describe 'WeatherFileDownload' do it 'weather file download from the nrel site with the help of state and city name' do state, city = get_state_and_city_name('building_151', 'auc') - BuildingSync::GetBCLWeatherFile.new.download_weather_file_from_city_name(state, city) + epw_path = BuildingSync::GetBCLWeatherFile.new.download_weather_file_from_city_name(state, city) + + check_weather_file_exist(epw_path) end it 'weather file download from the nrel site with the help of weather station ID' do weather_id = get_weather_id('building_151', 'auc') - BuildingSync::GetBCLWeatherFile.new.download_weather_file_from_weather_id(weather_id) + epw_path = BuildingSync::GetBCLWeatherFile.new.download_weather_file_from_weather_id(weather_id) + + check_weather_file_exist(epw_path) + end + + def check_weather_file_exist(epw_path) + # check weather file exist or not + expect(File.exist?(epw_path)).to be true + + epw_path['.epw'] = '.ddy' + + # check design day file exist or not + expect(File.exist?(epw_path)).to be true end def get_state_and_city_name(file_name, ns) diff --git a/spec/tests/translator_baseline_generation_spec.rb b/spec/tests/translator_baseline_generation_spec.rb index 7da5fb4a..c3a9b217 100644 --- a/spec/tests/translator_baseline_generation_spec.rb +++ b/spec/tests/translator_baseline_generation_spec.rb @@ -64,10 +64,6 @@ test_baseline_creation('DC GSA Headquarters.xml', ASHRAE90_1, 'CZ01RV2.epw') end - it 'should parse and write DC GSA Headquarters.xml (phase zero) without weather file' do - test_baseline_creation('DC GSA Headquarters.xml', ASHRAE90_1) - end - it 'should parse and write BuildingSync Website Valid Schema.xml (phase zero) with Title 24' do test_baseline_creation('BuildingSync Website Valid Schema.xml', CA_TITLE24, 'CZ01RV2.epw') end @@ -84,7 +80,7 @@ test_baseline_creation('Golden Test File.xml', ASHRAE90_1, 'CZ01RV2.epw') end - it 'should parse and write Golden Test File.xml (phase zero) with ASHRAE 90.1' do + it 'should parse and write Golden Test File.xml (phase zero) with ASHRAE 90.1 and without weather file' do test_baseline_creation('Golden Test File.xml', ASHRAE90_1) end end