WSL/SLF GitLab Repository

Array.h 14.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/***********************************************************************************/
/*  Copyright 2009 WSL Institute for Snow and Avalanche Research    SLF-DAVOS      */
/***********************************************************************************/
/* This file is part of MeteoIO.
    MeteoIO is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MeteoIO is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with MeteoIO.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ARRAY_H
#define ARRAY_H

#include <vector>
#include <limits>
#include <iostream>

25
26
27
#include <meteoio/IOUtils.h>
#include <meteoio/IOExceptions.h>

28
29
30
31
32
33
34
#define NOSAFECHECKS
namespace mio {

/**
 * @class Array
 * @brief The template class Array is a 1D array (vector) able to hold any type of object as datatype
 *
35
 * @ingroup data_str
36
37
38
39
40
41
42
43
 * @author Thomas Egger
 * @date   2009-05-02
 */

template<class T> class Array {
	public:
		Array(const unsigned int& asize=0);

44
45
46
47
48
49
50
		/**
		* A constructor that creates an array filled with constant values
		* @param asize size of the new array
		* @param init initial value to fill the array with
		*/
		Array(const unsigned int& asize, const T& init);

51
52
		/**
		* @brief set how to process nodata values (ie: as nodata or as normal numbers)
53
54
		* @param i_keep_nodata true means that NODATA is interpreted as NODATA, false means that it is a normal number
		By default, arrays keep nodata.
55
		*/
56
57
58
59
60
61
62
		void setKeepNodata(const bool i_keep_nodata);

		/**
		* @brief get how to process nodata values (ie: as nodata or as normal numbers)
		* @return true means that NODATA is interpreted as NODATA, false means that it is a normal number
		*/
		bool getKeepNodata();
63

64
65
		unsigned int size();
		void resize(const unsigned int& asize);
66
		void resize(const unsigned int& asize, const T& init);
67
68
69
70
71
72
73
		void clear();
		void insertAt(const int& index, T e);
		void removeAt(const unsigned int& index);
		/**
		* @brief returns the minimum value contained in the grid
		* @return minimum value
		*/
74
		T getMin() const;
75
76
77
78
		/**
		* @brief returns the maximum value contained in the grid
		* @return maximum value
		*/
79
		T getMax() const;
80
81
82
83
		/**
		* @brief returns the mean value contained in the grid
		* @return mean value
		*/
84
		T getMean() const;
85
		/**
86
87
88
		* @brief returns the number of points contained in the grid.
		* If setNodataHandling(IOUtils::RAW_NODATA), then the number of points is the size of the grid.
		* If setNodataHandling(IOUtils::PARSE_NODATA), then it is the number of non-nodata values in the grid
89
		* @return count
90
		*/
91
		size_t getCount() const;
92
93
94
95
		/**
		* @brief returns the grid of the absolute value of values contained in the grid
		* @return grid of abs(grid)
		*/
96
97
		const Array<T> getAbs() const;
		void abs();
98

99
100
101
102
103
104
105
106

		template<class P> friend std::ostream& operator<<(std::ostream& os, const Array<P>& array);
		T& operator [](const unsigned int& index);
		const T operator [](const unsigned int& index) const;
		T& operator ()(const unsigned int& index);
		const T operator ()(const unsigned int& index) const;

		Array<T>& operator =(const Array<T>&);
107
		Array<T>& operator =(const T& value);
108

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
		Array<T>& operator+=(const T& rhs);
		const Array<T> operator+(const T& rhs);
		Array<T>& operator+=(const Array<T>& rhs);
		const Array<T> operator+(const Array<T>& rhs);

		Array<T>& operator-=(const T& rhs);
		const Array<T> operator-(const T& rhs);
		Array<T>& operator-=(const Array<T>& rhs);
		const Array<T> operator-(const Array<T>& rhs);

		Array<T>& operator*=(const T& rhs);
		const Array<T> operator*(const T& rhs);
		Array<T>& operator*=(const Array<T>& rhs);
		const Array<T> operator*(const Array<T>& rhs);

		Array<T>& operator/=(const T& rhs);
		const Array<T> operator/(const T& rhs);
		Array<T>& operator/=(const Array<T>& rhs);
		const Array<T> operator/(const Array<T>& rhs);

	protected:
		std::vector<T> vecData; ///<the actual data structure, that holds the objects of type T
		unsigned int nx; ///<this is introduced to omit the costly vecData.size()
132
		bool keep_nodata;
133
134
135
136
};

