First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!
1 | initial version | asked 2014-11-18 23:07:31 -0500 Anonymous |
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?
import os, sys
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
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')
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
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])
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])
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]
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
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()
2 | No.2 Revision |
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?
----------------------------
import os,