WSL/SLF GitLab Repository

Array2D.h 17.4 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/***********************************************************************************/
/*  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 ARRAY2D_H
#define ARRAY2D_H

#include <meteoio/IOExceptions.h>
#include <meteoio/IOUtils.h>

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

#define NOSAFECHECKS

namespace mio {

template <class T> class Array2D;

/**
 * @class Array2DProxy
 * @brief The template class Array2DProxy is a helper class for the template class Array2D
 *        with the purpose of adding the [][] operator to Array2D
 *
 * @author Thomas Egger
 */
template <class T> class Array2DProxy {
	public:
		friend class Array2D<T>;
		T& operator[](const unsigned int& j) {
			return array2D(anx, j);
		}

	private:
49
		Array2DProxy(Array2D<T>& i_array2D, const unsigned int& i_anx) : array2D(i_array2D), anx(i_anx){}
50
51
		Array2D<T>& array2D;
		const unsigned int anx;
52
};
53
54
55

/**
 * @class Array2D
56
57
 * @brief The template class Array2D is a 2D Array (Matrix) able to hold any type of object as datatype. 
 * It relies on the Array2DProxy class to provide the [][] operator (slower than the (i,j) call).
58
 *
59
 * @ingroup data_str
60
61
62
63
64
 * @author Thomas Egger
 */
template<class T> class Array2D {
	public:
		Array2D();
65
66
67
68
69
70

		/**
		* A constructor that creates an array of a given size
		* @param anx number of columns of the new array
		* @param any number of rows of the new array
		*/
71
		Array2D(const unsigned int& anx, const unsigned int& any);
72
73
74
75
76
77
78

		/**
		* A constructor that creates an array filled with constant values
		* @param anx number of columns of the new array
		* @param any number of rows of the new array
		* @param init initial value to fill the array with
		*/
79
80
81
82
		Array2D(const unsigned int& anx, const unsigned int& any, const T& init);

		/**
		* A constructor that can be used to create an Array2D object that is contained in the
83
84
85
86
87
88
89
		* one passed as i_array2D argument. The resulting Array2D object is a by value copy of
		* a subplane of the plane spanned by the i_array2D
		* @param i_array2D array containing to extract the values from
		* @param i_nx lower left corner cell X index
		* @param i_ny lower left corner cell Y index
		* @param i_ncols number of columns of the new array
		* @param i_nrows number of rows of the new array
90
		*/
91
92
		Array2D(const Array2D<T>& i_array2D, const unsigned int& i_nx, const unsigned int& i_ny,
			    const unsigned int& i_ncols, const unsigned int& i_nrows);
93

94
		/**
95
		* @brief A method that can be used to cut out a subplane of an existing Array2D object
96
97
98
99
100
101
102
		* that is passed as i_array2D argument. The resulting Array2D object is a by value copy of
		* a subplane of the plane spanned by the i_array2D
		* @param i_array2D array containing to extract the values from
		* @param i_nx lower left corner cell X index
		* @param i_ny lower left corner cell Y index
		* @param i_ncols number of columns of the new array
		* @param i_nrows number of rows of the new array
103
		*/
104
105
		void subset(const Array2D<T>& i_array2D, const unsigned int& i_nx, const unsigned int& i_ny,
		            const unsigned int& i_ncols, const unsigned int& i_nrows);
106

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
		void resize(const unsigned int& nx, const unsigned int& ny);
		void resize(const unsigned int& nx, const unsigned int& ny, const T& init);
		void size(unsigned int& nx, unsigned int& ny) const;

		/**
		* @brief returns the minimum value contained in the grid
		* @param flag_nodata specify how to process nodata values (see NODATA_HANLDING)
		* @return minimum value
		*/
		T getMin(const IOUtils::nodata_handling flag_nodata=IOUtils::PARSE_NODATA) const;
		/**
		* @brief returns the maximum value contained in the grid
		* @param flag_nodata specify how to process nodata values (see NODATA_HANLDING)
		* @return maximum value
		*/
		T getMax(const IOUtils::nodata_handling flag_nodata=IOUtils::PARSE_NODATA) const;
		/**
		* @brief returns the mean value contained in the grid
		* @param flag_nodata specify how to process nodata values (see NODATA_HANLDING)
		* @return mean value
		*/
		T getMean(const IOUtils::nodata_handling flag_nodata=IOUtils::PARSE_NODATA) const;
129
130
131
132
133
134
135
136
137
138
139
140
141
142
		/**
		* @brief returns the sum of values contained in the grid
		* @param flag_nodata specify how to process nodata values (see NODATA_HANLDING)
		* @return sum
		*/
		T getSum(const IOUtils::nodata_handling flag_nodata=IOUtils::PARSE_NODATA) const;
		/**
		* @brief returns the grid of the absolute value of values contained in the grid
		* @param flag_nodata specify how to process nodata values (see NODATA_HANLDING)
		* @return grid of abs(grid)
		*/
		const Array2D<T> getAbs(const IOUtils::nodata_handling flag_nodata=IOUtils::PARSE_NODATA) const;
		void abs(const IOUtils::nodata_handling flag_nodata=IOUtils::PARSE_NODATA);

143
144
145
146
147
148

		template<class P> friend std::ostream& operator<<(std::ostream& os, const Array2D<P>& array);

		void clear();
		T& operator ()(const unsigned int& x, const unsigned int& y);
		const T operator ()(const unsigned int& x, const unsigned int& y) const;
149
150
		T& operator ()(const unsigned int& i);
		const T operator ()(const unsigned int& i) const;
151
152
153
		Array2DProxy<T> operator[](const unsigned int& i);

		Array2D<T>& operator =(const Array2D<T>&);
154
		Array2D<T>& operator =(const T& value);
155

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
		Array2D<T>& operator+=(const T& rhs);
		const Array2D<T> operator+(const T& rhs);
		Array2D<T>& operator+=(const Array2D<T>& rhs);
		const Array2D<T> operator+(const Array2D<T>& rhs);

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

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

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

	protected:
		std::vector<T> vecData;
		unsigned int nx;
		unsigned int ny;
};

