Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / MEDMEMBinTest / ensight2med.cxx
1 // Copyright (C) 2007-2013  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
23 #include<string>
24 #include<deque>
25 #include<vector>
26
27 #include "MEDMEM_Exception.hxx"
28 #include "MEDMEM_define.hxx"
29
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 #include "MEDMEM_EnsightFieldDriver.hxx"
36 #include "MEDMEM_EnsightMeshDriver.hxx"
37
38 using namespace std;
39 using namespace MEDMEM;
40
41 static void usage(char * name)
42 {
43   cout << " ERROR ABOUT SYNTAX " << endl ;
44   cout << "  " << name << " <input ensight file> <output med file> [I - to read into MED_INT32 fields]"
45        << endl ;
46   exit(-1);
47 }
48
49 // ensight2med toto.case toto.med => input : toto.case => output : toto.med....
50
51 int main (int argc, char ** argv) {
52
53   string filenameIN ;
54   string filenameOUT;
55  
56   if ( argc == 3 ) {
57     filenameIN  = argv[1] ;
58     filenameOUT = argv[2] ;
59     cout << " reading all into the Ensight file " << filenameIN << " and writing all into the Med file " << filenameOUT <<  endl ;
60
61     vector< FIELD_* > fields;
62     vector< const GMESH* > meshes;
63
64     // Read
65
66     ENSIGHT_MED_RDONLY_DRIVER medDriver(filenameIN, fields);
67     medDriver.open();
68     medDriver.read();
69     medDriver.close();
70     if ( !fields.empty() )
71     {
72       set<const GMESH*> uniqueMeshes;
73       for ( unsigned i = 0; i < fields.size(); ++i )
74         uniqueMeshes.insert( fields[i]->getSupport()->getMesh() );
75       meshes.assign( uniqueMeshes.begin(), uniqueMeshes.end() );
76     }
77     else
78     {
79       // no fields but only meshes in the file
80       MESH* mesh = 0;
81       int meshIndex = 1;
82       do
83       {
84         mesh = new MESH;
85         meshes.push_back( mesh );
86         ENSIGHT_MESH_RDONLY_DRIVER meshDriver( filenameIN, mesh, meshIndex++ );
87         meshDriver.open();
88         meshDriver.read();
89         meshDriver.close();
90       } while ( mesh->getNumberOfNodes() > 0 );
91       meshes.back()->removeReference();
92       meshes.pop_back();
93     }
94
95     // Write
96
97     for ( unsigned i = 0; i < meshes.size(); ++i )
98     {
99       meshes[i]->write(MED_DRIVER,filenameOUT);
100     }
101     for ( unsigned i = 0; i < fields.size(); ++i )
102     {
103       fields[i]->write(MED_DRIVER,filenameOUT);
104       fields[i]->removeReference();
105     }
106     for ( unsigned i = 0; i < meshes.size(); ++i )
107       meshes[i]->removeReference();
108   }
109   else if ( argc == 4 && strncmp(argv[3], "I", 1 )==0 )
110   {
111     // we read all variables into INT32 fields
112     // (we need such fields for test_operation_fieldint)
113
114     filenameIN  = argv[1] ;
115     filenameOUT = argv[2] ;
116
117     vector< FIELD_* > fields;
118
119     // Read
120
121     ENSIGHT_MED_RDONLY_DRIVER medDriver(filenameIN, fields);
122     medDriver.open();
123     medDriver.readFileStruct();
124     medDriver.close();
125
126     if ( fields.empty() ) {
127       cout << "No fileds found in EnSight file " << filenameIN << endl;
128       return -1;
129     }
130     // read-write the mesh
131     const GMESH* mesh = fields[0]->getSupport()->getMesh();
132     const_cast<GMESH*>( mesh )->read();
133     mesh->write(MED_DRIVER, filenameOUT );
134
135     // read-write fields
136     for ( unsigned i = 0; i < fields.size(); ++i )
137     {
138       for ( int timeStep = 1; ; ++timeStep )
139       {
140         FIELD<int> *intF=new FIELD<int>;
141         intF->setName( fields[i]->getName() );
142         try
143         {
144           ENSIGHT_FIELD_RDONLY_DRIVER fieldDriver( filenameIN, intF, timeStep );
145           fieldDriver.open();
146           fieldDriver.read();
147           fieldDriver.close();
148           // replace zero values as theses fields are used for division
149           int nbVals = intF->getValueLength();
150           int* values = const_cast<int*>(intF->getValue());
151           while ( nbVals-- ) {
152             if ( values[nbVals]==0 )
153               values[nbVals]= nbVals%5 + 1;
154           }
155         }
156         catch ( const MEDEXCEPTION& ex)
157         {
158           intF->removeReference();
159           intF=0;
160           break;
161         }
162         intF->write(MED_DRIVER,filenameOUT);
163         intF->removeReference();
164       }
165       fields[i]->removeReference();
166     }
167     mesh->removeReference();
168   }
169   else usage(argv[0]);
170
171 }
172
173