Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Plot multiple PSSE "out" files

Hi,

I recently came across some code that shows how to run PSSE from python and the some tricks with the dyntools module. This is the code.

The code works great but my problem is, instead of entering the name of each out file, is there a way of plotting all the outfiles in a directory?

Thanks.

import os, sys

=============================================================================================

Get installed location of latest PSS(R)E version

def latestpsselocation(): import winreg ptiloc = r"SOFTWARE\PTI" ptikey = _winreg.OpenKey(winreg.HKEYLOCALMACHINE, ptiloc, 0, winreg.KEYREAD) ptikeyinfo = _winreg.QueryInfoKey(ptikey) numptisubkeys = ptikeyinfo[0] vdict = {} for i in range(numptisubkeys): vernum = _winreg.EnumKey(ptikey, i) try: n = int(vernum[-2:]) vdict[n]=vernum except: pass

vers = vdict.keys()
vers.sort()
k = vers[-1]
lver = vdict[k]
lverloc = ptiloc + "\\" + lver + "\\" + "Product Paths"
lverkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, lverloc, 0, _winreg.KEY_READ)
lverdir, stype = _winreg.QueryValueEx(lverkey, 'PsseInstallPath')
_winreg.CloseKey(ptikey)
_winreg.CloseKey(lverkey)
return lverdir

pssedir = latestpsselocation() pssedir = str(pssedir) # convert unicode to str

=============================================================================================

Files Used

pssbindir = os.path.join(pssedir,'PSSBIN') exampledir = os.path.join(pssedir,'EXAMPLE')

savfile = os.path.join(exampledir,'savcnv.sav') snpfile = os.path.join(exampledir,'savnw.snp') outfile1 = os.path.join(exampledir,'bus154fault.out') outfile2 = os.path.join(exampledir,'bus3018gentrip.out') outfile3 = os.path.join(exampledir,'brn30053007trip.out') prgfile = os.path.join(exampledir,'dyntoolsdemoprogress.txt')

=============================================================================================

Check if running from Python Interpreter

exename = sys.executable p, nx = os.path.split(exename) nx = nx.lower() if nx in ['python.exe', 'pythonw.exe']: os.environ['PATH'] = pssbindir + ';' + os.environ['PATH'] sys.path.insert(0,pssbindir)

=============================================================================================

import dyntools

=============================================================================================

Run Dynamic simulation on SAVNW to generate .out files

def runsavnwsimulation(): import redirect redirect.psse2py()
import psspy ierr = psspy.psseinit(buses=80000) # choose here bus numbers you want

psspy.lines_per_page_one_device(1,90)
psspy.progress_output(2,prgfile,[0,0])

psspy.case(savfile)
psspy.rstr(snpfile)
psspy.strt(0,outfile1)
psspy.run(0, 1.0,1000,1,0)
psspy.dist_bus_fault(154,1, 230.0,[0.0,-0.2E+10])
psspy.run(0, 1.05,1000,1,0)
psspy.dist_clear_fault(1)
psspy.run(0, 5.0,1000,1,0)

psspy.case(savfile)
psspy.rstr(snpfile)
psspy.strt(0,outfile2)
psspy.run(0, 1.0,1000,1,0)
psspy.dist_machine_trip(3018,'1')
psspy.run(0, 5.0,1000,1,0)

psspy.case(savfile)
psspy.rstr(snpfile)
psspy.strt(0,outfile3)
psspy.run(0, 1.0,1000,1,0)
psspy.dist_branch_trip(3005,3007,'1')
psspy.run(0, 5.0,1000,1,0)

psspy.lines_per_page_one_device(2,10000000)
psspy.progress_output(1,"",[0,0])

=============================================================================================

1. Data extraction/information

def testdataextraction(chnfobj):
print '\n Testing call to getdata' shttl, chid, chdata = chnfobj.getdata() print shttl print ch_id

print '\n Testing call to get_id'
sh_ttl, ch_id = chnfobj.get_id()
print sh_ttl
print ch_id

print '\n Testing call to get_range'
ch_range = chnfobj.get_range()
print ch_range

print '\n Testing call to get_scale'
ch_scale = chnfobj.get_scale()
print ch_scale

