Class: BuildingSync::GetBCLWeatherFile

Inherits:
Object
  • Object
show all
Defined in:
lib/buildingsync/get_bcl_weather_file.rb

Overview

GetBCLWeatherFile class to manage the process of getting weather files from BCL

Instance Method Summary collapse

Constructor Details

#initializeGetBCLWeatherFile

Returns a new instance of GetBCLWeatherFile.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/buildingsync/get_bcl_weather_file.rb', line 46

def initialize
  # prefix for data weather path
  @weather_file_path_prefix = WEATHER_DIR
  @weather_file = File.join(@weather_file_path_prefix, 'weather_file.json')
  # Above structured as {"weather_file_name":[],"city_name":[],"state_code":[],"weather_id":[]}

  FileUtils.mkdir_p(@weather_file_path_prefix)
  if File.exist?(@weather_file)
    File.open(@weather_file, 'r') do |file|
      @weather_json = JSON.parse(file.read, symbolize_names: true)
    end
  else
    arr = []
    @weather_json = {
      'weather_file_name': arr,
      'city_name': arr,
      'state_code': arr,
      'weather_id': arr
    }
    File.open(@weather_file, 'w') { |f| f.write(@weather_json.to_json) }
  end
end

Instance Method Details

#city_found_in_json_data(city) ⇒ array<boolean, int>

city found in JSON data

Parameters:

  • city (String)

Returns:

  • (array<boolean, int>)


296
297
298
299
300
301
302
303
304
305
# File 'lib/buildingsync/get_bcl_weather_file.rb', line 296

def city_found_in_json_data(city)
  counter = 0
  @weather_json[:city_name].each do |cname|
    if cname.include? city
      return true, counter
    end
    counter += 1
  end
  return false, counter
end

#create_ddy_file(idf_path_collection, epw_path) ⇒ Boolean

create design day file (ddy)

Parameters:

  • idf_path_collection (array<string>)
  • epw_path (String)

Returns:

  • (Boolean)


241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/buildingsync/get_bcl_weather_file.rb', line 241

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

#download_design_day_file(wmo_no, epw_path) ⇒ Object

download design day file

Parameters:

  • wmo_no (String)
  • epw_path (String)


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/buildingsync/get_bcl_weather_file.rb', line 196

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 |uid|
    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
        OpenStudio.logFree(OpenStudio::Error, 'BuildingSync.GetBCLWeatherFile.download_design_day_file',
                           "Error, cannot find the design day file within the downloaded zip container with uid: #{uid}.  Please try a different weather file.")

        raise "Error, cannot find the design day file within the downloaded zip container with uid: #{uid}.  Please try a different weather file."
      end
    else
      OpenStudio.logFree(OpenStudio::Error, 'BuildingSync.GetBCLWeatherFile.download_design_day_file',
                         "Error, cannot find local component for: #{uid}.  Please try a different weather file.")
      raise "Error, cannot find local component for: #{uid}.  Please try a different weather file."
    end
  end

  puts "Successfully downloaded ddy file to #{epw_path}"
  create_ddy_file(idf_path_collection, epw_path)
  puts "Successfully combined design day files to ddy file in #{epw_path}"
end

#download_weather_file(remote, choices) ⇒ Object

download weather file

Parameters:

  • remote (OpenStudio::RemoteBCL)
  • choices (OpenStudio::StringVector)

Returns:

  • string



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/buildingsync/get_bcl_weather_file.rb', line 154

def download_weather_file(remote, choices)
  epw_path = ''

  choices.each do |uid|
    remote.downloadComponent(uid)
    component = remote.waitForComponentDownload

    if component.empty?
      OpenStudio.logFree(OpenStudio::Error, 'BuildingSync.GetBCLWeatherFile.download_weather_file',
                         "Error, cannot find the EPW weather file with uid: #{uid}.  Please try a different weather file.")
      return false
    end

    component = component.get

    files = component.files('epw')

    if files.empty?
      OpenStudio.logFree(OpenStudio::Error, 'BuildingSync.GetBCLWeatherFile.download_weather_file',
                         "Error, cannot find the EPW weather file within the downloaded zip container with uid: #{uid}.  Please try a different weather file.")
      return false
    end

    epw_weather_file_path = component.files('epw')[0]
    dir_path = File.dirname(epw_weather_file_path)
    weather_file_name = File.basename(epw_weather_file_path)

    epw_path = File.expand_path(@weather_file_path_prefix.to_s, File.dirname(__FILE__))

    Dir.glob("#{dir_path}/**/*.*").each do |filename|
      FileUtils.mv(filename, epw_path)
    end
    epw_path = File.expand_path("#{@weather_file_path_prefix}/#{weather_file_name}", File.dirname(__FILE__))
  end

  puts "Successfully set weather file to #{epw_path}"
  return epw_path
