Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question
2

Load OSM file in measure scripting

asked 2018-05-09 13:39:35 -0600

pow_skier's avatar

updated 2018-05-09 15:33:53 -0600

How do I get a measure to load a specific OSM from a resources directory. I am using the following code and it finds the model but doesn't load it in to be run.

  # assign the user inputs to variables
osm_file_name = runner.getStringArgumentValue("osm_file_name", user_arguments)
osm_directory = runner.getStringArgumentValue("osm_directory", user_arguments)

# get the path
osm_directory = File.expand_path(File.join(File.dirname(__FILE__), osm_directory))

#merge file name and path
osm_file = File.join(osm_directory, osm_file_name)


translator = OpenStudio::OSVersion::VersionTranslator.new
ospath = OpenStudio::Path.new(osm_directory)
model = translator.loadModel(ospath)
model = model.get
edit retag flag offensive close merge delete

Comments

Thank you Eric and Matt. I am not having any luck yet. But I did start cross referencing this thread https://unmethours.com/question/19123... and seem to be going in the right direction. Can I use a relative path? Because I think my path definition is wrong.

pow_skier's avatar pow_skier  ( 2018-05-09 15:06:57 -0600 )edit

I think it depends...is osm_directory located in the measure folder? Like, I have a measure where the folder contains a /resources subfolder, and it lets me load "#{File.dirname(__FILE__)}/resources/#{file_name}". If you're trying to access a folder outside of the measure folder, I think a relative path will fail because the measure gets moved around when it's applied (also depending on how it's applied).

ericringold's avatar ericringold  ( 2018-05-09 15:47:50 -0600 )edit

I agree with Eric here - I think you'll need a hardcoded path unless you put a file in the resources folder of the measure.

mdahlhausen's avatar mdahlhausen  ( 2018-05-09 16:57:45 -0600 )edit

Thank you for all of the feedback. I am going to start with hardcoding the path and then build out from there. Baby steps for me.

I will update and mark complete once I've tried.

pow_skier's avatar pow_skier  ( 2018-05-10 07:24:01 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2018-05-09 14:06:13 -0600

Use the VersionTranslator:

translator = OpenStudio::OSVersion::VersionTranslator.new
ospath = OpenStudio::Path.new(myfilepath)
model = translator.loadModel(ospath)
model = model.get
edit flag offensive delete link more

Comments

Thank you Matt for your reply.

This did not work for me. I called the version translator and got an "Error: Optional not initialized" for the 'get'

Thought I could skip the version translator because I am certain that my OSM I want to load is the same version.

If you can would you please review how I invoked the VersionTranslator? Edited my question to reflect the code update

pow_skier's avatar pow_skier  ( 2018-05-09 14:21:07 -0600 )edit
1

answered 2018-05-09 14:35:19 -0600

updated 2018-05-09 15:09:05 -0600

If you're certain the osm version is the same as the API you are using, the following should work:

model = OpenStudio::Model::Model.load(osm_file).get

The 'safer' way, as @mdahlhausen suggested, is to load thru the version translator, as well as checking that everything exists ('is_initialized', or not 'empty'). As a handy function, it would look like:

def load_model(osm_path)
  model_path = OpenStudio::Path.new(osm_path)
  version_translator = OpenStudio::OSVersion::VersionTranslator.new

  if OpenStudio::exists(model_path)
    model = version_translator.loadModel(model_path)
    if model.empty?
      puts "Version translation failed for #{model_path}"
    else
      model = model.get
      return model
    end
  else
    puts "The model couldn't be found at #{model_path}"
  end
end
edit flag offensive delete link more

Comments

Do I need to anything after the return model?

model = model.get return model

While it seems to like my path and model file it doesnt actually load the model in for simulation.

Thank you.

pow_skier's avatar pow_skier  ( 2018-05-10 12:16:09 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Training Workshops

Careers

Question Tools

1 follower

Stats

Asked: 2018-05-09 13:39:35 -0600

Seen: 207 times

Last updated: May 09 '18