Mentions légales du service

Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
1 result

Target

Select target project
  • tranus/Tranus_Python
  • tcapelle/Tranus_Python
2 results
Select Git revision
  • master
1 result
Show changes
Commits on Source (4)
This program is used to read and write a binary file generated by TRANUS (.L1S file). This file contains the output of the program LCAL(one of the TRANUS binary programs). Most of the output parameters can be extracted from TRANUS text files such as : .L0E file,.Z1E file,.L1E file.
Few output parameters are calculated by LCAL :
- Pro : Production of sector m in zone i
- Cospro : Cost of production of an unit of m in the zone i
- Precio : Price of sector n in the production zone j
- Dem : Induced demand
- Coscon : Consumption cost of sector n in zone i
- Utcon : Disutility of consuming sector n in zone i
- Atrac : Attractor of sector n in zone i
- Ajust : Shadow price of sector n in the production zone j
......@@ -29,20 +29,23 @@ def modifyStr(string,lenType):
return strModified
def loadFiles(TranusProject,Scenario):
''' This method loads TRANUS files needed to extract L1S parameters and creates new L1S file'''
CTL_filepath = os.path.join(TranusProject,'W_TRANUS.CTL')
L0E_filepath = glob.glob(os.path.join(TranusProject,'W_*.L0E'))[0]
Z1E_filepath = glob.glob(os.path.join(TranusProject,'W_*.Z1E'))[0]
ScenarioPath = TranusProject+'\\'+Scenario
ScenarioPath = os.path.join(TranusProject,Scenario)
L1E_filepath = glob.glob(os.path.join(ScenarioPath,'W_*.L1E'))[0]
L1S_filepath = os.path.join(ScenarioPath,"NEW_LCAL"+"_"+Scenario+".L1S")
return CTL_filepath,L0E_filepath,Z1E_filepath,L1E_filepath,L1S_filepath
def numberingZones(Z1E_filepath):
'''This method returns number of internal and external zones '''
with open(Z1E_filepath,"r") as f :
......@@ -63,6 +66,8 @@ def numberingZones(Z1E_filepath):
return numZones,numZonesExt
def numberingSectors(L1E_filepath):
'''This method returns number of sectors '''
with open(L1E_filepath,"r") as f :
......@@ -78,11 +83,12 @@ def numberingSectors(L1E_filepath):
return numSectors
class L1Sparam:
def __init__(self,TranusProject,Scenario):
#tranus files used to extract parameters and L1S file
#tranus files used to extract parameters and new L1S file created
self.CTL_file,self.L0E_file,self.Z1E_file,self.L1E_file,self.L1S_file, = loadFiles(TranusProject,Scenario)
#numZones,nbZones,numZonesExt,nbZonesExt,numSectors,nbSectors
......@@ -187,7 +193,7 @@ class L1Sparam:
self.dem = np.zeros((self.nbSectors,self.nbTotZones))
def extractionL0Eparam(self):
''' This method returns L0E parameters'''
with open(self.L0E_file,"r") as f:
lines = f.readlines()
length_lines = len(lines)
......@@ -259,6 +265,7 @@ class L1Sparam:
return self.xpro,self.probase,self.xdem,self.prebase,self.valag,self.atrain,self.rmin,self.rmax
def extractionZ1Eparam(self):
''' This method returns Z1E parameters'''
with open (self.Z1E_file,"r") as f :
......@@ -310,6 +317,7 @@ class L1Sparam:
return self.nzn,self.nz1,self.nz2,self.numZon,self.nomZon,self.jer1,self.jer2
def extractionL1Eparam(self):
''' This method returns L1E parameters'''
with open(self.L1E_file,"r") as f:
lines = f.readlines()
......@@ -454,7 +462,7 @@ class L1Sparam:
return self.ns, self.nflu, self.lflu,self.numSec,self.nomSec,self.beta_1,self.beta_2,self.gama_1,self.gama_2,self.minPriceCostToRatio,self.sectorType,self.targetSector,self.demin,self.demax,self.delas,self.selas,self.suslgsc,self.xalfa_1,self.xalfa_2,self.xalfapro,self.xalfapre,self.xalfacap,self.alfa_1,self.alfa_2
def extractionCTLparam(self):
''' This method returns CTL parameters'''
with open(self.CTL_file,"r") as f:
lines = f.readlines()
length_lines = len(lines)
......@@ -521,6 +529,7 @@ class L1Sparam:
return self.area,self.estudio,self.pol,self.npol,self.iPrevPol,self.prevPolType,self.nomPol,self.descPol,self.iPol
def getDateTime(self):
'''This method returns date and time of execution '''
date_time = datetime.now().strftime('%Y %m %d %H %M')
self.ian = int(date_time.split(' ')[0])
......@@ -532,8 +541,7 @@ class L1Sparam:
return self.ian,self.mes,self.idia,self.ihr,self.mins
def extractothers(self):
#modify this part to use the parameters calculated by Thomas
'''This method extracts other parameters that will be calculated by Thomas : to be modified !!!'''
others=L1S_READ("C:\Users\emna\Desktop\Modele_Grenoble\\00A\NEW00A.L1S",self.nbSectors,self.nbTotZones).read_L1S()
self.pro = others[0]
......@@ -546,7 +554,7 @@ class L1Sparam:
self.dem = others[7]
def runParametersExtraction(self):
''' This method runs all the methods needed to get L1S parameters'''
self.extractionL0Eparam()
self.extractionZ1Eparam()
self.extractionL1Eparam()
......@@ -555,10 +563,8 @@ class L1Sparam:
self.extractothers()
def WRLPAR(self,file):
self.extractothers()
self.getDateTime()
''' This method is used to write L1S file (part 1) '''
listHeader = [self.fileMajor,self.fileMinor,self.fileRelease,self.ifmtL1S]
header = struct.pack("<3hi",*listHeader)
file.write(header)
......@@ -618,7 +624,7 @@ class L1Sparam:
file.write(ns)
def WritePolInfo(self,file):
''' This method is used to write L1S file (part 2) '''
listNpol = [self.npol,self.npol_neg]
npol = struct.pack("<2i",*listNpol)
file.write(npol)
......@@ -635,6 +641,8 @@ class L1Sparam:
def GRAL1S(self):
''' This method is used to write L1S file (part 3) '''
with open(self.L1S_file,"wb") as file:
self.WRLPAR(file)
......@@ -665,15 +673,13 @@ class L1Sparam:
for n in range(self.ns):
#print "self.rmax[%s,%s]"%(n,i),self.rmax[n,i]
listL1S = [n+1,self.xpro[n,i],self.probase[n,i],self.pro[n,i],self.cospro[n,i],self.prebase[n,i],self.precio[n,i],self.xdem[n,i],self.dem[n,i],self.coscon[n,i],self.utcon[n,i],self.rmin[n,i],self.rmax[n,i],self.atrac[n,i],self.valag[n,i],self.ajuste[n,i],self.atrain[n,i],self.stock[n,i],self.unstock[n,i]]
paramL1S = struct.pack("<idf2dfd3fd4fd3f",*listL1S)
file.write(paramL1S)
def RDLPAR(self,data,offset):
''' This method is used to read L1S file (part 1) '''
header=struct.unpack_from("<3h2i5h3s80s3s80s",data,offset)
offset=offset+3*2+4+4+5*2+3+80+3+80
......@@ -766,6 +772,7 @@ class L1Sparam:
return offset
def skipPolInfo(self,data,offset):
''' This method is used to read L1S file (part 2) '''
numPolicy = struct.unpack_from("<2i",data,offset)
offset = offset+2*4
......@@ -795,6 +802,8 @@ class L1Sparam:
def LEEL1S(self):
''' This method is used to read L1S file (part 3) '''
offset = 0
with open (self.L1S_file,"rb") as file:
......@@ -867,13 +876,13 @@ class L1Sparam:
def run(self):
# parameters extraction
'''parameters extraction'''
self.runParametersExtraction()
# Writing L1S file
'''Writing L1S file'''
self.GRAL1S()
#Reading L1S file
'''Reading L1S file'''
self.LEEL1S()
if __name__=='__main__':
......
File added
......@@ -2,20 +2,17 @@
import struct
import numpy as np
#from L1Sparam import L1Sparam
class L1S_READ(object):
def __init__(self,file_name,nbSectors,nbZones):
#nbSectors,nbZones : calculated in L1S_args file
# path to L1S file
self.file_name=file_name
self.iter=0
self.ian=0
self.mes=0
self.idia=0
self.ihr=0
self.mins=0
#Thomas parameters
self.pro = np.zeros((nbSectors,nbZones))
self.cospro = np.zeros((nbSectors,nbZones))
self.precio = np.zeros((nbSectors,nbZones))
......@@ -27,12 +24,11 @@ class L1S_READ(object):
def read_L1S(self):
'''This method is used to read L1S file'''
offset=0
list_L1S=[]
#test=L1Sparam("C:\Users\emna\Desktop\Modele_Grenoble\W_NEW.Z1E","C:\Users\emna\Desktop\Modele_Grenoble\W_NEW.L0E","C:\Users\emna\Desktop\Modele_Grenoble\\00A\W_NEW00A.L1E","C:\Users\emna\Desktop\Modele_Grenoble\W_TRANUS.CTL")
#nsust_compare=test.extractionL1Eparam()
with open(self.file_name, "rb") as f:
......@@ -41,61 +37,39 @@ class L1S_READ(object):
header=struct.unpack_from("<3h2i5h3s80s3s80s",data,offset)
offset=offset+3*2+4+4+5*2+3+80+3+80
file_major=header[0]
#print "file_major",file_major
list_L1S.append(file_major)
file_minor=header[1]
#print "file_minor",file_minor
list_L1S.append(file_minor)
file_release=header[2]
#print "file_release",file_release
list_L1S.append(file_release)
ifmt_L1S=header[3]
#print "ifmt_L1S",ifmt_L1S
list_L1S.append(ifmt_L1S)
iter_=header[4]
#print "iter",iter_
list_L1S.append(iter_)
ian=header[5]
self.ian=ian
#print "ian",ian
list_L1S.append(ian)
mes=header[6]
#print "mes",mes
self.mes=mes
list_L1S.append(mes)
idia=header[7]
self.idia=idia
#print "idia",idia
list_L1S.append(idia)
ihr=header[8]
self.ihr=ihr
#print "ihr",ihr
list_L1S.append(ihr)
mins=header[9]
self.mins=mins
#print "mins",mins
list_L1S.append(mins)
area=header[10]
#print "area",area
list_L1S.append(area)
estudio=header[11]
#print "estudio",estudio
list_L1S.append(estudio)
pol=header[12]
#print "pol",pol
list_L1S.append(pol)
nombre=header[13]
#print "nombre",nombre
list_L1S.append(nombre)
num_policy=struct.unpack_from("<2i",data,offset)
offset=offset+2*4
npol=num_policy[0]
#print "npol",npol
list_L1S.append(npol)
npol_neg=num_policy[1]
#print "npol_neg",npol_neg
list_L1S.append(npol_neg)
for i in range(npol):
......@@ -103,40 +77,30 @@ class L1S_READ(object):
info_policy=struct.unpack_from("<2ib5s32s",data,offset)
offset=offset+2*4+1+5+32
index_i=info_policy[0]
#print "index_i",index_i
list_L1S.append(index_i)
i_prev_pol=info_policy[1]
#print "i_prev_pol",i_prev_pol
list_L1S.append(i_prev_pol)
prev_pol_type=info_policy[2]
#print "prev_pol_type",prev_pol_type
list_L1S.append(prev_pol_type)
nom_pol=info_policy[3]
#print "nom_pol",nom_pol
list_L1S.append(nom_pol)
desc_pol=info_policy[4]
#print "desc_pol",desc_pol
list_L1S.append(desc_pol)
npol_ipol_tuple=struct.unpack_from("<2i",data,offset)
offset=offset+2*4
npol=npol_ipol_tuple[0]
#print "npol",npol
list_L1S.append(npol)
ipol=npol_ipol_tuple[1]
#print "ipol",ipol
list_L1S.append(ipol)
sector_1=struct.unpack_from("<3i",data,offset)
offset=offset+3*4
ns=sector_1[0]
#print "ns",ns
list_L1S.append(ns)
ns_neg=sector_1[1]
#print "ns_neg",ns_neg
list_L1S.append(ns_neg)
nflu=sector_1[2]
#print "nflu",nflu
list_L1S.append(nflu)
for i in range(ns):
......@@ -144,52 +108,38 @@ class L1S_READ(object):
sector_2=struct.unpack_from("<2i32s?5f2i",data,offset)
offset=offset+2*4+32+1+5*4+2*4
index_i=sector_2[0]
#print "index_i",index_i
list_L1S.append(index_i)
numsec=sector_2[1]
#print "numsec",numsec
list_L1S.append(numsec)
nomsec=sector_2[2]
#print "nomsec",nomsec
list_L1S.append(nomsec)
lflu=sector_2[3]
#print "lflu",lflu
list_L1S.append(lflu)
beta_1=sector_2[4]
#print "beta_1",beta_1
list_L1S.append(beta_1)
beta_2=sector_2[5]
#print "beta_2",beta_2
list_L1S.append(beta_2)
gama_1=sector_2[6]
#print "gama_1",gama_1
list_L1S.append(gama_1)
gama_2=sector_2[7]
#print "gama_2",gama_2
list_L1S.append(gama_2)
min_price_to_cost_ratio=sector_2[8]
#print "min_price_to_cost_ratio",min_price_to_cost_ratio
list_L1S.append(min_price_to_cost_ratio)
sector_type=sector_2[9]
#print "sector_type",sector_type
list_L1S.append(sector_type)
target_sector=sector_2[10]
#print "target_sector",target_sector
list_L1S.append(target_sector)
ns_tuple=struct.unpack_from("<i",data,offset)
offset=offset+4
ns=ns_tuple[0]
#print "ns",ns
list_L1S.append(ns)
num_sect=struct.unpack_from("<2i",data,offset)
offset=offset+2*4 #[NS,-NS]*
offset=offset+2*4
ns=num_sect[0]
#print "ns",ns
list_L1S.append(ns)
ns_neg=num_sect[1]
#print "ns_neg",ns_neg
list_L1S.append(ns_neg)
for i in range(ns):
......@@ -197,7 +147,6 @@ class L1S_READ(object):
index_i_tuple=struct.unpack_from("<i",data,offset)
offset=offset+4
index_i=index_i_tuple[0]
#print "index_i",index_i
list_L1S.append(index_i)
for j in range(ns):
......@@ -205,59 +154,32 @@ class L1S_READ(object):
demand_functions=struct.unpack_from("<i12fi",data,offset)
offset=offset+4+12*4+4
index_j=demand_functions[0]
#print "index_j",index_j
list_L1S.append(index_j)
demin=demand_functions[1]
#print "demin",demin
#print "demin[%s,%s]" %(j,i),demin
list_L1S.append(demin)
demax=demand_functions[2]
#print "demax",demax
#print "demax[%s,%s]" %(j,i),demax
list_L1S.append(demax)
delas=demand_functions[3]
#print "delas",delas
#print "delas[%s,%s]" %(j,i),delas
list_L1S.append(delas)
selas=demand_functions[4]
#print "selas",selas
#print "selas[%s,%s]" %(j,i),selas
list_L1S.append(selas)
suslgsc=demand_functions[5]
#print "suslgsc",suslgsc
#print "suslgsc[%s,%s]" %(j,i),suslgsc
list_L1S.append(suslgsc)
xalfa_1=demand_functions[6]
#print "xalfa_1",xalfa_1
#print "xalfa_1[%s,%s]" %(i,j),xalfa_1
list_L1S.append(xalfa_1)
xalfa_2=demand_functions[7]
#print "xalfa_2",xalfa_2
#print "xalfa_2[%s,%s]" %(i,j),xalfa_2
list_L1S.append(xalfa_2)
xalfapro=demand_functions[8]
#print "xalfapro",xalfapro
#print "xalfapro[%s,%s]" %(i,j),xalfapro
list_L1S.append(xalfapro)
xalfapre=demand_functions[9]
#print "xalfapre",xalfapre
#print "xalfapre[%s,%s]" %(j,i),xalfapre
list_L1S.append(xalfapre)
xalfacap=demand_functions[10]
#print "xalfacap",xalfacap
#print "xalfacap[%s,%s]" %(i,j),xalfacap
list_L1S.append(xalfacap)
alfa_1=demand_functions[11]
#print "alfa_1[%s,%s]" %(i,j),alfa_1
#print "alfa_1",alfa_1
list_L1S.append(alfa_1)
alfa_2=demand_functions[12]
#print "alfa_2",alfa_2
#print "alfa_2[%s,%s]" %(i,j),alfa_2
list_L1S.append(alfa_2)
mxsust=demand_functions[13]
#print "mxsust",mxsust
list_L1S.append(mxsust)
for k in range(mxsust):
......@@ -265,32 +187,22 @@ class L1S_READ(object):
nsust_tuple=struct.unpack_from("<i",data,offset)
offset=offset+4
nsust=nsust_tuple[0]
#print "nsust",nsust
"""if nsust !=0 :
print "nsust[%s,%s,%s]"%(i,j,k),nsust
if nsust_compare[i,j,k] == nsust:
print "Ok"""
list_L1S.append(nsust)
ns_tuple=struct.unpack_from("<i",data,offset)
offset=offset+4
ns=ns_tuple[0]
#print "ns",ns
list_L1S.append(ns)
param_nzn=struct.unpack_from("<4i",data,offset)
offset=offset+4*4
nzn=param_nzn[0]
#print "nzn",nzn
list_L1S.append(nzn)
nzn_neg=param_nzn[1]
#print "nzn_neg",nzn_neg
list_L1S.append(nzn_neg)
nz_1=param_nzn[2]
#print "nz_1",nz_1
list_L1S.append(nz_1)
nz_2=param_nzn[3]
#print "nz_2",nz_2
nz_2=param_nzn[3]
list_L1S.append(nz_2)
for i in range(nzn):
......@@ -298,34 +210,26 @@ class L1S_READ(object):
param_numzon=struct.unpack_from("<2i32s2i",data,offset)
offset=offset+2*4+32+2*4
index_i=param_numzon[0]
#print "index_i",index_i
list_L1S.append(index_i)
numzon=param_numzon[1]
#print "numzon",numzon
list_L1S.append(numzon)
nomzon=param_numzon[2]
#print "nomzon",nomzon
list_L1S.append(nomzon)
jer_1=param_numzon[3]
#print "jer_1",jer_1
jer_1=param_numzon[3]
list_L1S.append(jer_1)
jer_2=param_numzon[4]
#print "jer_2",jer_2
list_L1S.append(jer_2)
nzn_tuple=struct.unpack_from("<i",data,offset)
offset=offset+4
nzn=nzn_tuple[0]
#print "nzn",nzn
list_L1S.append(nzn)
nzn_ns=struct.unpack_from("<2i",data,offset)
offset=offset+2*4
nzn=nzn_ns[0]
#print "nzn",nzn
list_L1S.append(nzn)
ns=nzn_ns[1]
#print "ns",ns
list_L1S.append(ns)
for i in range (nzn):
......@@ -333,10 +237,8 @@ class L1S_READ(object):
numzon_tuple=struct.unpack_from("<2i", data, offset)
offset=offset+2*4
index_i=numzon_tuple[0]
#print "index_i",index_i
list_L1S.append(index_i)
numzon=numzon_tuple[1]
#print "numzon",numzon
list_L1S.append(numzon)
for n in range(ns):
......@@ -346,99 +248,56 @@ class L1S_READ(object):
param_l1s=struct.unpack_from(fmt,data,offset)
offset=offset+len_fmt
index_n=param_l1s[0]
#print "index_n",index_n
list_L1S.append(index_n)
xpro=param_l1s[1]
#print "xpro",xpro
list_L1S.append(xpro)
probase=param_l1s[2]
#print "probase",probase
list_L1S.append(probase)
pro=param_l1s[3]
self.pro[n,i]=pro
#print "pro",pro
list_L1S.append(pro)
cospro=param_l1s[4]
self.cospro[n,i]=cospro
#print "cospro",cospro
list_L1S.append(cospro)
prebase=param_l1s[5]
#print "prebase",prebase
list_L1S.append(prebase)
precio=param_l1s[6]
self.precio[n,i]=precio
#print "precio",precio
list_L1S.append(precio)
xdem=param_l1s[7]
#print "xdem",xdem
list_L1S.append(xdem)
dem_xdem=param_l1s[8]
#print "dem_xdem",dem_xdem
self.dem[n,i]=dem_xdem
list_L1S.append(dem_xdem)
coscon=param_l1s[9]
#print "coscon",coscon
self.coscon[n,i]=coscon
list_L1S.append(coscon)
utcon=param_l1s[10]
#print "utcon",utcon
self.utcon[n,i]=utcon
list_L1S.append(utcon)
rmin=param_l1s[11]
#print "rmin",rmin
list_L1S.append(rmin)
rmax=param_l1s[12]
#print "rmax[%s,%s]" %(n,i),rmax
#print "rmax",rmax
list_L1S.append(rmax)
atrac=param_l1s[13]
#print "atrac",atrac
self.atrac[n,i]=atrac
list_L1S.append(atrac)
valag=param_l1s[14]
#print "valag",valag
list_L1S.append(valag)
ajuste=param_l1s[15]
self.ajuste[n,i]=ajuste
#print "ajuste",ajuste
list_L1S.append(ajuste)
atrain=param_l1s[16]
#print "atrain",atrain
list_L1S.append(atrain)
stock=param_l1s[17]
#print "stock",stock
list_L1S.append(stock)
unstock=param_l1s[18]
#print "unstock",unstock
list_L1S.append(unstock)
#return list_L1S
'''return list of Thomas parameters'''
return [self.pro,self.cospro,self.precio,self.coscon,self.utcon,self.atrac,self.ajuste,self.dem]
"""if __name__ == '__main__':
exemple_1=L1S_READ("C:\Users\emna\Desktop\Modele_Grenoble\\00A\NEW00A.L1S")
list_L1S=exemple_1.read_L1S()
#print list_L1S"""