C++ main module for emicrom Package  1.0
EMM_Array.hpp
Go to the documentation of this file.
1 #ifndef EMM_Array_HPP
2 #define EMM_Array_HPP
3 
4 #include "CORE_Time.h"
5 #include "CORE_Out.h"
6 #include "EMM_Exception.h"
7 
8 
9 template<class T>
10 tBoolean EMM_Array<T>::saveToFile(const tString& fileName) const {
11  ofstream f(fileName.c_str(),ios::out);
12 
13  if (f) {
14  CORE_Time time;
15  f << "# file generated at "<<time.toString("DD/MM/YYYY-HHhMM")<<"\n";
16  f << "# save a steady field \n";
17  f << "#N is the number of points\n";
18  f << "#values : the values of the array. One value by line\n";
20  f <<n<<"\n";
21 
22  //save to stream
23  saveToStream(f,1);
24 
25 
26  //free the file pointer
27  f.close();
28  } else {
29  throw EMM_Exception("EMM_Array::saveToFile("+fileName+")","impossible to open file for saving");
30  return false;
31  }
32  return true;
33 }
34 
35 
36 template<class T>
37 tBoolean EMM_Array<T>::saveToStream(ofstream& f,const tUSInt& dim) const {
39  tUIndex i,p=n/dim;
40  tUSInt j;
41 
42  const T *vs=&(*this)[0];
43  //read all the points
44  for (i=0;i<p;i++) {
45  //copy dim values per line
46  for (j=0;j<dim;j++) {
47  f << CORE_String::toString(*vs)<<"\t";
48  vs++;
49  }
50  f<<"\n";
51  }
52  return true;
53 }
54 
55 template<class T>
56 tBoolean EMM_Array<T>::saveToStream(ofstream& f,const vector<tUSInt>& dims,const vector<const EMM_Array<T>*>& indics) {
57 
58  tUIndex nIndics=indics.size();
59  //verify the size of dims equals to size of indics
60  if (dims.size()!=indics.size()) return false;
61 
62  //verify the packs number are the same
63  //packs number
64  tUIndex nPacks=0;
65  //pointers to indicator
66 
67  vector<const T*> arrays(nIndics);
68  typename vector<const T* >::iterator iterA=arrays.begin();
69 
70  //iterator on indicators
71  typename vector<const EMM_Array<T>*>::const_iterator iterI=indics.begin();
72 
73  //iterator on dimension of packs
74  typename vector<tUSInt>::const_iterator iterD=dims.begin();
75 
76  while (iterI!=indics.end()) {
77  //set the pointer
78  *iterA=&(*(*iterI))[0];
79 
80  if (nPacks==0) {
81  //compute the numbe rof packs
82  nPacks=(*iterI)->getSize()/(*iterD);
83  } else {
84  //the number of packs must be the same for all indicators
85  if ((*iterI)->getSize()/(*iterD)<nPacks) return false;
86  }
87  //next iter
88  iterI++;
89  iterD++;
90  iterA++;
91  }
92 
93 
94  //write the arrays
95  tUIndex i;
96  tUSInt j;
97  for (i=0;i<nPacks;i++) {
98  //write the indicators by packed of size (*iterD)
99  iterD=dims.begin();
100  for (iterA=arrays.begin();iterA!=arrays.end();iterA++) {
101  for (j=0;j<(*iterD);j++) {
102  //write the values of the indicator
103  f << CORE_String::toString((*(*iterA)))<<"\t";
104  //next values of indicator
105  (*iterA)++;
106  }//loop on size of pack
107  //next packs number of array
108  iterD++;
109  }//loop on indicators
110  f<<"\n";
111  }//end loop on pack
112  return true;
113 }
114 
115 template<class T>
117  ifstream f(fileName.c_str(),ios::in);
118 
119  if (f) {
120  // number of line
121  tUInteger numLine=0;
123 
124  //for reading a line
125  tUSInt len=1024;
126  char line[len];
127 
128  //for analysing a line
129  SP::CORE_String tokenizer=CORE_String::New();
130 
131  //number of tokens per line
132  tUInteger nTokens;
133 
134  //index of comment char
135  tUIndex iComment;
136 
137 
138  tUIndex nPoints=0;
139 
140 
141  tUInteger nEmptyLines=0,nMaxEmptyLines=10;
142 
143  while (!f.eof() && (numLine<maxLines) && (nEmptyLines<nMaxEmptyLines) ) {
144  //read the line
145  f.getline(line,len);
146  numLine++;
147 
148  //analyse the line
149  tokenizer->setString(line);
150 
151  //ignore comment part
152  iComment=tokenizer->getString().find("#");
153  if (iComment!=tString::npos) {
154 
155  //comment line
156  if (iComment==0) continue;
157 
158  //ignore all chars after #
159  tokenizer->setString(tokenizer->getString().substr(0,iComment));
160  }
161 
162  //erase blanks before and after line
163  tokenizer->trim();
164 
165  //anaylyse all the tokens of the line
166  tokenizer->tokenize();
167 
168  //get the number of token
169  nTokens=tokenizer->getTokensNumber();
170  if (nTokens==0) {
171  nEmptyLines++;
172 
173  } else {
174  nEmptyLines=0;
175  if ((nPoints==0) && (nTokens>=1)) {
176  //read the number of points
177  nPoints=CORE_Integer::parseInt(tokenizer->nextToken());
178  CORE_Array<T>::setSize(nPoints);
179 
180  //load the data from stream
181  if (nPoints>0) loadFromStream(f,1);
182 
183  }
184  }
185 
186 
187 
188  }//reading next line
189 
190 
191 
192  //free the file pointer
193  f.close();
194  if (nEmptyLines>=nMaxEmptyLines) {
195  throw EMM_Exception("EMM_Array::loadFromFile("+fileName+")"," too many empty lines detected "+
196  CORE_Integer::toString(nEmptyLines)+ "max empty lines is "+CORE_Integer::toString(nMaxEmptyLines) );
197  return false;
198  }
199  } else {
200  throw EMM_Exception("EMM_Array::loadFromFile("+fileName+")","impossible to open file");
201  return false;
202  }
203  return true;
204 }
205 
206 template<class T>
209 
210  //number of packs
211  tUIndex i,p=n/dim;
212  tUSInt j;
213  T *vs=&(*this)[0];
214  //read all the points
215  for (i=0;i<p;i++) {
216  //read the values by pack of size dim
217  for (j=0;j<dim;j++) {
218  if (f.eof()) {
219  throw EMM_Exception("EMM_Array<T>::loadFromStream(f,dim)",
220  "unexpected end of file for reading pack "+CORE_Integer::toString(i)+" of size "+CORE_Integer::toString(dim));
221  }
222  f >> (*vs);
223  if (isnan(*vs)) {
224  throw EMM_Exception("EMM_Array<T>::loadFromStream(...)",
225  "nan error in reading file at line "+CORE_Integer::toString(i));
226  }
227  vs++;
228  } //end loop on j
229 
230  }//end loop on i
231 
232  return true;
233 }
234 template<class T>
235 tBoolean EMM_Array<T>::loadFromStream(ifstream& f,const vector<tUSInt>& dims,const vector<EMM_Array<T>* >& arrays) const {
236 
237 
238 
239  //number of arrays to load
240  tUIndex nArrays=arrays.size();
241 
242  //dims'size & arrays'size must be same
243  if (dims.size()!=arrays.size()) return false;
244 
245 
246  //vector of pointers of arrays
247  vector<T*> Parrays(nArrays);
248  typename vector<T* >::iterator iterP=Parrays.begin();
249 
250  //arrays interator
251  typename vector<EMM_Array<T>*>::const_iterator iterA=arrays.begin();
252 
253  //dim iterator
254  typename vector<tUSInt>::const_iterator iterD=dims.begin();
255 
256  //fill the pointer arrays
257  // get the size of the pointers
258  tUIndex size=0;
259  while (iterA!=arrays.end()) {
260  //set the pointer of indicators
261  *iterP=&(*(*iterA))[0];
262  //compute the size of arrays: size*dim=array.size()
263  if (size==0) {
264  //initialize it
265  size=(*iterA)->getSize()/(*iterD);
266  } else {
267  //the size must be the same for each array to load
268  if ((*iterA)->getSize()/(*iterD)<size) {
270  "error in reading aim file: the size of arrays to read is not compatible");
271  return false;
272  }
273  }
274 
275  //next iter
276  iterA++;
277  iterD++;
278  iterP++;
279  }
280 
281 
282  //read the arrays
283  tUIndex i;
284  tUSInt j,dim;
285  tString token;
286  tBoolean jumpLine=false;
287  for (i=0;i<size;i++) {
288 
289 
290  //loop on arrays
291  iterD=dims.begin();
292  iterP=Parrays.begin();
293  while (iterP!=Parrays.end()) {
294  dim=(*iterD);//dimension of the array
295  T* &values=(*iterP);//values of the array
296 
297  for (j=0;j<dim;j++) {
298  //write the values of the indicator
299  if (f.eof()) {
300  throw EMM_Exception("EMM_Array<T>::loadFromStream(...)",
301  "unexpected end of file for reading pack "+CORE_Integer::toString(i)+" of size "+CORE_Integer::toString((*iterD)));
302  }
303  f >> token;
304  if (CORE_String::isDigit(token[0])) {
305  (*values)=CORE_Real::parseReal(token);
306  //next values of arrays
307  values++;
308  } else {
309  jumpLine=true;
310  break;
311  }
312 
313  }
314  if (jumpLine) break;
315  //next array
316  iterD++;
317  iterP++;
318  }
319  if (jumpLine) {
320  size++;
321  jumpLine=false;
322  }
323  }
324  return true;
325 }
326 
327 
328 #endif
const tUIndex & getSize() const
return the size of the array for reading
Definition: CORE_Array.h:1018
static tLLInt parseInt(const tString &str)
return the integer associated to the string
Definition: CORE_Integer.cpp:102
virtual tString toString() const
return the time into a string
Definition: CORE_Time.cpp:344
tBoolean loadFromStream(ifstream &f, const vector< tUSInt > &dims, const vector< EMM_Array< T > * > &arrays) const
load the values to indicators from the stream
Definition: EMM_Array.hpp:235
virtual void println(const tFlag &type, const tString &message)
print the message with an end of line
Definition: CORE_Out.h:342
#define tUInteger
Definition: types.h:91
this class describes a time class
Definition: CORE_Time.h:64
static tLDouble parseReal(const tString &str)
return the real associated to the string
Definition: CORE_Real.h:201
#define tUSInt
Definition: types.h:28
#define tBoolean
Definition: types.h:139
void setSize(const tUIndex &n)
set the size
Definition: CORE_Array.h:292
virtual tString toString() const
return the string associated to the string
Definition: CORE_String.h:223
tString toString() const
return the string associated to the integer
Definition: CORE_Integer.h:106
tBoolean loadFromFile(const tString &fileName)
load the steady array from file
Definition: EMM_Array.hpp:116
tBoolean saveToStream(ofstream &f) const
save the values of the array into the stream with 1 value per line
Definition: EMM_Array.h:208
tBoolean saveToFile(const tString &fileName) const
save the steady array into file
Definition: EMM_Array.hpp:10
#define tUIndex
Definition: types.h:126
#define tString
Definition: types.h:135
static CORE_Out & out()
get the output
Definition: CORE_Object.h:198
static const tFlag ERROR_MSG
Definition: CORE_Out.h:41
this class describes the exceptions raised for E-MicromM package
Definition: EMM_Exception.h:14
static SP::CORE_String New()
create a class String
Definition: CORE_String.h:96
static tBoolean isDigit(const char &v)
return if the char is a digit
Definition: CORE_String.h:719
This class describes a general array.
Definition: EMM_Array.h:18
static tUInt getMaxUInt()
get the max value for tUInt type
Definition: CORE_Object.h:399