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

how do I export Space name and SpaceType name into csv with an OpenStudio measure?

asked 2017-02-22 13:59:48 -0500

Santiago Velez's avatar

updated 2017-08-05 07:49:43 -0500

My goal is to exporta a CSV file with information about spaces and zones. I'm writing a measure in which I first loop through spaces and get the names (space.name) and spacetypes (space.spaceType). The problem is that for spacetype I get the handle instead of the name. So I need to get the spacetype name using that handle. Space loop:

spaces.each do |space|
    spaceHandle = space.handle
    spaceName = space.name
    spaceType = space.spaceType
    spaceList << [space.handle, space.name,spaceType,0,]
end

Then I loop through the spacetypes and (try to) get their names by matching spacetype handles I stored in the previous loop.

spaceTypes = model.getSpaceTypes
spaceList.each do |row|
    spaceTypes.each do |spaceType|
      if row[2] == spaceType.handle
        row[3] == spaceType.name
      end  
    end
end

The script never goes into the if statement because space.spaceType never matches the spaceType...

spaceType.handle = {f34459af-1179-4794-8987-3ac9e38bd80d}
space.spaceType = #<OpenStudio::Model::OptionalSpaceType:0x00000006b639c8>

I'm a bit lost. Can somebody enlighten me? Is there a better way to achieve this?

edit retag flag offensive close merge delete

Comments

just wondering, is this part of open studio/energy plus, or you are using csv output and a third-party compiler?

payam's avatar payam  ( 2023-12-13 10:26:19 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
5

answered 2017-02-22 14:52:22 -0500

updated 2018-09-17 05:43:09 -0500

IIUC, you want a table with two columns: Space Name, and Space Type Name.

As @adhilton mentioned, space.spaceType returns an optional SpaceType, so you need to check if it's empty or not, and if not do space.spaceType.get that will return an actual SpaceType, and then call .name which returns an optional string so convert that to a string using .to_s. Chained together: space.spaceType.get.name.to_s

You should really do this in one loop for efficiency (and because there's no reason to do it in two...). I like to store such tables in an array of hashes, as it is very convenient to work with in Ruby if you need to some more processing, and really easy to export to a CSV file using the standard bit of code I'm posting here.

require 'csv'

space_hash = []
model.getSpaces.each do |space|
  if space.spaceType.empty?
     space_type = "No Space Type Defined"
  else
     space_type = space.spaceType.get.name.to_s
  end
  space_hash << {:space => space.name.to_s, :space_type => space_type}
end

Then you can export to a csv:

# Export to a csv file
CSV.open("space_spacetypes.csv", "wb") do |csv|
  csv << space_hash.first.keys # adds the attributes name on the first line
  space_hash.each do |hash|
    csv << hash.values
  end
end
edit flag offensive delete link more

Comments

To the top. Extra characters added to conform to comment minimum.

Adam Hilton's avatar Adam Hilton  ( 2017-02-22 15:05:12 -0500 )edit

Got it. That looks a lot cleaner. Thanks.

Santiago Velez's avatar Santiago Velez  ( 2017-02-22 15:09:56 -0500 )edit

this is great. thank you! Although I had to flip your if statement to get it to work

space_hash = [] model.getSpaces.each do |space| if space.spaceType.empty? space_type = "No Space Type Defined" else space_type = space.spaceType.get.name.to_s end space_hash << {:space => space.name.to_s, :space_type => space_type} end

sspielman's avatar sspielman  ( 2018-09-14 17:43:37 -0500 )edit

Thanks for reporting the typo, fixed in the answer now.

Julien Marrec's avatar Julien Marrec  ( 2018-09-17 05:43:42 -0500 )edit
3

answered 2017-02-22 14:41:51 -0500

Adam Hilton's avatar

The .spaceType method returns a SpaceType as an optional, as you can see in the variable output you posted. If you use .get on an optional that is true, it will return the object wrapped in the optional. In your case space.spaceType.get.handle will return the handle of the space type assigned to the space if that's what you'd like to compare. Is there a particular reason you need to compare space type handles?

edit flag offensive delete link more

Comments

Got it thanks! The reason I was comparing space type handles is because I was failing to get the spacetype name in the the space loop. So I thought I need to loop through spacetypes and get the name there. I change the code as you suggested:

space.spaceType.get.name

Now it returns the spaceType name, so the second bit of my code is redundant. Thanks again.

Santiago Velez's avatar Santiago Velez  ( 2017-02-22 14:51:35 -0500 )edit

Be careful, space.spaceType.get.name returns an optional string..., so convert that to a string...

Julien Marrec's avatar Julien Marrec  ( 2017-02-22 14:55:05 -0500 )edit

Yes, sorry. Did this comment before I saw your answer. I understand the logic now.

Santiago Velez's avatar Santiago Velez  ( 2017-02-22 15:19:39 -0500 )edit

Your Answer

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

Add Answer

Careers

Question Tools

2 followers

Stats

Asked: 2017-02-22 13:59:48 -0500

Seen: 266 times

Last updated: Sep 17 '18