print '\n Testing call to print_scale'
chnfobj.print_scale()

print '\n Testing call to txtout'
chnfobj.txtout(channels=[1,4])

print '\n Testing call to xlsout'
chnfobj.xlsout(channels=[2,3,4,7,8,10])

=============================================================================================

2. Multiple subplots in a figure, but one trace in each subplot

def testsubplotsone_trace(chnfobj): optnppg = {'size':'Letter', 'orientation':'Portrait'} optnfmt = {'rows':3,'columns':2,'dpi':300,'showttl':True}

optnchn1 = {1:{'chns':1},2:{'chns':6},3:{'chns':11},4:{'chns':16},5:{'chns':26},6:{'chns':40}}
pn,x     = os.path.splitext(outfile1)
pltfile1 = pn+'.eps'

optnchn2 = {1:{'chns':{outfile2:1}},
            2:{'chns':{outfile2:6}},
            3:{'chns':{outfile2:11}},
            4:{'chns':{outfile2:16}},
            5:{'chns':{outfile2:26}},
            6:{'chns':{outfile2:40}},
            }
pn,x     = os.path.splitext(outfile2)
pltfile2 = pn+'.eps'

figfiles1 = chnfobj.xyplots(optnchn1,optnppg,optnfmt,pltfile1)

figfiles2 = chnfobj.xyplots(optnchn2,optnppg,optnfmt,pltfile2)

print 'Plot fils saved:'
print '   ', figfiles1[0]
print '   ', figfiles2[0]

=============================================================================================

3. Multiple subplots in a figure and more than one trace in each subplot

def testsubplotsmult_trace(chnfobj): optnppg = {'size':'Letter', 'orientation':'Portrait'} optnfmt = {'rows':2,'columns':2,'dpi':300,'showttl':True}

optnchn1 = {1:{'chns':[1]},2:{'chns':[2]},3:{'chns':[3]},4:{'chns':[4]},5:{'chns':[5]}}
pn,x     = os.path.splitext(outfile1)
pltfile1 = pn+'.png'

optnchn2 = {1:{'chns':{outfile2:1}},
            2:{'chns':{'v82_test1_bus_fault.out':3}},
            3:{'chns':4},
            4:{'chns':[5]}
           }
pn,x     = os.path.splitext(outfile2)
pltfile2 = pn+'.pdf'

optnchn3 = {1:{'chns':{'v80_test1_bus_fault.out':1}},
            2:{'chns':{'v80_test2_complex_wind.out':[1,5]}},
            3:{'chns':{'v82_test1_bus_fault.out':3}},
            5:{'chns':[4,5]},
           }
pn,x     = os.path.splitext(outfile3)
pltfile3 = pn+'.eps'

figfiles1 = chnfobj.xyplots(optnchn1,optnppg,optnfmt,pltfile1)
figfiles2 = chnfobj.xyplots(optnchn2,optnppg,optnfmt,pltfile2)
figfiles3 = chnfobj.xyplots(optnchn3,optnppg,optnfmt,pltfile3)

figfiles = figfiles1[:]
figfiles.extend(figfiles2)
figfiles.extend(figfiles3)
print 'Plot fils saved:'
for f in figfiles:
    print '    ', f

=============================================================================================

4. Do XY plots and insert them into word file

def testplots2word(chnfobj): p,nx = os.path.split(outfile1) docfile = os.path.join(p,'savnwresponse.doc') show = True overwrite = True caption = True align = 'center' captionpos = 'below' height = 0.0 width = 0.0 rotate = 0.0

optnppg  = {'size':'Letter', 'orientation':'Portrait'}
optnfmt  = {'rows':3,'columns':1,'dpi':300,'showttl':True}

optnchn  = {1:{'chns':{outfile1:1,  outfile2:1,  outfile3:1} },
            2:{'chns':{outfile1:7,  outfile2:7,  outfile3:7} },
            3:{'chns':{outfile1:17, outfile2:17, outfile3:17} },
            4:{'chns':[1,2,3,4,5]},
            5:{'chns':{outfile2:[1,2,3,4,5]} },
            6:{'chns':{outfile3:[1,2,3,4,5]} },
           }