182
183
184
185
186
187
188
template<class T> T& Array2D<T>::operator()(const unsigned int& i) {
	return vecData[i];
}

template<class T> const T Array2D<T>::operator()(const unsigned int& i) const {
	return vecData[i];
}
189
190
191
192
193
194
template<class T> T& Array2D<T>::operator()(const unsigned int& x, const unsigned int& y) {
#ifndef NOSAFECHECKS
	if ((x >= nx) || (y >= ny)) {
		throw IndexOutOfBoundsException("", AT);
	}
#endif
195
196
	//ROW-MAJOR alignment of the vector: fully C-compatible memory layout
	return vecData[x + y*nx];
197
198
199
200
201
202
203
204
}

template<class T> const T Array2D<T>::operator()(const unsigned int& x, const unsigned int& y) const {
#ifndef NOSAFECHECKS
	if ((x >= nx) || (y >= ny)) {
		throw IndexOutOfBoundsException("", AT);
	}
#endif
205
	return vecData[x + y*nx];
206
207
208
}

template<class T> Array2DProxy<T> Array2D<T>::operator[](const unsigned int& i) {
209
	return Array2DProxy<T>(*this, i);
210
211
212
213
214
215
}

template<class T> Array2D<T>::Array2D() {
	nx = ny = 0;
}

216
217
template<class T> Array2D<T>::Array2D(const Array2D<T>& i_array2D, const unsigned int& i_nx, const unsigned int& i_ny,
                                      const unsigned int& i_ncols, const unsigned int& i_nrows)
218
{
219
	subset(i_array2D, i_nx, i_ny, i_ncols, i_nrows);
220
221
}

222
223
template<class T> void Array2D<T>::subset(const Array2D<T>& i_array2D, const unsigned int& i_nx, const unsigned int& i_ny,
                                          const unsigned int& i_ncols, const unsigned int& i_nrows)
224
{
225
	if (((i_nx+i_ncols) > i_array2D.nx) || ((i_ny+i_nrows) > i_array2D.ny))
226
227
		throw IndexOutOfBoundsException("Trying to cut an array to a size bigger than its original size!", AT);

228
	if ((i_ncols == 0) || (i_nrows == 0)) //the plane to copy has to make sense
229
230
		throw IndexOutOfBoundsException("Copying an array into a null sized array!", AT);

231
	resize(i_ncols, i_nrows); //create new Array2D object
232
233

	//Copy by value subspace
234
235
	for (unsigned int jj=0; jj<ny; jj++) {
		for (unsigned int ii=0; ii<nx; ii++) {
236
			operator()(ii,jj) = i_array2D(i_nx+ii, i_ny+jj);
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
		}
	}
}

template<class T> Array2D<T>::Array2D(const unsigned int& anx, const unsigned int& any, const T& init) {
	nx = ny = 0;
	resize(anx,any,init);
}

template<class T> Array2D<T>::Array2D(const unsigned int& anx, const unsigned int& any) {
	nx = ny = 0;
	resize(anx,any);
}

