Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDMEM_SWIG / ensightMedEnsight_test.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 ############################################################################
23 # This script tests conversion of EnSight to MEDMEM by performing following
24 # operations on all available EnSight files:
25 # - read EnSight file into MEDMEM and breifly dump it's content;
26 # - store MEDMEM to med file;
27 # - write EnSight file in different formats: Gold and EnSight6, ASCII and binary;
28 # - checks generated EnSight files using ens_checker utility (if available).
29 # EnSight samples are in EXAMPLES salome CVS repository, in directory SAMPLES_SRC/EnSight.
30 # DATA_DIR should contain path to SAMPLES_SRC
31 ############################################################################
32 #
33 from medmem import *
34 from dumpMEDMEM import *
35 from re import search
36 import sys
37
38 ASCII = False
39 Binary = True
40 formats = [
41     (ENSIGHT_GOLD,ASCII)
42     ,(ENSIGHT_GOLD,Binary)
43     ,(ENSIGHT_6,ASCII)
44     ,(ENSIGHT_6,Binary)
45     ]
46
47 dataDir = os.getenv("DATA_DIR")
48 tmpDir  = os.getenv("TMP")
49 if not tmpDir:
50     tmpDir = os.getenv("TMPDIR", "/tmp")
51
52 # EnSight samples are in EXAMPLES CVS repository, in directory SAMPLES_SRC/EnSight.
53 # DATA_DIR should contain path to SAMPLES_SRC
54 inDir = os.path.join( dataDir, "EnSight")
55 if not os.access(inDir, os.F_OK):
56     raise RuntimeError, "Path to EnSight files not found, check DATA_DIR environment"
57
58 outDir = os.path.join( tmpDir,"Ensight_out")
59 if not os.access(outDir, os.F_OK):
60     os.mkdir( outDir )
61     pass
62
63 # find out if ens_checker is available
64 has_ens_checker = False
65 tmpFile = os.path.join( outDir, "has_ens_checker" )
66 os.system("ens_checker _.case > %s 2>&1" % tmpFile)
67 tmpFile = open( tmpFile ).read()
68 if search("EnSight Data Format Checker", tmpFile):
69     has_ens_checker = True
70 else:
71     print "\nWarning: ens_checker utility NOT available"
72     pass
73
74
75 dumpMesh = False
76 dumpMesh = True
77
78 dumpField= False
79 dumpField= True
80
81 def check_ens(casefile, logfile):
82     if not has_ens_checker:
83         return
84     cmd = "(cd %s; ens_checker %s > %s 2>&1)" % (outDir, casefile, logfile)
85     err = os.system( cmd )
86     if os.access(logfile, os.F_OK):
87         log = open(logfile).read()
88         if search("bummer", log):
89             print log
90             raise RuntimeError, "cd %s; ens_checker %s" % (outDir, casefile)
91         if search("Warning:", log):
92             print log
93         pass
94     pass
95
96 def compatibilityPb():
97     "Print traceback and return true if exception is due to EnSight-MEDMEM incompatibility"
98     isCompatibilityPb = ( sys.exc_value.__str__().find("compatibility problem") > 0 )
99     if isCompatibilityPb:
100         print '$$$$$$$$$$$$$$$$$$$$ COMPATIBILITY PROBLEM $$$$$$$$$$$$$$$$$$$$$$$$'
101         print sys.exc_value, "\n"
102     else:
103         import traceback
104         traceback.print_exc()
105     return isCompatibilityPb
106
107 from dircache import listdir
108
109 inFiles = [
110     "frame.case"
111 #     ,"blow1_bin.case"
112 #     ,"zmat2d_esca.case"
113 #     ,"dyna.case"
114 #     ,"en6.case"
115 #     ,"engold.case"
116 #     ,"engoldup.case"
117 #     ,"frame.case"
118 #     ,"ghost_structured.case"
119 #     ,"ghost_unstructured.case"
120 #     ,"ironProt_ascii.case"
121 #     ,"ironProt_bin.case"
122 #     ,"mandelbrot1.case"
123 #     ,"mandelbrot2.case"
124 #     ,"naca.bin.case"
125 #     ,"office6_bin.case"
126 #     ,"office_ascii.case"
127 #     ,"office_bin.case"
128 #     ,"performance.case"
129 #     ,"range_structured.case"
130 #     ,"x29.case"
131 #     ,"RectGrid_ascii.case"
132 #     ,"RectGrid_bin.case"
133 #     ,"ami.case"
134 #     ,"ami6.case"
135 #     ,"anim.case"
136 #     ,"blow2_ascii.case"
137 #     ,"blow2_bin.case"
138 #     ,"blow3_bin.case"
139 #     ,"blow4_bin.case"
140 #     ,"blow5_ascii.case"
141 #     ,"crash.case"
142 #     ,"cube.case"
143 #     ,"cubeE.case"
144     ]
145 inFiles = listdir( inDir )
146
147
148 for inFile in inFiles: # loop on all files in inDir
149
150     # filter .case files
151     basename, ext = os.path.splitext( inFile )
152     if ext != ".case": continue
153
154     # read EnSight into MEDMEM
155
156     setIgnoreIncompatibility(0)
157     
158     ensFile = os.path.join( inDir, inFile )
159     print "\nreading",ensFile
160     incompatible = False
161     meshes = []
162     fields = []
163     try:
164         medFromEns = ENSIGHT_MED_RDONLY_DRIVER(ensFile);
165         fields = medFromEns.read()
166         if not fields:
167             mesh = MESH(ENSIGHT_DRIVER,ensFile,"");
168             meshes.append( mesh )
169         else:
170             meshNames = []
171             for f in fields:
172                 mesh = f.getSupport().getMesh()
173                 if mesh and not mesh.getName() in meshNames:
174                     meshes.append( mesh )
175                     meshNames.append( mesh.getName() )
176                     pass
177                 pass
178             pass
179         pass
180     except:
181         if not compatibilityPb():
182             sys.exit(1)
183         else:
184             continue
185
186     # show MEDMEM contents
187     mesh = meshes[0]
188     if dumpMesh:
189         ShowMesh( mesh, 10, [10,10,10] )
190         ShowGroups( mesh )
191         pass
192     if dumpField:
193         ShowFields( fields, 10 )
194
195     # write MEDMEM into MED
196
197     medFile = os.path.join( outDir, basename + ".med" )
198     deleteFile(medFile)
199     print "write",medFile
200     for m in meshes:
201         m.write( MED_DRIVER,medFile )
202         pass
203     for f in fields:
204         fTyped = f.castToTypedField()
205         fTyped.write(MED_DRIVER,medFile)
206         pass
207
208     # write MEDMEM into EnSight
209
210     for format,bin in formats:
211         ensFile = os.path.join( outDir, basename )
212         if format == ENSIGHT_GOLD:
213             formatName = "ENSIGHT GOLD"
214             ensFile += ".g"
215         else:
216             formatName = "ENSIGHT 6"
217             ensFile += '.6'
218             pass
219         if bin:
220             formatName += " Binary"
221             ensFile += 'b'
222         else:
223             formatName += " ASCII"
224             ensFile += 'a'
225             pass
226         ensFile += ".case"
227         print '*'*80
228         print "Format: \t",formatName
229         print '*'*80
230
231         setEnSightFormatForWriting( format, bin )
232
233         print "writting", ensFile
234         incompatible = False
235         try:
236             if fields:
237                 medEnsDriver = ENSIGHT_MED_WRONLY_DRIVER (ensFile, fields)
238                 medEnsDriver.write()
239             else:
240                 mesh2EnsDriver = ENSIGHT_MESH_WRONLY_DRIVER( ensFile, mesh )
241                 mesh2EnsDriver.write()
242         except:
243             if not compatibilityPb():
244                 sys.exit(1)
245                 pass
246             # try to write anayway, maybe it will be ok
247             incompatible = True
248             setIgnoreIncompatibility(1)
249             try:
250                 if fields:
251                     medEnsDriver.write()
252                 else:
253                     mesh2EnsDriver.write()
254             except:
255                 if not compatibilityPb():
256                     sys.exit(1)
257                     pass
258                 continue
259             pass
260
261         # check generated EnSight
262         try:
263             checkFile = os.path.join( outDir, basename + ".ens_checker" )
264             check_ens(ensFile, checkFile)
265         except:
266             if incompatible:
267                 # Ok I was warned about it
268                 continue
269             sys.exit(1)
270             pass
271         pass
272     print "\n\n\n"
273
274
275 # remove created files
276 for f in listdir( outDir ):
277     deleteFile( os.path.join( outDir, f ))