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