template<class T> void Array2D<T>::resize(const unsigned int& anx, const unsigned int& any) {
252
253
254
255
	clear(); //we won't be able to "rescue" old values, so we reset the whole vector
	vecData.resize(anx*any);
	nx = anx;
	ny = any;
256
257
258
259
}

template<class T> void Array2D<T>::resize(const unsigned int& anx, const unsigned int& any, const T& init) {
	resize(anx, any);
260
	std::fill(vecData.begin(), vecData.end(), init);
261
262
263
264
265
266
267
268
269
270
271
272
273
274
}

template<class T> void Array2D<T>::size(unsigned int& anx, unsigned int& any) const{
	anx=nx;
	any=ny;
}

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

template<class T> std::ostream& operator<<(std::ostream& os, const Array2D<T>& array) {
	os << "<array2d>\n";
275
	for(unsigned int jj=0; jj<array.ny; jj++) {
276
		unsigned int jnx = jj*array.nx;
277
		for (unsigned int ii=0; ii<array.nx; ii++) {
278
			os << array(ii+jnx) << " ";
279
280
281
282
283
284
285
286
287
288
289
		}
		os << "\n";
	}
	os << "</array2d>\n";
	return os;
}

template<class T> T Array2D<T>::getMin(const IOUtils::nodata_handling flag_nodata) const {

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

290
	const unsigned int nxy = ny*nx;
291
	if(flag_nodata==IOUtils::RAW_NODATA) {
292
293
294
		for (unsigned int jj=0; jj<nxy; jj++) {
			const T val = operator()(jj);
			if(val<min) min=val;
295
296
297
		}
		return min;
	} else if(flag_nodata==IOUtils::PARSE_NODATA) {
298
299
300
		for (unsigned int jj=0; jj<nxy; jj++) {
			const T val = operator()(jj);
			if(val!=IOUtils::nodata && val<min) min=val;
301
302
303
304
305
306
307
308
309
310
311
312
		}
		if(min!=std::numeric_limits<T>::max()) return min;
		else return (T)IOUtils::nodata;
	} else {
		throw InvalidArgumentException("Unknown nodata_handling flag",AT);
	}
}

template<class T> T Array2D<T>::getMax(const IOUtils::nodata_handling flag_nodata) const {

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

313
	const unsigned int nxy = ny*nx;
314
	if(flag_nodata==IOUtils::RAW_NODATA) {
315
316
317
		for (unsigned int jj=0; jj<nxy; jj++) {
			const T val = operator()(jj);
			if(val>max) max=val;
318
319
320
		}
		return max;
	} else if(flag_nodata==IOUtils::PARSE_NODATA) {
321
322
323
		for (unsigned int jj=0; jj<nxy; jj++) {
			const T val = operator()(jj);
			if(val!=IOUtils::nodata && val>max) max=val;
324
325
326
327
328
329
330
331
332
333
334
		}
		if(max!=-std::numeric_limits<T>::max()) return max;
		else return (T)IOUtils::nodata;
	} else {
		throw InvalidArgumentException("Unknown nodata_handling flag",AT);
	}
}

template<class T> T Array2D<T>::getMean(const IOUtils::nodata_handling flag_nodata) const {

	T mean = 0;
335
336
	const unsigned int nxy = nx*ny;
	
337
	if(flag_nodata==IOUtils::RAW_NODATA) {
338
		for (unsigned int jj=0; jj<nxy; jj++) {
339
340
341
			const T val = operator()(jj);
			mean += val;
		}
342
		if(nxy>0) return mean/(T)(nxy);
343
344
345
		else return (T)0;
	} else if(flag_nodata==IOUtils::PARSE_NODATA) {
		unsigned int count = 0;
346
347
348
349
350
		for (unsigned int jj=0; jj<nxy; jj++) {
			const T val = operator()(jj);
			if(val!=IOUtils::nodata) {
				mean += val;
				count++;
351
352
353
354
355
356
357
358
359
			}
		}
		if(count>0) return mean/(T)(count);
		else return (T)IOUtils::nodata;
	} else {
		throw InvalidArgumentException("Unknown nodata_handling flag",AT);
	}
}

360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
template<class T> T Array2D<T>::getSum(const IOUtils::nodata_handling flag_nodata) const {

	T sum = 0;
	const unsigned int nxy = nx*ny;

	if(flag_nodata==IOUtils::RAW_NODATA) {
		for (unsigned int ii=0; ii<nxy; ii++) {
			sum += vecData[ii];
		}
		return sum;
	} else if(flag_nodata==IOUtils::PARSE_NODATA) {
		bool empty=true;
		for (unsigned int ii=0; ii<nxy; ii++) {
			const T val = vecData[ii];
			if(val!=IOUtils::nodata) {
				sum += val;
				empty=false;
			}
		}
		if(!empty) return sum;
		else return (T)IOUtils::nodata;
	} else {
		throw InvalidArgumentException("Unknown nodata_handling flag",AT);
	}
}

