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