]> SALOME platform Git repositories - modules/multipr.git/blob - src/MULTIPR/MULTIPR_Utils.cxx
Salome HOME
Module MULTIPR
[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 string multipr::removeExtension(const char* pFilename, const char* pExtension)
46 {
47         string strPrefix(pFilename); 
48         strPrefix.erase(strPrefix.rfind(pExtension), strlen(pExtension));
49         
50         return strPrefix;
51 }
52
53
54 void multipr::printArray2D(
55         const med_float* pData, 
56         const int        pNumElt,
57         const int        pDimElt,
58         const char*      pPrefix)
59 {
60         for (int itElt = 0 ; itElt < pNumElt ; itElt++)
61         {
62                 cout << pPrefix << " " << (itElt + 1) << ": ";
63                 for (int itDim = 0 ; itDim < pDimElt ; itDim++)
64                 {
65                         cout << pData[itElt * pDimElt + itDim] << " ";
66                 }
67                 cout << endl;
68         }
69 }
70
71
72 string multipr::realToString(med_float mV)
73 {
74         char str[32];
75         sprintf(str, "%lf", mV);
76         trim(str, '0');
77         int len = strlen(str);
78         if (str[len - 1] == '.')
79         {
80                 str[len] = '0';
81                 str[len + 1] = '\0';
82         }
83         return string(str);
84 }
85
86
87 vector<string> multipr::getListMeshes(const char* pMEDfilename)
88 {
89         if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
90         
91         vector<string> res;
92         med_err ret;
93         
94         //---------------------------------------------------------------------
95         // Open MED file (READ_ONLY)
96         //---------------------------------------------------------------------
97         med_idt file = MEDouvrir(const_cast<char*>(pMEDfilename), MED_LECTURE); // open MED file for reading
98         if (file <= 0) throw multipr::IOException("MED file not found or not a sequential MED file", __FILE__, __LINE__);
99         
100         //---------------------------------------------------------------------
101         // Read name of meshes
102         //---------------------------------------------------------------------
103         med_int nbMeshes = MEDnMaa(file);
104         if (nbMeshes <= 0) throw multipr::IOException("i/o error while reading number of meshes in MED file", __FILE__, __LINE__);      
105         
106         // for each mesh in the file (warning: first mesh is number 1)
107         for (int itMesh = 1 ; itMesh <= nbMeshes ; itMesh++)
108         {
109                 char meshName[MED_TAILLE_NOM + 1];
110                 
111                 int meshDim;
112                 med_maillage meshType;
113                 char meshDesc[MED_TAILLE_DESC + 1]; 
114                 
115                 ret = MEDmaaInfo(
116                         file, 
117                         itMesh, 
118                         meshName, 
119                         &meshDim, 
120                         &meshType, 
121                         meshDesc);
122                         
123                 if (ret != 0) throw multipr::IOException("i/o error while reading mesh information in MED file", __FILE__, __LINE__);
124                 
125                 res.push_back(meshName);
126         }
127         
128         //---------------------------------------------------------------------
129         // Close the MED file
130         //---------------------------------------------------------------------
131         ret = MEDfermer(file);
132         if (ret != 0) throw multipr::IOException("i/o error while closing MED file", __FILE__, __LINE__);
133         
134         return res;
135 }
136
137
138 vector<pair<string,int> > multipr::getListFields(const char* pMEDfilename)
139 {
140         if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
141         
142         vector<pair<string, int> > res;
143         med_err ret;
144         
145         //---------------------------------------------------------------------
146         // Open MED file (READ_ONLY)
147         //---------------------------------------------------------------------
148         med_idt file = MEDouvrir(const_cast<char*>(pMEDfilename), MED_LECTURE); // open MED file for reading
149         if (file <= 0) throw multipr::IOException("MED file not found or not a sequential MED file", __FILE__, __LINE__);
150         
151         //---------------------------------------------------------------------
152         // Read number of fields
153         //---------------------------------------------------------------------
154         med_int numFields = MEDnChamp(file, 0);
155         if (numFields <= 0) throw IOException("", __FILE__, __LINE__);
156         
157         //---------------------------------------------------------------------
158         // For each field, read its name
159         //---------------------------------------------------------------------
160         for (int itField = 1 ; itField <= numFields ; itField++)
161         {
162                 char           name[MED_TAILLE_NOM + 1];
163                 med_type_champ type;
164                 
165                 med_int numComponents = MEDnChamp(file, itField);
166         
167                 if (numComponents < 0) throw IOException("", __FILE__, __LINE__);
168                 
169                 // collect scalar field only (not vectorial fields)
170                 if (numComponents != 1) 
171                         continue;
172                 
173                 char* strComponent = new char[numComponents * MED_TAILLE_PNOM + 1];
174                 char* strUnit      = new char[numComponents * MED_TAILLE_PNOM + 1];
175                 
176                 strComponent[0] = '\0';
177                 strUnit[0] = '\0';
178                 
179                 med_err ret = MEDchampInfo(
180                         file, 
181                         itField, 
182                         name, 
183                         &(type), 
184                         strComponent, 
185                         strUnit, 
186                         numComponents);
187                 
188                 if (ret != 0) throw IOException("", __FILE__, __LINE__);
189                 
190                 delete[] strUnit;
191                 delete[] strComponent;
192                 
193                 med_int numTimeStep = MEDnPasdetemps(
194                         file, 
195                         name, 
196                         MED_NOEUD, 
197                         (med_geometrie_element) 0);
198                 
199                 if (numTimeStep < 0) throw IOException("", __FILE__, __LINE__);
200                 
201                 if (numTimeStep == 0)
202                 {
203                         numTimeStep = MEDnPasdetemps(
204                                 file, 
205                                 name, 
206                                 MED_MAILLE, 
207                                 MED_TETRA10);
208                 
209                         if (numTimeStep  < 0) throw IOException("", __FILE__, __LINE__);        
210                         
211                 }
212                 
213                 res.push_back(make_pair(name, numTimeStep));
214         }
215         
216         //---------------------------------------------------------------------
217         // Close the MED file
218         //---------------------------------------------------------------------
219         ret = MEDfermer(file);
220         if (ret != 0) throw multipr::IOException("i/o error while closing MED file", __FILE__, __LINE__);
221         
222         return res;
223 }
224
225
226 // EOF