Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDMEMBinTest / ensight2med.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<string>
23 #include<deque>
24 #include<vector>
25
26 #include "MEDMEM_Exception.hxx"
27 #include "MEDMEM_define.hxx"
28
29 #include "MEDMEM_Med.hxx"
30 #include "MEDMEM_Mesh.hxx"
31 #include "MEDMEM_Family.hxx"
32 #include "MEDMEM_Support.hxx"
33 #include "MEDMEM_Field.hxx"
34 #include "MEDMEM_EnsightMedDriver.hxx"
35
36 using namespace std;
37 using namespace MEDMEM;
38 void usage(char * name)
39 {
40   cout << " ERROR ABOUT SYNTAX " << endl ;
41   cout << "  " << name << " <input ensight file> <output med file> [I - to read into MED_INT32 fields]"
42        << endl ;
43   exit(-1);
44 }
45
46 // ensight2med toto.case toto.med => input : toto.case => output : toto.med....
47
48 int main (int argc, char ** argv) {
49
50   string filenameIN ;
51   string filenameOUT;
52  
53   if ( argc == 3 ) {
54     filenameIN  = argv[1] ;
55     filenameOUT = argv[2] ;
56     cout << " reading all into the Ensight file " << filenameIN << " and writing all into the Med file " << filenameOUT <<  endl ;
57
58     MED myMed(ENSIGHT_DRIVER,filenameIN) ;
59     myMed.read() ;
60
61     int id = myMed.addDriver(MED_DRIVER,filenameOUT);
62     myMed.write(id);
63   }
64   else if ( argc == 4 && strncmp(argv[3], "I", 1 )==0 )
65   {
66     // we read all variables into INT32 fields
67     // (we need such fields for test_operation_fieldint)
68
69     filenameIN  = argv[1] ;
70     filenameOUT = argv[2] ;
71     MED myMed(ENSIGHT_DRIVER,filenameIN);
72
73     // read-write the mesh
74     if ( myMed.getNumberOfMeshes() < 1 ) {
75       cout << "No meshes found in EnSight file " << filenameIN << endl;
76       return -1;
77     }
78     vector<string> meshNames( myMed.getNumberOfMeshes() );
79     myMed.getMeshNames( &meshNames[0] );
80     MESH * mesh = myMed.getMesh( meshNames[0] );
81     mesh->read();
82     int drv = mesh->addDriver(MED_DRIVER, filenameOUT, mesh->getName() );
83     mesh->write( drv );
84
85     // read-write fields
86     int nbFields = myMed.getNumberOfFields();
87     vector<string> fieldNames( nbFields );
88     myMed.getFieldNames( &fieldNames[0] );
89     for ( int i = 0; i < nbFields; ++i )
90     {
91       deque<DT_IT_> dis = myMed.getFieldIteration( fieldNames[i] );
92       for ( deque<DT_IT_>::iterator d_i = dis.begin(); d_i != dis.end(); ++d_i )
93       {
94         FIELD_* f = myMed.getField( fieldNames[i], d_i->dt, d_i->it );
95         FIELD<int> intF(ENSIGHT_DRIVER, filenameIN, f->getName() );
96         // replace zero values as theses fields are used for division
97         int nbVals = intF.getValueLength();
98         int* values = const_cast<int*>(intF.getValue());
99         while ( nbVals-- ) {
100           if ( values[nbVals]==0 )
101             values[nbVals]= nbVals%5 + 1;
102         }
103         int drv = intF.addDriver(MED_DRIVER, filenameOUT, f->getName() );
104         intF.write( drv );
105       }
106     }
107   }
108   else usage(argv[0]);
109
110 }
111
112