Class: OpenStudio::Workflow::InputAdapter::Local

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio/workflow/adapters/input/local.rb

Instance Method Summary collapse

Constructor Details

#initialize(osw_path = './workflow.osw') ⇒ Local



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 43

def initialize(osw_path = './workflow.osw')
  @osw_abs_path = File.absolute_path(osw_path, Dir.pwd)

  @workflow = nil
  if File.exist? @osw_abs_path
    @workflow = ::JSON.parse(File.read(@osw_abs_path), symbolize_names: true)
  end
  
  begin
    # configure the OSW with paths for loaded extension gems
    # Bundler.require is called in the CLI to load extension gems
    @workflow = OpenStudio::Extension::configure_osw(@workflow)          
  rescue NameError => e
  end
  
  @workflow_json = nil
  @run_options = nil
  if @workflow
    begin
      # Create a temporary WorkflowJSON, will not be same one used in registry during simulation
      @workflow_json = OpenStudio::WorkflowJSON.new(JSON.fast_generate(workflow))
      @workflow_json.setOswDir(osw_dir)
    rescue NameError => e
      @workflow_json = WorkflowJSON_Shim.new(workflow, osw_dir)
    end
    
    begin 
      @run_options = @workflow_json.runOptions
    rescue
    end
  end
end

Instance Method Details

#analysisObject

Get the associated OSA (analysis) definition from the local filesystem



311
312
313
314
315
316
317
318
319
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 311

def analysis
  # DLM: should this come from the OSW?  the osd id and checksum are specified there.
  osa_abs_path = File.join(osw_dir, '../analysis.json')
  result = nil
  if File.exist? osa_abs_path
    result = ::JSON.parse(File.read(osa_abs_path), symbolize_names: true)
  end
  return result
end

#cleanup(user_options, default) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 247

def cleanup(user_options, default)
  
  # user option trumps all others
  return user_options[:cleanup] if user_options[:cleanup]
  
  # try to read from OSW
  if @run_options && !@run_options.empty?
    return @run_options.get.cleanup
  end

  return default
end

#datapointObject

Get the associated OSD (datapoint) file from the local filesystem



299
300
301
302
303
304
305
306
307
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 299

def datapoint
  # DLM: should this come from the OSW?  the osd id and checksum are specified there.
  osd_abs_path = File.join(osw_dir, 'datapoint.osd')
  result = nil
  if File.exist? osd_abs_path
    result = ::JSON.parse(File.read(osd_abs_path), symbolize_names: true)
  end
  return result
end

#debug(user_options, default) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 164

def debug(user_options, default)
  
  # user option trumps all others
  return user_options[:debug] if user_options[:debug]
  
  # try to read from OSW
  if @run_options && !@run_options.empty?
    return @run_options.get.debug
  end

  return default
end

#energyplus_path(user_options, default) ⇒ Object



260
261
262
263
264
265
266
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 260

def energyplus_path(user_options, default)
  
  # user option trumps all others
  return user_options[:energyplus_path] if user_options[:energyplus_path]

  return default
end

#fast(user_options, default) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 177

def fast(user_options, default)

  # user option trumps all others
  return user_options[:fast] if user_options[:fast]
  
  # try to read from OSW
  if @run_options && !@run_options.empty?
    if @run_options.get.respond_to?(:fast)
      return @run_options.get.fast
    else
      if @workflow[:run_options]
        return @workflow[:run_options][:fast]
      end
    end
  end
  
  return default
end

#jobs(user_options, default, logger) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 149

def jobs(user_options, default, logger)
  
  # user option trumps all others
  return user_options[:jobs] if user_options[:jobs]

  # try to read from OSW
  begin
    #log_message = "Reading custom job states from OSW is not currently supported'"
    #logger.info log_message
  rescue
  end

  return default
end

#osw_dirObject

Get the OSW dir



91
92
93
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 91

def osw_dir
  File.dirname(@osw_abs_path)
end

#osw_pathObject

Get the OSW path



85
86
87
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 85

def osw_path
  @osw_abs_path
end

