Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You should take a look at the dyntools module for pulling out relevant data from a .out file. The matplotlib is also a great module for plotting very similar to Matlab. I use the following to spit out *.png files that contains Frequency, Voltage, and System Angles for each simulation. You can just use this routine (or similar) after the completion of each contingency, or after all contingencies have finished.

# To install matplotlib tool, open command prompt and type the following:
#    C:\python27\python.exe -m pip install --upgrade pip
#    C:\python27\python.exe -m pip install matplotlib   

import sys
import os
import matplotlib
import numpy
import dyntools
import glob

# Define Main subroutine
def main(out_folder):
    for out_file in glob.glob(out_folder + '\\*.out'):
        plot_out(out_file)

# Define plot subroutine    
def plot_out(out_file):
    # Open file and grab data
    chan = dyntools.CHNF(out_file)

    # Set up plot details
    chan_range = chan.get_range()
    titles, ids, data = chan.get_data()
    fig = plt.figure(figsize=(8.27, 11.69))
    title = os.path.splitext(out_file)[0]
    fig.suptitle(title, fontsize=24)

    tmax = int(chan_range['time']['max'])
    if not tmax:
        print "%s has no data" % title
        return
    trange = numpy.arange(0, tmax, 1)

    # Create subplots
    ax1 = fig.add_subplot(311)
    ax2 = fig.add_subplot(312)
    ax3 = fig.add_subplot(313)
    for key, value in ids.iteritems():
        if 'VOLT' in value:
            ax2.plot(data['time'], data[key],label="key", linewidth=0.25)
        if 'FREQ' in value:
            data2 = [60 + x * 60 for x in data[key]]
            ax1.plot(data['time'], data2,label="key", linewidth=0.25)
        if 'ANGL' in value:
            ax3.plot(data['time'], data[key],label="key", linewidth=1)

    # Plot data
    ax1.set_xlabel('Time (sec)')
    ax1.set_title('Frequency')
    ax1.set_ylabel('Frequency (Hz)')
    ax1.set_xticks(trange)
    ax1.set_xbound(0,tmax)
    ax1.set_ybound(58,62)

    ax2.set_xlabel('Time (sec)')
    ax2.set_title('Voltage')
    ax2.set_ylabel('Voltage (pu)')
    ax2.set_xticks(trange)
    ax2.set_yticks([0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1.0,1.1,1.2])
    ax2.set_xbound(0,tmax)
    ax2.set_ybound(0,1.2)

    ax3.set_xlabel('Time (sec)')
    ax3.set_title('System Slack Bus Angle')
    ax3.set_ylabel('Angle (deg)')
    ax3.set_xticks(trange)
    ax3.set_xbound(0,tmax)

    ax1.grid(True)
    ax2.grid(True)
    ax3.grid(True)

    plt.tight_layout(rect=[0, 0.03, 1, 0.95])

    fname = title + '.png'
    plt.savefig(fname, dpi=600)     
    plt.close(fig)

if __name__ == "__main__":
    main(r'C:\Path\To\Out\Files')