1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "MEDMEM_MedFileBrowser.hxx"
25 #include "MEDMEM_Utilities.hxx"
26 #include "MEDMEM_STRING.hxx"
27 #include "MEDMEM_DriversDef.hxx"
28 #include "MEDMEM_MedVersion.hxx"
33 using namespace MEDMEM;
34 using namespace MED_EN;
39 * \brief Structure to open a med file and to close it at destruction
40 * (especially useful when we throw an exception)
45 MED_FILE(const string fileName)
47 _id = med_2_3::MEDfileOpen(fileName.c_str(),med_2_3::MED_ACC_RDONLY);
52 med_2_3::MEDfileClose(_id);
54 operator med_2_3::med_idt() const { return _id; }
59 \defgroup MEDFILEBROWSER_constructors Initialization
61 \defgroup MEDFILEBROWSER_query Query methods
62 These methods enable the user to retrieve information
63 about a MED file structure, i.e. the meshes and fields it contains.
68 \addtogroup MEDFILEBROWSER_constructors
72 //================================================================================
74 * \brief Constructor of an empty MEDFILEBROWSER to be filled with \ref readFileStruct()
76 //================================================================================
78 MEDFILEBROWSER::MEDFILEBROWSER()
82 //================================================================================
84 * \brief This constructor constructs the MEDFILEBROWSER object from the file \a filename.
85 * Names of meshes and fields are read.
87 //================================================================================
89 MEDFILEBROWSER::MEDFILEBROWSER(const std::string & fileName) throw (MED_EXCEPTION)
91 readFileStruct( fileName );
94 //================================================================================
96 * \brief Read names of meshes and fields from the file \a fileName
98 //================================================================================
100 void MEDFILEBROWSER::readFileStruct(const std::string & fileName) throw (MEDEXCEPTION)
102 const char * LOC = "MEDFILEBROWSER::readFileStruct() : ";
105 _fileName = fileName;
113 if ( _fileName.empty() )
115 ( LOCALIZED( STRING(LOC) << "_fileName is |\"\"|, please provide a correct fileName"));
117 MED_EN::medFileVersion version = getMedFileVersion(_fileName);
118 if ( version == MED_EN::V21 )
120 ( LOCALIZED( STRING(LOC) << _fileName << " is med-2.1 file which is of no longer supported version"));
122 MED_FILE medIdt(_fileName);
126 (LOCALIZED( STRING(LOC)<< "Can't open |" << _fileName<< "|, medIdt : " << medIdt));
130 // =========================================
131 // 2. Read number of meshes and their names
132 // =========================================
134 char meshName[MED_NAME_SIZE+1]="";
135 char meshDescription[MED_COMMENT_SIZE+1]="";
136 med_2_3::med_int meshDim;
137 med_2_3::med_mesh_type meshType;
139 int numberOfMeshes = med_2_3::MEDnMesh(medIdt) ;
140 if ( numberOfMeshes <= 0 )
141 MESSAGE_MED(LOC << "Be careful there is no mesh in file |"<<_fileName<<"| !");
143 for (i=1;i<=numberOfMeshes;i++)
145 //get information on the i^th mesh
147 int naxis=med_2_3::MEDmeshnAxis(medIdt,i);
148 med_2_3::med_axis_type axistype;
149 med_2_3::med_sorting_type stype;
150 char *axisname=new char[naxis*MED_SNAME_SIZE+1];
151 char *axisunit=new char[naxis*MED_SNAME_SIZE+1];
153 char dtunit[MED_COMMENT_SIZE+1];
154 err = med_2_3::MEDmeshInfo(medIdt, i, meshName, &spaceDim, &meshDim, &meshType, meshDescription, dtunit, &stype, &nstep, &axistype, axisname, axisunit) ;
157 if (err != MED_VALID)
158 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << ": can't get information about the mesh #" << i << " of the file |" << _fileName << "| !"));
160 // Don't want to prevent the user from reading other correct meshes
161 // if (meshType != med_2_3::MED_STRUCTURE &&
162 // meshType != med_2_3::MED_NON_STRUCTURE )
163 // throw MEDEXCEPTION
164 // (LOCALIZED(STRING(LOC) << "Bad mesh type of mesh #"<<i <<" in file |"<<_fileName<<"|"));
165 _meshes.insert( make_pair( string(meshName), meshType == med_2_3::MED_STRUCTURED_MESH ));
169 // ===================================================================
170 // 3. Read number of fields and their (timeStepNumber,iterationNumber)
171 // ===================================================================
173 // char fieldName[MED_NAME_SIZE+1] = "";
174 char fieldName[MED_NAME_SIZE+1] ; // to avoid a crash if the field name is longer than MED_NAME_SIZE....
175 char * componentName = (char *) MED_NULL;
176 char * unitName = (char *) MED_NULL;
177 char meshName[MED_NAME_SIZE+1] ;
178 med_2_3::med_field_type type;
179 MESH_ENTITIES::const_iterator currentEntity;
180 list<MED_EN::medGeometryElement>::const_iterator currentGeometry;
181 med_2_3::med_int timeStepNumber = -1;
182 char timeStepUnit[MED_LNAME_SIZE+1] ;
183 double timeStep = 0.0;
184 med_2_3::med_int orderNumber = -1;
185 med_2_3::med_bool meshLink;
187 int numberOfFields = med_2_3::MEDnField(medIdt) ;
189 for (i=1;i<=numberOfFields;i++)
191 int numberOfComponents = med_2_3::MEDfieldnComponent(medIdt,i) ;
193 componentName = new char[numberOfComponents*MED_SNAME_SIZE+1] ;
194 unitName = new char[numberOfComponents*MED_SNAME_SIZE+1] ;
196 err = med_2_3::MEDfieldInfo(medIdt, i, fieldName, meshName, &meshLink, &type, componentName,
197 unitName, timeStepUnit, &nstepsp3) ;
198 delete[] componentName ;
201 MESSAGE_MED("Field #" << i << " is named " << fieldName);
203 if (err != MED_VALID)
204 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << ": can't get information about the field #" << i <<" of the file |" << _fileName << "| !"));
206 map< string, FIELD_DATA_ >::iterator name2data =
207 _fields.insert( make_pair( fieldName, FIELD_DATA_() )).first;
209 FIELD_DATA_& fieldData = name2data->second;
210 fieldData._type = MED_EN::med_type_champ( type );
212 // find all dtid of this field
213 set<DT_IT_, LT_DT_IT_> set_dtit;
215 for (j=1;j <= nstepsp3; j++)
217 err = med_2_3::MEDfieldComputingStepInfo(medIdt, fieldName, j, &timeStepNumber, &orderNumber, &timeStep);
219 if (err == MED_VALID) // we have found for (*currentEntity).first and (*currentGeometry)
222 if (timeStepNumber<0) timeStepNumber=-1 ;
223 dtIt.dt = timeStepNumber;
224 dtIt.it = orderNumber;
226 set_dtit.insert( dtIt );
229 fieldData._meshName = meshName;
230 fieldData._vec_dtit.assign( set_dtit.begin(), set_dtit.end() );
232 } // loop on numberOfFields
237 /*! \if MEDMEM_ug @} \endif */
240 \addtogroup MEDFILEBROWSER_query
245 //================================================================================
247 * \brief Returns the name of a browsed file
249 //================================================================================
251 std::string MEDFILEBROWSER::getFileName() const
256 //================================================================================
258 \brief Gets the number of meshes in med file.
260 //================================================================================
262 int MEDFILEBROWSER::getNumberOfMeshes ( void ) const
264 return _meshes.size();
267 //================================================================================
269 \brief Gets the number of fields in med file.
271 //================================================================================
273 int MEDFILEBROWSER::getNumberOfFields ( void ) const
275 return _fields.size(); // we get number of field with different name
278 //================================================================================
280 \brief Gets the names of all meshes.
282 meshNames is an in/out argument.
284 It is a string array of size the
285 number of meshes. It must be allocated before calling
286 this method. All names are put in it.
288 //================================================================================
290 void MEDFILEBROWSER::getMeshNames ( std::string * meshNames ) const
292 map< string, bool >::const_iterator name_isstruct = _meshes.begin();
293 for ( int i=0; name_isstruct != _meshes.end(); ++i, ++name_isstruct )
294 meshNames[i] = name_isstruct->first;
297 //================================================================================
299 \brief Gets the names of all MESH objects.
301 Returns a vector<string> object which contain the name of all MESH objects.
303 //================================================================================
305 vector<string> MEDFILEBROWSER::getMeshNames () const
307 vector<string> meshNames( _meshes.size() );
308 map< string, bool >::const_iterator name_isstruct = _meshes.begin();
309 for ( int i=0; name_isstruct != _meshes.end(); ++i, ++name_isstruct )
310 meshNames[i] = name_isstruct->first;
314 //================================================================================
316 \brief Gets the names of all fields.
318 fieldNames is an in/out argument.
320 It is an array of string of size the
321 number of fields. It must be allocated before calling
322 this method. All names are put in it.
324 //================================================================================
326 void MEDFILEBROWSER::getFieldNames ( std::string * fieldNames ) const
328 map< string, FIELD_DATA_ >::const_iterator name_data = _fields.begin();
329 for( int i = 0; name_data != _fields.end(); ++name_data, ++i )
330 fieldNames[i] = name_data->first;
333 //================================================================================
335 \brief Gets the names of all fields.
337 //================================================================================
339 vector<string> MEDFILEBROWSER::getFieldNames () const
341 vector<string> fieldNames( _fields.size() );
342 map< string, FIELD_DATA_ >::const_iterator name_data = _fields.begin();
343 for( int i = 0; name_data != _fields.end(); ++name_data, ++i )
344 fieldNames[i] = name_data->first;
348 //================================================================================
350 * \brief Return true if mesh \a meshName is a structured one
352 //================================================================================
354 bool MEDFILEBROWSER::isStructuredMesh(const std::string & meshName) const throw (MEDEXCEPTION)
356 const char* LOC="MEDFILEBROWSER::isStructuredMesh(meshName)";
358 map< std::string, bool >::const_iterator name_isstruct = _meshes.find(meshName);
359 if ( name_isstruct == _meshes.end() )
361 ( LOCALIZED( STRING(LOC)<< "There is no known mesh named |"<< meshName
362 << "| in file |" << _fileName <<"|"));
364 return name_isstruct->second;
367 //================================================================================
369 * \brief Return type of values of FIELD named \a fieldName
371 //================================================================================
373 med_type_champ MEDFILEBROWSER::getFieldType(const std::string & fieldName) const throw (MEDEXCEPTION)
375 const char* LOC="MEDFILEBROWSER::getFieldIteration (fieldName)";
377 std::map< std::string, FIELD_DATA_ >::const_iterator name_data = _fields.find(fieldName);
379 if ( name_data == _fields.end() )
381 ( LOCALIZED( STRING(LOC)<< "There is no known field named |"<< fieldName
382 << "| in file |" << _fileName <<"|"));
384 return name_data->second._type;
387 //================================================================================
389 * \brief Return name of mesh supporting the FIELD named \a fieldName
391 //================================================================================
393 std::string MEDFILEBROWSER::getMeshName (const std::string & fieldName) const throw (MEDEXCEPTION)
395 const char* LOC="MEDFILEBROWSER::getMeshName (fieldName)";
397 std::map< std::string, FIELD_DATA_ >::const_iterator name_data = _fields.find(fieldName);
399 if ( name_data == _fields.end() )
401 ( LOCALIZED( STRING(LOC)<< "There is no known field named |"<< fieldName
402 << "| in file |" << _fileName <<"|"));
404 return name_data->second._meshName;
407 //================================================================================
409 \brief Returns a vector<DT_IT_> which contain all iteration step for the FIELD
410 identified by its name.
414 typedef struct { int dt; int it; } DT_IT_;
417 \a dt represents the time iteration number, while \a it represents
418 the inner iteration number.
420 //================================================================================
422 vector<DT_IT_> MEDFILEBROWSER::getFieldIteration (const std::string & fieldName) const
423 throw (MED_EXCEPTION)
425 const char* LOC="MEDFILEBROWSER::getFieldIteration (fieldName)";
427 std::map< std::string, FIELD_DATA_ >::const_iterator name_data = _fields.find(fieldName);
429 if ( name_data == _fields.end() )
431 ( LOCALIZED( STRING(LOC)<< "There is no known field named |"<< fieldName
432 << "| in file |" << _fileName <<"|"));
434 return name_data->second._vec_dtit;
436 /*!\if MEDMEM_ug @} \endif */