First time here? Check out the Help page!

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

OS measure - object list from CSV

asked 9 years ago

updated 7 years ago

Is there a way to parse a list of OS objects from a CSV file and create an array or hash in Ruby? I would like to create a custom list of thermal zones in an OS measure to loop through. Also, how would I path the file location? I have seen other measures on the BCL reference additional files in a 'resources' sub-folder located alongside the measure, but I can't quite discern the correct syntax.

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
7

answered 9 years ago

updated 9 years ago

You can put your zones.csv into a resources subfolder indeed, this way it will stay with your measure (it seems that you can only have measure.rb and measure.xml at the root of your measure folder).

In your zones.csv:

ZoneName
Zone 1
Zone 2
Zone 3

then in your measure:

require 'csv'
# File.dirname(__FILE__) points to the location of the current file, so your `measure.rb`
zones = CSV.table(File.dirname(__FILE__) + '/resources/zones.csv')
#

At this point if you check the headers:

[1] (main)> zones.headers
 => [:zonename]

Then you can do a bunch of stuff, like you can loop on each and grab the thermal zone associated with it

zones.each do |row|
    zonename = row[:zonename]
    zone = model.getThermalZoneByName(zonename)
    if zone.empty?
        puts "couldn't find zone named #{zonename}"
    else
        zone = zone.get
        # Do something else
    end
end

Or if it's easier you can always load all the zone name in a more familiar array object:

# Create empty array to store the zone names
 zone_names = []
 zones.each{|row| zone_names << row[:zonename]}
Preview: (hide)
link

Comments

Thanks @Julien Marrec , this is very helpful! Can you elaborate on the code section under 'At this point if you check the headers:' ?

Lyle K's avatar Lyle K  ( 9 years ago )

If you are using an interactive prompt you should just call zones.headers. If you want to double check stuff in a measure you should call puts zones.headers. Otherwise just skip it.

Julien Marrec's avatar Julien Marrec  ( 9 years ago )
5

answered 9 years ago

Adding to @Julien Marrec's answer:

There's not currently a path argument available to OS measures, but you can approximate that function with a simple string argument:

#make an argument for location of .csv file
csv_file_path = OpenStudio::Ruleset::OSArgument::makeStringArgument("csv_file_path",true)
csv_file_path.setDefaultValue("C:\Users\Eric\Desktop\zones.csv")
args << csv_file_path

Then in the run section, apply that string to a variable that you pass into the CSV method of your choice.

Preview: (hide)
link

Comments

Path arguments are coming in PAT 2.0!

macumber's avatar macumber  ( 9 years ago )

So hyped for PAT 2.0! :)

ericringold's avatar ericringold  ( 9 years ago )
1

answered 9 years ago

updated 9 years ago

Just to through in more options. If your list isn't going to be too long, you can have a string argument that expects comma separated values in it, instead of using an external file.

Then in the measure you parse it apart using code like this.

array_from_arg = arg.split(',')

Here is another option that is possible but not really recommended - The arguments section of the measure can loop through zones and make a bool argument for each zone. Then you can check the zones you want included. This isn't ideal because having a variable/unknown number of arguments for a measure can cause issues with large scale analysis, and if you have a really big model this may blow up. If intended to used only with apply measures now, then this could be ok.

Preview: (hide)
link

Comments

Thanks for the additional suggestions, I saw this post where you had previously described these approaches (and others). I was able to successfully implement both methods you describe above, however they aren't the most feasible alternatives when you have many objects (like thermal zones). Managing a specific list of objects (in a CSV) to apply a measure to has great utility; we were routinely doing this using the SDK with python.

Lyle K's avatar Lyle K  ( 9 years ago )

That being said, it would still be great to have multiple choice arguments available to measures with less trickery. There are other scenarios where this is can be very useful.

Lyle K's avatar Lyle K  ( 9 years ago )

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

2 followers

Stats

Asked: 9 years ago

Seen: 272 times

Last updated: Jan 27 '16