template<class T> Array<T>::Array(const unsigned int& asize) {
	resize(asize);
137
138
139
140
}

template<class T> Array<T>::Array(const unsigned int& asize, const T& init) {
	resize(asize, init);
141
142
}

143
144
template<class T> void Array<T>::setKeepNodata(const bool i_keep_nodata) {
	keep_nodata = i_keep_nodata;
145
146
}

147
148
149
150
151
template<class T> bool Array<T>::getKeepNodata() {
	return keep_nodata;
}


152
153
154
155
156
template<class T> unsigned int Array<T>::size() {
	return nx;
}

template<class T> void Array<T>::resize(const unsigned int& asize) {
157
158
	vecData.resize(asize);
	nx = asize;
159
160
}

161
162
163
164
165
166
template<class T> void Array<T>::resize(const unsigned int& asize, const T& init) {
	resize(asize);
	std::fill(vecData.begin(), vecData.end(), init);
}


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
template<class T> T& Array<T>::operator()(const unsigned int& index) {
#ifndef NOSAFECHECKS
	if (index >= nx) {
		throw IndexOutOfBoundsException("", AT);
	}
#endif
	return vecData[index];
}

template<class T> const T Array<T>::operator()(const unsigned int& index) const {
#ifndef NOSAFECHECKS
	if (index >= nx) {
		throw IndexOutOfBoundsException("", AT);
	}
#endif
	return vecData[index];
}

template<class T> T& Array<T>::operator [](const unsigned int& index) {
#ifndef NOSAFECHECKS
	if (index >= nx) {
		throw IndexOutOfBoundsException("", AT);
	}
#endif
191

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
	return vecData[index];
}

template<class T> const T Array<T>::operator [](const unsigned int& index) const {
#ifndef NOSAFECHECKS
	if (index >= nx) {
		throw IndexOutOfBoundsException("", AT);
	}
#endif

	return vecData[index];
}

template<class T> void Array<T>::clear() {
	vecData.clear();
	nx = 0;
}

template<class T> std::ostream& operator<<(std::ostream& os, const Array<T>& array) {
	os << "<array1d>\n";
	for(unsigned int ii=0; ii<array.nx; ii++) {
		os << array(ii) << " ";
	}
	os << "\n</array1d>\n";
	return os;
}

template<class T> void Array<T>::insertAt(const int& index, T e) {
	if (index < 0) {
		vecData.push_back(e);
                nx++;
	} else if ((index >= 0) && (index < (int)vecData.size())) {
		vecData.insert(vecData.begin() + index, e);
		nx++;
	} else {
		throw IndexOutOfBoundsException("", AT);
	}
}

template<class T> void Array<T>::removeAt(const unsigned int& index) {
	if (index < vecData.size()) {
		vecData.erase(vecData.begin()+index);
		nx--;
	}
}

238
template<class T> T Array<T>::getMin() const {
239
240
241

	T min = std::numeric_limits<T>::max();

242
	if(keep_nodata==false) {
243
244
245
246
247
		for (unsigned int ii=0; ii<nx; ii++) {
			const T val = vecData[ii];
			if(val<min) min=val;
		}
		return min;
248
	} else {
249
250
251
252
253
254
255
256
257
		for (unsigned int ii=0; ii<nx; ii++) {
			const T val = vecData[ii];
			if(val!=IOUtils::nodata && val<min) min=val;
		}
		if(min!=std::numeric_limits<T>::max()) return min;
		else return (T)IOUtils::nodata;
	}
}

258
template<class T> T Array<T>::getMax() const {
259
260
261

	T max = -std::numeric_limits<T>::max();

262
	if(keep_nodata==false) {
263
264
265
266
267
		for (unsigned int ii=0; ii<nx; ii++) {
			const T val = vecData[ii];
			if(val>max) max=val;
		}
		return max;
268
	} else {
269
270
271
272
273
274
275
276
277
		for (unsigned int ii=0; ii<nx; ii++) {
			const T val = vecData[ii];
			if(val!=IOUtils::nodata && val>max) max=val;
		}
		if(max!=-std::numeric_limits<T>::max()) return max;
		else return (T)IOUtils::nodata;
	}
}