end

#download_weather_file_from_city_name(state, city) ⇒ Object

download weather file from city name

Parameters:

  • state (String)
  • city (String)

    c

Returns:

  • string



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/buildingsync/get_bcl_weather_file.rb', line 73

def download_weather_file_from_city_name(state, city)
  weather_file_name = get_weather_file_from_city(city)

  if !weather_file_name.empty?
    return File.join(@weather_file_path_prefix, weather_file_name)
  else
    wmo_no = 0
    remote = OpenStudio::RemoteBCL.new

    # Search for weather files
    responses = remote.searchComponentLibrary(city, 'Weather File')
    choices = OpenStudio::StringVector.new

    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
      OpenStudio.logFree(OpenStudio::Error, 'BuildingSync.GetBCLWeatherFile.download_weather_file_from_city_name',
                         "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

    epw_path = download_weather_file(remote, choices)
    download_design_day_file(wmo_no, epw_path)
    return epw_path
  end
end

#download_weather_file_from_weather_id(weather_id) ⇒ Object

download weather file from weather id

Parameters:

  • weather_id (String)

Returns:

  • string



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/buildingsync/get_bcl_weather_file.rb', line 112

def download_weather_file_from_weather_id(weather_id)
  weather_file_name = get_weather_file_from_weather_id(weather_id)

  if !weather_file_name.empty?
    return File.join(@weather_file_path_prefix, weather_file_name)
  else
    wmo_no = 0
    remote = OpenStudio::RemoteBCL.new

    # Search for weather files
    responses = remote.searchComponentLibrary(weather_id, 'Weather File')

    choices = OpenStudio::StringVector.new

    responses.each do |response|
      if response.name.include? 'TMY3'
        choices << response.uid

        response.attributes.each do |attribute|
          if attribute.name == 'WMO'
            wmo_no = attribute.valueAsDouble
          end
        end
      end
    end

    if choices.count == 0
      OpenStudio.logFree(OpenStudio::Error, 'BuildingSync.GetBCLWeatherFile.download_weather_file_from_weather_id',
                         "Error, could not find uid for #{weather_id}.  Please try a different weather file.")
      return false
    end

    epw_path = download_weather_file(remote, choices)
    download_design_day_file(wmo_no, epw_path)
    return epw_path
  end
end

#find_response_from_given_state(responses, state) ⇒ BCLSearchResult

find response from given state

Parameters:

  • responses (array<BCLSearchResult>)
  • state (String)

Returns:

  • (BCLSearchResult)


311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/buildingsync/get_bcl_weather_file.rb', line 311

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

#find_weather_counter(weather_id) ⇒ array<boolean, int>

check if weather ID is found in JSON data

Parameters:

  • weather_id (String)

Returns:

  • (array<boolean, int>)


272
273
274
275
276
277
278
279
280
281
# File 'lib/buildingsync/get_bcl_weather_file.rb', line 272

def find_weather_counter(weather_id)
  counter = 0
  @weather_json[:weather_id].each do |cname|
    if cname.include? weather_id
      return true, counter
    end
    counter += 1
  end
  return false, counter
end

#get_weather_file_from_city(city) ⇒ String

get weather file from city

Parameters:

  • city (String)

Returns:

  • (String)


286
287
288
289
290
291
# File 'lib/buildingsync/get_bcl_weather_file.rb', line 286

def get_weather_file_from_city(city)
  weather_file_name = ''
  is_found, counter = city_found_in_json_data(city)
  weather_file_name = @weather_json[:weather_file_name][counter] if is_found
  return weather_file_name
end

#get_weather_file_from_weather_id(weather_id) ⇒ String

get weather file from weather ID

Parameters:

  • weather_id (String)

Returns:

  • (String)


262
263
264
265
266
267
# File 'lib/buildingsync/get_bcl_weather_file.rb', line 262

def get_weather_file_from_weather_id(weather_id)
  weather_file_name = ''
  is_found, counter = find_weather_counter(weather_id)
  weather_file_name = @weather_json[:weather_file_name][counter] if is_found
  return weather_file_name
end