From 8f309b3cb1e3a62a53364bb08ae7fd947dd21631 Mon Sep 17 00:00:00 2001 From: abn Date: Mon, 27 Aug 2018 15:42:49 +0200 Subject: [PATCH] Further porting to Python3 --- src/MEDLoader/Swig/CaseIO.py | 4 ++-- src/MEDLoader/Swig/CaseReader.py | 37 ++++++++++++++++---------------- src/MEDLoader/Swig/CaseWriter.py | 2 +- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/MEDLoader/Swig/CaseIO.py b/src/MEDLoader/Swig/CaseIO.py index 986a408a0..f49d943e7 100644 --- a/src/MEDLoader/Swig/CaseIO.py +++ b/src/MEDLoader/Swig/CaseIO.py @@ -22,10 +22,10 @@ from MEDLoader import * class CaseIO: - dictMCTyp={NORM_HEXA8:"hexa8",NORM_POLYHED:"nfaced",NORM_QUAD4:"quad4",NORM_POLYGON:"nsided",NORM_POINT1:"point",NORM_SEG2:"bar2",NORM_SEG3:"bar3",NORM_TRI3:"tria3",NORM_TRI6:"tria6",NORM_QUAD8:"quad8",NORM_TETRA4:"tetra4",NORM_TETRA10:"tetra10",NORM_PYRA5:"pyramid5",NORM_PYRA13:"pyramid13",NORM_PENTA6:"penta6",NORM_PENTA15:"penta15",NORM_HEXA20:"hexa20"} + dictMCTyp_str={NORM_HEXA8:"hexa8",NORM_POLYHED:"nfaced",NORM_QUAD4:"quad4",NORM_POLYGON:"nsided",NORM_POINT1:"point",NORM_SEG2:"bar2",NORM_SEG3:"bar3",NORM_TRI3:"tria3",NORM_TRI6:"tria6",NORM_QUAD8:"quad8",NORM_TETRA4:"tetra4",NORM_TETRA10:"tetra10",NORM_PYRA5:"pyramid5",NORM_PYRA13:"pyramid13",NORM_PENTA6:"penta6",NORM_PENTA15:"penta15",NORM_HEXA20:"hexa20"} + dictMCTyp = {k:bytes(v, "ascii") for k, v in list(dictMCTyp_str.items())} discSpatial={ON_CELLS:"element",ON_NODES:"node"} dictCompo={1:"scalar",3:"vector",6:"tensor",9:"tensor9"} dictMCTyp2 = {v:k for k, v in list(dictMCTyp.items())} discSpatial2 = {v:k for k, v in list(discSpatial.items())} dictCompo2 = {v:k for k, v in list(dictCompo.items())} - pass diff --git a/src/MEDLoader/Swig/CaseReader.py b/src/MEDLoader/Swig/CaseReader.py index 4801c93cb..131d63108 100644 --- a/src/MEDLoader/Swig/CaseReader.py +++ b/src/MEDLoader/Swig/CaseReader.py @@ -38,6 +38,7 @@ class CaseReader(CaseIO): def __init__(self,fileName): """ Constructor """ + CaseIO.__init__(self) self._fileName=fileName self._dirName=os.path.dirname(self._fileName) pass @@ -48,7 +49,7 @@ class CaseReader(CaseIO): coo=np.array(coords,dtype="float64") ; coo=coo.reshape(nbCoords,3) coo=DataArrayDouble(coo) ; coo=coo.fromNoInterlace() ct=self.dictMCTyp2[typ] - m=MEDCouplingUMesh(name,MEDCouplingUMesh.GetDimensionOfGeometricType(ct)) + m=MEDCouplingUMesh(str(name),MEDCouplingUMesh.GetDimensionOfGeometricType(ct)) m.setCoords(coo) nbNodesPerCell=MEDCouplingMesh.GetNumberOfNodesOfGeometricType(ct) cI=DataArrayInt(len(cells)+1) ; cI.iota() ; cI*=nbNodesPerCell+1 @@ -112,11 +113,11 @@ class CaseReader(CaseIO): """ Convert all the geometry (all the meshes) contained in the CASE file into MEDCouplingUMesh'es. """ fd=open(os.path.join(self._dirName,geoFileName),"r+b") ; fd.seek(0,2) ; end=fd.tell() ; fd.seek(0) ; title=fd.read(80) title=title.strip().lower() - if "binary" not in title: + if b"binary" not in title: raise Exception("Error only binary geo files are supported for the moment !") pass zeType=True - if "fortran" in title: + if b"fortran" in title: mcmeshes=self.__convertGeo2MEDFortran(fd,end) ; zeType=False else: mcmeshes=self.__convertGeo2MEDC(fd,end) @@ -139,15 +140,15 @@ class CaseReader(CaseIO): pos=fd.tell() elt=fd.read(80) ; elt=elt.strip() ; pos=fd.tell() mcmeshes2=[] - typ="part" + typ=b"part" nbOfTurn=0 - while abs(pos-end)>8 and "part" in typ: - if "part" not in elt: + while abs(pos-end)>8 and b"part" in typ: + if b"part" not in elt: raise Exception("Error on reading mesh fortran #1 !") fd.seek(fd.tell()+4)# skip # tmp=fd.read(80) ; meshName=tmp.split("P")[-1] tmp=fd.read(80) - if "coordinates" not in tmp: + if b"coordinates" not in tmp: raise Exception("Error on reading mesh fortran #2 !") pos=fd.tell() # 644 if nbOfTurn==0: @@ -165,7 +166,7 @@ class CaseReader(CaseIO): coo=coo.reshape(nbNodes,3) pos+=nbNodes*3*4 ; fd.seek(pos)#np.array(0,dtype='float%i'%(typeOfCoo)).nbytes typ=fd.read(80).strip() ; pos=fd.tell() - zeK="" + zeK=b"" for k in self.dictMCTyp2: if k in typ: zeK=k @@ -201,11 +202,11 @@ class CaseReader(CaseIO): mcmeshes=[] elt=fd.read(80) ; elt=elt.strip() ; pos+=80 while pos!=end: - if "part" not in elt: + if b"part" not in elt: raise Exception("Error on reading mesh #1 !") fd.seek(fd.tell()+4) meshName=fd.read(80).strip() - if fd.read(len("coordinates"))!="coordinates": + if fd.read(len("coordinates"))!=b"coordinates": raise Exception("Error on reading mesh #2 !") pos=fd.tell() typeOfCoo=np.memmap(fd,dtype='byte',mode='r',offset=int(pos),shape=(1)).tolist()[0] @@ -216,7 +217,7 @@ class CaseReader(CaseIO): pos+=nbNodes*3*4 ; fd.seek(pos)#np.array(0,dtype='float%i'%(typeOfCoo)).nbytes typ=fd.read(80).strip() ; pos=fd.tell() mcmeshes2=[] - while pos!=end and typ!="part": + while pos!=end and typ!=b"part": if typ[0]=='\0': pos+=1; continue mctyp=self.dictMCTyp2[typ] nbCellsOfType=np.memmap(fd,dtype='int32',mode='r',offset=int(pos),shape=(1,)).tolist()[0] @@ -264,13 +265,13 @@ class CaseReader(CaseIO): st="%0"+str(len(stars))+"i" trueFileName=fileName.replace(stars,st%(it)) fd=open(os.path.join(self._dirName,trueFileName),"r+b") ; fd.seek(0,2) ; end=fd.tell() ; fd.seek(0) - name=fd.read(80).strip().split(" ")[0] + name=fd.read(80).strip().split(b" ")[0] if name!=fieldName: raise Exception("ConvertField : mismatch") pos=fd.tell() st=fd.read(80) ; st=st.strip() ; pos=fd.tell() while pos!=end: - if st!="part": + if st!=b"part": raise Exception("ConvertField : mismatch #2") fdisc=MEDCouplingFieldDiscretization.New(self.discSpatial2[discr]) meshId=np.memmap(fd,dtype='int32',mode='r',offset=int(pos),shape=(1)).tolist()[0]-1 @@ -281,8 +282,8 @@ class CaseReader(CaseIO): fd.seek(pos+4) st=fd.read(80).strip() ; pos=fd.tell() offset=0 - while pos!=end and st!="part": - if st!="coordinates": + while pos!=end and st!=b"part": + if st!=b"coordinates": nbOfValsOfTyp=mcmeshes[meshId].getNumberOfCellsWithType(self.dictMCTyp2[st]) else: nbOfValsOfTyp=nbOfValues @@ -315,13 +316,13 @@ class CaseReader(CaseIO): raise Exception("ConvertField : mismatch") pos=fd.tell() st=fd.read(80) ; st=st.strip() ; pos=fd.tell() - if "part" not in st: + if b"part" not in st: raise Exception("ConvertField : mismatch #2") st=fd.read(80).strip() ; pos=fd.tell() pos+=12 # I love it offset=0 nbTurn=0 - while pos!=end and "part" not in st: + while pos!=end and b"part" not in st: fdisc=MEDCouplingFieldDiscretization.New(self.discSpatial2[discr]) nbOfValues=fdisc.getNumberOfTuples(mcmeshes[nbTurn]) vals2=DataArrayDouble(nbOfValues,nbCompo) @@ -356,7 +357,7 @@ class CaseReader(CaseIO): def loadInMEDFileDS(self): """ Load a CASE file into a MEDFileData object. """ - f=file(self._fileName) + f=open(self._fileName) lines=f.readlines() ind=lines.index("GEOMETRY\n") if ind==-1: diff --git a/src/MEDLoader/Swig/CaseWriter.py b/src/MEDLoader/Swig/CaseWriter.py index 438e5cd13..b29f44be2 100644 --- a/src/MEDLoader/Swig/CaseWriter.py +++ b/src/MEDLoader/Swig/CaseWriter.py @@ -319,7 +319,7 @@ time values: def __str80(cls,st): if len(st)>79: raise Exception("String \"%s\" is too long (>79) !"%(st)) - return st.ljust(79)+"\n" + return bytes(str(st).ljust(79)+"\n", "ascii") def __computeSizeOfGeoFile(self,listOfMeshes,nn): sz=0 -- 2.39.2