Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to use psspy inside a function??

Hello People,

I defined a function in python which was supposed to do some tasks. Inside the function I was using the API routines but i am getting the error " Name Error: global name "psspy" not defined". This is happening when i am importing a file named temp and using certain parts of it. Without the file temp, it works fine, bust since temp file is an integral part of the whole project i have to use it. Now how do i make psspy run inside the function without getting such an error.

#Files Necessary for GUI part of this CODE
from Tkinter import *
import tkMessageBox
import Tkinter
import Tkinter, tkMessageBox
import tkFileDialog
import temp

#Calling of the PSSE location in Python and also informing PSSE about its environment
import os
import sys
psse_path=r'C:\Program Files\PTI\PSSE33\PSSBIN'
sys.path.append(psse_path)
os.environ['PATH']+=';'+psse_path
print "\n"


import psspy
psspy.throwPsseException=True
import redirect
redirect.psse2py() 
psspy.psseinit(8000)
psspy.case(temp.filenam)
from psspy import _i
from psspy import _f
from psspy import _s
print "\n"

print temp.con_file
print "***********************************************************\n\n\n"

def TS1_S1_LOADRELIEF():
    l1=psspy.loddt2(7824,"1",'TOTAL','ACT')
    l2=psspy.loddt2(8032,"1",'TOTAL','ACT')
    l3=psspy.loddt2(8033,"1",'TOTAL','ACT')
    l4=psspy.loddt2(7928,"1",'TOTAL','ACT')
    l5=psspy.loddt2(7865,"1",'TOTAL','ACT')
    L1=l1[1].real+l2[1].real+l3[1].real+l4[1].real+l5[1].real
    l1=psspy.loddt2(36591,"1",'TOTAL','ACT')
    l2=psspy.loddt2(36588,"1",'TOTAL','ACT')
    L2=l1[1].real+l2[1].real
    l1=psspy.loddt2(36576,"1",'TOTAL','ACT')
    psspy.bsys(1,0,[0.0,0.0],0,[],5,[7824,8032,8033,7928,7865],0,[],0,[])
    psspy.scal_2(1,0,1,[0,0,0,0,0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0])
    psspy.scal_2(0,1,2,[_i,1,0,4,0],[L1-150,0.0,0.0,-.0, 67.0,-.0, 0.95])
    psspy.bsys(1,0,[0.0,0.0],0,[],2,[36591,36588],0,[],0,[])
    psspy.scal_2(1,0,1,[0,0,0,0,0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0])
    psspy.scal_2(0,1,2,[_i,1,0,4,0],[L2-220,0.0,0.0,-.0,0.0,-.0, 0.95])
    psspy.bsys(1,0,[0.0,0.0],0,[],1,[36576],0,[],0,[])
    psspy.scal_2(1,0,1,[0,0,0,0,0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0])
    psspy.scal_2(0,1,2,[_i,1,0,4,0],[l1[1].real-100,0.0,0.0,-.0,0.0,-.0, 0.95])

def calculate_PI():
    s=0
    psspy.fnsl([0,0,0,1,0,0,0,0])
    ival=psspy.solved()
    print "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n"
    print ival
    print "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n"
    if ival!=0:
        psspy.fdns([0,0,0,1,1,1,99,0])
        psspy.fnsl([0,0,0,1,0,0,0,0])
    psspy.bsys(0,1,[ 400., 765.],37,[513,501,510,511,512,514,532,524,502,521,531,541,507,503,545,516,504,515,509,546,533,523,543,506,548,547,550,505,522,542,518,508,544,519,551,552,517],0,[],0,[],0,[])
   ierr, rarray1 = psspy.abrnreal(0,1,3,1,1,'P')
   ierr, rarray2 = psspy.abrnreal(0,1,3,1,1,'RATEB')
   for i in range(199):
        a=float(rarray1[0][i])
        b=float(rarray2[0][i])
        c=float(a/b)
        c=c*c*c*c*c*c*c*c*c*c*c*c*c*c*c*c*c*c*c*c
        s=s+c
    print s
    return s

SPSRANK=[]
temp.con_file.pop(0)



for i,j in enumerate(temp.con_file):
     bus_i,i_name,bus_j,j_name,bus_id,pi=temp.con_file[i]
     psspy.branch_chng(int(bus_i),int(bus_j),str(int(bus_id)),[0,_i,_i,_i,_i,_i],[_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f])
     #TRIP SIGNAL 1 in S1
     TS1_S1_LOADRELIEF()
     a2=calculate_PI()
     TS1_S1_RETAIN_COND()
     print pi-a2
     psspy.branch_chng(int(bus_i),int(bus_j),str(int(bus_id)),[1,_i,_i,_i,_i,_i],[_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f])

How to use psspy inside a function??

Hello People,