#output_adapter(user_options, default, logger) ⇒ Object



108
109
110
111
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
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 108

def output_adapter(user_options, default, logger)
  
  # user option trumps all others
  return user_options[:output_adapter] if user_options[:output_adapter]
  
  # try to read from OSW
  if @run_options && !@run_options.empty?
    custom_adapter = @run_options.get.customOutputAdapter
    if !custom_adapter.empty?
      begin
        custom_file_name = custom_adapter.get.customFileName
        class_name = custom_adapter.get.className
        options = ::JSON.parse(custom_adapter.get.options, :symbolize_names => true)
        
        # merge with user options, user options will replace options loaded from OSW
        options.merge!(user_options)
          
        # stick output_directory in options
        options[:output_directory] = run_dir
        
        p = @workflow_json.findFile(custom_file_name)
        if !p.empty?
          load(p.get.to_s)
          output_adapter = eval("#{class_name}.new(options)")
          return output_adapter
        else
          log_message = "Failed to load custom adapter file '#{custom_file_name}'"
          logger.error log_message
          raise log_message
        end
      rescue
        log_message = "Failed to load custom adapter '#{class_name}' from file '#{custom_file_name}'"
        logger.error log_message
        raise log_message
      end
    end
  end

  return default
end

#preserve_run_dir(user_options, default) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 196

def preserve_run_dir(user_options, default)
  
  # user option trumps all others
  return user_options[:preserve_run_dir] if user_options[:preserve_run_dir]
  
  # try to read from OSW
  if @run_options && !@run_options.empty?
    return @run_options.get.preserveRunDir
  end

  return default
end

#profile(user_options, default) ⇒ Object



268
269
270
271
272
273
274
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 268

def profile(user_options, default)
  
  # user option trumps all others
  return user_options[:profile] if user_options[:profile]

  return default
end

#run_dirObject

Get the run dir



97
98
99
100
101
102
103
104
105
106
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 97

def run_dir
  result = File.join(osw_dir, 'run')
  if @workflow_json
    begin
      result = @workflow_json.absoluteRunDir.to_s
    rescue
    end
  end
  result
end

#skip_energyplus_preprocess(user_options, default) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 228

def skip_energyplus_preprocess(user_options, default)
  
  # user option trumps all others
  return user_options[:skip_energyplus_preprocess] if user_options[:skip_energyplus_preprocess]
  
  # try to read from OSW
  if @run_options && !@run_options.empty?
    if @run_options.get.respond_to?(:skipEnergyPlusPreprocess)
      return @run_options.get.skipEnergyPlusPreprocess
    else
      if @workflow[:run_options]
        return @workflow[:run_options][:skip_energyplus_preprocess]
      end
    end
  end

  return default
end

#skip_expand_objects(user_options, default) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 209

def skip_expand_objects(user_options, default)
  
  # user option trumps all others
  return user_options[:skip_expand_objects] if user_options[:skip_expand_objects]
   
  # try to read from OSW
  if @run_options && !@run_options.empty?
    if @run_options.get.respond_to?(:skipExpandObjects)
      return @run_options.get.skipExpandObjects
    else
      if @workflow[:run_options]
        return @workflow[:run_options][:skip_expand_objects]
      end
    end
  end

  return default
end

#verify_osw(user_options, default) ⇒ Object



276
277
278
279
280
281
282
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 276

def verify_osw(user_options, default)
  
  # user option trumps all others
  return user_options[:verify_osw] if user_options[:verify_osw]

  return default
end

#weather_file(user_options, default) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 284

def weather_file(user_options, default)
  
  # user option trumps all others
  return user_options[:weather_file] if user_options[:weather_file]
  
  # try to read from OSW
  if !@workflow_json.weatherFile.empty?
    return @workflow_json.weatherFile.get.to_s
  end

  return default
end

#workflowObject

Get the OSW file from the local filesystem



78
79
80
81
# File 'lib/openstudio/workflow/adapters/input/local.rb', line 78

def workflow
  raise "Could not read workflow from #{@osw_abs_path}" if @workflow.nil?
  @workflow
end