Back to index

salome-med  6.5.0
ensightMedEnsight_test.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #  -*- coding: iso-8859-1 -*-
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # This library is free software; you can redistribute it and/or
00006 # modify it under the terms of the GNU Lesser General Public
00007 # License as published by the Free Software Foundation; either
00008 # version 2.1 of the License.
00009 #
00010 # This library is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 # Lesser General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU Lesser General Public
00016 # License along with this library; if not, write to the Free Software
00017 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 #
00019 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00020 #
00021 
00022 ############################################################################
00023 # This script tests conversion of EnSight to MEDMEM by performing following
00024 # operations on all available EnSight files:
00025 # - read EnSight file into MEDMEM and breifly dump it's content;
00026 # - store MEDMEM to med file;
00027 # - write EnSight file in different formats: Gold and EnSight6, ASCII and binary;
00028 # - checks generated EnSight files using ens_checker utility (if available).
00029 # EnSight samples are in EXAMPLES salome CVS repository, in directory SAMPLES_SRC/EnSight.
00030 # DATA_DIR should contain path to SAMPLES_SRC
00031 ############################################################################
00032 #
00033 from medmem import *
00034 from dumpMEDMEM import *
00035 from re import search
00036 import sys
00037 
00038 ASCII = False
00039 Binary = True
00040 formats = [
00041     (ENSIGHT_GOLD,ASCII)
00042     ,(ENSIGHT_GOLD,Binary)
00043     ,(ENSIGHT_6,ASCII)
00044     ,(ENSIGHT_6,Binary)
00045     ]
00046 
00047 dataDir = os.getenv("DATA_DIR")
00048 tmpDir  = os.getenv("TMP")
00049 if not tmpDir:
00050     tmpDir = os.getenv("TMPDIR", "/tmp")
00051 
00052 # EnSight samples are in EXAMPLES CVS repository, in directory SAMPLES_SRC/EnSight.
00053 # DATA_DIR should contain path to SAMPLES_SRC
00054 inDir = os.path.join( dataDir, "EnSight")
00055 if not os.access(inDir, os.F_OK):
00056     raise RuntimeError, "Path to EnSight files not found, check DATA_DIR environment"
00057 
00058 outDir = os.path.join( tmpDir,"Ensight_out")
00059 if not os.access(outDir, os.F_OK):
00060     os.mkdir( outDir )
00061     pass
00062 
00063 # find out if ens_checker is available
00064 has_ens_checker = False
00065 tmpFile = os.path.join( outDir, "has_ens_checker" )
00066 os.system("ens_checker _.case > %s 2>&1" % tmpFile)
00067 tmpFile = open( tmpFile ).read()
00068 if search("EnSight Data Format Checker", tmpFile):
00069     has_ens_checker = True
00070 else:
00071     print "\nWarning: ens_checker utility NOT available"
00072     pass
00073 
00074 
00075 dumpMesh = False
00076 dumpMesh = True
00077 
00078 dumpField= False
00079 dumpField= True
00080 
00081 def check_ens(casefile, logfile):
00082     if not has_ens_checker:
00083         return
00084     cmd = "(cd %s; ens_checker %s > %s 2>&1)" % (outDir, casefile, logfile)
00085     err = os.system( cmd )
00086     if os.access(logfile, os.F_OK):
00087         log = open(logfile).read()
00088         if search("bummer", log):
00089             print log
00090             raise RuntimeError, "cd %s; ens_checker %s" % (outDir, casefile)
00091         if search("Warning:", log):
00092             print log
00093         pass
00094     pass
00095 
00096 def compatibilityPb():
00097     "Print traceback and return true if exception is due to EnSight-MEDMEM incompatibility"
00098     isCompatibilityPb = ( sys.exc_value.__str__().find("compatibility problem") > 0 )
00099     if isCompatibilityPb:
00100         print '$$$$$$$$$$$$$$$$$$$$ COMPATIBILITY PROBLEM $$$$$$$$$$$$$$$$$$$$$$$$'
00101         print sys.exc_value, "\n"
00102     else:
00103         import traceback
00104         traceback.print_exc()
00105     return isCompatibilityPb
00106 
00107 from dircache import listdir
00108 
00109 inFiles = [
00110     "frame.case"
00111 #     ,"blow1_bin.case"
00112 #     ,"zmat2d_esca.case"
00113 #     ,"dyna.case"
00114 #     ,"en6.case"
00115 #     ,"engold.case"
00116 #     ,"engoldup.case"
00117 #     ,"frame.case"
00118 #     ,"ghost_structured.case"
00119 #     ,"ghost_unstructured.case"
00120 #     ,"ironProt_ascii.case"
00121 #     ,"ironProt_bin.case"
00122 #     ,"mandelbrot1.case"
00123 #     ,"mandelbrot2.case"
00124 #     ,"naca.bin.case"
00125 #     ,"office6_bin.case"
00126 #     ,"office_ascii.case"
00127 #     ,"office_bin.case"
00128 #     ,"performance.case"
00129 #     ,"range_structured.case"
00130 #     ,"x29.case"
00131 #     ,"RectGrid_ascii.case"
00132 #     ,"RectGrid_bin.case"
00133 #     ,"ami.case"
00134 #     ,"ami6.case"
00135 #     ,"anim.case"
00136 #     ,"blow2_ascii.case"
00137 #     ,"blow2_bin.case"
00138 #     ,"blow3_bin.case"
00139 #     ,"blow4_bin.case"
00140 #     ,"blow5_ascii.case"
00141 #     ,"crash.case"
00142 #     ,"cube.case"
00143 #     ,"cubeE.case"
00144     ]
00145 inFiles = listdir( inDir )
00146 
00147 
00148 for inFile in inFiles: # loop on all files in inDir
00149 
00150     # filter .case files
00151     basename, ext = os.path.splitext( inFile )
00152     if ext != ".case": continue
00153 
00154     # read EnSight into MEDMEM
00155 
00156     setIgnoreIncompatibility(0)
00157     
00158     ensFile = os.path.join( inDir, inFile )
00159     print "\nreading",ensFile
00160     incompatible = False
00161     meshes = []
00162     fields = []
00163     try:
00164         medFromEns = ENSIGHT_MED_RDONLY_DRIVER(ensFile);
00165         fields = medFromEns.read()
00166         if not fields:
00167             mesh = MESH(ENSIGHT_DRIVER,ensFile,"");
00168             meshes.append( mesh )
00169         else:
00170             meshNames = []
00171             for f in fields:
00172                 mesh = f.getSupport().getMesh()
00173                 if mesh and not mesh.getName() in meshNames:
00174                     meshes.append( mesh )
00175                     meshNames.append( mesh.getName() )
00176                     pass
00177                 pass
00178             pass
00179         pass
00180     except:
00181         if not compatibilityPb():
00182             sys.exit(1)
00183         else:
00184             continue
00185 
00186     # show MEDMEM contents
00187     mesh = meshes[0]
00188     if dumpMesh:
00189         ShowMesh( mesh, 10, [10,10,10] )
00190         ShowGroups( mesh )
00191         pass
00192     if dumpField:
00193         ShowFields( fields, 10 )
00194 
00195     # write MEDMEM into MED
00196 
00197     medFile = os.path.join( outDir, basename + ".med" )
00198     deleteFile(medFile)
00199     print "write",medFile
00200     for m in meshes:
00201         m.write( MED_DRIVER,medFile )
00202         pass
00203     for f in fields:
00204         fTyped = f.castToTypedField()
00205         fTyped.write(MED_DRIVER,medFile)
00206         pass
00207 
00208     # write MEDMEM into EnSight
00209 
00210     for format,bin in formats:
00211         ensFile = os.path.join( outDir, basename )
00212         if format == ENSIGHT_GOLD:
00213             formatName = "ENSIGHT GOLD"
00214             ensFile += ".g"
00215         else:
00216             formatName = "ENSIGHT 6"
00217             ensFile += '.6'
00218             pass
00219         if bin:
00220             formatName += " Binary"
00221             ensFile += 'b'
00222         else:
00223             formatName += " ASCII"
00224             ensFile += 'a'
00225             pass
00226         ensFile += ".case"
00227         print '*'*80
00228         print "Format: \t",formatName
00229         print '*'*80
00230 
00231         setEnSightFormatForWriting( format, bin )
00232 
00233         print "writting", ensFile
00234         incompatible = False
00235         try:
00236             if fields:
00237                 medEnsDriver = ENSIGHT_MED_WRONLY_DRIVER (ensFile, fields)
00238                 medEnsDriver.write()
00239             else:
00240                 mesh2EnsDriver = ENSIGHT_MESH_WRONLY_DRIVER( ensFile, mesh )
00241                 mesh2EnsDriver.write()
00242         except:
00243             if not compatibilityPb():
00244                 sys.exit(1)
00245                 pass
00246             # try to write anayway, maybe it will be ok
00247             incompatible = True
00248             setIgnoreIncompatibility(1)
00249             try:
00250                 if fields:
00251                     medEnsDriver.write()
00252                 else:
00253                     mesh2EnsDriver.write()
00254             except:
00255                 if not compatibilityPb():
00256                     sys.exit(1)
00257                     pass
00258                 continue
00259             pass
00260 
00261         # check generated EnSight
00262         try:
00263             checkFile = os.path.join( outDir, basename + ".ens_checker" )
00264             check_ens(ensFile, checkFile)
00265         except:
00266             if incompatible:
00267                 # Ok I was warned about it
00268                 continue
00269             sys.exit(1)
00270             pass
00271         pass
00272     print "\n\n\n"
00273 
00274 
00275 # remove created files
00276 for f in listdir( outDir ):
00277     deleteFile( os.path.join( outDir, f ))