I defined a function in python which was supposed to do some tasks. Inside the function I was using the API routines but i am getting the error " Name Error: global name "psspy" not defined". This is happening when i am importing a file named temp and using certain parts of it. Without the file temp, it works fine, bust since temp file is an integral part of the whole project i have to use it. Now how do i make psspy run inside the function without getting such an error.

NOTE:- This code is being run through command prompt. That is probably the place where problem lies. When i am running in python shell, the code works fine

#Files Necessary for GUI part of this CODE
from Tkinter import *
import tkMessageBox
import Tkinter
import Tkinter, tkMessageBox
import tkFileDialog
import temp

#Calling of the PSSE location in Python and also informing PSSE about its environment
import os
import sys
psse_path=r'C:\Program Files\PTI\PSSE33\PSSBIN'
sys.path.append(psse_path)
os.environ['PATH']+=';'+psse_path
print "\n"


import psspy
psspy.throwPsseException=True
import redirect
redirect.psse2py() 
psspy.psseinit(8000)
psspy.case(temp.filenam)
from psspy import _i
from psspy import _f
from psspy import _s
print "\n"

print temp.con_file
print "***********************************************************\n\n\n"

def TS1_S1_LOADRELIEF():
    l1=psspy.loddt2(7824,"1",'TOTAL','ACT')
    l2=psspy.loddt2(8032,"1",'TOTAL','ACT')
    l3=psspy.loddt2(8033,"1",'TOTAL','ACT')
    l4=psspy.loddt2(7928,"1",'TOTAL','ACT')
    l5=psspy.loddt2(7865,"1",'TOTAL','ACT')
    L1=l1[1].real+l2[1].real+l3[1].real+l4[1].real+l5[1].real
    l1=psspy.loddt2(36591,"1",'TOTAL','ACT')
    l2=psspy.loddt2(36588,"1",'TOTAL','ACT')
    L2=l1[1].real+l2[1].real
    l1=psspy.loddt2(36576,"1",'TOTAL','ACT')
    psspy.bsys(1,0,[0.0,0.0],0,[],5,[7824,8032,8033,7928,7865],0,[],0,[])
    psspy.scal_2(1,0,1,[0,0,0,0,0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0])
    psspy.scal_2(0,1,2,[_i,1,0,4,0],[L1-150,0.0,0.0,-.0, 67.0,-.0, 0.95])
    psspy.bsys(1,0,[0.0,0.0],0,[],2,[36591,36588],0,[],0,[])
    psspy.scal_2(1,0,1,[0,0,0,0,0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0])
    psspy.scal_2(0,1,2,[_i,1,0,4,0],[L2-220,0.0,0.0,-.0,0.0,-.0, 0.95])
    psspy.bsys(1,0,[0.0,0.0],0,[],1,[36576],0,[],0,[])
    psspy.scal_2(1,0,1,[0,0,0,0,0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0])
    psspy.scal_2(0,1,2,[_i,1,0,4,0],[l1[1].real-100,0.0,0.0,-.0,0.0,-.0, 0.95])

def calculate_PI():
    s=0
    psspy.fnsl([0,0,0,1,0,0,0,0])
    ival=psspy.solved()
    print "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n"
    print ival
    print "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n"
    if ival!=0:
        psspy.fdns([0,0,0,1,1,1,99,0])
        psspy.fnsl([0,0,0,1,0,0,0,0])
    psspy.bsys(0,1,[ 400., 765.],37,[513,501,510,511,512,514,532,524,502,521,531,541,507,503,545,516,504,515,509,546,533,523,543,506,548,547,550,505,522,542,518,508,544,519,551,552,517],0,[],0,[],0,[])
   ierr, rarray1 = psspy.abrnreal(0,1,3,1,1,'P')
   ierr, rarray2 = psspy.abrnreal(0,1,3,1,1,'RATEB')
   for i in range(199):
        a=float(rarray1[0][i])
        b=float(rarray2[0][i])
        c=float(a/b)
        c=c*c*c*c*c*c*c*c*c*c*c*c*c*c*c*c*c*c*c*c
        s=s+c
    print s
    return s

SPSRANK=[]
temp.con_file.pop(0)



for i,j in enumerate(temp.con_file):
     bus_i,i_name,bus_j,j_name,bus_id,pi=temp.con_file[i]
     psspy.branch_chng(int(bus_i),int(bus_j),str(int(bus_id)),[0,_i,_i,_i,_i,_i],[_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f])
     #TRIP SIGNAL 1 in S1
     TS1_S1_LOADRELIEF()
     a2=calculate_PI()
     TS1_S1_RETAIN_COND()
     print pi-a2
     psspy.branch_chng(int(bus_i),int(bus_j),str(int(bus_id)),[1,_i,_i,_i,_i,_i],[_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f])