Salome HOME
*** empty log message ***
[modules/multipr.git] / src / MULTIPR / MULTIPR_Utils.cxx
1 // Project MULTIPR, IOLS WP1.2.1 - EDF/CS
2 // Partitioning/decimation module for the SALOME v3.2 platform
3
4 /**
5  * \file    MULTIPR_Utils.cxx
6  *
7  * \brief   see MULTIPR_Utils.hxx
8  *
9  * \author  Olivier LE ROUX - CS, Virtual Reality Dpt
10  * 
11  * \date    01/2007
12  */
13
14 //*****************************************************************************
15 // Includes section
16 //*****************************************************************************
17
18 #include "MULTIPR_Utils.hxx"
19 #include "MULTIPR_Exceptions.hxx"
20
21 #include <iostream>
22
23 using namespace std;
24
25
26 //*****************************************************************************
27 // Implementation
28 //*****************************************************************************
29
30 void multipr::trim(char* pStr, char pChar)
31 {
32         int len = strlen(pStr) - 1;
33         int p = len;
34         while (pStr[p] == pChar)
35         {
36                 p--;
37         }
38         if (p != len)
39         {
40                 pStr[p + 1] = '\0';
41         }
42 }
43
44
45 bool multipr::startWith(const char* pStr, const char* pStrPrefix)
46 {
47         int lenStr = strlen(pStr);
48         
49         int i = 0;
50         while ((pStr[i] != '\0') && (pStrPrefix[i] != '\0') && (pStr[i] == pStrPrefix[i]))
51         {
52                 i++;
53         }
54         return (pStrPrefix[i] == '\0');
55 }
56
57
58 string multipr::removeExtension(const char* pFilename, const char* pExtension)
59 {
60         string strPrefix(pFilename); 
61         strPrefix.erase(strPrefix.rfind(pExtension), strlen(pExtension));
62         
63         return strPrefix;
64 }
65
66
67 void multipr::printArray2D(
68         const med_float* pData, 
69         const int        pNumElt,
70         const int        pDimElt,
71         const char*      pPrefix)
72 {
73         for (int itElt = 0 ; itElt < pNumElt ; itElt++)
74         {
75                 cout << pPrefix << " " << (itElt + 1) << ": ";
76                 for (int itDim = 0 ; itDim < pDimElt ; itDim++)
77                 {
78                         cout << pData[itElt * pDimElt + itDim] << " ";
79                 }
80                 cout << endl;
81         }
82 }
83
84
85 string multipr::realToString(med_float mV)
86 {
87         char str[32];
88         sprintf(str, "%lf", mV);
89         trim(str, '0');
90         int len = strlen(str);
91         if (str[len - 1] == '.')
92         {
93                 str[len] = '0';
94                 str[len + 1] = '\0';
95         }
96         return string(str);
97 }
98
99
100 vector<string> multipr::getListMeshes(const char* pMEDfilename)
101 {
102         if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
103         
104         vector<string> res;
105         med_err ret;
106         
107         //---------------------------------------------------------------------
108         // Open MED file (READ_ONLY)
109         //---------------------------------------------------------------------
110         med_idt file = MEDouvrir(const_cast<char*>(pMEDfilename), MED_LECTURE); // open MED file for reading
111         if (file <= 0) throw multipr::IOException("MED file not found or not a sequential MED file", __FILE__, __LINE__);
112         
113         //---------------------------------------------------------------------
114         // Read name of meshes
115         //---------------------------------------------------------------------
116         med_int nbMeshes = MEDnMaa(file);
117         if (nbMeshes <= 0) throw multipr::IOException("i/o error while reading number of meshes in MED file", __FILE__, __LINE__);      
118         
119         // for each mesh in the file (warning: first mesh is number 1)
120         for (int itMesh = 1 ; itMesh <= nbMeshes ; itMesh++)
121         {
122                 char meshName[MED_TAILLE_NOM + 1];
123                 
124                 int meshDim;
125                 med_maillage meshType;
126                 char meshDesc[MED_TAILLE_DESC + 1]; 
127                 
128                 ret = MEDmaaInfo(
129                         file, 
130                         itMesh, 
131                         meshName, 
132                         &meshDim, 
133                         &meshType, 
134                         meshDesc);
135                         
136                 if (ret != 0) throw multipr::IOException("i/o error while reading mesh information in MED file", __FILE__, __LINE__);
137                 
138                 res.push_back(meshName);
139         }
140         
141         //---------------------------------------------------------------------
142         // Close the MED file
143         //---------------------------------------------------------------------
144         ret = MEDfermer(file);
145         if (ret != 0) throw multipr::IOException("i/o error while closing MED file", __FILE__, __LINE__);
146         
147         return res;
148 }
149
150
151 vector<pair<string,int> > multipr::getListFields(const char* pMEDfilename)
152 {
153         if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
154         
155         vector<pair<string, int> > res;
156         med_err ret;
157         
158         //---------------------------------------------------------------------
159         // Open MED file (READ_ONLY)
160         //---------------------------------------------------------------------
161         med_idt file = MEDouvrir(const_cast<char*>(pMEDfilename), MED_LECTURE); // open MED file for reading
162         if (file <= 0) throw multipr::IOException("MED file not found or not a sequential MED file", __FILE__, __LINE__);
163         
164         //---------------------------------------------------------------------
165         // Read number of fields
166         //---------------------------------------------------------------------
167         med_int numFields = MEDnChamp(file, 0);
168         if (numFields <= 0) throw IOException("", __FILE__, __LINE__);
169         
170         //---------------------------------------------------------------------
171         // For each field, read its name
172         //---------------------------------------------------------------------
173         for (int itField = 1 ; itField <= numFields ; itField++)
174         {
175                 char           name[MED_TAILLE_NOM + 1];
176                 med_type_champ type;
177                 
178                 med_int numComponents = MEDnChamp(file, itField);
179         
180                 if (numComponents < 0) throw IOException("", __FILE__, __LINE__);
181                 
182                 // collect scalar field only (not vectorial fields)
183                 if (numComponents != 1) 
184                         continue;
185                 
186                 char* strComponent = new char[numComponents * MED_TAILLE_PNOM + 1];
187                 char* strUnit      = new char[numComponents * MED_TAILLE_PNOM + 1];
188                 
189                 strComponent[0] = '\0';
190                 strUnit[0] = '\0';
191                 
192                 med_err ret = MEDchampInfo(
193                         file, 
194                         itField, 
195                         name, 
196                         &(type), 
197                         strComponent, 
198                         strUnit, 
199                         numComponents);
200                 
201                 if (ret != 0) throw IOException("", __FILE__, __LINE__);
202                 
203                 delete[] strUnit;
204                 delete[] strComponent;
205                 
206                 med_int numTimeStep = MEDnPasdetemps(
207                         file, 
208                         name, 
209                         MED_NOEUD, 
210                         (med_geometrie_element) 0);
211                 
212                 if (numTimeStep < 0) throw IOException("", __FILE__, __LINE__);
213                 
214                 if (numTimeStep == 0)
215                 {
216                         numTimeStep = MEDnPasdetemps(
217                                 file, 
218                                 name, 
219                                 MED_MAILLE, 
220                                 MED_TETRA10);
221                 
222                         if (numTimeStep  < 0) throw IOException("", __FILE__, __LINE__);        
223                         
224                 }
225                 
226                 res.push_back(make_pair(name, numTimeStep));
227         }
228         
229         //---------------------------------------------------------------------
230         // Close the MED file
231         //---------------------------------------------------------------------
232         ret = MEDfermer(file);
233         if (ret != 0) throw multipr::IOException("i/o error while closing MED file", __FILE__, __LINE__);
234         
235         return res;
236 }
237
238
239 // EOF