Back to index

salome-med  6.5.0
medEnsightMed_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 MEDMEM to EnSight by performing following
00024 # operations on available med files:
00025 # - read med file into MEDMEM and breifly dump it's content;
00026 # - write EnSight file in different formats: Gold and EnSight6, ASCII and binary;
00027 # - checks generated EnSight files using ens_checker utility (if available);
00028 # - read generated EnSight file into MEDMEM and breifly dump it's content;
00029 # - store MEDMEM to med file.
00030 ############################################################################
00031 #
00032 from medmem import *
00033 from dumpMEDMEM import *
00034 from re import search
00035 import sys
00036 
00037 ASCII = False
00038 Binary = True
00039 formats = [
00040     (ENSIGHT_GOLD,ASCII)
00041     ,(ENSIGHT_GOLD,Binary)
00042     ,(ENSIGHT_6,ASCII)
00043     ,(ENSIGHT_6,Binary)
00044     ]
00045 
00046 tmpDir  = os.getenv("TMP")
00047 if not tmpDir:
00048     tmpDir = os.getenv("TMPDIR","/tmp")
00049 
00050 inDir = os.path.join(os.getenv("MED_ROOT_DIR"), "share","salome","resources","med")
00051 if not os.access(inDir, os.F_OK):
00052     raise RuntimeError, "Path to med files not found, MED_ROOT_DIR incorrect?"
00053 
00054 outDir = os.path.join( tmpDir,"Ensight_out")
00055 if not os.access(outDir, os.F_OK):
00056     os.mkdir( outDir )
00057     pass
00058 
00059 # find out if ens_checker is present
00060 has_ens_checker = False
00061 tmpFile = os.path.join( outDir, "has_ens_checker" )
00062 os.system("ens_checker _.case > %s 2>&1" % tmpFile)
00063 tmpFile = open( tmpFile ).read()
00064 if search("EnSight Data Format Checker", tmpFile):
00065     has_ens_checker = True
00066 else:
00067     print "\nWarning: ens_checker utility NOT available"
00068     pass
00069 
00070 dumpMesh = False
00071 dumpMesh = True
00072 
00073 dumpField= False
00074 dumpField= True
00075 
00076 def check_ens(casefile, logfile):
00077     cmd = "(cd %s; ens_checker %s > %s 2>&1)" % (outDir, casefile, logfile)
00078     err = os.system( cmd )
00079     if os.access(logfile, os.F_OK):
00080         log = open(logfile).read()
00081         if search("bummer", log):
00082             print log
00083             raise RuntimeError, "cd %s; ens_checker %s" % (outDir, casefile)
00084         if search("Warning:", log):
00085             print log
00086         pass
00087     pass
00088 
00089 def compatibilityPb():
00090     "Print traceback and return true if exception is due to EnSight-MEDMEM incompatibility"
00091     isCompatibilityPb = ( sys.exc_value.__str__().find("compatibility problem") > 0 )
00092     if isCompatibilityPb:
00093         print '$$$$$$$$$$$$$$$$$$$$ COMPATIBILITY PROBLEM $$$$$$$$$$$$$$$$$$$$$$$$'
00094         print sys.exc_value, "\n"
00095     else:
00096         import traceback
00097         traceback.print_exc()
00098     return isCompatibilityPb
00099 
00100 from dircache import listdir
00101 
00102 # all files sorted by size increasing
00103 medFiles = [
00104      "ChampsDarcy.med"
00105 #     ,"Old_maillage_chemvalIV_cas1_40elts.med"
00106 #     ,"maillage_UniSegFam.med"
00107 #     ,"champ1.med"
00108 #     ,"carre_en_quad4.med"
00109 #     ,"poly3D.med"
00110 #     ,"Old_Deff_fdt_5.8_castem_vf_diff_conc_dom.med"
00111 #     ,"Old_Deff_fdt_5.8_castem_efmh_diff_conc_dom.med"
00112 #     ,"test18.med"
00113 #     ,"cas_defaut_domaine_fluide.med"
00114 #     ,"carre_en_quad4_seg2.med"
00115 #     ,"polyedres.med"
00116 #     ,"Fields_group3D.med"
00117 #     ,"maillage_5_5_5.med"
00118 #     ,"cube_hexa8.med"
00119 #     ,"new_pointe.med"
00120 #     ,"mesh.med"
00121 #     ,"cube_hexa8_quad4.med"
00122 #     ,"jurassique.med"
00123 #     ,"test_2D.med"
00124 #     ,"pointe_4fields.med"
00125 #     ,"pointe.med"
00126 #     ,"test19.med"
00127 #     ,"extendedtransport53_triangles.med"
00128 #     ,"recoll_bord.med"
00129 #     ,"polygones.med"
00130 #     ,"zzzz121b_without_tr6.med"
00131 #     ,"trio_2D.med"
00132 #     ,"essaiminimail.med"
00133 #     ,"maill.0.med"
00134 #     ,"elle_3D_HPr_2x2x2_2.med"
00135 #     ,"maill.00_nofield.med"
00136 #     ,"elle_3D_HPr_4x4x4_2.med"
00137 #     ,"maill.00_nomesh.med"
00138 #     ,"fra1.med"
00139 #     ,"fra.med"
00140 #     ,"maill.00_without_seg2.med"
00141 #     ,"freebord.med"
00142 #     ,"maill.00.med"
00143 #     ,"zzzz121b.med"
00144 #     ,"ResOK_0000.med"
00145 #     ,"Darcy3_3D_H_10x10x10_2.med"
00146 #     ,"maill.0_volumes.med"
00147 #     ,"Old_darcy_1.3_resTRACES.med"
00148 #     ,"Old_darcy_1.3_resCASTEM.med"
00149 #     ,"darcy_1.3_resTRACES.med"
00150 #     ,"darcy_1.3_resCASTEM.med"
00151 #     ,"geomMesh22.med"
00152 #     ,"geomMesh21.med"
00153 #     ,"Old_darcy_1.3_resPORFLOW.med"
00154 #     ,"darcy_1.3_resPORFLOW.med"
00155 #     ,"test_hydro_darcy1a_out.med"
00156 #     ,"maillage_2D.med"
00157 #     ,"elle_3D_HPr_10x10x10_2.med"
00158 #     ,"Old_darcy_1.1_res.med"
00159 #     ,"darcy_1.1_res.med"
00160 #     ,"maill.1.med"
00161 #     ,"clo.med"
00162 #     ,"clo2.med"
00163 #     ,"Old_H_Traces_Se79_COUPLEX1.med"
00164 #     ,"Old_H_Traces_I129_COUPLEX1.med"
00165 #     ,"Old_H_PorfPorf_Se79_COUPLEX1.med"
00166 #     ,"Old_H_PorfPorf_I129_COUPLEX1.med"
00167 #     ,"Old_H_PorfCast_EFMH_Se79_COUPLEX1.med"
00168 #     ,"Old_H_PorfCast_EFMH_I129_COUPLEX1.med"
00169 #     ,"Old_H_CastPorf_Se79_COUPLEX1.med"
00170 #     ,"Old_H_CastPorf_I129_COUPLEX1.med"
00171 #     ,"Old_H_CastCast_VF_Se79_COUPLEX1.med"
00172 #     ,"Old_H_CastCast_VF_I129_COUPLEX1.med"
00173 #     ,"Old_H_CastCast_EFMH_I129_COUPLEX1.med"
00174 #     ,"H_Traces_Se79_COUPLEX1.med"
00175 #     ,"H_Traces_I129_COUPLEX1.med"
00176 #     ,"H_PorfPorf_I129_COUPLEX1.med"
00177 #     ,"H_PorfCast_EFMH_Se79_COUPLEX1.med"
00178 #     ,"H_PorfCast_EFMH_I129_COUPLEX1.med"
00179 #     ,"H_CastPorf_Se79_COUPLEX1.med"
00180 #     ,"H_CastPorf_I129_COUPLEX1.med"
00181 #     ,"H_CastCast_VF_Se79_COUPLEX1.med"
00182 #     ,"H_CastCast_VF_I129_COUPLEX1.med"
00183 #     ,"H_CastCast_EFMH_I129_COUPLEX1.med"
00184 #     ,"puitsinst.med"
00185 #     ,"Old_darcy2_Castem_EFMH.med"
00186 #     ,"darcy2_Castem_EFMH.med"
00187 #     ,"Old_darcy2_Castem_qua_VF.med"
00188 #     ,"Old_darcy2_Castem_qua_EFMH.med"
00189 #     ,"darcy2_Castem_qua_VF.med"
00190 #     ,"darcy2_Castem_qua_EFMH.med"
00191 #     ,"maillage_migr3d.med"
00192 #     ,"Mistrat.med"
00193 #     ,"Old_ChampsDarcy.med"
00194 #     ,"ChampsDarcy.med"
00195 #     ,"resu.2.med"
00196 #     ,"maill.2.med"
00197 #     ,"TimeStamps.med"
00198 #     ,"hexa_28320_ELEM.med"
00199     ]
00200 
00201 badFiles = [ # files that can't be read by MEDMEM
00202     "Deff_fdt_5.8_castem_efmh_diff_conc_dom.med"
00203     ,"Deff_fdt_5.8_castem_vf_diff_conc_dom.med"
00204     ]
00205 
00206 medFiles = listdir( inDir )
00207 
00208 
00209 for medFile in medFiles: # loop on med files in inDir
00210 
00211     basename, ext = os.path.splitext( medFile )
00212     if ext != ".med" or medFile in badFiles: continue
00213 
00214     # read MED file
00215     medFile = os.path.join( inDir, medFile )
00216     print "reading",medFile
00217     fields = []
00218     try:
00219         med = MEDFILEBROWSER(medFile)
00220         if med.getNumberOfMeshes() < 1: continue
00221         m2m_nom  = med.getMeshNames()[0]
00222         if med.isStructuredMesh(m2m_nom):
00223             mesh = GRID(MED_DRIVER,medFile,m2m_nom);
00224         else:
00225             mesh = MESH(MED_DRIVER,medFile,m2m_nom);
00226             pass
00227 
00228         for f_name in med.getFieldNames():
00229             if med.getMeshName(f_name) != mesh.getName(): continue
00230             dtit_list = med.getFieldIteration( f_name )
00231             for dtit in dtit_list:
00232                 if med.getFieldType( f_name ) == MED_REEL64:
00233                     field = FIELDDOUBLE(MED_DRIVER, medFile, f_name, dtit.dt, dtit.it, mesh )
00234                 else:
00235                     field = FIELDINT(MED_DRIVER, medFile, f_name, dtit.dt, dtit.it, mesh )
00236                     pass
00237                 fields.append( field )
00238     except:
00239         print sys.exc_value
00240         continue
00241 
00242     ShowMesh( mesh )
00243     print "Nb Fields",len(fields)
00244     #ShowGroups( mesh )
00245     #ShowFields( fields, 10 )
00246 
00247     basename = os.path.join( outDir, basename ) + '.'
00248     checkFile = basename + "ens_checker"
00249 
00250     for format,bin in formats:
00251         if format == ENSIGHT_GOLD:
00252             formatName = "ENSIGHT GOLD"
00253             formatSuffix = "g"
00254         else:
00255             formatName = "ENSIGHT 6"
00256             formatSuffix = '6'
00257             pass
00258         if bin:
00259             formatName += " Binary"
00260             formatSuffix += 'b'
00261         else:
00262             formatName += " ASCII"
00263             formatSuffix += 'a'
00264             pass
00265         ensFile = basename + formatSuffix + ".case"
00266         print '*'*80
00267         print "Format: \t",formatName
00268         print '*'*80
00269 
00270         setEnSightFormatForWriting( format, bin )
00271 
00272         # write a mesh to EnSight
00273         # print "write a mesh to EnSight"
00274         # myMeshDriver = ENSIGHT_MESH_WRONLY_DRIVER(ensFile,mesh)
00275         # myMeshDriver.write()
00276 
00277         # if med.getNumberOfFields() > 0:
00278         #     fName = med.getFieldName( 0 )
00279         #     dt_it_= med.getFieldIteration( fName, 0 )
00280         #     field = med.getField( fName, dt_it_.dt, dt_it_.it )
00281         #     print "add a field to EnSight"
00282         #     myFieldDriver = ENSIGHT_FIELD_WRONLY_DRIVER(ensFile,field);
00283         #     myFieldDriver.write();
00284 
00285         # check_ens(ensFile, checkFile)
00286 
00287         # write EnSight
00288         print "writting", ensFile
00289         try:
00290             if fields:
00291                 medEnsDriver = ENSIGHT_MED_WRONLY_DRIVER (ensFile, fields)
00292                 medEnsDriver.write()
00293             else:
00294                 mesh.write( ENSIGHT_DRIVER, ensFile )
00295         except:
00296             if not compatibilityPb():
00297                 sys.exit(1)
00298             continue
00299 
00300         # check generated EnSight
00301         check_ens(ensFile, checkFile)
00302 
00303         # read generated EnSight into MEDMEM
00304         print "reading",ensFile
00305         if fields:
00306             medFromEns = ENSIGHT_MED_RDONLY_DRIVER(ensFile)
00307             fields = medFromEns.read()
00308             mesh = fields[0].getSupport().getMesh()
00309         else:
00310             mesh = MESH(ENSIGHT_DRIVER, ensFile, "")
00311 
00312         # dump generated EnSight
00313         if dumpMesh:
00314             #ShowMesh( mesh )
00315             ShowMesh( mesh, 10, [10,10,10] )
00316             #ShowGroups( mesh )
00317             pass
00318         if dumpField:
00319             ShowFields( fields, 10 )
00320             pass
00321 
00322         # write EnSight to MED
00323         wFile = basename + formatSuffix + ".med"
00324         deleteFile(wFile)
00325         print "write",wFile
00326         try:
00327             mesh.write( MED_DRIVER,wFile )
00328             for f in fields:
00329                 fTyped = f.castToTypedField()
00330                 fTyped.write(MED_DRIVER,wFile)
00331         except:
00332             import traceback
00333             traceback.print_exc()
00334             pass
00335         pass
00336 
00337     print "\n\n\n"
00338             
00339 
00340 # remove created files
00341 for f in listdir( outDir ):
00342     deleteFile( os.path.join( outDir, f ))