chnfobj.xyplots2doc(optnchn,optnppg,optnfmt,docfile,show,overwrite,caption,align,
                    captionpos,height,width,rotate)

print 'Plots saved to file:'
print '    ', docfile

=============================================================================================

def test1():

# create out files, run this only once and comment out once you have the .out files
run_savnw_simulation()

# create object
outlst = [outfile1, outfile2, outfile3]
chnf = dyntools.CHNF(outlst)

test_data_extraction(chnf)

=============================================================================================

def test2():

# create object
outlst = [outfile1, outfile2, outfile3]
chnf = dyntools.CHNF(outlst)

test_subplots_one_trace(chnf)
chnf.plots_show()

=============================================================================================

def test3():

# create object
outlst = [outfile1, outfile2, outfile3]
chnf = dyntools.CHNF(outlst)

test_subplots_mult_trace(chnf)
chnf.plots_show()

=============================================================================================

def test4():

# create object
outlst = [outfile1, outfile2, outfile3]
chnf = dyntools.CHNF(outlst)

test_plots2word(chnf)

=============================================================================================

if name == 'main':

# Need to run test1 before running test2, test3 or test4
# After running test1, you need to run test2, test3 and test4 one at a time.
test1()
#test2()
#test3()
#test4()

=============================================================================================

click to hide/show revision 2
No.2 Revision

Plot multiple PSSE "out" files

Hi,

I recently came across some code that shows how to run PSSE from python and the some tricks with the dyntools module. This is the code.

The code works great but my problem is, instead of entering the name of each out file, is there a way of plotting all the outfiles in a directory?

Thanks.

----------------------------
import os, sys

=============================================================================================

sys # ============================================================================================= # Get installed location of latest PSS(R)E version

version def latestpsselocation(): latest_psse_location(): import winreg _winreg ptiloc = r"SOFTWARE\PTI" ptikey = _winreg.OpenKey(winreg.HKEYLOCALMACHINE, _w inreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, ptiloc, 0, winreg.KEYREAD) _winreg.KEY_READ) ptikeyinfo = _winreg.QueryInfoKey(ptikey) numptisubkeys = ptikeyinfo[0] vdict = {} for i in range(numptisubkeys): vernum = _winreg.EnumKey(ptikey, i) try: n = int(vernum[-2:]) vdict[n]=vernum except: pass

pass

    vers = vdict.keys()
 vers.sort()
 k = vers[-1]
 lver = vdict[k]
 lverloc = ptiloc + "\\" + lver + "\\" + "Product Paths"
 lverkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, lverloc, 0, _winreg.KEY_READ)
 lverdir, stype = _winreg.QueryValueEx(lverkey, 'PsseInstallPath')
 _winreg.CloseKey(ptikey)
 _winreg.CloseKey(lverkey)
 return lverdir

pssedir = latestpsselocation() latest_psse_location() pssedir = str(pssedir) # convert unicode to str

=============================================================================================

str # ============================================================================================= # Files Used

Used pssbindir = os.path.join(pssedir,'PSSBIN') exampledir = os.path.join(pssedir,'EXAMPLE')

os.path.join(pssedir,'EXAMPLE') savfile = os.path.join(exampledir,'savcnv.sav') snpfile = os.path.join(exampledir,'savnw.snp') outfile1 = os.path.join(exampledir,'bus154fault.out') os.path.join(exampledir,'bus154_fault.out') outfile2 = os.path.join(exampledir,'bus3018gentrip.out') os.path.join(exampledir,'bus3018_gentrip.out') outfile3 = os.path.join(exampledir,'brn30053007trip.out') os.path.join(exampledir,'brn3005_3007_trip.out') prgfile = os.path.join(exampledir,'dyntoolsdemoprogress.txt')

=============================================================================================

os.path.join(exampledir,'dyntools_demo_progress.txt') # ============================================================================================= # Check if running from Python Interpreter

Interpreter exename = sys.executable p, nx = os.path.split(exename) nx = nx.lower() if nx in ['python.exe', 'pythonw.exe']: os.environ['PATH'] = pssbindir + ';' + os.environ['PATH'] sys.path.insert(0,pssbindir)

=============================================================================================

sys.path.insert(0,pssbindir) # ============================================================================================= import dyntools

=============================================================================================

