Salome HOME
Final version of the V2_2_0 in the main trunk of the CVS tree.
[modules/med.git] / src / MEDMEM / MEDMEM_MedFieldDriver.hxx
1 #ifndef MED_FIELD_DRIVER_HXX
2 #define MED_FIELD_DRIVER_HXX
3
4 #include <string>
5
6 #include "MEDMEM_define.hxx"
7
8 #include "MEDMEM_GenDriver.hxx"
9 #include "utilities.h"
10
11 #include "MEDMEM_STRING.hxx"
12 #include "MEDMEM_Exception.hxx"
13 #include "MEDMEM_Unit.hxx"
14 #include "MEDMEM_Array.hxx"
15 #include "MEDMEM_Support.hxx"
16 //#include "MEDMEM_Field.hxx"
17 #include "MEDMEM_Mesh.hxx"
18
19 namespace MEDMEM {
20 template <class T> class FIELD;
21
22 // A QD LA CLASSE MED_ALL_ELEMENTS_DRIVER.... :) pour mutualiser le open ..... avec led _medIdt...
23
24 /*!
25
26   Driver Med for FIELD.
27
28   Generic part : implement open and close methods.
29
30 */
31
32 template <class T> class MED_FIELD_DRIVER : public GENDRIVER
33 {
34 protected:
35   
36   FIELD<T> *     _ptrField;
37   med_2_1::med_idt        _medIdt;
38   string         _fieldName;
39   int            _fieldNum;
40  
41   void search_field() ;
42   
43 public :
44
45   // all MED cell type ?? Classe de Définition ??
46   //   static const medGeometryElement all_cell_type[MED_NBR_GEOMETRIE_MAILLE];
47   
48   //   static const char * const all_cell_type_tab [MED_NBR_GEOMETRIE_MAILLE];
49   
50   /*!
51     Constructor.
52   */
53   MED_FIELD_DRIVER():GENDRIVER(),
54                      _ptrField((FIELD<T> *)MED_NULL),_medIdt(MED_INVALID),
55                      _fieldName(""),_fieldNum(MED_INVALID) {}
56   /*!
57     Constructor.
58   */
59   MED_FIELD_DRIVER(const string & fileName, FIELD<T> * ptrField, 
60                    MED_EN::med_mode_acces accessMode)
61     : GENDRIVER(fileName,accessMode),
62       _ptrField((FIELD<T> *) ptrField),_medIdt(MED_INVALID), 
63       _fieldName(fileName),_fieldNum(MED_INVALID) 
64   {
65   }
66
67   /*!
68     Copy constructor.
69   */
70   MED_FIELD_DRIVER(const MED_FIELD_DRIVER & fieldDriver):
71     GENDRIVER(fieldDriver),
72     _ptrField(fieldDriver._ptrField),
73     _medIdt(MED_INVALID),
74     _fieldName(fieldDriver._fieldName),
75     _fieldNum(fieldDriver._fieldNum) 
76   {
77   }
78
79   /*!
80     Destructor.
81   */
82   virtual ~MED_FIELD_DRIVER() { 
83   }
84
85   void open() throw (MEDEXCEPTION)
86   {
87     const char * LOC = "MED_FIELD_DRIVER::open() ";
88     BEGIN_OF(LOC);
89
90     // we must set fieldname before open, because we must find field number in file (if it exist !!!)
91     if ( _fileName == "" )
92       throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
93                                        << "_fileName is |\"\"|, please set a correct fileName before calling open()"
94                                        )
95                             );
96
97     MESSAGE(LOC<<"_fileName.c_str : "<< _fileName.c_str()<<",mode : "<< _accessMode);
98     _medIdt = med_2_1::MEDouvrir( (const_cast <char *> (_fileName.c_str())),(med_2_1::med_mode_acces) _accessMode);
99     MESSAGE(LOC<<"_medIdt : "<< _medIdt );
100     if (_medIdt > 0) 
101       _status=MED_OPENED;
102     else {
103       _status = MED_INVALID;
104       _medIdt = MED_INVALID;
105       throw MED_EXCEPTION (LOCALIZED( STRING(LOC) 
106                                       << "Can't open |"  << _fileName 
107                                       << "|, _medIdt : " << _medIdt
108                                       )
109                            );
110     }
111
112     END_OF(LOC);
113   }
114   
115   void close() {
116     BEGIN_OF("MED_FIELD_DRIVER::close()");
117     med_2_1::med_int err = 0;
118     if (_status == MED_OPENED) {
119       err=med_2_1::MEDfermer(_medIdt);
120       //H5close(); // If we call H5close() all the files are closed.
121       _status = MED_CLOSED;
122       _medIdt = MED_INVALID;
123       MESSAGE(" MED_FIELD_DRIVER::close() : MEDfermer : _medIdt= " << _medIdt );
124       MESSAGE(" MED_FIELD_DRIVER::close() : MEDfermer : err    = " << err );
125     }
126     END_OF("MED_FIELD_DRIVER::close()");
127   }
128
129   virtual void write( void ) const = 0 ;
130   virtual void read ( void ) = 0 ;
131
132   /*!
133     Set the name of the FIELD asked in file.
134
135     It could be different than the name of the FIELD object.
136   */
137   void   setFieldName(const string & fieldName) ;
138   /*!
139     Get the name of the FIELD asked in file.
140   */
141   string getFieldName() const ;
142
143 private:
144   virtual GENDRIVER * copy ( void ) const = 0 ;
145
146 };
147
148 /*!
149
150   Driver Med for FIELD : Read only.
151
152   Implement read method.
153
154 */
155
156 template <class T> class MED_FIELD_RDONLY_DRIVER : public virtual MED_FIELD_DRIVER<T>
157 {
158  
159 public :
160   
161   /*!
162     Constructor.
163   */
164   MED_FIELD_RDONLY_DRIVER():MED_FIELD_DRIVER<T>() {};
165   
166   /*!
167     Constructor.
168   */
169   MED_FIELD_RDONLY_DRIVER(const string & fileName,  FIELD<T> * ptrField):
170     MED_FIELD_DRIVER<T>(fileName,ptrField,MED_RDONLY) { 
171     BEGIN_OF("MED_FIELD_RDONLY_DRIVER::MED_FIELD_RDONLY_DRIVER(const string & fileName, const FIELD<T> * ptrField)");
172     END_OF("MED_FIELD_RDONLY_DRIVER::MED_FIELD_RDONLY_DRIVER(const string & fileName, const FIELD<T> * ptrField)");
173   }
174   
175   /*!
176     Copy constructor.
177   */
178   MED_FIELD_RDONLY_DRIVER(const MED_FIELD_RDONLY_DRIVER & fieldDriver):
179     MED_FIELD_DRIVER<T>(fieldDriver) {};
180   
181   /*!
182     Destructor.
183   */
184   virtual ~MED_FIELD_RDONLY_DRIVER() {};
185
186   // CREER UNE METHODE POUR LIRE LA LISTE DES MAILLAGES .....
187
188   /*!
189     Return a MEDEXCEPTION : it is the read-only driver.
190   */
191   void write( void ) const throw (MEDEXCEPTION) ;
192   /*!
193     Read FIELD in the specified file.
194   */
195   void read ( void ) throw (MEDEXCEPTION) ;
196
197 private:
198   GENDRIVER * copy( void ) const ;
199
200 };
201
202 /*!
203
204   Driver Med for FIELD : Write only.
205
206   Implement write method.
207
208 */
209
210 template <class T> class MED_FIELD_WRONLY_DRIVER : public virtual MED_FIELD_DRIVER<T> {
211   
212 public :
213   
214   /*!
215     Constructor.
216   */
217   MED_FIELD_WRONLY_DRIVER():MED_FIELD_DRIVER<T>() {}
218   
219   /*!
220     Constructor.
221   */
222   MED_FIELD_WRONLY_DRIVER(const string & fileName, FIELD<T> * ptrField):
223     MED_FIELD_DRIVER<T>(fileName,ptrField,MED_WRONLY)
224   {
225     BEGIN_OF("MED_FIELD_WRONLY_DRIVER::MED_FIELD_WRONLY_DRIVER(const string & fileName, const FIELD<T> * ptrField)");
226     END_OF("MED_FIELD_WRONLY_DRIVER::MED_FIELD_WRONLY_DRIVER(const string & fileName, const FIELD<T> * ptrField)");
227   };
228
229   /*!
230     Copy constructor.
231   */
232   MED_FIELD_WRONLY_DRIVER(const MED_FIELD_WRONLY_DRIVER & fieldDriver):
233     MED_FIELD_DRIVER<T>(fieldDriver) {};
234   
235   /*!
236     Destructor.
237   */
238   virtual ~MED_FIELD_WRONLY_DRIVER() {};
239
240   /*!
241     Write FIELD in the specified file.
242   */
243   void write( void ) const throw (MEDEXCEPTION) ;
244   /*!
245     Return a MEDEXCEPTION : it is the write-only driver.
246   */
247   void read ( void ) throw (MEDEXCEPTION) ;
248
249 private:
250   GENDRIVER * copy( void ) const ;
251
252 };
253
254
255 /*!
256
257   Driver Med for FIELD : Read write.
258   - Use read method from MED_FIELD_RDONLY_DRIVER
259   - Use write method from MED_FIELD_WDONLY_DRIVER
260
261 */
262
263 template <class T> class MED_FIELD_RDWR_DRIVER : public MED_FIELD_RDONLY_DRIVER<T>, public MED_FIELD_WRONLY_DRIVER<T> {
264   
265 public :
266   
267   /*!
268     Constructor.
269   */
270   MED_FIELD_RDWR_DRIVER():MED_FIELD_DRIVER<T>() {}
271   
272   /*!
273     Constructor.
274   */
275   MED_FIELD_RDWR_DRIVER(const string & fileName, FIELD<T> * ptrField):
276     MED_FIELD_DRIVER<T>(fileName,ptrField,MED_EN::MED_RDWR)
277   {
278     BEGIN_OF("MED_FIELD_RDWR_DRIVER::MED_FIELD_RDWR_DRIVER(const string & fileName, const FIELD<T> * ptrField)");
279     //_accessMode = MED_RDWR ;
280     END_OF("MED_FIELD_RDWR_DRIVER::MED_FIELD_RDWR_DRIVER(const string & fileName, const FIELD<T> * ptrField)");
281   };
282
283   /*!
284     Copy constructor.
285   */
286   MED_FIELD_RDWR_DRIVER(const MED_FIELD_RDWR_DRIVER & fieldDriver):
287     MED_FIELD_DRIVER<T>(fieldDriver) {};
288   
289   /*!
290     Destructor.
291   */
292   ~MED_FIELD_RDWR_DRIVER() {};
293
294   /*!
295     Write FIELD in the specified file.
296   */
297   void write(void) const throw (MEDEXCEPTION) ;
298   /*!
299     Read FIELD in the specified file.
300   */
301   void read (void) throw (MEDEXCEPTION) ;
302
303 private:
304   GENDRIVER * copy( void ) const ;
305
306 };
307
308
309 /*-------------------------*/
310 /* template implementation */
311 /*-------------------------*/
312 /*--------------------- DRIVER PART -------------------------------*/
313
314 template <class T> void MED_FIELD_DRIVER<T>::setFieldName(const string & fieldName)
315 {
316   _fieldName = fieldName; 
317 }
318
319 template <class T> string  MED_FIELD_DRIVER<T>::getFieldName() const
320 {
321   return _fieldName;
322 }
323
324 //  template <class T> void MED_FIELD_DRIVER<T>::search_field() {
325 //    const char * LOC = "template <class T> class MED_FIELD_DRIVER::search_field() :";
326     
327 //    // we search the field number !!!!
328 //    if (_status==MED_OPENED)
329 //      if (_fieldNum==MED_INVALID) {
330 //        int err ;
331 //        int    numberOfFields              = 0;      //MED_INVALID
332 //        //    char   fieldName[MED_TAILLE_NOM+1] = "";
333 //        char   fieldName[MED_TAILLE_NOM+1] ;
334 //        int    numberOfComponents          = 0;
335 //        char * componentName               = (char *) MED_NULL;
336 //        char * unitName                    = (char *) MED_NULL;
337 //        med_2_1::med_type_champ type ;
338 //        numberOfFields = med_2_1::MEDnChamp(_medIdt,0) ;
339 //        if ( numberOfFields <= 0 ) 
340 //      throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": No Field found !"));
341 //        for (int i=1;i<=numberOfFields;i++) {
342           
343 //      numberOfComponents = med_2_1::MEDnChamp(_medIdt,i) ;
344 //      if ( numberOfComponents <= 0 ) 
345 //        throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
346 //                                         <<  "Be careful there is no compound for field n°" 
347 //                                         << i << "in file |"<<_fileName<<"| !"));
348           
349 //      componentName = new char[numberOfComponents*MED_TAILLE_PNOM+1] ;
350 //      unitName      = new char[numberOfComponents*MED_TAILLE_PNOM+1] ;   
351           
352 //      err = med_2_1::MEDchampInfo(_medIdt, i, fieldName, &type, componentName, 
353 //                                 unitName, numberOfComponents) ;
354           
355 //      delete[] componentName ;
356 //      delete[] unitName ;
357 //      MESSAGE("Champ "<<i<<" : #" << fieldName <<"# et recherche #"<<_fieldName.c_str()<<"#");
358 //      if ( !strcmp(fieldName,_fieldName.c_str()) ) {
359 //        MESSAGE("FOUND FIELD "<< fieldName <<" : "<<i);
360 //        _fieldNum = i ;
361 //        break ;
362 //      }
363 //        }
364 //      }
365 //  }
366   
367 /*--------------------- RDONLY PART -------------------------------*/
368
369 template <class T> GENDRIVER * MED_FIELD_RDONLY_DRIVER<T>::copy(void) const
370 {
371   MED_FIELD_RDONLY_DRIVER<T> * myDriver = 
372     new MED_FIELD_RDONLY_DRIVER<T>(*this);
373   return myDriver ;
374 }
375
376 template <class T> void MED_FIELD_RDONLY_DRIVER<T>::read(void)
377   throw (MEDEXCEPTION)
378 {
379   const char * LOC = " MED_FIELD_RDONLY_DRIVER::read() " ;
380   BEGIN_OF(LOC);
381
382   if (MED_FIELD_DRIVER<T>::_ptrField->_name=="")
383     MED_FIELD_DRIVER<T>::_ptrField->_name = MED_FIELD_DRIVER<T>::_fieldName ; 
384   else
385     MED_FIELD_DRIVER<T>::_fieldName = MED_FIELD_DRIVER<T>::_ptrField->_name; // bug indetermine ! apres avoir fait readfilestruct, lorsque je recupere le champ, tout est bon sauf le nom du champ dans le driver !!!!!
386
387   MESSAGE("###### "<<LOC<<" fieldNameDRIVER : "<<MED_FIELD_DRIVER<T>::_fieldName<<" fieldName : "<<MED_FIELD_DRIVER<T>::_ptrField->_name);
388
389   string MeshName =  MED_FIELD_DRIVER<T>::_ptrField->getSupport()->getMesh()->getName() ;
390
391   if (MED_FIELD_DRIVER<T>::_status==MED_OPENED)
392     {
393
394       //        search_field() ;
395
396       char *  fieldName ;
397       fieldName = new char[MED_TAILLE_NOM+1] ;
398       int err ;
399       int    numberOfComponents          = 0;
400       char * componentName               = (char *) MED_NULL;
401       char * unitName                    = (char *) MED_NULL;
402       med_2_1::med_type_champ type ;
403
404       // we search the field number !!!!
405       if (MED_FIELD_DRIVER<T>::_fieldNum==MED_INVALID) {
406         int    numberOfFields              = 0;      //MED_INVALID
407         numberOfFields = med_2_1::MEDnChamp(MED_FIELD_DRIVER<T>::_medIdt,0) ;
408         if ( numberOfFields <= 0 ) 
409           throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": No Field found !"));
410         for (int i=1;i<=numberOfFields;i++) {
411             
412           numberOfComponents = med_2_1::MEDnChamp(MED_FIELD_DRIVER<T>::_medIdt,i) ;
413           if ( numberOfComponents <= 0 ) 
414             //                throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
415             //                                                 <<  "Be careful there is no compound for field n°" 
416             //                                                 << i << "in file |"<<_fileName<<"| !"));
417             MESSAGE(LOC<<"Be careful there is no compound for field n°"<<i<<"in file |"<<_fileName<<"| !");
418
419           componentName = new char[numberOfComponents*MED_TAILLE_PNOM+1] ;
420           unitName      = new char[numberOfComponents*MED_TAILLE_PNOM+1] ;   
421             
422           err = med_2_1::MEDchampInfo(MED_FIELD_DRIVER<T>::_medIdt, i, fieldName, &type, componentName, 
423                                      unitName, numberOfComponents) ;
424             
425           MESSAGE("Champ "<<i<<" : #" << fieldName <<"# et recherche #"<<MED_FIELD_DRIVER<T>::_fieldName.c_str()<<"#");
426           if ( !strcmp(fieldName,MED_FIELD_DRIVER<T>::_fieldName.c_str()) ) {
427             MESSAGE("FOUND FIELD "<< fieldName <<" : "<<i);
428             MED_FIELD_DRIVER<T>::_fieldNum = i ;
429             break ;
430           }
431           // not found : release memory and search next field !
432           delete[] componentName ;
433           delete[] unitName ;
434         }
435       }
436       
437       delete[] fieldName ;
438
439       if (MED_FIELD_DRIVER<T>::_fieldNum==MED_INVALID)
440         throw MEDEXCEPTION(LOCALIZED( STRING(LOC) << ": Field "<<MED_FIELD_DRIVER<T>::_fieldName << " not found in file " << MED_FIELD_DRIVER<T>::_fileName ) );
441       MESSAGE ("FieldNum : "<<MED_FIELD_DRIVER<T>::_fieldNum);
442
443       //        int err ;
444       //        int NumberOfComponents = med_2_1::MEDnChamp(MED_FIELD_DRIVER<T>::_medIdt,MED_FIELD_DRIVER<T>::_fieldNum) ;
445       if (numberOfComponents < 1) 
446         throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<"no component")) ; // use iostring !
447       // test type to check if it is rigth !!!???
448       MED_FIELD_DRIVER<T>::_ptrField->_numberOfComponents = numberOfComponents ;
449       MED_FIELD_DRIVER<T>::_ptrField->_componentsTypes = new int[numberOfComponents] ;
450       MED_FIELD_DRIVER<T>::_ptrField->_componentsNames = new string[numberOfComponents] ;
451       MED_FIELD_DRIVER<T>::_ptrField->_componentsUnits = new UNIT[numberOfComponents] ;
452       MED_FIELD_DRIVER<T>::_ptrField->_componentsDescriptions = new string[numberOfComponents] ;
453       MED_FIELD_DRIVER<T>::_ptrField->_MEDComponentsUnits = new string[numberOfComponents] ;
454       for (int i=0; i<numberOfComponents; i++) {
455         MED_FIELD_DRIVER<T>::_ptrField->_componentsTypes[i] = 1 ;
456
457         // PG : what about space !!!
458         MED_FIELD_DRIVER<T>::_ptrField->_componentsNames[i] = string(componentName,i*MED_TAILLE_PNOM,MED_TAILLE_PNOM) ;
459         SCRUTE(MED_FIELD_DRIVER<T>::_ptrField->_componentsNames[i]);
460         MED_FIELD_DRIVER<T>::_ptrField->_MEDComponentsUnits[i] = string(unitName,i*MED_TAILLE_PNOM,MED_TAILLE_PNOM) ;
461         SCRUTE(MED_FIELD_DRIVER<T>::_ptrField->_MEDComponentsUnits[i]);
462       }
463       delete[] componentName;
464       delete[] unitName;
465
466       // read values for each geometric type in _support
467       int NumberOfTypes = MED_FIELD_DRIVER<T>::_ptrField->_support->getNumberOfTypes() ;
468       const MED_EN::medGeometryElement *Types = MED_FIELD_DRIVER<T>::_ptrField->_support->getTypes() ;
469       T ** myValues = new T*[NumberOfTypes] ;
470       int * NumberOfValues = new int[NumberOfTypes] ;
471       int TotalNumberOfValues = 0 ;
472       MESSAGE ("NumberOfTypes :"<< NumberOfTypes);
473       _ptrField->_numberOfValues=0 ;
474       for (int i=0; i<NumberOfTypes; i++) {
475         MESSAGE ("Type["<<i+1<<"] :"<< Types[i]);
476         MESSAGE ("Entity :"<<MED_FIELD_DRIVER<T>::_ptrField->_support->getEntity());
477         NumberOfValues[i] = 
478           MEDnVal(MED_FIELD_DRIVER<T>::_medIdt,
479                   const_cast <char*> (MED_FIELD_DRIVER<T>::_fieldName.c_str()),
480                   (med_2_1::med_entite_maillage)MED_FIELD_DRIVER<T>::_ptrField->_support->getEntity(),
481                   (med_2_1::med_geometrie_element)Types[i],
482                   MED_FIELD_DRIVER<T>::_ptrField->_iterationNumber,
483                   MED_FIELD_DRIVER<T>::_ptrField->_orderNumber) ; // no time step ! prend en compte le nbre de pt de gauss
484         // test if NumberOfValues is the same in _support !!! TODO that !!
485         // we suppose it is
486         // we could allocate array
487         myValues[i] = new T[ NumberOfValues[i]*numberOfComponents ] ;
488         TotalNumberOfValues+=NumberOfValues[i] ;// diviser par le nombre de point de gauss 
489         char * ProfilName = new char[MED_TAILLE_NOM+1];
490         MESSAGE ("NumberOfValues :"<< NumberOfValues[i]);
491         MESSAGE ("NumberOfComponents :"<< numberOfComponents);
492         MESSAGE ("MESH_NAME :"<< MeshName.c_str());
493         MESSAGE ("FIELD_NAME :"<< MED_FIELD_DRIVER<T>::_fieldName.c_str());
494         MESSAGE ("MED_ENTITE :"<< (med_2_1::med_entite_maillage) MED_FIELD_DRIVER<T>::_ptrField->_support->getEntity());
495         MESSAGE("MED_GEOM :"<<(med_2_1::med_geometrie_element)Types[i]);
496         MESSAGE("Iteration :"<<MED_FIELD_DRIVER<T>::_ptrField->getIterationNumber());
497         MESSAGE("Order :"<<MED_FIELD_DRIVER<T>::_ptrField->getOrderNumber());
498         MED_FIELD_DRIVER<T>::_ptrField->_numberOfValues+=NumberOfValues[i]; // problem with gauss point : _numberOfValues != TotalNumberOfValues !!!!!!!
499         med_2_1::med_err ret;
500 #if defined(IRIX64) || defined(OSF1) || defined(VPP5000)
501         int lgth2=NumberOfValues[i]*numberOfComponents;
502         if(_ptrField->getValueType()==MED_EN::MED_INT32)
503           {
504             med_2_1::med_int *temp=new med_2_1::med_int[lgth2];
505             ret=med_2_1::MEDchampLire(MED_FIELD_DRIVER<T>::_medIdt,const_cast <char*> (MeshName.c_str()),
506                                   const_cast <char*> (MED_FIELD_DRIVER<T>::_fieldName.c_str()),
507                                   (unsigned char*) temp,
508                                   med_2_1::MED_NO_INTERLACE,
509                                   MED_ALL,
510                                   ProfilName,
511                                   (med_2_1::med_entite_maillage) MED_FIELD_DRIVER<T>::_ptrField->_support->getEntity(),(med_2_1::med_geometrie_element)Types[i],
512                                   MED_FIELD_DRIVER<T>::_ptrField->getIterationNumber(),
513                                   MED_FIELD_DRIVER<T>::_ptrField->getOrderNumber()
514                                  );
515             for(int i2=0;i2<lgth2;i2++)
516               myValues[i][i2]=(int)(temp[i2]);
517             delete [] temp;
518           }
519         else
520 #endif
521         ret=med_2_1::MEDchampLire(MED_FIELD_DRIVER<T>::_medIdt,const_cast <char*> (MeshName.c_str()),
522                                   const_cast <char*> (MED_FIELD_DRIVER<T>::_fieldName.c_str()),
523                                   (unsigned char*) myValues[i],
524                                   med_2_1::MED_NO_INTERLACE,
525                                   MED_ALL,
526                                   ProfilName,
527                                   (med_2_1::med_entite_maillage) MED_FIELD_DRIVER<T>::_ptrField->_support->getEntity(),(med_2_1::med_geometrie_element)Types[i],
528                                   MED_FIELD_DRIVER<T>::_ptrField->getIterationNumber(),
529                                   MED_FIELD_DRIVER<T>::_ptrField->getOrderNumber()
530                                  ); 
531           if (ret < 0) {
532           // we must do some delete !!!
533           for(int j=0; j<=i;j++)
534             delete[] myValues[j];
535           delete[] myValues;
536           delete[] NumberOfValues ;
537           delete[] ProfilName;
538           delete[] MED_FIELD_DRIVER<T>::_ptrField->_componentsTypes ;
539           delete[] MED_FIELD_DRIVER<T>::_ptrField->_componentsNames ;
540           delete[] MED_FIELD_DRIVER<T>::_ptrField->_componentsUnits ;
541           delete[] MED_FIELD_DRIVER<T>::_ptrField->_componentsDescriptions ;
542           delete[] MED_FIELD_DRIVER<T>::_ptrField->_MEDComponentsUnits ;
543           MED_FIELD_DRIVER<T>::_ptrField->_componentsTypes = NULL ;
544           MED_FIELD_DRIVER<T>::_ptrField->_componentsNames = NULL ;
545           MED_FIELD_DRIVER<T>::_ptrField->_componentsUnits = NULL ;
546           MED_FIELD_DRIVER<T>::_ptrField->_componentsDescriptions = NULL ;
547           MED_FIELD_DRIVER<T>::_ptrField->_MEDComponentsUnits = NULL ;
548           MED_FIELD_DRIVER<T>::_fieldNum = MED_INVALID ; // we have not found right field, so reset the field number 
549           throw MEDEXCEPTION( LOCALIZED( STRING(LOC) <<": ERROR when read value")) ;
550         }
551
552         delete[] ProfilName ;
553       }
554       // allocate _value
555       // probleme avec les points de gauss : voir lorsqu-il y en a (!= 1)
556       //      MEDARRAY<T> * Values = new MEDARRAY<T>(MED_FIELD_DRIVER<T>::_ptrField->getNumberOfComponents(),TotalNumberOfValues/MED_FIELD_DRIVER<T>::_ptrField->getNumberOfComponents(),MED_EN::MED_NO_INTERLACE);
557
558       if (MED_FIELD_DRIVER<T>::_ptrField->_value==NULL)
559         MED_FIELD_DRIVER<T>::_ptrField->_value=new MEDARRAY<T>(numberOfComponents,TotalNumberOfValues,MED_EN::MED_NO_INTERLACE);
560
561       MEDARRAY<T> * Values = MED_FIELD_DRIVER<T>::_ptrField->_value ; // create by constructor ???
562       // check if dimensions are right : inutile : c'est dans le constructeur !!!
563       //if (Values->getLeadingValue() != numberOfComponents)
564       //  throw MEDEXCEPTION( LOCALIZED( STRING(LOC) <<": leading dimension are false : "<<Values->getLeadingValue()<<" and "<<numberOfComponents) ) ;
565       //if (Values->getLengthValue() != TotalNumberOfValues)
566       //  throw MEDEXCEPTION( LOCALIZED( STRING(LOC) <<": length dimension are false : "<<Values->getLengthValue()<<" and "<<TotalNumberOfValues) ) ;
567
568       for (int i=0; i<numberOfComponents; i++) {
569         //T * ValuesT = Values->getRow(i+1) ;
570         int Count = 1 ;
571         for (int j=0; j<NumberOfTypes; j++) {
572           T * myValue = myValues[j] ;
573           int NumberOf = NumberOfValues[j] ;
574 //        MED_FIELD_DRIVER<T>::_ptrField->_numberOfValues+=NumberOf; // problem with gauss point : _numberOfValues != TotalNumberOfValues !!!!!!!
575           int offset = NumberOf*i ;
576           for (int k=0 ; k<NumberOf; k++) {
577             //ValuesT[Count]=myValue[k+offset] ;
578             Values->setIJ(Count,i+1,myValue[k+offset]);
579             SCRUTE(Count);
580             SCRUTE(Values->getIJ(Count,i+1));
581             Count++;
582           }
583         }
584       }
585       
586       for (int j=0; j<NumberOfTypes; j++)
587         delete[] myValues[j] ;
588       delete[] myValues ;
589       delete[] NumberOfValues ;
590
591       MED_FIELD_DRIVER<T>::_ptrField->_isRead = true ;
592     }
593
594   END_OF(LOC);
595 }
596
597 template <class T> void MED_FIELD_RDONLY_DRIVER<T>::write( void ) const
598   throw (MEDEXCEPTION)
599 {
600   throw MEDEXCEPTION("MED_FIELD_RDONLY_DRIVER::write : Can't write with a RDONLY driver !");
601 }
602
603 /*--------------------- WRONLY PART -------------------------------*/
604
605 template <class T> GENDRIVER * MED_FIELD_WRONLY_DRIVER<T>::copy(void) const
606 {
607   MED_FIELD_WRONLY_DRIVER<T> * myDriver = 
608     new MED_FIELD_WRONLY_DRIVER<T>(*this);
609   return myDriver ;
610 }
611
612 template <class T> void MED_FIELD_WRONLY_DRIVER<T>::read (void)
613   throw (MEDEXCEPTION)
614 {
615   throw MEDEXCEPTION("MED_FIELD_WRONLY_DRIVER::read : Can't read with a WRONLY driver !");
616 }
617
618 template <class T> void MED_FIELD_WRONLY_DRIVER<T>::write(void) const
619   throw (MEDEXCEPTION)
620 {
621   const char * LOC = "MED_FIELD_WRONLY_DRIVER::write(void) const " ;
622   BEGIN_OF(LOC);
623   if (MED_FIELD_DRIVER<T>::_status==MED_OPENED)
624     {
625       int err ;
626
627       int component_count=MED_FIELD_DRIVER<T>::_ptrField->getNumberOfComponents();
628       string   component_name(component_count*MED_TAILLE_PNOM,' ') ;
629       string   component_unit(component_count*MED_TAILLE_PNOM,' ') ;
630
631       const string * listcomponent_name=MED_FIELD_DRIVER<T>::_ptrField->getComponentsNames() ;
632       const string * listcomponent_unit=MED_FIELD_DRIVER<T>::_ptrField->getMEDComponentsUnits() ;
633       int length ;
634       for (int i=0; i < component_count ; i++) {
635         length = min(MED_TAILLE_PNOM,(int)listcomponent_name[i].size());
636         component_name.replace(i*MED_TAILLE_PNOM,length,
637                                listcomponent_name[i],0,length);
638         length = min(MED_TAILLE_PNOM,(int)listcomponent_unit[i].size());
639         component_unit.replace(i*MED_TAILLE_PNOM,length,
640                                listcomponent_unit[i],0,length);
641       }
642
643       MESSAGE("component_name=|"<<component_name<<"|");
644       MESSAGE("component_unit=|"<<component_unit<<"|");
645
646       MED_EN::med_type_champ ValueType=MED_FIELD_DRIVER<T>::_ptrField->getValueType() ;
647       
648       MESSAGE("Template Type =|"<<ValueType<<"|");
649       
650       // le champ existe deja ???
651       char * champName = new char[MED_TAILLE_NOM+1] ;
652       med_2_1::med_type_champ type ;
653       char * compName ;
654       char * compUnit ;
655       bool Find = false ;
656       int n = med_2_1::MEDnChamp(MED_FIELD_DRIVER<T>::_medIdt,0);
657       int nbComp ;
658       for (int i=1; i<=n; i++) {
659         nbComp = med_2_1::MEDnChamp(MED_FIELD_DRIVER<T>::_medIdt,i);
660         compName = new char[MED_TAILLE_PNOM*nbComp+1];
661         compUnit = new char[MED_TAILLE_PNOM*nbComp+1];
662         err = med_2_1::MEDchampInfo(MED_FIELD_DRIVER<T>::_medIdt,i,champName,&type,compName,compUnit,nbComp);
663         if (err == 0)
664           if (strcmp(champName,MED_FIELD_DRIVER<T>::_ptrField->getName().c_str())==0) { // Found !
665             Find = true ;
666             break ;
667           }
668         delete[] compName ;
669         delete[] compUnit ;
670       }
671       delete[] champName ;
672       if (Find) {
673         // the same ?
674         if (nbComp != component_count)
675           throw MEDEXCEPTION( LOCALIZED (STRING(LOC)
676                                          <<": Field exist in file, but number of component are different : "<<nbComp<<" in file and "<<component_count<<" in memory."
677                                          )
678                               );
679         // component name and unit
680         MESSAGE(LOC<<" Component name in file : "<<compName);
681         MESSAGE(LOC<<" Component name in memory : "<<component_name);
682         MESSAGE(LOC<<" Component unit in file : "<<compUnit);
683         MESSAGE(LOC<<" Component unit in memory : "<<component_unit);
684         delete[] compName ;
685         delete[] compUnit ;
686
687       } else {
688         // Verify the field doesn't exist
689
690         string dataGroupName =  "/CHA/";
691         dataGroupName        += MED_FIELD_DRIVER<T>::_ptrField->getName();
692         MESSAGE(LOC << "|" << dataGroupName << "|" );
693         MED_EN::med_idt gid =  H5Gopen(MED_FIELD_DRIVER<T>::_medIdt, dataGroupName.c_str() );
694         
695         if ( gid < 0 ) {
696           // create field :
697           err=med_2_1::MEDchampCr(MED_FIELD_DRIVER<T>::_medIdt, 
698                                  const_cast <char*> ((MED_FIELD_DRIVER<T>::_ptrField->getName()).c_str()),
699                                  (med_2_1::med_type_champ) ValueType,
700                                  const_cast <char*> ( component_name.c_str() ),
701                                  const_cast <char*> ( component_unit.c_str() ),
702                                  component_count);
703           if ( err < 0 )
704             throw MEDEXCEPTION( LOCALIZED (STRING(LOC) 
705                                            << ": Error MEDchampCr : "<<err
706                                            )
707                                 );
708         }
709         else H5Gclose(gid);
710       }
711
712       const SUPPORT * mySupport = MED_FIELD_DRIVER<T>::_ptrField->getSupport() ;
713
714       if (! mySupport->isOnAllElements())
715         throw MEDEXCEPTION( LOCALIZED (STRING(LOC) 
716                                        <<": Field must be on all entity"
717                                        )
718                             );
719       
720       MESH * myMesh = mySupport->getMesh() ;
721       string MeshName = myMesh->getName() ;
722       //MED_EN::medModeSwitch Mode = MED_FIELD_DRIVER<T>::_ptrField->_value->getMode() ;
723       // on boucle sur tout les types pour ecrire les tableaux de valeur
724       int NumberOfType = mySupport->getNumberOfTypes() ;
725       int Index = 1 ;
726       const MED_EN::medGeometryElement * Types = mySupport->getTypes() ;
727       const int * NumberOfGaussPoint = mySupport->getNumberOfGaussPoint() ;
728       for (int i=0;i<NumberOfType;i++) {
729         int NumberOfElements = mySupport->getNumberOfElements(Types[i]) ;
730         
731         const T * value = MED_FIELD_DRIVER<T>::_ptrField->getValueI(MED_EN::MED_FULL_INTERLACE,Index) ;
732         
733         MESSAGE("MED_FIELD_DRIVER<T>::_medIdt                         : "<<MED_FIELD_DRIVER<T>::_medIdt);
734         MESSAGE("MeshName.c_str()                : "<<MeshName.c_str());
735         MESSAGE("MED_FIELD_DRIVER<T>::_ptrField->getName()            : "<<MED_FIELD_DRIVER<T>::_ptrField->getName());
736         MESSAGE("value                           : "<<value);
737         MESSAGE("NumberOfElements                : "<<NumberOfElements);
738         MESSAGE("NumberOfGaussPoint[i]           : "<<NumberOfGaussPoint[i]);
739         MESSAGE("mySupport->getEntity()          : "<<mySupport->getEntity());
740         MESSAGE("Types[i]                        : "<<Types[i]);
741         MESSAGE("MED_FIELD_DRIVER<T>::_ptrField->getIterationNumber() : "<<MED_FIELD_DRIVER<T>::_ptrField->getIterationNumber());
742         MESSAGE("MED_FIELD_DRIVER<T>::_ptrField->getTime()            : "<<MED_FIELD_DRIVER<T>::_ptrField->getTime());
743         MESSAGE("MED_FIELD_DRIVER<T>::_ptrField->getOrderNumber()     : "<<MED_FIELD_DRIVER<T>::_ptrField->getOrderNumber());
744         
745 /*      char chanom[MED_TAILLE_NOM+1];
746         char chacomp[MED_TAILLE_NOM+1];
747         char chaunit[MED_TAILLE_NOM+1];
748         med_2_1::med_type_champ chatype;
749         med_int chancomp=1;
750         
751         err=med_2_1::MEDchampInfo(MED_FIELD_DRIVER<T>::_medIdt,1,chanom,&chatype,chacomp,chaunit,chancomp);
752
753         if (err<0) 
754                 {
755                 cout<<"=======================================================================> gros probleme"<<endl;
756                 exit(-1);
757                 }
758         cout<<"==================> nom lu            = "<<chanom<<endl;
759         cout<<"==================> type lu           = "<<chatype<<endl;
760         cout<<"==================> nom composante lu = "<<chacomp<<endl;
761         cout<<"==================> nom unit lu       = "<<chaunit<<endl;
762         cout<<"==================> valeur de med_2_1::MED_REEL64 = "<<med_2_1::MED_REEL64<<endl;
763 */      
764 #if defined(IRIX64) || defined(OSF1) || defined(VPP5000)
765         if(_ptrField->getValueType()==MED_EN::MED_INT32)
766           {
767             int lgth2=_ptrField->getNumberOfValues();
768             med_2_1::med_int *temp=new med_2_1::med_int[lgth2];
769             for(int i2=0;i2<lgth2;i2++)
770               temp[i2]=(int)(value[i2]);
771             err=med_2_1::MEDchampEcr(MED_FIELD_DRIVER<T>::_medIdt, 
772                                     const_cast <char*> ( MeshName.c_str()) ,                         //( string(mesh_name).resize(MED_TAILLE_NOM).c_str())
773                                     const_cast <char*> ( (MED_FIELD_DRIVER<T>::_ptrField->getName()).c_str()),
774                                     (unsigned char*)temp, 
775                                     med_2_1::MED_FULL_INTERLACE,
776                                     NumberOfElements,
777                                     NumberOfGaussPoint[i],
778                                     MED_ALL,
779                                     MED_NOPFL,
780                                     med_2_1::MED_REMP,  // PROFIL NON GERE, mode de remplacement non géré
781                                     (med_2_1::med_entite_maillage)mySupport->getEntity(),
782                                     (med_2_1::med_geometrie_element)Types[i],
783                                     MED_FIELD_DRIVER<T>::_ptrField->getIterationNumber(),
784                                     "        ",
785                                     MED_FIELD_DRIVER<T>::_ptrField->getTime(),
786                                     MED_FIELD_DRIVER<T>::_ptrField->getOrderNumber()
787                                     );
788             delete [] temp;
789           }
790         else
791 #endif
792         err=med_2_1::MEDchampEcr(MED_FIELD_DRIVER<T>::_medIdt, 
793                                 const_cast <char*> ( MeshName.c_str()) ,                         //( string(mesh_name).resize(MED_TAILLE_NOM).c_str())
794                                 const_cast <char*> ( (MED_FIELD_DRIVER<T>::_ptrField->getName()).c_str()),
795                                 (unsigned char*)value, 
796                                 med_2_1::MED_FULL_INTERLACE,
797                                 NumberOfElements,
798                                 NumberOfGaussPoint[i],
799                                 MED_ALL,
800                                 MED_NOPFL,
801                                 med_2_1::MED_REMP,  // PROFIL NON GERE, mode de remplacement non géré
802                                 (med_2_1::med_entite_maillage)mySupport->getEntity(),
803                                 (med_2_1::med_geometrie_element)Types[i],
804                                 MED_FIELD_DRIVER<T>::_ptrField->getIterationNumber(),
805                                 "        ",
806                                 MED_FIELD_DRIVER<T>::_ptrField->getTime(),
807                                 MED_FIELD_DRIVER<T>::_ptrField->getOrderNumber()
808                                 );
809         if (err < MED_VALID )
810           throw MEDEXCEPTION(LOCALIZED( STRING(LOC)
811                                         <<": Error in writing Field "<< MED_FIELD_DRIVER<T>::_ptrField->getName() <<", type "<<Types[i]
812                                         )
813                              );
814
815         Index += NumberOfElements ;
816         
817       }
818     }
819   
820   END_OF(LOC);
821 }
822
823 /*--------------------- RDWR PART -------------------------------*/
824
825 template <class T> GENDRIVER * MED_FIELD_RDWR_DRIVER<T>::copy(void) const
826 {
827   MED_FIELD_RDWR_DRIVER<T> * myDriver = 
828     new MED_FIELD_RDWR_DRIVER<T>(*this);
829   return myDriver ;
830 }
831
832 template <class T> void MED_FIELD_RDWR_DRIVER<T>::write(void) const
833   throw (MEDEXCEPTION)
834 {
835   BEGIN_OF("MED_FIELD_RDWR_DRIVER::write(void)");
836   MED_FIELD_WRONLY_DRIVER<T>::write(); 
837   END_OF("MED_FIELD_RDWR_DRIVER::write(void)");
838
839
840 template <class T> void MED_FIELD_RDWR_DRIVER<T>::read (void)
841   throw (MEDEXCEPTION)
842 {
843   BEGIN_OF("MED_FIELD_RDWR_DRIVER::read(void)");
844   MED_FIELD_RDONLY_DRIVER<T>::read();
845   END_OF("MED_FIELD_RDWR_DRIVER::read(void)");
846 }
847 }//End namespace MEDMEM
848 /*-----------------------------------------------------------------*/
849
850 #endif /* MED_FIELD_DRIVER_HXX */
851