Back to index

salome-med  6.5.0
MEDMEM_MedFileBrowser.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "MEDMEM_MedFileBrowser.hxx"
00024 
00025 #include "MEDMEM_Utilities.hxx"
00026 #include "MEDMEM_STRING.hxx"
00027 #include "MEDMEM_DriversDef.hxx"
00028 #include "MEDMEM_MedVersion.hxx"
00029 
00030 #include <set>
00031 
00032 using namespace std;
00033 using namespace MEDMEM;
00034 using namespace MED_EN;
00035 
00036 namespace 
00037 {
00042   struct MED_FILE
00043   {
00044     med_2_3::med_idt _id;
00045     MED_FILE(const string fileName)
00046     {
00047       _id = med_2_3::MEDfileOpen(fileName.c_str(),med_2_3::MED_ACC_RDONLY);
00048     }
00049     ~MED_FILE()
00050     {
00051       if ( _id > 0 )
00052         med_2_3::MEDfileClose(_id);
00053     }
00054     operator med_2_3::med_idt() const { return _id; }
00055   };
00056 }
00072 //================================================================================
00076 //================================================================================
00077 
00078 MEDFILEBROWSER::MEDFILEBROWSER()
00079 {
00080 }
00081 
00082 //================================================================================
00087 //================================================================================
00088 
00089 MEDFILEBROWSER::MEDFILEBROWSER(const std::string & fileName) throw (MED_EXCEPTION)
00090 {
00091   readFileStruct( fileName );
00092 }
00093 
00094 //================================================================================
00098 //================================================================================
00099 
00100 void MEDFILEBROWSER::readFileStruct(const std::string & fileName) throw (MEDEXCEPTION)
00101 {
00102   const char * LOC = "MEDFILEBROWSER::readFileStruct() : ";
00103   BEGIN_OF_MED(LOC);
00104 
00105   _fileName = fileName;
00106   _meshes.clear();
00107   _fields.clear();
00108 
00109   // =================
00110   // 1. Open the file
00111   // =================
00112 
00113   if ( _fileName.empty() )
00114     throw MED_EXCEPTION
00115       ( LOCALIZED( STRING(LOC) << "_fileName is |\"\"|, please provide a correct fileName"));
00116 
00117   MED_EN::medFileVersion version = getMedFileVersion(_fileName);
00118   if ( version == MED_EN::V21 )
00119     throw MED_EXCEPTION
00120       ( LOCALIZED( STRING(LOC) << _fileName << " is med-2.1 file which is of no longer supported version"));
00121 
00122   MED_FILE medIdt(_fileName);
00123 
00124   if (medIdt < 0)
00125     throw MED_EXCEPTION
00126       (LOCALIZED( STRING(LOC)<< "Can't open |"  << _fileName<< "|, medIdt : " << medIdt));
00127 
00128   int err,i,j;
00129 
00130   // =========================================
00131   // 2. Read number of meshes and their names
00132   // =========================================
00133   {
00134     char                  meshName[MED_NAME_SIZE+1]="";
00135     char                  meshDescription[MED_COMMENT_SIZE+1]="";
00136     med_2_3::med_int      meshDim;
00137     med_2_3::med_mesh_type meshType;
00138 
00139     int numberOfMeshes = med_2_3::MEDnMesh(medIdt) ;
00140     if ( numberOfMeshes <= 0 ) 
00141       MESSAGE_MED(LOC << "Be careful there is no mesh in file |"<<_fileName<<"| !");
00142 
00143     for (i=1;i<=numberOfMeshes;i++)
00144     {
00145       //get information on the i^th mesh
00146       int spaceDim;
00147       int naxis=med_2_3::MEDmeshnAxis(medIdt,i);
00148       med_2_3::med_axis_type axistype;
00149       med_2_3::med_sorting_type stype;
00150       char *axisname=new char[naxis*MED_SNAME_SIZE+1];
00151       char *axisunit=new char[naxis*MED_SNAME_SIZE+1];
00152       int nstep;
00153       char dtunit[MED_COMMENT_SIZE+1];
00154       err = med_2_3::MEDmeshInfo(medIdt, i, meshName, &spaceDim, &meshDim, &meshType, meshDescription, dtunit, &stype, &nstep, &axistype, axisname, axisunit) ;
00155       delete [] axisname;
00156       delete [] axisunit;
00157       if (err != MED_VALID)
00158         throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << ": can't get information about the mesh #" << i << " of the file |" << _fileName << "| !"));
00159 
00160       // Don't want to prevent the user from reading other correct meshes
00161 //       if (meshType != med_2_3::MED_STRUCTURE &&
00162 //           meshType != med_2_3::MED_NON_STRUCTURE )
00163 //         throw MEDEXCEPTION
00164 //           (LOCALIZED(STRING(LOC) << "Bad mesh type of mesh #"<<i <<" in file |"<<_fileName<<"|"));
00165       _meshes.insert( make_pair( string(meshName), meshType == med_2_3::MED_STRUCTURED_MESH ));
00166     }
00167   }
00168 
00169   // ===================================================================
00170   // 3. Read number of fields and their (timeStepNumber,iterationNumber)
00171   // ===================================================================
00172   {
00173     //    char                          fieldName[MED_NAME_SIZE+1] = "";
00174     char                          fieldName[MED_NAME_SIZE+1] ; // to avoid a crash if the field name is longer than MED_NAME_SIZE....
00175     char                          * componentName              = (char *) MED_NULL;
00176     char                          * unitName                   = (char *) MED_NULL;
00177     char                          meshName[MED_NAME_SIZE+1]  ;
00178     med_2_3::med_field_type       type;
00179     MESH_ENTITIES::const_iterator currentEntity; 
00180     list<MED_EN::medGeometryElement>::const_iterator currentGeometry;
00181     med_2_3::med_int              timeStepNumber               =  -1;
00182     char                          timeStepUnit[MED_LNAME_SIZE+1] ;
00183     double                        timeStep                     = 0.0;
00184     med_2_3::med_int              orderNumber                  =  -1;
00185     med_2_3::med_bool             meshLink;
00186 
00187     int numberOfFields = med_2_3::MEDnField(medIdt) ;
00188 
00189     for (i=1;i<=numberOfFields;i++)
00190     {
00191       int numberOfComponents = med_2_3::MEDfieldnComponent(medIdt,i) ;
00192 
00193       componentName = new char[numberOfComponents*MED_SNAME_SIZE+1] ;
00194       unitName      = new char[numberOfComponents*MED_SNAME_SIZE+1] ;
00195       int nstepsp3;
00196       err = med_2_3::MEDfieldInfo(medIdt, i, fieldName, meshName, &meshLink, &type, componentName, 
00197                                   unitName, timeStepUnit, &nstepsp3) ;
00198       delete[] componentName ;
00199       delete[] unitName ;
00200 
00201       MESSAGE_MED("Field #" << i << " is named " << fieldName);
00202 
00203       if (err != MED_VALID)
00204         throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << ": can't get information about the field #" << i <<" of the file |" << _fileName << "| !"));
00205 
00206       map< string, FIELD_DATA_ >::iterator name2data =
00207         _fields.insert( make_pair( fieldName, FIELD_DATA_() )).first;
00208 
00209       FIELD_DATA_& fieldData = name2data->second;
00210       fieldData._type = MED_EN::med_type_champ( type );
00211 
00212       // find all dtid of this field
00213       set<DT_IT_, LT_DT_IT_> set_dtit;
00214 
00215       for (j=1;j <= nstepsp3; j++)
00216         {
00217           err = med_2_3::MEDfieldComputingStepInfo(medIdt, fieldName, j, &timeStepNumber, &orderNumber, &timeStep);
00218           
00219           if (err == MED_VALID) // we have found for (*currentEntity).first and (*currentGeometry)
00220             {
00221               DT_IT_ dtIt;
00222               if (timeStepNumber<0)  timeStepNumber=-1 ;
00223               dtIt.dt  = timeStepNumber;
00224               dtIt.it  = orderNumber;
00225               
00226               set_dtit.insert( dtIt );
00227             }
00228         }
00229       fieldData._meshName = meshName;
00230       fieldData._vec_dtit.assign( set_dtit.begin(), set_dtit.end() );
00231 
00232     } // loop on numberOfFields
00233   }
00234 
00235   END_OF_MED(LOC);
00236 }
00245 //================================================================================
00249 //================================================================================
00250 
00251 std::string MEDFILEBROWSER::getFileName() const
00252 {
00253   return _fileName;
00254 }
00255 
00256 //================================================================================
00260 //================================================================================
00261 
00262 int MEDFILEBROWSER::getNumberOfMeshes ( void ) const
00263 {
00264   return _meshes.size();
00265 }   
00266     
00267 //================================================================================
00271 //================================================================================
00272 
00273 int MEDFILEBROWSER::getNumberOfFields ( void ) const
00274 {
00275   return _fields.size(); // we get number of field with different name
00276 }       
00277 
00278 //================================================================================
00288 //================================================================================
00289 
00290 void MEDFILEBROWSER::getMeshNames      ( std::string * meshNames ) const
00291 {
00292   map< string, bool >::const_iterator name_isstruct = _meshes.begin();
00293   for ( int i=0; name_isstruct != _meshes.end(); ++i, ++name_isstruct )
00294     meshNames[i] = name_isstruct->first;
00295 }
00296 
00297 //================================================================================
00303 //================================================================================
00304 
00305 vector<string> MEDFILEBROWSER::getMeshNames      () const
00306 {
00307   vector<string> meshNames( _meshes.size() );
00308   map< string, bool >::const_iterator name_isstruct = _meshes.begin();
00309   for ( int i=0; name_isstruct != _meshes.end(); ++i, ++name_isstruct )
00310     meshNames[i] = name_isstruct->first;
00311   return meshNames;
00312 }
00313 
00314 //================================================================================
00324 //================================================================================
00325 
00326 void MEDFILEBROWSER::getFieldNames     ( std::string * fieldNames ) const
00327 {
00328   map< string, FIELD_DATA_ >::const_iterator name_data = _fields.begin();
00329   for( int i = 0; name_data != _fields.end(); ++name_data, ++i )
00330     fieldNames[i] = name_data->first;
00331 }
00332 
00333 //================================================================================
00337 //================================================================================
00338 
00339 vector<string> MEDFILEBROWSER::getFieldNames     () const
00340 {
00341   vector<string> fieldNames( _fields.size() );
00342   map< string, FIELD_DATA_ >::const_iterator name_data = _fields.begin();
00343   for( int i = 0; name_data != _fields.end(); ++name_data, ++i )
00344     fieldNames[i] = name_data->first;
00345   return fieldNames;
00346 }
00347 
00348 //================================================================================
00352 //================================================================================
00353 
00354 bool MEDFILEBROWSER::isStructuredMesh(const std::string & meshName) const throw (MEDEXCEPTION)
00355 {
00356   const char* LOC="MEDFILEBROWSER::isStructuredMesh(meshName)";
00357 
00358   map< std::string, bool >::const_iterator name_isstruct = _meshes.find(meshName);
00359   if ( name_isstruct == _meshes.end() )
00360     throw MEDEXCEPTION
00361       ( LOCALIZED( STRING(LOC)<< "There is no known mesh named |"<< meshName
00362                    << "| in file |" << _fileName <<"|"));
00363 
00364   return name_isstruct->second;
00365 }
00366 
00367 //================================================================================
00371 //================================================================================
00372 
00373 med_type_champ MEDFILEBROWSER::getFieldType(const std::string & fieldName) const throw (MEDEXCEPTION)
00374 {
00375   const char* LOC="MEDFILEBROWSER::getFieldIteration (fieldName)";
00376 
00377   std::map< std::string, FIELD_DATA_ >::const_iterator name_data = _fields.find(fieldName);
00378 
00379   if ( name_data == _fields.end() )
00380     throw MEDEXCEPTION
00381       ( LOCALIZED( STRING(LOC)<< "There is no known field named |"<< fieldName
00382                    << "| in file |" << _fileName <<"|"));
00383 
00384   return name_data->second._type;
00385 }
00386 
00387 //================================================================================
00391 //================================================================================
00392 
00393 std::string MEDFILEBROWSER::getMeshName (const std::string & fieldName) const throw (MEDEXCEPTION)
00394 {
00395   const char* LOC="MEDFILEBROWSER::getMeshName (fieldName)";
00396 
00397   std::map< std::string, FIELD_DATA_ >::const_iterator name_data = _fields.find(fieldName);
00398 
00399   if ( name_data == _fields.end() )
00400     throw MEDEXCEPTION
00401       ( LOCALIZED( STRING(LOC)<< "There is no known field named |"<< fieldName
00402                    << "| in file |" << _fileName <<"|"));
00403 
00404   return name_data->second._meshName;
00405 }
00406 
00407 //================================================================================
00420 //================================================================================
00421 
00422 vector<DT_IT_> MEDFILEBROWSER::getFieldIteration (const std::string & fieldName) const
00423   throw (MED_EXCEPTION)
00424 {
00425   const char* LOC="MEDFILEBROWSER::getFieldIteration (fieldName)";
00426 
00427   std::map< std::string, FIELD_DATA_ >::const_iterator name_data = _fields.find(fieldName);
00428 
00429   if ( name_data == _fields.end() )
00430     throw MEDEXCEPTION
00431       ( LOCALIZED( STRING(LOC)<< "There is no known field named |"<< fieldName
00432                    << "| in file |" << _fileName <<"|"));
00433 
00434   return name_data->second._vec_dtit;
00435 }