278
template<class T> T Array<T>::getMean() const {
279
280
281

	T mean = 0;

282
	if(keep_nodata==false) {
283
284
285
286
287
288
289
		for (unsigned int ii=0; ii<nx; ii++) {
			const T val = vecData[ii];
			mean += val;
		}
		const unsigned int count = nx;
		if(count>0) return mean/(T)(count);
		else return (T)0;
290
	} else {
291
292
293
294
295
296
297
298
299
300
301
302
303
		unsigned int count = 0;
		for (unsigned int ii=0; ii<nx; ii++) {
			const T val = vecData[ii];
			if(val!=IOUtils::nodata) {
				mean += val;
				count++;
			}
		}
		if(count>0) return mean/(T)(count);
		else return (T)IOUtils::nodata;
	}
}

304
template<class T> size_t Array<T>::getCount() const
305
{
306
	if(keep_nodata==false) {
307
		return (size_t)nx;
308
	} else {
309
		size_t count = 0;
310
		for (unsigned int ii=0; ii<nx; ii++) {
311
			if(vecData[ii]!=IOUtils::nodata) count++;
312
		}
313
		return count;
314
315
316
	}
}

317
template<class T> void Array<T>::abs() {
318
	if(std::numeric_limits<T>::is_signed) {
319
		if(keep_nodata==false) {
320
321
322
323
			for (unsigned int ii=0; ii<nx; ii++) {
				T& val = operator()(ii);
				if(val<0) val=-val;
			}
324
		} else {
325
326
327
328
329
330
331
332
333
			for (unsigned int ii=0; ii<nx; ii++) {
				T& val = operator()(ii);
				if(val<0 && val!=IOUtils::nodata) val=-val;
			}
		}
	}
}


334
template<class T> const Array<T> Array<T>::getAbs() const {
335
	Array<T> result = *this; //make a copy
336
	result.abs(); //already implemented
337
338
339
340

	return result;
}

341
342
343
344
345
//arithmetic operators
template<class T> Array<T>& Array<T>::operator=(const Array<T>& source) {
	if(this != &source) {
		vecData = source.vecData;
		nx = source.nx;
346
		keep_nodata = source.keep_nodata;
347
348
349
350
	}
	return *this;
}

351
template<class T> Array<T>& Array<T>::operator=(const T& value) {
352
	//reset every single member of the Array<T>
353
354
355
356
	std::fill(vecData.begin(), vecData.end(), value);
	return *this;
}

357
358
359
360
361
362
363
template<class T> Array<T>& Array<T>::operator+=(const Array<T>& rhs)
{
	//They have to have equal size
	if (rhs.nx != nx)
		throw IOException("Trying to add two Array objects with different dimensions", AT);

	//Add to every single member of the Array<T>
364
365
366
367
368
369
370
371
372
373
374
	if(keep_nodata==false) {
		for (unsigned int ii=0; ii<nx; ii++) {
			operator()(ii) += rhs(ii);
		}
	} else {
		for (unsigned int ii=0; ii<nx; ii++) {
			if(operator()(ii)==IOUtils::nodata || rhs(ii)==IOUtils::nodata)
				operator()(ii) = IOUtils::nodata;
			else
				operator()(ii) += rhs(ii);
		}
375
	}
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390

	return *this;
}

template<class T> const Array<T> Array<T>::operator+(const Array<T>& rhs)
{
	Array<T> result = *this; //make a copy
	result += rhs; //already implemented

	return result;
}

template<class T> Array<T>& Array<T>::operator+=(const T& rhs)
{
	//Add to every single member of the Array<T>
391
392
393
394
395
396
397
398
399
	if(keep_nodata==false) {
		for (unsigned int ii=0; ii<nx; ii++) {
			operator()(ii) += rhs;
		}
	} else {
		for (unsigned int ii=0; ii<nx; ii++) {
			if(operator()(ii)!=IOUtils::nodata)
				operator()(ii) += rhs;
		}
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
	}

	return *this;
}

template<class T> const Array<T> Array<T>::operator+(const T& rhs)
{
	Array<T> result = *this;
	result += rhs; //already implemented

	return result;
}

template<class T> Array<T>& Array<T>::operator-=(const Array<T>& rhs)
{
	//They have to have equal size
	if (rhs.nx != nx)
		throw IOException("Trying to substract two Array objects with different dimensions", AT);

	//Substract to every single member of the Array<T>
420
421
422
423
424
425
426
427
428
429
430
	if(keep_nodata==false) {
		for (unsigned int ii=0; ii<nx; ii++) {
			operator()(ii) -= rhs(ii);
		}
	} else {
		for (unsigned int ii=0; ii<nx; ii++) {
			if(operator()(ii)==IOUtils::nodata || rhs(ii)==IOUtils::nodata)
				operator()(ii) = IOUtils::nodata;
			else
				operator()(ii) -= rhs(ii);
		}
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
	}

	return *this;
}

