-############################################################
-# ojectif de ce module: extraire des donnes pour afficher dans l'interface Eficas
-############################################################
+###################################################################################
+# objectif de ce module: extraire des donnees pour afficher dans l'interface Eficas
+###################################################################################
NoBreakersandSwitches = True
+#_____________ TODO : demander a Claire si il faut absolument garder cela
+# si oui, eventuellement en faire une option propre
+import os
+import sys
+import numpy as np
+import pickle
from utils import nettoyeName
-def ExtractGeneratorLoadLineandTransfoDico(NetworkFile,PSSE_PATH, PSSPY_PATH):
- import os
- import sys
- import numpy as np
+def initPSSE ( PSSE_PATH, PSSPY_PATH):
+#_____________________________________
+# initialisation de PSSE
+# Appele de ExtractGeneratorLoadLineandTransfoDico
- #print NetworkFile
- #print PSSE_PATH
- #psspy_path34 = os.path.join("../", PSSE_PATH, "PSSPY27")
sys.path.append(PSSPY_PATH)
os.environ['PATH'] += ';' + PSSE_PATH + ';'
islct=6 # 6=no output; 1=standard
psspy.progress_output(islct)
- #open Network File
+
+def ExtractGeneratorLoadLineandTransfoDico(NetworkFile,PSSE_PATH, PSSPY_PATH,debug=False):
+# ________________________________________________________________________________________
+# genere un dictionnaire contient reseau pour interface Eficas
+# a partir d un fichier PSSE
+# return des 5 dicos et ecriture dans le fichier temp.txt
+
+ # initialisation PSS
+ if debug : print ('initialisation PSSE')
+ initPSSE ( PSSE_PATH, PSSPY_PATH)
+
+ # extration du Reseau
psspy.case(NetworkFile)
- #Extract Loads
+ # Remplit Les dicos pour eficas
+
+ #Extract Loads depuis PSSE
+ if debug : print ("Read Load")
sid = -1 #all buses
flag = 1 #all in service loads/generators (4 all loads/generators)
-
string = ['NUMBER']
ierr,iarray = psspy.aloadint(sid,flag,string)
-
string = ['NAME','ID','EXNAME']
ierr,carray = psspy.aloadchar(sid,flag,string)
-
string = ['mvaact']
ierr, xdata = psspy.aloadcplx(sid, flag, string)
+
LoadDico = {} # [Bus name, load ID, extended bus name, bus number]
for i in range(len(iarray[0])):
idname = "Lo" + carray[1][i].strip()
-# try: #id is an integer
-# idname = "Lo" + str(int(carray[1][i]))
-# except: #id is not an integer
-# idname = "Lo" + carray[1][i]
loadname = carray[0][i].strip()+ "__" + idname
loadname=nettoyeName(loadname)
- #loadname = loadname.replace(" ","_")
- #loadname = loadname.replace("-","_")
- #loadname = loadname.replace(".","_")
- #loadname = loadname.replace("&","and")
- #loadname = loadname.replace("%","pct")
- #loadname = loadname.replace("=","eq")
- #loadname = loadname.replace("#","_")
- #loadname = loadname.replace("$","_")
- #loadname = loadname.replace("/","_")
- #loadname = loadname.replace("\\","_")
- #try:
- # int(loadname[0])
- # loadname="_" + loadname
- #except:
- # pass
+
LoadDico[loadname]= {}
- LoadDico[loadname]['NAME'] = carray[0][i].strip()
- LoadDico[loadname]['ID'] = carray[1][i]
- LoadDico[loadname]['EXNAME'] =carray[2][i]
- LoadDico[loadname]['NUMBER']=iarray[0][i]
- LoadDico[loadname]['P']=np.real(xdata)[0][i]
- LoadDico[loadname]['Q']=np.imag(xdata)[0][i]
+ LoadDico[loadname]['NAME'] = carray[0][i].strip()
+ LoadDico[loadname]['ID'] = carray[1][i]
+ LoadDico[loadname]['EXNAME'] = carray[2][i]
+ LoadDico[loadname]['NUMBER'] = iarray[0][i]
+ LoadDico[loadname]['P'] = np.real(xdata)[0][i]
+ LoadDico[loadname]['Q'] = np.imag(xdata)[0][i]
#Extract Generators
+ if debug : print ("Read Generators")
sid = -1 #all buses
flag = 1 #all in service loads/generators (4 all loads/generators)
-
string = ['NUMBER']
ierr,iarray = psspy.amachint(sid,flag,string)
-
string = ['NAME','ID','EXNAME']
ierr,carray = psspy.amachchar(sid,flag,string)
-
rstrings = ['pgen','qgen','mbase','pmax','qmax','pmin','qmin']
ierr, rarray = psspy.amachreal(sid, flag, rstrings)
MachineDico = {} # [Bus name, machine ID, extended bus name, bus number]
for i in range(len(iarray[0])):
idname = "Gr" + carray[1][i].strip()
-## try:
-## idname = "Gr" + str(int(carray[1][i]))
-## except:
-## idname = "Gr" + carray[1][i]
machinename = carray[0][i].strip()+ "__" + idname
machinename=nettoyeName(machinename)
- #machinename = machinename.replace(" ","_")
- #machinename = machinename.replace("-","_")
- #machinename = machinename.replace(".","_")
- #machinename = machinename.replace("&","and")
- #machinename = machinename.replace("%","pct")
- #machinename = machinename.replace("=","eq")
- #machinename = machinename.replace("#","_")
- #machinename = machinename.replace("$","_")
- #machinename = machinename.replace("/","_")
- #machinename = machinename.replace("\\","_")
- #try:
- # int(machinename[0])
- # machinename="_" + machinename
- #except:
- # pass
MachineDico[machinename]={}
- MachineDico[machinename]['NAME'] = carray[0][i].strip()
- MachineDico[machinename]['ID'] = carray[1][i]
- MachineDico[machinename]['EXNAME'] =carray[2][i]
- MachineDico[machinename]['NUMBER']=iarray[0][i]
- MachineDico[machinename]['P']=rarray[0][i]
- MachineDico[machinename]['Q']=rarray[1][i]
- MachineDico[machinename]['PMAX']=rarray[3][i]
- MachineDico[machinename]['QMAX']=rarray[4][i]
- MachineDico[machinename]['PMIN']=rarray[5][i]
- MachineDico[machinename]['QMIN']=rarray[6][i]
+ MachineDico[machinename]['NAME'] = carray[0][i].strip()
+ MachineDico[machinename]['ID'] = carray[1][i]
+ MachineDico[machinename]['EXNAME'] = carray[2][i]
+ MachineDico[machinename]['NUMBER'] = iarray[0][i]
+ MachineDico[machinename]['P'] = rarray[0][i]
+ MachineDico[machinename]['Q'] = rarray[1][i]
+ MachineDico[machinename]['PMAX'] = rarray[3][i]
+ MachineDico[machinename]['QMAX'] = rarray[4][i]
+ MachineDico[machinename]['PMIN'] = rarray[5][i]
+ MachineDico[machinename]['QMIN'] = rarray[6][i]
#Extract Motors
+ if debug : print ("Read Motors")
sid = -1 #all buses
flag = 1 #all in service loads/generators (4 all loads/generators)
string = ['NUMBER','PSETCODE','BASECODE']
ierr,iarray = psspy.aindmacint(sid,flag,string)
-
string = ['NAME','ID','EXNAME']
ierr,carray = psspy.aindmacchar(sid,flag,string)
-
rstrings = ['psetpoint','mbase','p','q']
ierr, rarray = psspy.aindmacreal(sid, flag, rstrings)
-
MotorDico = {} # [Bus name, machine ID, extended bus name, bus number]
for i in range(len(iarray[0])):
idname = "Mo" + carray[1][i].strip()
-## try:
-## idname = "Gr" + str(int(carray[1][i]))
-## except:
-## idname = "Gr" + carray[1][i]
motorname = carray[0][i].strip()+ "__" + idname
- motorname = motorname.replace(" ","_")
- motorname = motorname.replace("-","_")
- motorname = motorname.replace(".","_")
- motorname = motorname.replace("&","and")
- motorname = motorname.replace("%","pct")
- motorname = motorname.replace("=","eq")
- motorname = motorname.replace("#","_")
- motorname = motorname.replace("$","_")
- motorname = motorname.replace("/","_")
- motorname = motorname.replace("\\","_")
-
- try:
- int(motorname[0])
- motorname="_" + motorname
- except:
- pass
+ motorname = nettoyeName(motorname)
MotorDico[motorname]={}
- MotorDico[motorname]['NAME'] = carray[0][i].strip()
- MotorDico[motorname]['ID'] = carray[1][i]
- MotorDico[motorname]['EXNAME'] =carray[2][i]
- MotorDico[motorname]['NUMBER']=iarray[0][i]
- MotorDico[motorname]['PSETCODE']=iarray[1][i]
- MotorDico[motorname]['BASECODE']=iarray[2][i]
- MotorDico[motorname]['PSETPOINT']=rarray[0][i]
- MotorDico[motorname]['MBASE']=rarray[1][i]
- MotorDico[motorname]['P']=rarray[2][i]
- MotorDico[motorname]['Q']=rarray[3][i]
-
-
- #Extract Lignes
- sid = -1
+ MotorDico[motorname]['NAME'] = carray[0][i].strip()
+ MotorDico[motorname]['ID'] = carray[1][i]
+ MotorDico[motorname]['EXNAME'] = carray[2][i]
+ MotorDico[motorname]['NUMBER'] = iarray[0][i]
+ MotorDico[motorname]['PSETCODE'] = iarray[1][i]
+ MotorDico[motorname]['BASECODE'] = iarray[2][i]
+ MotorDico[motorname]['PSETPOINT'] = rarray[0][i]
+ MotorDico[motorname]['MBASE'] = rarray[1][i]
+ MotorDico[motorname]['P'] = rarray[2][i]
+ MotorDico[motorname]['Q'] = rarray[3][i]
+
+
+ #Extract Line
+ if debug : print ("Read Line")
+ sid = -1
owner = 1
- ties = 1
- flag = 2 #6 for two-winding transfos
- entry = 1 #each branch once, not both directions
+ ties = 1
+ flag = 2 #6 for two-winding transfos
+ entry = 1 #each branch once, not both directions
string = ['FROMNUMBER','TONUMBER']
ierr,iarray = psspy.abrnint(sid,owner,ties,flag,entry,string)
string = ['FROMNAME','TONAME','FROMEXNAME','TOEXNAME','ID']
LineDico = {} #[linename, Bus name 1, Bus name 2, ID, extended bus name 1, extended bus name 2, bus number 1, bus number 2]
for i in range(len(iarray[0])):
idname = carray[4][i].strip()
- #idname = carray[4][i]
- if '@' in idname:
- idname = idname.replace('@','Br')
- elif '*' in idname:
- idname = idname.replace('*','Sw')
- else:
- try:
- idname = 'Li' + str(int(idname))
- except:
- idname = 'Li' + idname
- linename =carray[0][i].strip() + "__" + carray[1][i].strip() + "__" + idname
- linename = linename.replace(" ","_")
- linename = linename.replace("-","_")
- linename = linename.replace(".","_")
- linename = linename.replace("&","and")
- linename = linename.replace("%","pct")
- linename = linename.replace("=","eq")
- linename = linename.replace("#","_")
- linename = linename.replace("$","_")
- linename = linename.replace("/","_")
- linename = linename.replace("\\","_")
- try:
- int(linename[0])
- linename="_" + linename
- except:
- pass
+ linename = nettoyeName(linename)
+
+# TODO ______
+# Attention : ceci peut exlure des lignes qui auraient leur place ici et qui s apelleraient BRquelquechose
+# faire le test avant le remplacement dans la chaine et utilser continue
if NoBreakersandSwitches:
if 'Br' not in idname and 'Sw' not in idname:
LineDico[linename]={}
- LineDico[linename]['FROMNAME']=carray[0][i].strip()
- LineDico[linename]['TONAME']=carray[1][i].strip()
- LineDico[linename]['ID']=carray[4][i]
- LineDico[linename]['FROMEXNAME']=carray[2][i]
- LineDico[linename]['TOEXNAME']=carray[3][i]
- LineDico[linename]['FROMNUMBER']=iarray[0][i]
- LineDico[linename]['TONUMBER']=iarray[1][i]
+ LineDico[linename]['FROMNAME'] = carray[0][i].strip()
+ LineDico[linename]['TONAME'] = carray[1][i].strip()
+ LineDico[linename]['ID'] = carray[4][i]
+ LineDico[linename]['FROMEXNAME'] = carray[2][i]
+ LineDico[linename]['TOEXNAME'] = carray[3][i]
+ LineDico[linename]['FROMNUMBER'] = iarray[0][i]
+ LineDico[linename]['TONUMBER'] = iarray[1][i]
- print "Read lines"
#Extract Transfos
+ if debug : print ("Read Transfos")
+# TODO ______
+# se renseigner sur sid owner ...
sid = -1
owner = 1
ties = 1
- flag = 6 #two-winding transfos
- entry = 1 #each branch once, not both directions
+ flag = 6 #two-winding transfos
+ entry = 1 #each branch once, not both directions
string = ['FROMNUMBER','TONUMBER']
ierr,iarray = psspy.abrnint(sid,owner,ties,flag,entry,string)
string = ['FROMNAME','TONAME','FROMEXNAME','TOEXNAME','ID']
ierr,carray = psspy.abrnchar(sid,owner,ties,flag,entry,string)
- TfoDico = {} #[linename, Bus name 1, Bus name 2, machine ID, extended bus name 1, extended bus name 2, bus number 1, bus number 2]
+ TfoDico = {} #[linename, Bus name 1, Bus name 2, machine ID, extended bus name 1, extended bus name 2, bus number 1, bus number 2]
for i in range(len(iarray[0])):
idname = 'Tr' + carray[4][i].strip()
-## try:
-## idname = 'Tr' + str(int(carray[4][i]))
-## except:
-## idname = 'Tr' + carray[4][i]
tfoname = carray[0][i].strip() + "__" + carray[1][i].strip() + "__" + idname
- tfoname = tfoname.replace(" ","_")
- tfoname = tfoname.replace("-","_")
- tfoname = tfoname.replace(".","_")
- tfoname = tfoname.replace("&","and")
- tfoname = tfoname.replace("%","pct")
- tfoname = tfoname.replace("=","eq")
- tfoname = tfoname.replace("#","_")
- tfoname = tfoname.replace("$","_")
- tfoname = tfoname.replace("/","_")
- tfoname = tfoname.replace("\\","_")
- try:
- int(tfoname[0])
- tfoname="_" + tfoname
- except:
- pass
+ tfoname = nettoyeName(tfoname)
TfoDico[tfoname]={}
- TfoDico[tfoname]['FROMNAME']=carray[0][i].strip()
- TfoDico[tfoname]['TONAME']=carray[1][i].strip()
- TfoDico[tfoname]['ID']=carray[4][i]
- TfoDico[tfoname]['FROMEXNAME']=carray[2][i]
- TfoDico[tfoname]['TOEXNAME']=carray[3][i]
- TfoDico[tfoname]['FROMNUMBER']=iarray[0][i]
- TfoDico[tfoname]['TONUMBER']=iarray[1][i]
- TfoDico[tfoname]['#WIND']=2
- print "Read Transfos"
+ TfoDico[tfoname]['FROMNAME'] = carray[0][i].strip()
+ TfoDico[tfoname]['TONAME'] = carray[1][i].strip()
+ TfoDico[tfoname]['ID'] = carray[4][i]
+ TfoDico[tfoname]['FROMEXNAME'] = carray[2][i]
+ TfoDico[tfoname]['TOEXNAME'] = carray[3][i]
+ TfoDico[tfoname]['FROMNUMBER'] = iarray[0][i]
+ TfoDico[tfoname]['TONUMBER'] = iarray[1][i]
+ TfoDico[tfoname]['#WIND'] = 2
#Extract 3 winding Transfos
- sid = -1 #assume a subsystem containing all buses in working case
- owner_3flow = 1 #1 = use bus ownership 2 = use tfo ownership
- ties_3flow = 3 #ignored bc sid is negative. 3 = interior subsystem and subsystem tie 3 winding transformers
- flag=3 #all 3 winding transfo windings
- string = ['wind1number','wind2number','wind3number']
+ if debug : print ("Read winding Transfos")
+ sid = -1 # assume a subsystem containing all buses in working case
+ owner_3flow = 1 #1 = use bus ownership 2 = use tfo ownership
+ ties_3flow = 3 #ignored bc sid is negative. 3 = interior subsystem and subsystem tie 3 winding transformers
+ flag=3 #all 3 winding transfo windings
+ string = ['wind1number','wind2number','wind3number']
ierr,iarray = psspy.awndint(sid,owner,ties,flag,entry,string)
- string = ['wind1name','wind2name','wind3name','wind1exname','wind2exname','wind3exname','id']
+ string = ['wind1name','wind2name','wind3name','wind1exname','wind2exname','wind3exname','id']
ierr,carray = psspy.awndchar(sid,owner,ties,flag,entry,string)
#[Bus name 1, Bus name 2, Bus name 3, machine ID, extended bus name 1, extended bus name 2, extended bus name 3, bus number 1, bus number 2, bus number 3]
+
for i in range(len(iarray[0])):
idname = 'Tr' + carray[6][i].strip()
-## try:
-## idname = 'Tr' + str(int(carray[4][i]))
-## except:
-## idname = 'Tr' + carray[4][i]
+ tfoname = nettoyeName(tfoname)
tfoname = carray[0][i].strip() + "__" + carray[1][i].strip() + "__" + carray[2][i].strip() + "__" + idname
- tfoname = tfoname.replace(" ","_")
- tfoname = tfoname.replace("-","_")
- tfoname = tfoname.replace(".","_")
- tfoname = tfoname.replace("&","and")
- tfoname = tfoname.replace("%","pct")
- tfoname = tfoname.replace("=","eq")
- tfoname = tfoname.replace("#","_")
- tfoname = tfoname.replace("$","_")
- tfoname = tfoname.replace("/","_")
- tfoname = tfoname.replace("\\","_")
- try:
- int(tfoname[0])
- tfoname="_" + tfoname
- except:
- pass
TfoDico[tfoname]={}
- TfoDico[tfoname]['FROMNAME']=carray[0][i].strip()
- TfoDico[tfoname]['TONAME']=carray[1][i].strip()
- TfoDico[tfoname]['3NAME']=carray[2][i].strip()
- TfoDico[tfoname]['ID']=carray[6][i]
- TfoDico[tfoname]['FROMEXNAME']=carray[3][i]
- TfoDico[tfoname]['TOEXNAME']=carray[4][i]
- TfoDico[tfoname]['3EXNAME']=carray[5][i]
- TfoDico[tfoname]['FROMNUMBER']=iarray[0][i]
- TfoDico[tfoname]['TONUMBER']=iarray[1][i]
- TfoDico[tfoname]['3NUMBER']=iarray[2][i]
- TfoDico[tfoname]['#WIND']=3
- print "Read 3-Winding Transfos"
- # path1 = os.getcwd()
- # import json
- # path_temp=os.path.dirname(NetworkFile)
- # # filew = open(path_temp+'/temp.txt', 'w')
- # json.dump(MachineDico, open("toto.txt", 'w'))
- # # filew.write(MachineDico)
- # # filew.close()
+ TfoDico[tfoname]['FROMNAME'] = carray[0][i].strip()
+ TfoDico[tfoname]['TONAME'] = carray[1][i].strip()
+ TfoDico[tfoname]['3NAME'] = carray[2][i].strip()
+ TfoDico[tfoname]['ID'] = carray[6][i]
+ TfoDico[tfoname]['FROMEXNAME'] = carray[3][i]
+ TfoDico[tfoname]['TOEXNAME'] = carray[4][i]
+ TfoDico[tfoname]['3EXNAME'] = carray[5][i]
+ TfoDico[tfoname]['FROMNUMBER'] = iarray[0][i]
+ TfoDico[tfoname]['TONUMBER'] = iarray[1][i]
+ TfoDico[tfoname]['3NUMBER'] = iarray[2][i]
+ TfoDico[tfoname]['#WIND'] = 3
+
+# ___ TODO __ verifier si on peut les enlever pour PSSE ?
filew = open('temp.txt', 'w')
filew.write(NetworkFile + '\n')
filew.write(PSSE_PATH + '\n')
return MachineDico, LoadDico, LineDico, TfoDico, MotorDico
-def PFExtractGeneratorLoadLineandTransfoDico(NetworkFile, PF_PATH,Python3_path):
- # PSEN sous PowerFactory Lecture les donnees de Generateur,Load,Line,Transfo
- import os,pickle
- import sys, subprocess
- import numpy as np
+def convert(data):
+# _________________
+# fonction recursive permettant de relire les data issues de python3
+# appelee uniquement par PFExtractGeneratorLoadLineandTransfoDico
+# a resorber si le code est compatible python 2 / 3
+
+ import collections
+ if isinstance(data, basestring):
+ return str(data)
+ elif isinstance(data, collections.Mapping):
+ return dict(map(convert, data.iteritems()))
+ elif isinstance(data, collections.Iterable):
+ return type(data)(map(convert, data))
+ else:
+ return data
+
+def PFExtractGeneratorLoadLineandTransfoDico(NetworkFile, PF_PATH, Python3_path):
+#_________________________________________________________________________________
+# PSEN sous PowerFactory Lecture les donnees de Generateur,Load,Line,Transfo
+# fonction identique mais lit un reseau PowerFactory et non PSSE
+# attention --> python3 et non python2 dou subprocess et ecriture de fichier pour passer/recuperer les donnees
+# a simplifier lorsqu on aura un code compatible 2.7 et 3
+# lance le code com.py dans un sous process
+
path1=os.getcwd()
- # path_temp=os.path.dirname(NetworkFile)
filew=open('temp.txt','w')
filew.write(NetworkFile+'\n')
filew.write(PF_PATH + '\n')
filew.write(Python3_path + '\n')
filew.close()
- print('changer le chemin de Python3 executable')
- lancer = [Python3_path+'/python.exe', path1+'/com.py']# changer le chemin de Python3 executable
+
+# ___ TODO __ verifier que python.exe est bien sous Python3_path et sinon
+# afficher un message
+ #print('changer le chemin de Python3 executable')
+
+ lancer = [Python3_path+'/python.exe', path1+'/com.py']
+ import subprocess
proc = subprocess.Popen(lancer)
proc.wait()
+
+# reconvertit les donnees crees en python3 pour les returner a Eficas
with open('Data_for_interface', 'rb') as fichier:
mon_depickler = pickle.Unpickler(fichier)
data_file = mon_depickler.load()
-
- import collections
- def convert(data):
- if isinstance(data, basestring):
- return str(data)
- elif isinstance(data, collections.Mapping):
- return dict(map(convert, data.iteritems()))
- elif isinstance(data, collections.Iterable):
- return type(data)(map(convert, data))
- else:
- return data
data=convert(data_file)
- MachineDico=data['MachineDico']
- LoadDico = data['LoadDico']
- LineDico = data['LineDico']
- TfoDico = data['TransfoDico']
- MotorDico = data['MotorDico']
- os.remove('Data_for_interface')
+ MachineDico = data['MachineDico']
+ LoadDico = data['LoadDico']
+ LineDico = data['LineDico']
+ TfoDico = data['TransfoDico']
+ MotorDico = data['MotorDico']
+ os.remove('Data_for_interface')
return MachineDico, LoadDico, LineDico, TfoDico, MotorDico
+if __name__ == "__main__":
+ pass
-#NetworkFile=r"C:\Users\j15773\Documents\Projects\DEWA\Task 4\FEWA_GCCIA_SUMMAX2018_OPF_EQ2.sav"
-
-#PSSE_PATH=r"C:\Program Files (x86)\PTI\PSSE34\PSSBIN"
-#PSSPY_PATH=r"C:\Program Files (x86)\PTI\PSSE34\PSSPY27"
-
-#PSSE_PATH=r"C:\Program Files (x86)\PTI\PSSE33\PSSBIN"
-#PSSPY_PATH=r"C:\Program Files (x86)\PTI\PSSE33\PSSBIN"
-
-#MachineDico, LoadDico, LineDico, TfoDico, MotorDico = ExtractGeneratorLoadLineandTransfoDico(NetworkFile,PSSE_PATH,PSSPY_PATH)
+ # tester convert
+ # faire 2 methodes testPFExtractGeneratorLoadLineandTransfoDico et testExtractGeneratorLoadLineandTransfoDico
+ # appelle les methodes PFExtractGeneratorLoadLineandTransfoDico et ExtractGeneratorLoadLineandTransfoDico avec un mini reseau connu
+ # on verifie que MachineDico, LoadDico, LineDico, TfoDico, MotorDico contiennent bien ce qu on attendait
+ # les 2 fichiers contenant les reseaux seront dans la base git