template<class T> void Array2D<T>::abs(const IOUtils::nodata_handling flag_nodata) {
	if(std::numeric_limits<T>::is_signed) {
		const unsigned int nxy = nx*ny;
		if(flag_nodata==IOUtils::RAW_NODATA) {
			for (unsigned int ii=0; ii<nxy; ii++) {
				T& val = operator()(ii);
				if(val<0) val=-val;
			}
		} else if(flag_nodata==IOUtils::PARSE_NODATA) {
			for (unsigned int ii=0; ii<nxy; ii++) {
				T& val = operator()(ii);
				if(val<0 && val!=IOUtils::nodata) val=-val;
			}
		} else {
			throw InvalidArgumentException("Unknown nodata_handling flag",AT);
		}
	}
}

template<class T> const Array2D<T> Array2D<T>::getAbs(const IOUtils::nodata_handling flag_nodata) const {
	Array2D<T> result = *this; //make a copy
	result.abs(flag_nodata); //already implemented

	return result;
}


413
414
415
416
417
418
419
420
421
422
//arithmetic operators
template<class T> Array2D<T>& Array2D<T>::operator=(const Array2D<T>& source) {
	if(this != &source) {
		nx = source.nx;
		ny = source.ny;
		vecData = source.vecData;
	}
	return *this;
}

423
424
425
426
427
template<class T> Array2D<T>& Array2D<T>::operator=(const T& value) {
	std::fill(vecData.begin(), vecData.end(), value);
	return *this;
}

428
429
430
431
432
433
template<class T> Array2D<T>& Array2D<T>::operator+=(const Array2D<T>& rhs)
{
	//They have to have equal size
	if ((rhs.nx != nx) || (rhs.ny != ny))
		throw IOException("Trying to add two Array2D objects with different dimensions", AT);

434
	const unsigned int nxy = nx*ny;
435
	//Add to every single member of the Array2D<T>
436
437
	for (unsigned int jj=0; jj<nxy; jj++)
			operator()(jj) += rhs(jj);
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452

	return *this;
}

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

	return result;
}

template<class T> Array2D<T>& Array2D<T>::operator+=(const T& rhs)
{
	//Add to every single member of the Array2D<T>
453
454
455
	const unsigned int nxy = nx*ny;
	for (unsigned int jj=0; jj<nxy; jj++)
		operator()(jj) += 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 Array2D<T> Array2D<T>::operator+(const T& rhs)
{
	Array2D<T> result = *this;
	result += rhs; //already implemented

	return result;
}

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

	//Substract to every single member of the Array2D<T>
475
476
477
	const unsigned int nxy = nx*ny;
	for (unsigned int jj=0; jj<nxy; jj++)
		operator()(jj) -= rhs(jj);
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492

	return *this;
}

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

	return result;
}

template<class T> Array2D<T>& Array2D<T>::operator-=(const T& rhs)
{
	//Substract to every single member of the Array2D<T>
493
494
495
	const unsigned int nxy = nx*ny;
	for (unsigned int jj=0; jj<nxy; jj++)
		operator()(jj) -= rhs;
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514

	return *this;
}

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

	return result;
}

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

	//Add to every single member of the Array2D<T>
515
516
517
	const unsigned int nxy = nx*ny;
	for (unsigned int jj=0; jj<nxy; jj++)
		operator()(jj) *= rhs(jj);
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532

	return *this;
}

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

	return result;
}

template<class T> Array2D<T>& Array2D<T>::operator*=(const T& rhs)
{
	//Add to every single member of the Array2D<T>
533
534
535
	const unsigned int nxy = nx*ny;
	for (unsigned int jj=0; jj<nxy; jj++)
			operator()(jj) *= rhs;
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554

	return *this;
}

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

	return result;
}

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

	//Divide every single member of the Array2D<T>
555
556
557
	const unsigned int nxy = nx*ny;
	for (unsigned int jj=0; jj<nxy; jj++)
		operator()(jj) /= rhs(jj);
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572

	return *this;
}

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

	return result;
}

template<class T> Array2D<T>& Array2D<T>::operator/=(const T& rhs)
{
	//Divide every single member of the Array2D<T>
573
574
575
	const unsigned int nxy = nx*ny;
	for (unsigned int jj=0; jj<nxy; jj++)
		operator()(jj) /= rhs;
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590

	return *this;
}

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

	return result;
}

} //end namespace mio

#endif