Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDMEM / MEDMEM_MedMedDriver21.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "MEDMEM_Compatibility21_22.hxx"
23 #include "MEDMEM_MedMedDriver21.hxx"
24 #include "MEDMEM_MedMeshDriver.hxx"
25 #include "MEDMEM_DriversDef.hxx"
26
27 #include "MEDMEM_Mesh.hxx"
28 #include "MEDMEM_Grid.hxx"
29 #include "MEDMEM_Field.hxx"
30 // EN ATTENDANT L'utilisation de MedFieldDriver.hxx ds Field.hxx
31 #include "MEDMEM_MedFieldDriver.hxx"
32 #include "MEDMEM_Med.hxx"
33 #include "MEDMEM_define.hxx"
34
35 #include <string>
36
37 using namespace std;
38 using namespace MED_EN;
39 using namespace MEDMEM;
40
41 MED_MED_DRIVER21::MED_MED_DRIVER21(): _medIdt(MED_INVALID) 
42 {}
43
44 MED_MED_DRIVER21::MED_MED_DRIVER21(const string & fileName,  MED * const ptrMed):
45   MED_MED_DRIVER(fileName,ptrMed), _medIdt(MED_INVALID)
46 {
47   //_ptrMed->addDriver(*this); // The specific MED driver id is set within the addDriver method.
48 }
49
50 MED_MED_DRIVER21::MED_MED_DRIVER21(const string & fileName,
51                                    MED * const ptrMed,
52                                    MED_EN::med_mode_acces accessMode):
53   MED_MED_DRIVER(fileName,ptrMed,accessMode), _medIdt(MED_INVALID)
54 {
55 }
56 //REM :  As t'on besoin du champ _status :  _medIdt <-> _status  ?  Oui
57
58 MED_MED_DRIVER21::MED_MED_DRIVER21(const MED_MED_DRIVER21 & driver):
59   MED_MED_DRIVER(driver),_medIdt(driver._medIdt)
60 {
61 }
62
63 MED_MED_DRIVER21::~MED_MED_DRIVER21()
64 {
65   MESSAGE_MED("MED_MED_DRIVER21::~MED_MED_DRIVER21() has been destroyed");
66 }
67
68 void MED_MED_DRIVER21::read()
69 {
70 }
71 void MED_MED_DRIVER21::readFileStruct()
72 {
73 }
74 void MED_MED_DRIVER21::write() const
75 {
76 }
77 GENDRIVER * MED_MED_DRIVER21::copy(void) const
78 {
79   return new MED_MED_DRIVER21(*this) ;
80 }
81 void MED_MED_DRIVER21::writeFrom() const
82 {
83 }
84
85 void MED_MED_DRIVER21::open()
86   throw (MEDEXCEPTION)
87 {
88
89   const char * LOC ="MED_MED_DRIVER21::open() : ";
90   BEGIN_OF_MED(LOC);
91
92   // REFLECHIR SUR CE TEST PAR RAPPORT A L'OUVERTURE/FERMETURE
93 //    if ( _medIdt != MED_INVALID ) 
94 //      throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
95 //                                       << "_medIdt is already in use, please close the file |" 
96 //                                       << _fileName << "| before calling open()"
97 //                                       )
98 //                            );   
99   
100 //    if ( _status != MED_CLOSED ) 
101 //      throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
102 //                                       << "_status is closed, please close the file |"
103 //                                       << _fileName << "| before calling open()"
104 //                                       )
105 //                            );
106   
107   if ( _fileName == "" )
108     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
109                                      << "_fileName is |\"\"|, please set a correct fileName before calling open()"
110                                      )
111                           );
112   int accessMode = getMedAccessMode( _accessMode, MED_EN::V21 );
113   MESSAGE_MED(LOC<<"_fileName.c_str : "<< _fileName.c_str()<<",mode : "<< accessMode);
114   _medIdt = med_2_1::MEDouvrir( (const_cast <char *> (_fileName.c_str())), (med_2_1::med_mode_acces) accessMode);
115   MESSAGE_MED(LOC<<" _medIdt = "<<_medIdt);
116   
117   if (_medIdt > 0) 
118     _status=MED_OPENED; 
119   else {
120     _status = MED_CLOSED;
121     _medIdt = MED_INVALID;
122     throw MED_EXCEPTION (LOCALIZED( STRING(LOC) 
123                                     << "Can't open |"  << _fileName 
124                                     << "|, _medIdt : " << _medIdt
125                                     )
126                          );
127   }
128
129   END_OF_MED(LOC);
130 }
131
132
133 void MED_MED_DRIVER21::close()
134 {
135   med_2_1::med_int err = 0;
136   const char* LOC = "MED_MED_DRIVER21::close() : ";
137   BEGIN_OF_MED(LOC);
138   
139   
140 //    if ( _status == MED_CLOSED)
141 //      throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << ": the file |" 
142 //                                       << _fileName << "| is already closed"
143 //                                       )
144 //                            );
145    
146 //    if ( _medIdt == MED_INVALID ) 
147 //      throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "_medIdt invalid, but the file |" 
148 //                                       << _fileName << "| seems to be openned !"
149 //                                       )
150 //                            );   
151
152   if ( _medIdt != MED_INVALID )
153     err=med_2_1::MEDfermer(_medIdt);
154   
155 //    if (err != MED_VALID) 
156 //      throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "the file |" 
157 //                                       << _fileName << "| couldn't be closed"
158 //                                       )
159 //                            );   
160   
161   _status = MED_CLOSED;
162   _medIdt = MED_INVALID;
163     
164   END_OF_MED(LOC);
165 }
166
167
168 // ------------- Read Only Part --------------
169
170 MED_MED_RDONLY_DRIVER21::MED_MED_RDONLY_DRIVER21()
171 {
172   this->GENDRIVER::_accessMode = MED_EN::RDONLY;
173 }
174
175 MED_MED_RDONLY_DRIVER21::MED_MED_RDONLY_DRIVER21(const string & fileName,  MED * const ptrMed):
176   IMED_MED_RDONLY_DRIVER(fileName,ptrMed),
177   MED_MED_DRIVER21(fileName,ptrMed,MED_EN::RDONLY),
178   MED_MED_DRIVER(fileName,ptrMed,MED_EN::RDONLY)
179 {
180   MESSAGE_MED("MED_MED_RDONLY_DRIVER21::MED_MED_RDONLY_DRIVER21(const string & fileName,  MED * const ptrMed) Constructeur read only");
181 }
182
183 MED_MED_RDONLY_DRIVER21::MED_MED_RDONLY_DRIVER21(const MED_MED_RDONLY_DRIVER21 & driver):
184   IMED_MED_RDONLY_DRIVER(driver) ,MED_MED_DRIVER21(driver),MED_MED_DRIVER(driver)
185 {
186 }
187
188 MED_MED_RDONLY_DRIVER21::~MED_MED_RDONLY_DRIVER21()
189 {
190   MESSAGE_MED("MED_MED_RDONLY_DRIVER21::~MED_MED_RDONLY_DRIVER21() has been destroyed");
191
192
193 GENDRIVER * MED_MED_RDONLY_DRIVER21::copy(void) const
194 {
195   return new MED_MED_RDONLY_DRIVER21(*this) ;
196 }
197
198 void MED_MED_RDONLY_DRIVER21::write          ( void ) const throw (MEDEXCEPTION)
199 {
200   IMED_MED_RDONLY_DRIVER::write();
201 }
202
203 void MED_MED_RDONLY_DRIVER21::writeFrom      ( void ) const throw (MEDEXCEPTION)
204 {
205   IMED_MED_RDONLY_DRIVER::writeFrom();
206 }
207
208 void MED_MED_RDONLY_DRIVER21::readFileStruct( void ) 
209   throw (MEDEXCEPTION)
210 {
211   const char * LOC = "MED_MED_DRIVER21::readFileStruct() : ";
212   int          err,i,j;
213       
214   // PAL12192
215   if ( IMED_MED_RDONLY_DRIVER::_fileStructIsRead )
216     return;
217   else
218     IMED_MED_RDONLY_DRIVER::_fileStructIsRead = true;
219
220   BEGIN_OF_MED(LOC);
221
222   if ( _medIdt == MED_INVALID ) 
223     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
224                                      << "_medIdt is invalid, please open the file |" 
225                                      << _fileName << "| before calling readFileStruct()"
226                                      )
227                           );   
228   // Read number of meshes and their associated name
229   {
230     int          numberOfMeshes;
231     char         meshName[MED_TAILLE_NOM+1]="";
232     char         meshDescription[MED_TAILLE_DESC+1]="";
233     med_2_1::med_int          meshDim;
234     MESH *       ptrMesh;
235     //    MED_MESH_RDWR_DRIVER * ptrDriver; !! UNUSED VARIABLE !!
236     
237     numberOfMeshes = med_2_1::MEDnMaa(_medIdt) ;
238     if ( numberOfMeshes <= 0 ) 
239       MESSAGE_MED(LOC << "Be careful there is no mesh in file |"<<_fileName<<"| !");
240
241     MESH_ENTITIES::const_iterator currentEntity; 
242     for (i=1;i<=numberOfMeshes;i++) {
243
244       // find out if the mesh is a Grid
245       
246       med_2_1::med_int isAGrid = false;
247       med_2_1::med_grid_type type;
248       
249       err = MEDgridInfo (_medIdt, i, &isAGrid, &type);
250       if (err != MED_VALID) 
251         throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "error in MEDgridInfo()") );
252
253       err = med_2_1::MEDmaaInfo(_medIdt, i ,meshName, &meshDim) ;
254
255       strcpy(meshDescription,"Mesh from Med 2.2 format");
256
257       if (err != MED_VALID) 
258         throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << ": can't get information about the mesh n°"
259                                          << i <<" of the file |" << _fileName << "| !"
260                                          )
261                               );   
262       MESSAGE_MED(LOC<<": Mesh n°"<<i<<" nammed "<<meshName);
263
264       if (isAGrid)
265         ptrMesh = new GRID((MED_EN::med_grid_type) type);
266       else
267         ptrMesh = new MESH();
268
269       //MED_MESH_RDWR_DRIVER * _ptrDriver = new MED_MESH_RDWR_DRIVER(_fileName, ptrMesh);
270       MED_EN::med_mode_acces myMode = getAccessMode();
271 //       MED_MESH_DRIVER * ptrDriver ;
272 //       switch (myMode) {
273 //       case MED_EN::MED_LECT:
274 //      ptrDriver = new MED_MESH_RDONLY_DRIVER(_fileName, ptrMesh);
275 //      break ;
276 //       case MED_EN::MED_REMP: 
277 //      ptrDriver = new MED_MESH_RDWR_DRIVER(_fileName, ptrMesh);
278 //      break ;
279 //       case MED_EN::MED_ECRI: // should never append !!
280 //      ptrDriver = new MED_MESH_RDONLY_DRIVER(_fileName, ptrMesh);
281 //      break;
282 //       default:
283 //      throw MEDEXCEPTION(LOCALIZED(STRING(LOC) << "Bad file mode access !"));
284 //       }
285
286       GENDRIVER * ptrDriver =
287         DRIVERFACTORY::buildConcreteMedDriverForMesh(_fileName, ptrMesh,
288                                                      meshName, myMode, V21);
289
290       ptrDriver->setId       ( getId() );
291       ptrDriver->setMeshName ( meshName );
292       ptrMesh->setDescription(meshDescription);
293
294       SCRUTE_MED(ptrDriver);
295
296       ptrMesh->addDriver(*ptrDriver);
297       delete ptrDriver ;
298
299       if (isAGrid)
300         _ptrMed->_meshes[meshName] = (MESH *) ptrMesh;
301       else
302         _ptrMed->_meshes[meshName] = ptrMesh;
303
304       ptrMesh->setName(meshName);
305
306       // add by B. Secher for filter module
307       ptrMesh->setMeshDimension(meshDim);
308
309       SCRUTE_MED(ptrMesh);
310
311       MESSAGE_MED(LOC<<"is" << (isAGrid ? "" : " NOT") << " a GRID and its name is "<<ptrMesh->getName());
312
313       // we create all global support (for each entity type :
314       int index = 0;
315       for (currentEntity=meshEntities.begin();currentEntity != meshEntities.end(); currentEntity++) {
316         string supportName="SupportOnAll_" ;
317         supportName+=entNames[(*currentEntity).first] ;
318         //(_ptrMed->_support)[meshName][(MED_EN::medEntityMesh)(*currentEntity).first]=new SUPPORT(ptrMesh,supportName,(MED_EN::medEntityMesh) (*currentEntity).first) ;
319         SUPPORT* mySupport = new SUPPORT() ;
320         mySupport->setName(supportName);
321         mySupport->setMesh(ptrMesh);
322         mySupport->setEntity((MED_EN::medEntityMesh) (*currentEntity).first);
323         mySupport->setAll(true);
324         (_ptrMed->_support)[meshName][(MED_EN::medEntityMesh)(*currentEntity).first] = mySupport ;
325         MESSAGE_MED(LOC<< "The support " << supportName.c_str() << " on entity " << (*currentEntity).first << " is built");
326         index++;
327       }
328       MESSAGE_MED(LOC <<"The mesh " <<ptrMesh->getName() << " has " << index << " support(s)");
329     }
330
331     map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> >::const_iterator const_itSupportOnMesh ;
332
333     int index = 0;
334
335     vector<SUPPORT *> vectSupp;
336     for (const_itSupportOnMesh=_ptrMed->_support.begin(); const_itSupportOnMesh != _ptrMed->_support.end();
337          const_itSupportOnMesh++ )
338       {
339         map<MED_EN::medEntityMesh,SUPPORT *>::const_iterator const_itSupport ;
340         for (const_itSupport=(*const_itSupportOnMesh).second.begin();
341              const_itSupport!=(*const_itSupportOnMesh).second.end();const_itSupport++) index++;
342       }
343
344     MESSAGE_MED(LOC << "In this MED object there is(are) " << index << " support(s):");
345
346     vectSupp.resize(index);
347
348     index = 0;
349     for (const_itSupportOnMesh=_ptrMed->_support.begin(); const_itSupportOnMesh != _ptrMed->_support.end();
350          const_itSupportOnMesh++ )
351       {
352         map<MED_EN::medEntityMesh,SUPPORT *>::const_iterator const_itSupport ;
353         for (const_itSupport=(*const_itSupportOnMesh).second.begin();
354              const_itSupport!=(*const_itSupportOnMesh).second.end();const_itSupport++)
355           {
356             vectSupp[index] = (*const_itSupport).second;
357             SCRUTE_MED(vectSupp[index]);
358             // comment the next line for PAL19767 Modification for icc and icpc (INTEL compilators)
359             //MESSAGE_MED(STRING(LOC) << "Support number " << index << " is "<< *vectSupp[index]));
360             index++;
361           }
362       }
363
364   }
365   
366   
367   // Read number of fields, their associated name and their list of (timeStepNumber,iterationNumber)
368   {
369     int                           numberOfFields              = 0;      //MED_INVALID
370     //    char                          fieldName[MED_TAILLE_NOM+1] = "";
371     //    char                          fieldName[MED_TAILLE_NOM+1] ;
372     char                          fieldName[MED_TAILLE_LNOM+1] ; //SRN: to avoid a crash if the field name is longer than MED_TAILLE_NOM
373     int                           numberOfComponents           = 0;
374     char                          * componentName              = (char *) MED_NULL;
375     char                          * unitName                   =  (char *) MED_NULL;
376     //    char                          meshName[MED_TAILLE_NOM+1]  = "";
377     char                          meshName[MED_TAILLE_NOM+1]  ;
378     med_2_1::med_type_champ        type;
379     MESH                          * ptrMesh                        = (MESH  *) MED_NULL;
380     FIELD_                        * ptrField                       = (FIELD_ *) MED_NULL;
381     //MED_FIELD_RDWR_DRIVER         * ptrDriver                      = (MED_FIELD_RDWR_DRIVER * ) MED_NULL;
382     GENDRIVER                     * ptrDriver                      = (GENDRIVER * ) MED_NULL;
383     SUPPORT                       * ptrSupport                     = (SUPPORT *   ) MED_NULL;
384     MESH_ENTITIES::const_iterator currentEntity; 
385     list<MED_EN::medGeometryElement>::const_iterator currentGeometry;
386     med_2_1::med_int                           NbOfGaussPts                 =  0;
387     int                           numberOfTimeSteps            =  -1;
388     med_2_1::med_int                           timeStepNumber               =  -1;
389     //    char                          timeStepUnit[MED_TAILLE_PNOM]= "";
390     char                          timeStepUnit[MED_TAILLE_PNOM21+1] ;
391     double                        timeStep                     = 0.0;
392     med_2_1::med_int                           orderNumber                  =  -1;                           //???init?????
393     map<MESH_NAME_,MESH*>      & _meshes   =  _ptrMed->_meshes; 
394     map<FIELD_NAME_,MAP_DT_IT_> & _fields   =  _ptrMed->_fields; 
395     map<FIELD_ *, MESH_NAME_>  & _meshName =  _ptrMed->_meshName; 
396     map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> > & support = _ptrMed->_support;
397
398     
399     numberOfFields = med_2_1::MEDnChamp(_medIdt,0) ;
400     if ( numberOfFields <= 0 ) 
401       MESSAGE_MED(LOC << "Be careful there is no field in file |"<<_fileName<<"| !");
402   
403     for (i=1;i<=numberOfFields;i++) {
404
405       numberOfComponents = med_2_1::MEDnChamp(_medIdt,i) ;
406       if ( numberOfComponents <= 0 ) 
407         if (err != MED_VALID) 
408           throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) <<  "Be careful there is no compound for field n°" 
409                                            << i << "in file |"<<_fileName<<"| !"));
410         
411       componentName = new char[numberOfComponents*MED_TAILLE_PNOM21+1] ;
412       unitName      = new char[numberOfComponents*MED_TAILLE_PNOM21+1] ;   
413       
414       err = MEDchampInfo(_medIdt, i, fieldName, &type, componentName, 
415                          unitName, numberOfComponents) ;
416
417       if (err != MED_VALID) 
418         throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
419                                          << ": can't get information about the field n°"
420                                          << i <<" of the file |" << _fileName << "| !")); 
421       
422       MESSAGE_MED(LOC << "Field n°"<<i<<" nammed "<< fieldName 
423               << " ,component(s)  : " << componentName 
424               <<" ,unit(s) : "        << unitName);
425       
426
427       // Loop on all (entity type,geometry type) until you find an existing one then
428       // get the (n°dt,n°it) pairs list for the current (field,entity type,geometry type)
429       // We suppose there is the same list whatever the existing (entity type,geometry type) pair  
430       // support map :
431       for (currentEntity=meshEntities.begin();currentEntity != meshEntities.end(); currentEntity++) { 
432         //         numberOfTimeSteps  MUST be given by MEDchampInfo !!!!!
433         for (currentGeometry  = (*currentEntity).second.begin();currentGeometry != (*currentEntity).second.end(); currentGeometry++) {
434           MESSAGE_MED("Field information with Entity,Geom = "<<(*currentEntity).first<<","<<(*currentGeometry));
435           numberOfTimeSteps = MEDnPasdetemps(_medIdt, fieldName,
436                                              (med_2_1::med_entite_maillage)(*currentEntity).first,
437                                              (med_2_1::med_geometrie_element) (*currentGeometry) );
438           MESSAGE_MED("Field information 2 : NumberOfTimeStep :"<<numberOfTimeSteps);
439           if ( numberOfTimeSteps > MED_VALID ) 
440             break ; // There are value for some med_geometrie_element of this med_entite_maillage.
441         }
442         if (numberOfTimeSteps>0) // we have at least one
443           
444           for (currentGeometry  = (*currentEntity).second.begin();currentGeometry != (*currentEntity).second.end(); currentGeometry++) {            
445             
446             MESSAGE_MED("Field information 3 : Geom : "<<(*currentGeometry));
447             for (j=1;j <= numberOfTimeSteps; j++) {
448               
449               MESSAGE_MED("Field information 4 : time step j = "<<j);
450               err = MEDpasdetempsInfo( _medIdt, fieldName,
451                                        (med_2_1::med_entite_maillage) (*currentEntity).first, 
452                                        (med_2_1::med_geometrie_element) (*currentGeometry),j, 
453                                        meshName, &NbOfGaussPts,
454                                        &timeStepNumber, timeStepUnit, &timeStep,
455                                        &orderNumber);
456               if (err == MED_VALID) { // we have found for (*currentEntity).first and (*currentGeometry)
457                 
458                 MESSAGE_MED("Field information 5 ;: NumberOfGaussPoint : "<<NbOfGaussPts<<", timeStepNumber : "<<timeStepNumber);
459                 // CORRECT a bug in MEDpasdetempsInfo :
460                 // we get a value n'importe quoi in NbOfGaussPts !!!!
461                 
462                 if (NbOfGaussPts>100)
463                   NbOfGaussPts=1 ;
464                 if (timeStepNumber<0)
465                   timeStepNumber=-1 ;
466                 
467                 
468                 // ATTENTION TRAITER L'EXCEPTION !!!!!!!!!!!!
469                 
470                 // Il faudra traiter le cas d'un champ qui utilise +sieurs (entity,geom) voir le travail de patrick
471                 // Il faudra traiter le cas des profils...
472                 //             ptrField = new FIELD();
473                 //             _ptrDriver = new MED_FIELD_RDWR_DRIVER(_fileName, ptrField);
474                 //             ptrField->addDriver(_ptrDriver);
475                 //             _fields[fieldName]=ptrField;
476                 
477                 // Verify meshName is already known
478                 
479                 map<MESH_NAME_,MESH*>::iterator _meshesIt = _meshes.find(meshName);
480                 if ( _meshesIt == _meshes.end() ) {
481                   MESSAGE_MED(LOC << "There is no mesh |"
482                           << meshName                            <<"| in the file |"
483                           << _fileName <<"|, but  |" << meshName <<"| is referenced by field |"
484                           << fieldName                           <<"|, entity : |"
485                           << entNames [(*currentEntity).first] <<"|, geometric element of type |" 
486                           << geoNames [(*currentGeometry)]     <<"|" 
487                           ); 
488                 }  // POURQUOI SI JE NE MET PAS DE BLOCK J'AI UN PARSE ERROR : PG : c'est la macro MESSAGE_MED qui fait ca !
489                 else 
490                   ptrMesh = _meshes[meshName];
491                 
492                 ptrSupport     =  support[meshName][(MED_EN::medEntityMesh) (*currentEntity).first];
493 //              if (NbOfGaussPts != 1)
494 //                throw MEDEXCEPTION(LOCALIZED( STRING(LOC) <<"Number of Gauss Point must be equal to 1 for instance")) ;
495                 
496                 // init to null to prevent some error if not correctly allocated !
497                 ptrField = (FIELD_*)NULL ;
498 //              ptrDriver = (GENDRIVER*)NULL ;
499
500                 switch ( type) {
501                 case med_2_1::MED_INT64 :
502                   if ( sizeof(med_2_1::med_int) != 8 )
503                     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) <<
504                                                      " The Field type of |"
505                                                      << fieldName                         <<"|, entity : |"
506                                                      << entNames [(*currentEntity).first] <<"|, geometric element of type |" 
507                                                      << geoNames [(*currentGeometry) ]    <<
508                                                      "| is  MED_INT64 but size of med_int is not equal to 8 bytes !" 
509                                                      ) 
510                                           );
511                   break;
512                 case med_2_1::MED_INT32 : {
513 //                throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) <<
514 //                                                 "NOT IMPLEMENTED : BUG IN STL !")
515 //                                      ) ;
516                   // ptrField       =  new FIELD<med_2_1::med_int>   ( ptrSupport,numberOfComponents );   // Les valeurs du champ ne doivent pas Ãªtre lue pour l'instant
517
518                   ptrField       =  new FIELD<int> ( );   // Les valeurs du champ ne doivent pas Ãªtre lue pour l'instant
519                   ((FIELD<int>*) ptrField)->setSupport(ptrSupport);
520                   ((FIELD<int>*) ptrField)->setNumberOfComponents(numberOfComponents);
521                   ((FIELD<int>*) ptrField)->setName(fieldName) ; //provisoire, pour debug
522                   MESSAGE_MED("#### SET NAME in FIELD : "<<fieldName);
523
524                   MED_EN::med_mode_acces myMode = getAccessMode();
525 //                switch (myMode) {
526 //                case MED_EN::MED_LECT:
527 //                  ptrDriver = new MED_FIELD_RDONLY_DRIVER<int>(_fileName, (FIELD<int> *)ptrField);
528 //                  break ;
529 //                case MED_EN::MED_REMP:        
530 //                  ptrDriver = new MED_FIELD_RDWR_DRIVER<int>(_fileName, (FIELD<int> *)ptrField);
531 //                  break ;
532 //                case MED_EN::MED_ECRI: // should never append !!
533 //                  ptrDriver = new MED_FIELD_RDONLY_DRIVER<int>(_fileName, (FIELD<int> *)ptrField);
534 //                  break;
535 //                default:
536 //                  throw MEDEXCEPTION(LOCALIZED(STRING(LOC) << "Bad file mode access !"));
537 //                }
538
539                   ptrDriver = DRIVERFACTORY::buildConcreteMedDriverForField<int>(_fileName, (FIELD<int> *)ptrField, myMode, V21);
540
541                   SCRUTE_MED(ptrDriver);
542
543                   break;
544                 }
545                 case med_2_1::MED_REEL64 : {
546                   //              ptrField       =  new FIELD<med_2_1::med_float> ( ptrSupport,numberOfComponents );   // Les valeurs du champ ne doivent pas Ãªtre lue pour l'instant
547                   ptrField       =  new FIELD<double> ( );   // Les valeurs du champ ne doivent pas Ãªtre lue pour l'instant
548                   ((FIELD<med_2_1::med_float>*) ptrField)->setSupport(ptrSupport);
549                   ((FIELD<med_2_1::med_float>*) ptrField)->setNumberOfComponents(numberOfComponents);
550                   ((FIELD<med_2_1::med_float>*) ptrField)->setName(fieldName) ; //provisoire, pour debug
551                   MESSAGE_MED("#### SET NAME in FIELD : "<<fieldName);
552
553                   MED_EN::med_mode_acces myMode = getAccessMode();
554 //                switch (myMode) {
555 //                case MED_EN::MED_LECT:
556 //                  ptrDriver = new MED_FIELD_RDONLY_DRIVER<med_2_1::med_float>(_fileName, (FIELD<med_2_1::med_float> *)ptrField);
557 //                  break ;
558 //                case MED_EN::MED_REMP:        
559 //                  ptrDriver = new MED_FIELD_RDWR_DRIVER<med_2_1::med_float>(_fileName, (FIELD<med_2_1::med_float> *)ptrField);
560 //                  break ;
561 //                case MED_EN::MED_ECRI: // should never append !!
562 //                  ptrDriver = new MED_FIELD_RDONLY_DRIVER<med_2_1::med_float>(_fileName, (FIELD<med_2_1::med_float> *)ptrField);
563 //                  break;
564 //                default:
565 //                  throw MEDEXCEPTION(LOCALIZED(STRING(LOC) << "Bad file mode access !"));
566 //                }
567
568                   ptrDriver = DRIVERFACTORY::buildConcreteMedDriverForField<med_2_1::med_float>(_fileName, (FIELD<med_2_1::med_float> *)ptrField, myMode, V21);
569                   SCRUTE_MED(ptrDriver);
570
571                   break;
572                 }
573                 default : {
574                   if ( numberOfTimeSteps > 1) 
575                     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) <<
576                                                      " The Field type of |"
577                                                      << fieldName                         <<"|, entity : |"
578                                                      << entNames [(*currentEntity).first] 
579                                                      <<"|, geometric element of type |" 
580                                                      << geoNames [(*currentGeometry)]     
581                                                      <<"| is neither MED_INT, MED_INT32, MED_INT64 nor MED_REEL64 !" 
582                                                      ) 
583                                           );
584                   break ;
585                 }
586                 }
587                 
588                 MESSAGE_MED("timeStepNumber :"<<timeStepNumber<<",orderNumber :"<<orderNumber);
589                 ptrField->setIterationNumber ( timeStepNumber);      // A ajouter dans la classe FIELD
590                 ptrField->setOrderNumber     ( orderNumber); 
591                 ptrField->setTime            ( timeStep); 
592                 
593                 // Create a driver for this (field n°dt,n°it)
594                 ptrDriver->setId            ( getId() );
595                 MESSAGE_MED("###### ptrDriver->setFieldName : #"<<fieldName<<"#");
596                 ptrDriver->setFieldName(fieldName);
597                 ptrField->addDriver(*ptrDriver);
598                 // driver is duplicated : remove it
599                 delete ptrDriver;
600
601                 DT_IT_ dtIt;
602                 dtIt.dt  = timeStepNumber;
603                 dtIt.it  = orderNumber;
604                 
605                 (_fields  [fieldName])[dtIt] = ptrField;
606                 _meshName[ptrField ]       = meshName;
607               }
608             }
609           }
610       }
611       delete[] componentName ;
612       delete[] unitName ;
613     }
614   }
615   
616   // read profil count and their names
617   //  int support_count_= 0 ; !! UNUSED VARIABLE !!
618   // il faut lire les champs pour avoir les profils stockes !!!
619   // il faudrait implémenter la lecture des profils dans med !!!
620   
621   END_OF_MED(LOC);
622   
623 }
624
625 // This method ask the drivers of all MESH/FIELD objects created from this MED driver
626 // to read themselves
627 void MED_MED_RDONLY_DRIVER21::read( void )
628   throw (MEDEXCEPTION) // from objects method read !
629 {
630   const char* LOC = "MED_MED_DRIVER21::read() : ";
631   BEGIN_OF_MED(LOC);
632
633   // For PAL12192: assure that file structure is already read
634   this->open();
635   this->readFileStruct();
636   this->close();
637
638   const map<MESH_NAME_, MESH*> & _meshes = const_cast<const map<MESH_NAME_, MESH*>& > (_ptrMed->_meshes); 
639   map<MESH_NAME_,MESH*>::const_iterator  currentMesh;
640
641   const map<FIELD_ *, MESH_NAME_> & _meshName = const_cast<const map<FIELD_ *, MESH_NAME_>& > (_ptrMed->_meshName);
642   map<FIELD_ *, MESH_NAME_>::const_iterator currentField;
643   
644   for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) 
645     (*currentMesh).second->read(*this); 
646   //(*currentMesh).second->read(); // default reader, from readFileStruct
647     
648   // PROVISOIRE
649   _ptrMed->updateSupport() ;
650
651   for ( currentField =_meshName.begin(); currentField != _meshName.end(); currentField++ )
652     (*currentField).first->read(*this);
653   //(*currentField).first->read(); // default reader, from readFileStruct
654
655   END_OF_MED(LOC);
656 }
657
658 // ------------- Write Only Part --------------
659
660 MED_MED_WRONLY_DRIVER21::MED_MED_WRONLY_DRIVER21()
661 {
662   this->GENDRIVER::_accessMode = MED_EN::WRONLY;
663 }
664
665 MED_MED_WRONLY_DRIVER21::MED_MED_WRONLY_DRIVER21(const string & fileName,  MED * const ptrMed):
666   IMED_MED_WRONLY_DRIVER(fileName,ptrMed),
667   MED_MED_DRIVER21(fileName,ptrMed),
668   MED_MED_DRIVER(fileName,ptrMed,MED_EN::WRONLY)
669 {}
670
671 MED_MED_WRONLY_DRIVER21::MED_MED_WRONLY_DRIVER21(const MED_MED_WRONLY_DRIVER21 & driver):
672   IMED_MED_WRONLY_DRIVER(driver),MED_MED_DRIVER(driver),MED_MED_DRIVER21(driver)
673 {}
674
675 MED_MED_WRONLY_DRIVER21::~MED_MED_WRONLY_DRIVER21()
676 {
677   MESSAGE_MED("MED_MED_WRONLY_DRIVER21::~MED_MED_WRONLY_DRIVER21() has been destroyed");
678
679
680 GENDRIVER * MED_MED_WRONLY_DRIVER21::copy(void) const
681 {
682   return new MED_MED_WRONLY_DRIVER21(*this) ;
683 }
684
685 void MED_MED_WRONLY_DRIVER21::read           ( void ) throw (MEDEXCEPTION)
686 {
687   IMED_MED_WRONLY_DRIVER::read();
688 }
689
690 void MED_MED_WRONLY_DRIVER21::readFileStruct ( void ) throw (MEDEXCEPTION)
691 {
692   IMED_MED_WRONLY_DRIVER::readFileStruct();
693 }
694
695 // This method ask the drivers of all MESH/FIELD objects created from this MED driver
696 // to write themselves
697 void MED_MED_WRONLY_DRIVER21::writeFrom( void) const
698   throw (MEDEXCEPTION) //from object method write !
699 {
700   const char* LOC = "MED_MED_DRIVER21::writeFrom() : ";
701   BEGIN_OF_MED(LOC);
702
703   const map<MESH_NAME_, MESH*> & _meshes = const_cast<const map<MESH_NAME_, MESH*>& > (_ptrMed->_meshes); 
704   map<MESH_NAME_,MESH*>::const_iterator  currentMesh;
705
706   const map<FIELD_ *, MESH_NAME_> & _meshName = const_cast<const map<FIELD_ *, MESH_NAME_>& > (_ptrMed->_meshName);
707   map<FIELD_ *, MESH_NAME_>::const_iterator currentField;
708   
709   for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
710     try {
711       (*currentMesh).second->write(*this); 
712       // On utilise pour les objects MESH ET FIELD le write(GENDRIVER *) et le == ds GENDRIVER avec eventuellement 1 id 
713     }
714     catch ( const MED_DRIVER_NOT_FOUND_EXCEPTION & ) {
715       continue;
716     }
717   }
718
719   for ( currentField=_meshName.begin();currentField != _meshName.end(); currentField++ ) {
720     try {
721       (*currentField).first->write(*this);
722     }
723     catch ( const MED_DRIVER_NOT_FOUND_EXCEPTION & ) {
724       continue;
725     }
726   }
727
728   END_OF_MED(LOC);
729
730 }
731
732 void MED_MED_WRONLY_DRIVER21::write(void ) const
733   throw (MEDEXCEPTION) // from object method write !
734 {
735   int current;
736   const char* LOC = "MED_MED_DRIVER21::write() : ";
737   BEGIN_OF_MED(LOC);
738
739   // BCLE SUR LES OBJETS AVEC AJOUT DE DRIVER ET APPELS write
740
741   const map<MESH_NAME_, MESH*> & _meshes = const_cast<const map<MESH_NAME_, MESH*>& > (_ptrMed->_meshes); 
742   map<MESH_NAME_,MESH*>::const_iterator  currentMesh;
743
744   const map<FIELD_ *, MESH_NAME_> & _meshName = const_cast<const map<FIELD_ *, MESH_NAME_>& > (_ptrMed->_meshName);
745   map<FIELD_ *, MESH_NAME_>::const_iterator currentField;
746   
747   for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
748     //current = (*currentMesh).second->addDriver(MED_DRIVER,_fileName);
749     current = (*currentMesh).second->addDriver(MED_DRIVER,_fileName,(*currentMesh).second->getName());
750     // put right _id in Mesh driver (same as this._id)
751     (*currentMesh).second->_drivers[current]->setId( getId() );
752     (*currentMesh).second->write(current) ;
753   }
754
755   for ( currentField=_meshName.begin();currentField != _meshName.end(); currentField++ ) {
756     //current = (*currentField).first->addDriver(MED_DRIVER,_fileName);
757     current = (*currentField).first->addDriver(MED_DRIVER,_fileName,(*currentField).first->getName());
758     // put right _id in Field driver (same as this._id)
759     (*currentField).first->_drivers[current]->setId( getId() );
760     (*currentField).first->write(current) ;
761   }
762
763   // that's work, but it is more efficenty to write directly when we had driver, no ?
764   //  writeFrom();
765   
766   END_OF_MED(LOC);
767
768 }
769
770 // ------------- Read Write Part --------------
771
772 MED_MED_RDWR_DRIVER21::MED_MED_RDWR_DRIVER21()
773 {
774   this->GENDRIVER::_accessMode = MED_EN::RDWR;
775 }
776
777 MED_MED_RDWR_DRIVER21::MED_MED_RDWR_DRIVER21(const string & fileName,  MED * const ptrMed):
778   MED_MED_RDONLY_DRIVER21(fileName,ptrMed),
779   MED_MED_WRONLY_DRIVER21(fileName,ptrMed),
780   IMED_MED_RDWR_DRIVER(fileName,ptrMed),
781   MED_MED_DRIVER21(fileName,ptrMed,RDWR),
782   IMED_MED_WRONLY_DRIVER(fileName,ptrMed),
783   IMED_MED_RDONLY_DRIVER(fileName,ptrMed),
784   MED_MED_DRIVER(fileName,ptrMed,RDWR)
785 {}
786
787 MED_MED_RDWR_DRIVER21::MED_MED_RDWR_DRIVER21(const MED_MED_RDWR_DRIVER21 & driver):
788   MED_MED_RDONLY_DRIVER21(driver),
789   MED_MED_WRONLY_DRIVER21(driver),
790   IMED_MED_RDWR_DRIVER(driver),
791   MED_MED_DRIVER21(driver),
792   IMED_MED_WRONLY_DRIVER(driver),
793   IMED_MED_RDONLY_DRIVER(driver),
794   MED_MED_DRIVER(driver)
795 {}
796
797 MED_MED_RDWR_DRIVER21::~MED_MED_RDWR_DRIVER21() { 
798   MESSAGE_MED("MED_MED_RDWR_DRIVER21::~MED_MED_RDWR_DRIVER21() has been destroyed");
799 }
800
801 GENDRIVER * MED_MED_RDWR_DRIVER21::copy(void) const
802 {
803   return new MED_MED_RDWR_DRIVER21(*this) ;
804 }
805
806 void MED_MED_RDWR_DRIVER21::read(void)
807   throw (MEDEXCEPTION) // from MED_MED_RDONLY_DRIVER::read()
808 {
809   const char* LOC = "MED_MED_RDWR_DRIVER21::read(void)";
810   BEGIN_OF_MED(LOC);
811   MED_MED_RDONLY_DRIVER21::read();
812   END_OF_MED(LOC);
813 }
814
815 void MED_MED_RDWR_DRIVER21::readFileStruct(void)
816   throw (MEDEXCEPTION) // from MED_MED_RDONLY_DRIVER::readFileStruct()
817 {
818   const char* LOC = "MED_MED_RDWR_DRIVER21::readFileStruct(void)";
819   BEGIN_OF_MED(LOC);
820   MED_MED_RDONLY_DRIVER21::readFileStruct();
821   END_OF_MED(LOC);
822 }
823
824 void MED_MED_RDWR_DRIVER21::write(void) const
825   throw (MEDEXCEPTION) // from MED_MED_WRONLY_DRIVER::write()
826 {
827   const char* LOC = "MED_MED_RDWR_DRIVER21::write(void) const";
828   BEGIN_OF_MED(LOC);
829   MED_MED_WRONLY_DRIVER21::write();
830   END_OF_MED(LOC);
831 }
832
833 void MED_MED_RDWR_DRIVER21::writeFrom(void) const
834   throw (MEDEXCEPTION) // from MED_MED_WRONLY_DRIVER::writeFrom();
835 {
836   const char* LOC = "MED_MED_RDWR_DRIVER21::writeFrom(void) const";
837   BEGIN_OF_MED(LOC);
838   MED_MED_WRONLY_DRIVER21::writeFrom();
839   END_OF_MED(LOC);
840 }