First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!

Ask Your Question
1

Selecting data to send to Excel - set_range

asked Jun 28 '16

SAE_2016 gravatar image

Hi! I’m newbie with python and I try to export selected bus data to Excel. I made this code base on this video from Jervis Whitley

# import os,sys
PythonPath = r'C:\Program Files\PTI\PSSE32\PSSBIN'
sys.path.append(PythonPath)
os.environ['PATH'] = (r"C:\Program Files\PTI\PSSE32\PSSBIN;"
                      + os.environ['PATH'])
import excelpy
import psspy
import redirect
redirect.psse2py()
psspy.psseinit(10000)
psspy.case(r'savnw.sav')
# Buses in service 
ierr, nbuses = psspy.abuscount(-1, 1)
# Read data from sav...
ierr, buses = psspy.abusint(-1,2,string='NUMBER')
ierr, iarea = psspy.abusint(-1,2, string='AREA')
ierr, volts = psspy.abusreal(-1,2,string='PU')
# Open and Show excel
x1 = excelpy.workbook()
x1.show()
# Write Titel on Excel
x1.set_cell('a1', 'AREA')
x1.set_cell('b1', 'Bus Number')
x1.set_cell('c1', 'Voltage [pu]')
# Send data to Excel
x1.set_range(2,'a',zip(*iarea))
x1.set_range(2,'b',zip(*buses))
x1.set_range(2,'c',zip(*volts))
# Save Workbook
x1.save('demo.xlsx')

Thas work fine but when I try to send for example ONLY the data from Area 5 I can’t!. It’s a possibility in Pyhton to do a of this data? A code like:

for jj in range (0,nbuses):
    if iarea[jj]==5:
        x1.set_range(2,'a',zip(*iarea(jj)))
        x1.set_range(2,'b',zip(*buses(jj)))
        x1.set_range(2,'c',zip(*volts(jj)))

doesn’t work. Thanks a lot in advance!

3 answers

Sort by » oldest newest most voted
1

answered Jul 3 '16

jconto gravatar image

updated Aug 27 '17

In this version A5buses.py, I avoid the use of zip + enumerate and recognizing the format of the orginal arrays, select corresponding items valid for area 5 only.

Download it from my Google_drive. Edit 'PythonPath' within the code to fit your system.

link

Comments

Excellent! Also, I dont like also very much "enumerate". Thank you JConto, works all ok.

SAE_2016 gravatar imageSAE_2016 (Jul 5 '16)

I want to know how to use this method on psse33 & python2.7 in this video

YI-WEI gravatar imageYI-WEI (Aug 27 '17)
0

answered Jun 28 '16

JervisW gravatar image

updated Jun 28 '16

At this point here, you have all the information you want, but spread in three different lists (or columns). buses, iarea and volts

ierr, buses = psspy.abusint(-1,2,string='NUMBER')
ierr, iarea = psspy.abusint(-1,2, string='AREA')
ierr, volts = psspy.abusreal(-1,2,string='PU')

What you want is a way to filter for the rows where the area == 5

You need to transpose your columns into rows first.

rows = zip(buses, iarea, volts)

then you'll need to set the cells that you'd like. I don't think excelpy is efficiently using set_range. It is just calling set_cell individually. So we can do this without any performance penalty

for rowid, record in enumerate(rows):
    bus, area, voltage = record
    if area == 5:
        x1.set_cell(2 + rowid, 'a', area)
        x1.set_cell(2 + rowid, 'b', bus)
        x1.set_cell(2 + rowid, 'c', voltage)

where enumerate obviously gives us a 0-based index, and you wanted to start from row 2, that's why I add 2 to every rowid.

link
0

answered Jun 30 '16

SAE_2016 gravatar image

Hi Jervis!, Thank you very much for your answer. Unfortunately the script don't send any data to the Workbook. Python continuous still hard to me and I guess I made a fool mistake but "hello 5!" doesn't appear nowhere :(

# Read ...
ierr, buses = psspy.abusint(-1,2,string='NUMBER')
ierr, iarea = psspy.abusint(-1,2, string='AREA')
ierr, volts = psspy.abusreal(-1,2,string='PU')
# Open and Show excel
x1 = excelpy.workbook()
x1.show()
# Write Titel on Excel
x1.set_cell('a1', 'AREA')
x1.set_cell('b1', 'Bus Number')
x1.set_cell('c1', 'Voltage [pu]')
# Transpose the columns into rows 
rows = zip(buses, iarea, volts)
# Send data to Excel
for rowid, record in enumerate(rows):
    bus, area, voltage = record    
    print 'hello all!'
    if area == 5:
        print 'hello 5!'
        x1.set_range(2 + rowid, 'a', area)
        x1.set_range(2 + rowid, 'b', bus)
        x1.set_range(2 + rowid, 'c', voltage)
# Save Workbook
x1.save('demo.xlsx')
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.
Want to format code in your answer? Here is a one minute demo on Youtube

Add Answer

[hide preview]

Question Tools

2 followers

Stats

Asked: Jun 28 '16

Seen: 1,310 times

Last updated: Aug 27 '17