Python for Power Systems

A blog for power systems engineers to learn Python.

Silencing PSSE Output

Silence!

Thor 2011

PSSE prints a voluminous amount of information to the command line when you are running a program. Do you remember seeing this when you start PSSE up?

1
2
3
4
5
6
PSSĀ«E Version 32
Copyright (c) 1976-2012
Siemens Energy, Inc.,
Power Technologies International (PTI)
This program is a confidential unpublished work created and first
licensed in 1976. It is a trade secret which is the property of PTI

We will show you a little function that you can use to selectively turn off this output (and other PSSE output) or redirect it to a log file.

This blog post is in response to a question by chip on the python for power systems engineers forum. Chip wanted to ignore some of the output that PSSE was printing without ignoring all of the program’s output.

silence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from __future__ import with_statement
from contextlib import contextmanager
import sys
import os

@contextmanager
def silence(file_object=None):
    """
    Discard stdout (i.e. write to null device) or
    optionally write to given file-like object.
    """
    if file_object is None:
        file_object = open(os.devnull, 'w')

    old_stdout = sys.stdout
    try:
        sys.stdout = file_object
        yield
    finally:
        sys.stdout = old_stdout

The function silence can be used to selectively turn off PSSE output printing in your program. Use it like this:

using silence
1
2
with silence():
  psspy.psseinit(10000)

Printing from within the context block (the with statement) will be ignored. (Note that we have initialised PSSE with buses set to ten thousand – based on our research of the optimal bus number).

What if you must redirect the PSSE output to a file? Got you covered. Use the silence function like this:

1
2
3
psse_log = open('psse_logfile.log', 'w')
with silence(psse_log):
  psspy.psseinit(10000)

There are more juicy details about how the context manager works on the forum: Silencing PSSE Stdout

edit: You must also redirect PSSE output to Python using the redirect module included with every PSSE installation:

1
2
3
4
5
6
import redirect
redirect.psse2py()

psse_log = open('psse_logfile.log', 'w')
with silence(psse_log):
  psspy.psseinit(10000)