# -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2015 CEA/DEN, EDF R&D
+# Copyright (C) 2007-2022 CEA/DEN, EDF R&D
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# http://www-vis.lbl.gov/NERSC/Software/ensight/doc/OnlineHelp/UM-C11.pdf
import numpy as np
-from MEDLoader import *
+from medcoupling import *
from CaseIO import CaseIO
-import sys,re
+import sys,re,os
class CaseReader(CaseIO):
""" Converting a file in the Case format (Ensight) to the MED format.
def __init__(self,fileName):
""" Constructor """
+ CaseIO.__init__(self)
self._fileName=fileName
self._dirName=os.path.dirname(self._fileName)
pass
def __traduceMesh(self,name,typ,coords,cells):
""" Convert a CASE mesh into a MEDCouplingUMesh. """
+ name = name.decode("ascii")
nbCoords=len(coords)
coo=np.array(coords,dtype="float64") ; coo=coo.reshape(nbCoords,3)
coo=DataArrayDouble(coo) ; coo=coo.fromNoInterlace()
cI=DataArrayInt(len(cells)+1) ; cI.iota() ; cI*=nbNodesPerCell+1
#
cells2=cells.reshape(len(cells),nbNodesPerCell)
- if cells2.dtype=='int32':
- c2=DataArrayInt(cells2)
- else:
- c2=DataArrayInt(np.array(cells2,dtype="int32"))
- pass
+ c2=DataArrayInt(np.array(cells2,dtype="int{}".format(MEDCouplingSizeOfIDs())) )
c=DataArrayInt(len(cells),nbNodesPerCell+1) ; c[:,0]=ct ; c[:,1:]=c2-1 ; c.rearrange(1)
m.setConnectivity(c,cI,True)
m.checkConsistency()
arr0mc2=DataArrayInt(len(arr0),2) ; arr0mc2[:,0]=DataArrayInt(arr0)-1 ; arr0mc2[:,1]=1 ; arr0mc2.rearrange(1) ; arr0mc2.computeOffsetsFull()
arr0mc3=DataArrayInt.Range(0,2*len(arr0),2).buildExplicitArrByRanges(arr0mc2)
arr1mc0=DataArrayInt(arr1) ; arr1mc0.computeOffsetsFull()
- arr1mc1=arr1mc0[arr0mc0] ; arr1mc1[1:]+=arr0mc0[1:]
+ arr1mc1=arr1mc0[arr0mc0] ; arr1mc1[1:]+=arr0mc0[1:]
arr1mc2=DataArrayInt(arr1).deepCopy() ; arr1mc2+=1 ; arr1mc2.computeOffsetsFull()
arr2mc0=(arr1mc2[1:])[arr0mc3]
#
return m
def __convertGeo2MED(self,geoFileName):
- """ Convert all the geometry (all the meshes) contained in teh CASE file into MEDCouplingUMesh'es. """
+ """ 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)
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:
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=""
- for k in self.dictMCTyp2.keys():
+ zeK=b""
+ for k in self.dictMCTyp2:
if k in typ:
zeK=k
break
mcmeshes2.append(self.__traduceMesh(meshName,zeK,coo,nodalConn))
pos+=nbNodesPerCell*nbCellsOfType*4
if abs(pos-end)>8:
- fd.seek(pos) ;elt=fd.read(80) ; typ=elt[:] ; pos+=80
+ fd.seek(pos) ;elt=fd.read(80) ; typ=elt[:] ; pos+=80
pass
nbOfTurn+=1
pass
return mcmeshes2
def __convertGeo2MEDC(self,fd,end):
- fd.readline()
- name=fd.readline().strip() ; fd.readline() ; fd.readline()
+ #fd.readline()
+ #name=fd.readline().strip() ; fd.readline() ; fd.readline()
+ name=fd.read(80)
+ descrip=fd.read(80).strip() ; fd.read(80) ; fd.read(80)
pos=fd.tell()
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]
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]
pos+=4
elt=fd.read(80) ; elt=elt.strip() ; typ=elt[:] ; pos+=80
pass
pass
- coo=mcmeshes2[0].getCoords() ; name=mcmeshes2[0].getName()
- for itmesh in mcmeshes2: itmesh.setCoords(coo)
- m=MEDCouplingUMesh.MergeUMeshesOnSameCoords(mcmeshes2) ; m.setName(name)
- mcmeshes.append(m)
+ if mcmeshes2:
+ coo=mcmeshes2[0].getCoords() ; name=mcmeshes2[0].getName()
+ for itmesh in mcmeshes2: itmesh.setCoords(coo)
+ m=MEDCouplingUMesh.MergeUMeshesOnSameCoords(mcmeshes2) ; m.setName(name)
+ mcmeshes.append(m)
pass
return mcmeshes
-
-
+
+
def __convertField(self,mlfields, mcmeshes, fileName, fieldName, discr, nbCompo, locId, it):
""" Convert the fields. """
stars=re.search("[\*]+",fileName).group()
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.readline().strip().split(" ")[0]
+ name=fd.read(80).strip().split(b" ")[0].decode("ascii")
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
+ if meshId >= len( mcmeshes ):
+ return
nbOfValues=fdisc.getNumberOfTuples(mcmeshes[meshId])
vals2=DataArrayDouble(nbOfValues,nbCompo)
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
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)
nbOfValsOfTyp=np.memmap(fd,dtype='>i4',mode='r',offset=pos,shape=(1)).tolist()[0]/4
pos+=4
vals=np.zeros(dtype=">f4",shape=(nbOfValsOfTyp*nbCompo))
- for iii in xrange(nbCompo):
+ for iii in range(nbCompo):
valsTmp=np.memmap(fd,dtype='>f4',mode='r',offset=int(pos),shape=(nbOfValsOfTyp))
vals[iii*nbOfValsOfTyp:(iii+1)*nbOfValsOfTyp]=valsTmp
pos+=nbOfValsOfTyp*4
nbTurn+=1
pass
pass
-
+
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:
if "TIME\n" in lines:
end=lines.index("TIME\n")
pass
- for i in xrange(ind+1,end):
- m=re.match("^([\w]+)[\s]+\per[\s]+([\w]+)[\s]*\:[\s]*([\w]+)[\s]+([\S]+)$",lines[i])
+ for i in range(ind + 1,end):
+ m=re.match("^([\w]+)[\s]+per[\s]+([\w]+)[\s]*\:[\s]*[0-9]*[\s]*([\w]+)[\s]+([\S]+)$",lines[i])
if m:
if m.groups()[0]=="constant":
continue
spatialDisc=m.groups()[1] ; fieldName=m.groups()[2] ; nbOfCompo=self.dictCompo2[m.groups()[0]] ; fieldFileName=m.groups()[3]
- fieldsInfo.append((fieldName,spatialDisc,nbOfCompo,fieldFileName))
+ if "*" in fieldFileName:
+ fieldsInfo.append((fieldName,spatialDisc,nbOfCompo,fieldFileName))
pass
pass
-
+
expr=re.compile("number[\s]+of[\s]+steps[\s]*\:[\s]*([\d]+)")
- tmp=filter(expr.search,lines)
- if len(tmp)!=0:
- nbOfTimeSteps=int(expr.search(filter(expr.search,lines)[0]).group(1))
+ tmp = [line for line in lines if expr.search(line)]
+ if tmp:
+ nbOfTimeSteps = int(expr.search(tmp[0]).group(1))
expr=re.compile("filename[\s]+start[\s]+number[\s]*\:[\s]*([\d]+)")
- startIt=int(expr.search(filter(expr.search,lines)[0]).group(1))
+ startIt = int(expr.search([line for line in lines if expr.search(line)][0]).group(1))
expr=re.compile("filename[\s]+increment[\s]*\:[\s]*([\d]+)")
- incrIt=int(expr.search(filter(expr.search,lines)[0]).group(1))
+ incrIt = int(expr.search([line for line in lines if expr.search(line)][0]).group(1))
else:
nbOfTimeSteps=1
startIt=0
i+=1
pass
pass
- for ts in xrange(nbOfTimeSteps):
+ for ts in range(nbOfTimeSteps):
i=0
for field in fieldsInfo:
if typeOfFile:
pass
ret=MEDFileData()
ret.setMeshes(m2)
- del mlfields[filter(lambda x: len(mlfields[x])==0,range(len(mlfields)))]
+ del mlfields[[x for x in range(len(mlfields)) if len(mlfields[x]) == 0]]
ret.setFields(mlfields)
return ret