Salome HOME
Copyright update 2022
[tools/medcoupling.git] / src / MEDLoader / Swig / CaseWriter.py
index ee1f12c98823784f175b344bd4aafcdcd80d8083..e7554309dff653c0cd6f38d1f192c7f0d5b80ead 100644 (file)
@@ -1,5 +1,5 @@
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2014  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
@@ -28,9 +28,9 @@ import sys,re,os,mmap
 
 class CaseWriter(CaseIO):
     """ Converting MED file format in memory to a the Case file format (Ensight).
-    A new file with the same base name and the .case extension is created with its depencies (.geo ...).
+    A new file with the same base name and the .case extension is created with its dependencies (.geo ...).
     """
-    
+
     header="""FORMAT
 type: ensight gold
 GEOMETRY
@@ -45,7 +45,7 @@ filename increment:     1
 time values:
 %(TimeValues)s
 """
-    
+
     @classmethod
     def New(cls):
         """ Static constructor. """
@@ -70,7 +70,7 @@ time values:
         assert(isinstance(status,bool))
         self.__export_groups=status
         pass
-        
+
 
     def write(self,fileName):
         """ Write into the specified fileName series the result """
@@ -95,7 +95,7 @@ time values:
             self._real_written_file_name.append(realWrittenCaseFileNameForCurMesh)
             pass
         return self._real_written_file_name
-    
+
     def __writeMeshesPart(self,mdm,meshfn):
         try:
             os.remove(meshfn)
@@ -107,7 +107,7 @@ time values:
         assert(isinstance(mdm,MEDFileUMesh))
         ms2=[[mdm.getMeshAtLevel(lev) for lev in mdm.getNonEmptyLevels()[:1]]]
         if self.__export_groups:
-            for grpnm in mdm.getGroupsNames():    
+            for grpnm in mdm.getGroupsNames():
                 ms3=[]
                 for lev in mdm.getGrpNonEmptyLevels(grpnm)[:1]:
                     m=mdm.getGroup(lev,grpnm) ; m.zipCoords()
@@ -124,7 +124,7 @@ time values:
         a=np.memmap(f,dtype='byte',mode='w+',offset=0,shape=(sz,)) ; a.flush() # truncate to set the size of the file
         mm=mmap.mmap(f.fileno(),offset=0,length=0)
         mm.write(self.__str80("C Binary"))
-        mm.write(self.__str80("Exported from MEDCoupling/MEDLoader SALOME version %s"%(MEDCouplingVersionStr())))
+        mm.write(self.__str80("Exported from MEDCoupling/MEDLoader version %s"%(MEDCouplingVersionStr())))
         mm.write(self.__str80("Conversion using CaseWriter class"))
         mm.write(self.__str80("node id off"))
         mm.write(self.__str80("element id off"))
@@ -158,7 +158,7 @@ time values:
                         typ=MEDCouplingMesh.GetCorrespondingPolyType(typ)
                         pass
                     mp=m[i:i+nbelem]
-                    mm.write(self.__str80(self.dictMCTyp[typ]))
+                    mm.write(self.__str80(self.dictMCTyp_str[typ]))
                     a=np.memmap(f,dtype='int32',mode='w+',offset=mm.tell(),shape=(1,))
                     a[0]=nbelem ; a.flush() ; mm.seek(mm.tell()+4)
                     if typ!=NORM_POLYHED and typ!=NORM_POLYGON:
@@ -172,7 +172,7 @@ time values:
                         c=mp.computeNbOfFacesPerCell()
                         a=np.memmap(f,dtype='int32',mode='w+',offset=mm.tell(),shape=(nbelem,))
                         a[:]=c.toNumPyArray(); a.flush() ; mm.seek(mm.tell()+nbelem*4)