dyntools # ============================================================================================= # Run Dynamic simulation on SAVNW to generate .out files

files def runsavnwsimulation(): run_savnw_simulation(): import redirect redirect.psse2py()
import psspy ierr = psspy.psseinit(buses=80000) # choose here bus numbers you want

want

    psspy.lines_per_page_one_device(1,90)
 psspy.progress_output(2,prgfile,[0,0])

 psspy.case(savfile)
 psspy.rstr(snpfile)
 psspy.strt(0,outfile1)
 psspy.run(0, 1.0,1000,1,0)
 psspy.dist_bus_fault(154,1, 230.0,[0.0,-0.2E+10])
 psspy.run(0, 1.05,1000,1,0)
 psspy.dist_clear_fault(1)
 psspy.run(0, 5.0,1000,1,0)

 psspy.case(savfile)
 psspy.rstr(snpfile)
 psspy.strt(0,outfile2)
 psspy.run(0, 1.0,1000,1,0)
 psspy.dist_machine_trip(3018,'1')
 psspy.run(0, 5.0,1000,1,0)

 psspy.case(savfile)
 psspy.rstr(snpfile)
 psspy.strt(0,outfile3)
 psspy.run(0, 1.0,1000,1,0)
 psspy.dist_branch_trip(3005,3007,'1')
 psspy.run(0, 5.0,1000,1,0)

 psspy.lines_per_page_one_device(2,10000000)
 psspy.progress_output(1,"",[0,0])

=============================================================================================

# ============================================================================================= # 1. Data extraction/information

extraction/information def testdataextraction(chnfobj):
test_data_extraction(chnfobj): print '\n Testing call to getdata' shttl, chid, chdata = chnfobj.getdata() get_data' sh_ttl, ch_id, ch_data = chnfobj.get_data() print shttl sh_ttl print ch_id

ch_id

    print '\n Testing call to get_id'
 sh_ttl, ch_id = chnfobj.get_id()
 print sh_ttl
 print ch_id

 print '\n Testing call to get_range'
 ch_range = chnfobj.get_range()
 print ch_range

 print '\n Testing call to get_scale'
 ch_scale = chnfobj.get_scale()
 print ch_scale

 print '\n Testing call to print_scale'
 chnfobj.print_scale()

 print '\n Testing call to txtout'
 chnfobj.txtout(channels=[1,4])

 print '\n Testing call to xlsout'
 chnfobj.xlsout(channels=[2,3,4,7,8,10])

=============================================================================================

# ============================================================================================= # 2. Multiple subplots in a figure, but one trace in each subplot

subplot def testsubplotsone_trace(chnfobj): test_subplots_one_trace(chnfobj): optnppg = {'size':'Letter', 'orientation':'Portrait'} optnfmt = {'rows':3,'columns':2,'dpi':300,'showttl':True}

{'rows':3,'columns':2,'dpi':300,'showttl':True}

    optnchn1 = {1:{'chns':1},2:{'chns':6},3:{'chns':11},4:{'chns':16},5:{'chns':26},6:{'chns':40}}
 pn,x     = os.path.splitext(outfile1)
 pltfile1 = pn+'.eps'

 optnchn2 = {1:{'chns':{outfile2:1}},
             2:{'chns':{outfile2:6}},
             3:{'chns':{outfile2:11}},
             4:{'chns':{outfile2:16}},
             5:{'chns':{outfile2:26}},
             6:{'chns':{outfile2:40}},
             }
 pn,x     = os.path.splitext(outfile2)
 pltfile2 = pn+'.eps'

 figfiles1 = chnfobj.xyplots(optnchn1,optnppg,optnfmt,pltfile1)

 figfiles2 = chnfobj.xyplots(optnchn2,optnppg,optnfmt,pltfile2)

 print 'Plot fils saved:'
 print '   ', figfiles1[0]
 print '   ', figfiles2[0]

=============================================================================================

# ============================================================================================= # 3. Multiple subplots in a figure and more than one trace in each subplot

subplot def testsubplotsmult_trace(chnfobj): test_subplots_mult_trace(chnfobj): optnppg = {'size':'Letter', 'orientation':'Portrait'} optnfmt = {'rows':2,'columns':2,'dpi':300,'showttl':True}

