First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!
1 | initial version |
The dyntools module is a basic, 'hardcoded' implementation of plotting with matplotlib, where customization (positioning of legends, etc.) is not allowed. BTW, the above code did not work in my pc, so I made a few mods to it:
import os,sys
#---User vars -----------------
psseversion = 33
folder = r'C:\Users\jconto\Documents\Code\Python\pti\EXAMPLE33'
outfile = r"bus154_fault.out"
chans = [41,42]
#--------------------------------
if os.environ.has_key('PSSEVERSION'): #STR returned!
psseversion = int(os.environ['PSSEVERSION'])
if psseversion== 34 or psseversion== 33:
exec('import psse%s'%psseversion)
else:
pssepath = r"C:\Program Files (x86)\PTI\PSSE%s\PSSBIN"%psseversion
if pssepath not in sys.path:
sys.path.append(pssepath)
#--------------------------------
import psspy
#psspy.psseinit(15000)
#--------------------------------
import dyntools
import matplotlib.pyplot as plt
import time, datetime
def pltarray(chans):
# Determine the structure of the subplots (rows and columns based on the number of channels)
if not len(chans)>0:
return 1, 1
count = len(chans)
if (count == 1):
nRows = 1
nCols = 1
elif (count == 2):
nRows = 2
nCols = 1
elif (count == 3):
nRows = 3
nCols = 1
elif (count == 4):
nRows = 2
nCols = 2
elif (count in [5, 6]):
nRows = 4
nCols = 2
elif (count in [7, 8, 9]):
nRows = 4
nCols = 2
elif (count in [10, 11, 12]):
nRows = 4
nCols = 3
else:
nRows = 4
nCols = 2
return nRows, nCols
#--------------------------------
def plot_out_file(folder=[], outfile='', chans=None):
""" Plot specific channels from the out file specified and save as subplots on one image.
:param folder: Folder location that the outfile can be found
:param outfile: The file name of the out file to be plotted
:param chans: The channels of the out file to be plotted
:return: outfile
"""
# if there are no channels, assume all channels
if chans is None:
chans = []
chnfobj, optnchn, optnppg, optnfmt = [], [], [], []
# Load the outfile and extract the data
outlist = []
if not isinstance(folder, list):
folder = [folder,]
print ' 0 folder=', folder
for fol in folder:
outlist.append(os.path.join(fol, outfile))
chnfobj = dyntools.CHNF(outlist)
sh_ttl, ch_id, ch_data = chnfobj.get_data()
nRows, nCols = pltarray(chans)
# Determine the options for the subplots
optnchn = dict()
#optnppg = {'size': 'A3', 'orientation': 'Portrait'} # 'Letter', 'Legal', 'A3', 'A4', #'Portrait', 'Landscape'
optnfmt = {'rows': nRows, 'columns': nCols, 'dpi': 300, 'showttl': True}
plotID = 1
for i in range(0, len(chans)):
ch = dict()
for fol in folder:
ch[os.path.join(fol, outfile)] = chans[i]
if isinstance(chans[i], list):
id = ch_id[chans[i][0]]
else:
id = ch_id[chans[i]]
optnchn[plotID] = {'chns': ch,
'title': id,
'xscale': [0, 15]}
plotID = plotID + 1
time = str(datetime.datetime.now().strftime("%Y%m%d%H%M"))
# print os.path.join(folder, outfile)
# Plot the out file and save with the same file name with a *.png attachment
print 'folder=', folder
file_str = os.path.join(folder[0], outfile).replace('.out', '.png')
file_str = file_str.split(".png", 1)
file_str = file_str[0] + "_" + str(datetime.datetime.now().strftime("%Y%m%d_%H%M%S")) + ".png"
# file_str = file_str[0] + ".png"
figfiles = chnfobj.xyplots(optnchn, optnfmt, file_str)
if figfiles:
print 'Plot fils saved:'
for f in figfiles:
print ' ', f
chnfobj.plots_close()
#--------------------------------
#------ MAIN ------------------
plot_out_file(folder, outfile, chans)
2 | No.2 Revision |
The dyntools module is a basic, 'hardcoded' implementation of plotting with matplotlib, where customization (positioning of legends, etc.) is not allowed. BTW, the above code did not work in my pc, so I made a few mods to it:
import os,sys
#---User vars -----------------
psseversion = 33
folder = r'C:\Users\jconto\Documents\Code\Python\pti\EXAMPLE33'
r'C:\Users\xx\Documents\Code\Python\pti\EXAMPLE33'
outfile = r"bus154_fault.out"
chans = [41,42]
#--------------------------------
if os.environ.has_key('PSSEVERSION'): #STR returned!
psseversion = int(os.environ['PSSEVERSION'])
if psseversion== 34 or psseversion== 33:
exec('import psse%s'%psseversion)
else:
pssepath = r"C:\Program Files (x86)\PTI\PSSE%s\PSSBIN"%psseversion
if pssepath not in sys.path:
sys.path.append(pssepath)
#--------------------------------
import psspy
#psspy.psseinit(15000)
#--------------------------------
import dyntools
import matplotlib.pyplot as plt
import time, datetime
def pltarray(chans):
# Determine the structure of the subplots (rows and columns based on the number of channels)
if not len(chans)>0:
return 1, 1
count = len(chans)
if (count == 1):
nRows = 1
nCols = 1
elif (count == 2):
nRows = 2
nCols = 1
elif (count == 3):
nRows = 3
nCols = 1
elif (count == 4):
nRows = 2
nCols = 2
elif (count in [5, 6]):
nRows = 4
nCols = 2
elif (count in [7, 8, 9]):
nRows = 4
nCols = 2
elif (count in [10, 11, 12]):
nRows = 4
nCols = 3
else:
nRows = 4
nCols = 2
return nRows, nCols
#--------------------------------
def plot_out_file(folder=[], outfile='', chans=None):
""" Plot specific channels from the out file specified and save as subplots on one image.
:param folder: Folder location that the outfile can be found
:param outfile: The file name of the out file to be plotted
:param chans: The channels of the out file to be plotted
:return: outfile
"""
# if there are no channels, assume all channels
if chans is None:
chans = []
chnfobj, optnchn, optnppg, optnfmt = [], [], [], []
# Load the outfile and extract the data
outlist = []
if not isinstance(folder, list):
folder = [folder,]
print ' 0 folder=', folder
for fol in folder:
outlist.append(os.path.join(fol, outfile))
chnfobj = dyntools.CHNF(outlist)
sh_ttl, ch_id, ch_data = chnfobj.get_data()
nRows, nCols = pltarray(chans)
# Determine the options for the subplots
optnchn = dict()
#optnppg = {'size': 'A3', 'orientation': 'Portrait'} # 'Letter', 'Legal', 'A3', 'A4', #'Portrait', 'Landscape'
optnfmt = {'rows': nRows, 'columns': nCols, 'dpi': 300, 'showttl': True}
plotID = 1
for i in range(0, len(chans)):
ch = dict()
for fol in folder:
ch[os.path.join(fol, outfile)] = chans[i]
if isinstance(chans[i], list):
id = ch_id[chans[i][0]]
else:
id = ch_id[chans[i]]
optnchn[plotID] = {'chns': ch,
'title': id,
'xscale': [0, 15]}
plotID = plotID + 1
time = str(datetime.datetime.now().strftime("%Y%m%d%H%M"))
# print os.path.join(folder, outfile)
# Plot the out file and save with the same file name with a *.png attachment
print 'folder=', folder
file_str = os.path.join(folder[0], outfile).replace('.out', '.png')
file_str = file_str.split(".png", 1)
file_str = file_str[0] + "_" + str(datetime.datetime.now().strftime("%Y%m%d_%H%M%S")) + ".png"
# file_str = file_str[0] + ".png"
figfiles = chnfobj.xyplots(optnchn, optnfmt, file_str)
if figfiles:
print 'Plot fils saved:'
for f in figfiles:
print ' ', f
chnfobj.plots_close()
#--------------------------------
#------ MAIN ------------------
plot_out_file(folder, outfile, chans)