Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into DA
  • Loading branch information
TobiasMaile committed Jul 2, 2019
2 parents 69437dc + ef13518 commit 1d0f32f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 63 deletions.
143 changes: 89 additions & 54 deletions lib/buildingsync/get_bcl_weather_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/buildingsync/model_articulation/building.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 16 additions & 3 deletions spec/tests/model_articulation_test/weather_file_download_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions spec/tests/translator_baseline_generation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1d0f32f

Please sign in to comment.