{'rows':2,'columns':2,'dpi':300,'showttl':True}

    optnchn1 = {1:{'chns':[1]},2:{'chns':[2]},3:{'chns':[3]},4:{'chns':[4]},5:{'chns':[5]}}
 pn,x     = os.path.splitext(outfile1)
 pltfile1 = pn+'.png'

 optnchn2 = {1:{'chns':{outfile2:1}},
             2:{'chns':{'v82_test1_bus_fault.out':3}},
             3:{'chns':4},
             4:{'chns':[5]}
            }
 pn,x     = os.path.splitext(outfile2)
 pltfile2 = pn+'.pdf'

 optnchn3 = {1:{'chns':{'v80_test1_bus_fault.out':1}},
             2:{'chns':{'v80_test2_complex_wind.out':[1,5]}},
             3:{'chns':{'v82_test1_bus_fault.out':3}},
             5:{'chns':[4,5]},
            }
 pn,x     = os.path.splitext(outfile3)
 pltfile3 = pn+'.eps'

 figfiles1 = chnfobj.xyplots(optnchn1,optnppg,optnfmt,pltfile1)
 figfiles2 = chnfobj.xyplots(optnchn2,optnppg,optnfmt,pltfile2)
 figfiles3 = chnfobj.xyplots(optnchn3,optnppg,optnfmt,pltfile3)

 figfiles = figfiles1[:]
 figfiles.extend(figfiles2)
 figfiles.extend(figfiles3)
 print 'Plot fils saved:'
 for f in figfiles:
     print '    ', f

=============================================================================================

f # ============================================================================================= # 4. Do XY plots and insert them into word file

file def testplots2word(chnfobj): test_plots2word(chnfobj): p,nx = os.path.split(outfile1) docfile = os.path.join(p,'savnwresponse.doc') os.path.join(p,'savnw_response.doc') show = True overwrite = True caption = True align = 'center' captionpos = 'below' height = 0.0 width = 0.0 rotate = 0.0

0.0

    optnppg  = {'size':'Letter', 'orientation':'Portrait'}
 optnfmt  = {'rows':3,'columns':1,'dpi':300,'showttl':True}

 optnchn  = {1:{'chns':{outfile1:1,  outfile2:1,  outfile3:1} },
             2:{'chns':{outfile1:7,  outfile2:7,  outfile3:7} },
             3:{'chns':{outfile1:17, outfile2:17, outfile3:17} },
             4:{'chns':[1,2,3,4,5]},
             5:{'chns':{outfile2:[1,2,3,4,5]} },
             6:{'chns':{outfile3:[1,2,3,4,5]} },
            }
 chnfobj.xyplots2doc(optnchn,optnppg,optnfmt,docfile,show,overwrite,caption,align,
                     captionpos,height,width,rotate)

 print 'Plots saved to file:'
 print '    ', docfile

=============================================================================================

# ============================================================================================= def test1():

test1():

    # create out files, run this only once and comment out once you have the .out files
 run_savnw_simulation()

 # create object
 outlst = [outfile1, outfile2, outfile3]
 chnf = dyntools.CHNF(outlst)

 test_data_extraction(chnf)

=============================================================================================

# ============================================================================================= def test2():

test2():

    # create object
 outlst = [outfile1, outfile2, outfile3]
 chnf = dyntools.CHNF(outlst)

 test_subplots_one_trace(chnf)
 chnf.plots_show()

=============================================================================================

# ============================================================================================= def test3():

test3():

    # create object
 outlst = [outfile1, outfile2, outfile3]
 chnf = dyntools.CHNF(outlst)

 test_subplots_mult_trace(chnf)
 chnf.plots_show()

=============================================================================================

# ============================================================================================= def test4():

test4():

    # create object
 outlst = [outfile1, outfile2, outfile3]
 chnf = dyntools.CHNF(outlst)

 test_plots2word(chnf)

=============================================================================================

# ============================================================================================= if name __name__ == 'main':

'__main__':

    # Need to run test1 before running test2, test3 or test4
 # After running test1, you need to run test2, test3 and test4 one at a time.
 test1()
 #test2()
 #test3()
 #test4()

# =============================================================================================

=============================================================================================