-                        c=mp.getNodalConnectivity()[:] ; c.pushBackSilent(-1) ; c[mp.getNodalConnectivityIndex()[:-1]]=-1 ; ids=c.getIdsEqual(-1) ; nbOfNodesPerFace=ids.deltaShiftIndex()-1
+                        c=mp.getNodalConnectivity()[:] ; c.pushBackSilent(-1) ; c[mp.getNodalConnectivityIndex()[:-1]]=-1 ; ids=c.findIdsEqual(-1) ; nbOfNodesPerFace=ids.deltaShiftIndex()-1
                         a=np.memmap(f,dtype='int32',mode='w+',offset=mm.tell(),shape=(len(nbOfNodesPerFace),))
                         a[:]=nbOfNodesPerFace.toNumPyArray() ; a.flush() ; mm.seek(mm.tell()+len(nbOfNodesPerFace)*4)
                         ids2=ids.buildComplement(ids.back()+1)
@@ -194,14 +194,14 @@ time values:
                 pass
             pass
         pass
-    
+
     def __writeFieldsPart(self,mdfs):
         if not mdfs:
             return ""
         self._ze_top_dict={}
         its,areForgottenTS=mdfs.getCommonIterations()
         if areForgottenTS:
-            print "WARNING : some iterations are NOT present in all fields ! Kept iterations are : %s !"%(str(its))
+            print(("WARNING : some iterations are NOT present in all fields ! Kept iterations are : %s !"%(str(its))))
             pass
         TimeValues=""
         for it in its:
@@ -211,12 +211,12 @@ time values:
         for mdf in mdfs:
             nbCompo=mdf.getNumberOfComponents()
             if nbCompo not in self.dictCompo:
-                l=filter(lambda x:x-nbCompo>0,self.dictCompo.keys())
+                l = [x for x in self.dictCompo if x - nbCompo > 0]
                 if len(l)==0:
-                    print "Field \"%s\" will be ignored because number of components (%i) is too big to be %s supported by case files !"%(mdf.getName(),nbCompo,str(self.dictCompo.keys()))
+                    print(("Field \"%s\" will be ignored because number of components (%i) is too big to be %s supported by case files !"%(mdf.getName(),nbCompo,str(list(self.dictCompo.keys())))))
                     continue
                     pass
-                print "WARNING : Field \"%s\" will have its number of components (%i) set to %i, in order to be supported by case files (must be in %s) !"%(mdf.getName(),nbCompo,l[0],str(self.dictCompo.keys()))
+                print(("WARNING : Field \"%s\" will have its number of components (%i) set to %i, in order to be supported by case files (must be in %s) !"%(mdf.getName(),nbCompo,l[0],str(list(self.dictCompo.keys())))))
                 nbCompo=l[0]
                 pass
             if nbCompo in dictVars:
@@ -229,7 +229,7 @@ time values:
         for mdf in mdfs:
             nbCompo=mdf.getNumberOfComponents()
             if nbCompo not in self.dictCompo:
-                l=filter(lambda x:x-nbCompo>0,self.dictCompo.keys())
+                l = [x for x in self.dictCompo if x - nbCompo > 0]
                 if len(l)==0:
                     continue;
                 nbCompo=l[0]
@@ -270,13 +270,13 @@ time values:
                         if typ==curTyp:
                             arr=ff.getUndergroundDataArray()[bg:end].changeNbOfComponents(nbCompo,0.) ; arr=arr.toNoInterlace()
                             if typ==ON_CELLS:
-                                mm.write(self.__str80(self.dictMCTyp[geo]))
+                                mm.write(self.__str80(self.dictMCTyp_str[geo]))
                                 pass
                             elif typ==ON_NODES:
                                 mm.write(self.__str80("coordinates"))
                                 pass
                             else:
-                                print "UnManaged type of field for field \"%s\" !"%(mdf.getName())
+                                print(("UnManaged type of field for field \"%s\" !"%(mdf.getName())))
                                 pass
                             a=np.memmap(f,dtype='float32',mode='w+',offset=mm.tell(),shape=(nbCompo,end-bg))
                             b=arr.toNumPyArray() ; b=b.reshape(nbCompo,end-bg)
@@ -314,13 +314,13 @@ time values:
             headerPart+=htp
             pass
         return headerPart
-    
+
     @classmethod
     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
         for m in listOfMeshes: