I am power engineer and I often use python in PSS/E program. I am stacked and I want your help as programmers. I have this small code:
import os,sys
PSSE_LOCATION = r"C:\Program Files\PTI\PSSE33\PSSBIN"
sys.path.append(PSSE_LOCATION)
os.environ['PATH'] = os.environ['PATH'] + ';' + PSSE_LOCATION
import psspy
import redirect
redirect.psse2py()
#--------------------------------
# PSS/E Saved case
CASE = r"""D:\xxx\Desktop\TESTING\SUMMAX.sav"""
if __name__ == '__main__':
psspy.psseinit(2000)
psspy.case(CASE)
psspy.fnsl(
options1=0, # disable tap stepping adjustment.
options5=0, # disable switched shunt adjustment.
)
psspy.fdns([0,0,0,1,1,0,99,0])
psspy.area_2(0,1,1)
Code redirect.psse2py()
prints the program report in Console. Can you help me to get those outputs as variable?
Given I don't know much about PSSE, you can try the following:
import sys
import io
out, err = io.StringIO(), io.StringIO()
sys.stdout = out
sys.stderr = err
# rest of your code here
# once your code is finished
results = out.getvalue()
errors = err.getvalue()
My favorite solution so far is based on @J.F Sebastian answer Redirect stdout to a file in Python?.
Using the psspy.report_output()
and related functions at first seemed like the best option, but they are a little painful because you can only write to a file and not a StringIO
buffer.
Here is a complete example:
import contextlib
import sys
# Auto-magically setup PSSE environment.
# You can do this step the hard way if you want
import pssepath
pssepath.add_pssepath()
import psspy
import redirect
redirect.psse2py()
psspy.psseinit(50000)
@contextlib.contextmanager
def redirect_stdout(new_target):
old_target, sys.stdout = sys.stdout, new_target # replace sys.stdout
try:
yield new_target # run some code with the replaced stdout
finally:
sys.stdout = old_target # restore to the previous value
import StringIO
f = StringIO.StringIO()
print "before redirect"
with redirect_stdout(f):
psspy.report('123xxx(report)')
psspy.alert('abcyyy(alert)')
psspy.progress('foobar(progress)')
f.seek(0)
var = f.read()
print "after redirect"
print "var: %s" % var
which prints:
< snip PSSE init copyright header>
before redirect
after redirect
var: 123xxx(report)
abcyyy(alert)
foobar(progress)