template<class T> const Array<T> Array<T>::operator-(const Array<T>& rhs)
{
	Array<T> result = *this; //make a copy
	result -= rhs; //already implemented

	return result;
}

template<class T> Array<T>& Array<T>::operator-=(const T& rhs)
{
	//Substract to every single member of the Array<T>
447
448
449
450
451
452
453
454
455
	if(keep_nodata==false) {
		for (unsigned int ii=0; ii<nx; ii++) {
			operator()(ii) -= rhs;
		}
	} else {
		for (unsigned int ii=0; ii<nx; ii++) {
			if(operator()(ii)!=IOUtils::nodata)
				operator()(ii) -= rhs;
		}
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
	}

	return *this;
}

template<class T> const Array<T> Array<T>::operator-(const T& rhs)
{
	Array<T> result = *this;
	result -= rhs; //already implemented

	return result;
}

template<class T> Array<T>& Array<T>::operator*=(const Array<T>& rhs)
{
	//They have to have equal size
	if (rhs.nx != nx)
		throw IOException("Trying to multiply two Array objects with different dimensions", AT);

475
476
477
478
479
480
481
482
483
484
485
486
	//Multiply every single member of the Array<T>
	if(keep_nodata==false) {
		for (unsigned int ii=0; ii<nx; ii++) {
			operator()(ii) *= rhs(ii);
		}
	} else {
		for (unsigned int ii=0; ii<nx; ii++) {
			if(operator()(ii)==IOUtils::nodata || rhs(ii)==IOUtils::nodata)
				operator()(ii) = IOUtils::nodata;
			else
				operator()(ii) *= rhs(ii);
		}
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
	}

	return *this;
}

template<class T> const Array<T> Array<T>::operator*(const Array<T>& rhs)
{
	Array<T> result = *this; //make a copy
	result *= rhs; //already implemented

	return result;
}

template<class T> Array<T>& Array<T>::operator*=(const T& rhs)
{
502
503
504
505
506
507
508
509
510
511
	//Multiply every single member of the Array<T>
	if(keep_nodata==false) {
		for (unsigned int ii=0; ii<nx; ii++) {
			operator()(ii) *= rhs;
		}
	} else {
		for (unsigned int ii=0; ii<nx; ii++) {
			if(operator()(ii)!=IOUtils::nodata)
				operator()(ii) *= rhs;
		}
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
	}

	return *this;
}

template<class T> const Array<T> Array<T>::operator*(const T& rhs)
{
	Array<T> result = *this;
	result *= rhs; //already implemented

	return result;
}

template<class T> Array<T>& Array<T>::operator/=(const Array<T>& rhs)
{
	//They have to have equal size
	if (rhs.nx != nx)
		throw IOException("Trying to divide two Array objects with different dimensions", AT);

	//Divide every single member of the Array<T>
532
533
534
535
536
537
538
539
540
541
542
	if(keep_nodata==false) {
		for (unsigned int ii=0; ii<nx; ii++) {
			operator()(ii) /= rhs(ii);
		}
	} else {
		for (unsigned int ii=0; ii<nx; ii++) {
			if(operator()(ii)==IOUtils::nodata || rhs(ii)==IOUtils::nodata)
				operator()(ii) = IOUtils::nodata;
			else
				operator()(ii) /= rhs(ii);
		}
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
	}

	return *this;
}

template<class T> const Array<T> Array<T>::operator/(const Array<T>& rhs)
{
	Array<T> result = *this; //make a copy
	result /= rhs; //already implemented

	return result;
}

template<class T> Array<T>& Array<T>::operator/=(const T& rhs)
{
	//Divide every single member of the Array<T>
559
560
561
562
563
564
565
566
567
	if(keep_nodata==false) {
		for (unsigned int ii=0; ii<nx; ii++) {
			operator()(ii) /= rhs;
		}
	} else {
		for (unsigned int ii=0; ii<nx; ii++) {
			if(operator()(ii)!=IOUtils::nodata)
				operator()(ii) /= rhs;
		}
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
	}

	return *this;
}

template<class T> const Array<T> Array<T>::operator/(const T& rhs)
{
	Array<T> result = *this;
	result /= rhs; //already implemented

	return result;
}

} //end namespace mio

#endif