WSL/SLF GitLab Repository

Array2D.h 23.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
/***********************************************************************************/
/*  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>

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:
47
		Array2DProxy(Array2D<T>& i_array2D, const unsigned int& i_anx) : array2D(i_array2D), anx(i_anx){}
48
49
		Array2D<T>& array2D;
		const unsigned int anx;
50
};
51
52
53

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

		/**
		* 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
		*/
70
		Array2D(const unsigned int& anx, const unsigned int& any);
71
72
73
74
75
76
77

		/**
		* 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
		*/
78
79
		Array2D(const unsigned int& anx, const unsigned int& any, const T& init);

80
81
		virtual ~Array2D();

82
83
		/**
		* A constructor that can be used to create an Array2D object that is contained in the
84
85
86
87
88
89
90
		* 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
91
		*/
92
		Array2D(const Array2D<T>& i_array2D, const unsigned int& i_nx, const unsigned int& i_ny,
93
		        const unsigned int& i_ncols, const unsigned int& i_nrows);
94

95
		/**
96
		* @brief A method that can be used to cut out a subplane of an existing Array2D object
97
98
99
100
101
102
103
		* 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
104
		*/
105
106
		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);
107

108
109
110
111
112
113
114
115
116
117
118
		/**
		* @brief A method that can be used to insert a subplane into an existing Array2D object
		* that is passed as i_array2D argument. This is exactly the opposite of the subset method
		* an can be used to rebuild an array from subsets.
		* @param i_array2D array containing to subset values
		* @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
		*/
		void fill(const Array2D<T>& i_array2D, const unsigned int& i_nx, const unsigned int& i_ny,
119
		          const unsigned int& i_ncols, const unsigned int& i_nrows);
120

121
122
		void fill(const Array2D<T>& i_array2D, const unsigned int& i_nx, const unsigned int& i_ny);

123
124
		/**
		* @brief set how to process nodata values (ie: as nodata or as normal numbers)
125
126
		* @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.
127
		*/
128
129
130
131
132
133
134
		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();
135

136
137
138
		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;
139
140
		unsigned int getNx() const;
		unsigned int getNy() const;
141

142
143
144
		void clear();
		bool isEmpty() const;

145
146
147
148
		/**
		* @brief returns the minimum value contained in the grid
		* @return minimum value
		*/
149
		T getMin() const;
150
151
152
153
		/**
		* @brief returns the maximum value contained in the grid
		* @return maximum value
		*/
154
		T getMax() const;
155
156
157
158
		/**
		* @brief returns the mean value contained in the grid
		* @return mean value
		*/
159
		T getMean() const;
160
		/**
161
		* @brief returns the number of points contained in the grid.
162
163
		* 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
164
		* @return count
165
		*/
166
		size_t getCount() const;
167
168
169
170
		/**
		* @brief returns the grid of the absolute value of values contained in the grid
		* @return grid of abs(grid)
		*/
171
172
		const Array2D<T> getAbs() const;
		void abs();
173

174
175
176
		const std::string toString() const;
		template<class P> friend std::iostream& operator<<(std::iostream& os, const Array2D<P>& array);
		template<class P> friend std::iostream& operator>>(std::iostream& is, Array2D<P>& array);
177

178
179
180
		bool checkEpsilonEquality(const Array2D<double>& rhs, const double& epsilon) const;
		static bool checkEpsilonEquality(const Array2D<double>& rhs1, const Array2D<double>& rhs2, const double& epsilon);

181
182
		T& operator ()(const unsigned int& x, const unsigned int& y);
		const T operator ()(const unsigned int& x, const unsigned int& y) const;
183
184
		T& operator ()(const unsigned int& i);
		const T operator ()(const unsigned int& i) const;
185
186
187
		Array2DProxy<T> operator[](const unsigned int& i);

		Array2D<T>& operator =(const Array2D<T>&);
188
		Array2D<T>& operator =(const T& value);
189

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
		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);

210
211
212
		bool operator==(const Array2D<T>&) const; ///<Operator that tests for equality
		bool operator!=(const Array2D<T>&) const; ///<Operator that tests for inequality

213
214
215
216
	protected:
		std::vector<T> vecData;
		unsigned int nx;
		unsigned int ny;
217
		bool keep_nodata;
218
219
};

220
template<class T> inline T& Array2D<T>::operator()(const unsigned int& i) {
221
222
223
#ifndef NOSAFECHECKS
	return vecData.at(i);
#else
224
	return vecData[i];
225
#endif
226
227
}

228
template<class T> inline const T Array2D<T>::operator()(const unsigned int& i) const {
229
230
231
#ifndef NOSAFECHECKS
	return vecData.at(i);
#else
232
	return vecData[i];
233
#endif
234
}
235
template<class T> inline T& Array2D<T>::operator()(const unsigned int& x, const unsigned int& y) {
236
237
#ifndef NOSAFECHECKS
	if ((x >= nx) || (y >= ny)) {
238
239
		std::stringstream ss;
		ss << "Trying to access array(" << x << "," << y << ")";
240
		ss << " while array is (" << nx << "," << ny << ")";
241
		throw IndexOutOfBoundsException(ss.str(), AT);
242
243
	}
#endif
244
	//COLUMN-MAJOR alignment of the vector: fully C-compatible memory layout
245
	return vecData[x + y*nx];
246
247
}

248
template<class T> inline const T Array2D<T>::operator()(const unsigned int& x, const unsigned int& y) const {
249
250
#ifndef NOSAFECHECKS
	if ((x >= nx) || (y >= ny)) {
251
252
		std::stringstream ss;
		ss << "Trying to access array(" << x << "," << y << ")";
253
		ss << " while array is (" << nx << "," << ny << ")";
254
		throw IndexOutOfBoundsException(ss.str(), AT);
255
256
	}
#endif
257
	return vecData[x + y*nx];
258
259
260
}

template<class T> Array2DProxy<T> Array2D<T>::operator[](const unsigned int& i) {
261
	return Array2DProxy<T>(*this, i);
262
263
}

264
265
template<class T> Array2D<T>::Array2D() : vecData(), nx(0), ny(0), keep_nodata(true)
{
266
267
}

268
269
template<class T> Array2D<T>::~Array2D() { }

270
template<class T> Array2D<T>::Array2D(const Array2D<T>& i_array2D, const unsigned int& i_nx, const unsigned int& i_ny,
271
272
                                      const unsigned int& i_ncols, const unsigned int& i_nrows) :
                                      vecData(i_ncols*i_nrows), nx(i_ncols), ny(i_nrows), keep_nodata(true)
273
{
274
	subset(i_array2D, i_nx, i_ny, i_ncols, i_nrows);
275
276
}

277
278
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)
279
{
280
281
282
283
284
285
	if (((i_nx+i_ncols) > i_array2D.nx) || ((i_ny+i_nrows) > i_array2D.ny)) {
		std::stringstream ss;
		ss << "Trying to cut an array of size (" << nx << "," << ny << ") ";
		ss << "to size (" << i_ncols << "," << i_nrows << ") starting at (" << i_nx << "," << i_ny << ")";
		throw IndexOutOfBoundsException(ss.str(), AT);
	}
286

287
	if ((i_ncols == 0) || (i_nrows == 0)) //the plane to copy has to make sense
288
		throw IndexOutOfBoundsException("Trying to cut an array into a null sized array!", AT);
289

290
	resize(i_ncols, i_nrows); //create new Array2D object
291
	//Copy by value subspace
292
293
	for (unsigned int jj=0; jj<ny; jj++) {
		for (unsigned int ii=0; ii<nx; ii++) {
294
			operator()(ii,jj) = i_array2D(i_nx+ii, i_ny+jj);
295
296
297
298
		}
	}
}

299
300
301
302
303
304
305
template<class T> void Array2D<T>::fill(const Array2D<T>& i_array2D, const unsigned int& i_nx, const unsigned int& i_ny)
{
	unsigned int i_ncols, i_nrows;
	i_array2D.size(i_ncols, i_nrows);
	fill(i_array2D, i_nx, i_ny, i_ncols, i_nrows);
}

306
307
308
template<class T> void Array2D<T>::fill(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)
{
309
310
311
312
313
314
315
	if (((i_nx+i_ncols) > nx) || ((i_ny+i_nrows) > ny)) {
		std::stringstream ss;
		ss << "Filling an array of size (" << nx << "," << ny << ") ";
		ss << "with an array of size (" << i_ncols << "," << i_nrows << ") ";
		ss << "starting at (" << i_nx << "," << i_ny << ")";
		throw IndexOutOfBoundsException(ss.str(), AT);
	}
316
317

	if ((i_ncols == 0) || (i_nrows == 0)) //the plane to copy has to make sense
318
		throw IndexOutOfBoundsException("Filling an array with a null sized array!", AT);
319
320
321
322
323
324
325
326
327
328

	for(unsigned int jj=i_ny; jj<(i_ny+i_nrows); jj++) {
		for(unsigned int ii=i_nx; ii<(i_nx+i_ncols); ii++) {
			const unsigned int ix = ii-i_nx;
			const unsigned int iy = jj-i_ny;
			operator()(ii,jj) = i_array2D(ix, iy);
		}
	}
}

329
330
331
332
template<class T> Array2D<T>::Array2D(const unsigned int& anx, const unsigned int& any, const T& init) :
                  vecData(anx*any, init), nx(anx), ny(any), keep_nodata(true)
{
	//resize(anx,any,init);
333
334
}

335
336
337
338
template<class T> Array2D<T>::Array2D(const unsigned int& anx, const unsigned int& any) :
                  vecData(anx*any), nx(anx), ny(any), keep_nodata(true)
{
	//resize(anx,any);
339
340
}

341
342
343
344
345
346
template<class T> void Array2D<T>::setKeepNodata(const bool i_keep_nodata) {
	keep_nodata = i_keep_nodata;
}

template<class T> bool Array2D<T>::getKeepNodata() {
	return keep_nodata;
347
348
}

349
template<class T> void Array2D<T>::resize(const unsigned int& anx, const unsigned int& any) {
350
351
352
353
	clear(); //we won't be able to "rescue" old values, so we reset the whole vector
	vecData.resize(anx*any);
	nx = anx;
	ny = any;
354
355
356
}

template<class T> void Array2D<T>::resize(const unsigned int& anx, const unsigned int& any, const T& init) {
357
	clear(); //we won't be able to "rescue" old values, so we reset the whole vector
358
359
360
	vecData.resize(anx*any, init);
	nx = anx;
	ny = any;
361
362
}

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

368
369
370
371
372
373
374
375
template<class T> unsigned int Array2D<T>::getNx() const {
	return nx;
}

template<class T> unsigned int Array2D<T>::getNy() const {
	return ny;
}

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

381
382
383
384
template<class T> bool Array2D<T>::isEmpty() const {
	return (nx==0 && ny==0);
}

385
template<class T> const std::string Array2D<T>::toString() const {
386
	std::ostringstream os;
387
	os << "<array2d>\n";
388
389
390
	for(unsigned int jj=0; jj<ny; jj++) {
		const unsigned int jnx = jj*nx;
		for (unsigned int ii=0; ii<nx; ii++) {
391
			os << vecData[ii+jnx] << " "; //COLUMN-MAJOR alignment
392
393
394
395
		}
		os << "\n";
	}
	os << "</array2d>\n";
396
397
398
399
400
401
402
403
	return os.str();
}

template<class P> std::iostream& operator<<(std::iostream& os, const Array2D<P>& array) {
	os.write(reinterpret_cast<const char*>(&array.keep_nodata), sizeof(array.keep_nodata));
	os.write(reinterpret_cast<const char*>(&array.nx), sizeof(array.nx));
	os.write(reinterpret_cast<const char*>(&array.ny), sizeof(array.ny));
	os.write(reinterpret_cast<const char*>(&array.vecData[0]), array.nx*array.ny*sizeof(P));
404
405
406
	return os;
}

407
408
409
410
411
412
template<class P> std::iostream& operator>>(std::iostream& is, Array2D<P>& array) {
	is.read(reinterpret_cast<char*>(&array.keep_nodata), sizeof(array.keep_nodata));
	is.read(reinterpret_cast<char*>(&array.nx), sizeof(array.nx));
	is.read(reinterpret_cast<char*>(&array.ny), sizeof(array.ny));
	array.vecData.resize(array.nx*array.ny);
	is.read(reinterpret_cast<char*>(&array.vecData[0]), array.nx*array.ny*sizeof(P)); //30 times faster than assign() or copy()
413
	return is;
414
415
}

416
template<class T> T Array2D<T>::getMin() const {
417
418
419

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

420
	const unsigned int nxy = ny*nx;
421
	if(keep_nodata==false) {
422
		for (unsigned int jj=0; jj<nxy; jj++) {
423
			const T val = vecData[jj];
424
			if(val<min) min=val;
425
426
		}
		return min;
427
	} else {
428
		for (unsigned int jj=0; jj<nxy; jj++) {
429
			const T val = vecData[jj];
430
			if(val!=IOUtils::nodata && val<min) min=val;
431
432
433
434
435
436
		}
		if(min!=std::numeric_limits<T>::max()) return min;
		else return (T)IOUtils::nodata;
	}
}

437
template<class T> T Array2D<T>::getMax() const {
438
439
440

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

441
	const unsigned int nxy = ny*nx;
442
	if(keep_nodata==false) {
443
		for (unsigned int jj=0; jj<nxy; jj++) {
444
			const T val = vecData[jj];
445
			if(val>max) max=val;
446
447
		}
		return max;
448
	} else {
449
		for (unsigned int jj=0; jj<nxy; jj++) {
450
			const T val = vecData[jj];
451
			if(val!=IOUtils::nodata && val>max) max=val;
452
453
454
455
456
457
		}
		if(max!=-std::numeric_limits<T>::max()) return max;
		else return (T)IOUtils::nodata;
	}
}

458
template<class T> T Array2D<T>::getMean() const {
459
460

	T mean = 0;
461
	const unsigned int nxy = nx*ny;
462

463
	if(keep_nodata==false) {
464
		for (unsigned int jj=0; jj<nxy; jj++) {
465
			const T val = vecData[jj];
466
467
			mean += val;
		}
468
		if(nxy>0) return mean/(T)(nxy);
469
		else return (T)0;
470
	} else {
471
		unsigned int count = 0;
472
		for (unsigned int jj=0; jj<nxy; jj++) {
473
			const T val = vecData[jj];
474
475
476
			if(val!=IOUtils::nodata) {
				mean += val;
				count++;
477
478
479
480
481
482
483
			}
		}
		if(count>0) return mean/(T)(count);
		else return (T)IOUtils::nodata;
	}
}

484
template<class T> size_t Array2D<T>::getCount() const
485
{
486
487
	const unsigned int nxy = nx*ny;

488
	if(keep_nodata==false) {
489
		return (size_t)nxy;
490
	} else {
491
		size_t count = 0;
492
		for (unsigned int ii=0; ii<nxy; ii++) {
493
			if(vecData[ii]!=IOUtils::nodata) count++;
494
		}
495
		return count;
496
497
498
	}
}

499
template<class T> void Array2D<T>::abs() {
500
501
	if(std::numeric_limits<T>::is_signed) {
		const unsigned int nxy = nx*ny;
502
		if(keep_nodata==false) {
503
			for (unsigned int ii=0; ii<nxy; ii++) {
504
				T& val = vecData[ii];
505
506
				if(val<0) val=-val;
			}
507
		} else {
508
			for (unsigned int ii=0; ii<nxy; ii++) {
509
				T& val = vecData[ii];
510
511
512
513
514
515
				if(val<0 && val!=IOUtils::nodata) val=-val;
			}
		}
	}
}

516
template<class T> const Array2D<T> Array2D<T>::getAbs() const {
517
	Array2D<T> result = *this; //make a copy
518
	result.abs(); //already implemented
519
520
521
522
523

	return result;
}


524
//arithmetic operators
525
526
527
528
529
530
531
532
533
534
535
536
537
538
template<class T> bool Array2D<T>::checkEpsilonEquality(const Array2D<double>& rhs, const double& epsilon) const {
	if(nx!=rhs.nx || ny!=rhs.ny) return false;

	const unsigned int nxy = nx*ny;
	for (unsigned int jj=0; jj<nxy; jj++)
		if(IOUtils::checkEpsilonEquality(vecData[jj], rhs.vecData[jj], epsilon)==false) return false;

	return true;
}

template<class T> bool Array2D<T>::checkEpsilonEquality(const Array2D<double>& rhs1, const Array2D<double>& rhs2, const double& epsilon) {
	return rhs1.checkEpsilonEquality(rhs2, epsilon);
}

539
540
template<class T> Array2D<T>& Array2D<T>::operator=(const Array2D<T>& source) {
	if(this != &source) {
541
		keep_nodata = source.keep_nodata;
542
543
544
545
546
547
548
		nx = source.nx;
		ny = source.ny;
		vecData = source.vecData;
	}
	return *this;
}

549
template<class T> Array2D<T>& Array2D<T>::operator=(const T& value) {
550
	std::fill(vecData.begin(), vecData.end(), value);
551
552
553
	return *this;
}

554
555
556
template<class T> Array2D<T>& Array2D<T>::operator+=(const Array2D<T>& rhs)
{
	//They have to have equal size
557
558
559
560
561
562
	if ((rhs.nx != nx) || (rhs.ny != ny)) {
		std::stringstream ss;
		ss << "Trying to add two Array2D objects with different dimensions: ";
		ss << "(" << nx << "," << ny << ") + (" << rhs.nx << "," << rhs.ny << ")";
		throw IOException(ss.str(), AT);
	}
563

564
	const unsigned int nxy = nx*ny;
565
	//Add to every single member of the Array2D<T>
566
567
	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nxy; jj++)
568
			vecData[jj] += rhs(jj);
569
570
	} else {
		for (unsigned int jj=0; jj<nxy; jj++) {
571
572
			if(vecData[jj]==IOUtils::nodata || rhs(jj)==IOUtils::nodata)
				vecData[jj] = IOUtils::nodata;
573
			else
574
				vecData[jj] += rhs(jj);
575
576
		}
	}
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591

	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>
592
	const unsigned int nxy = nx*ny;
593
594
595

	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nxy; jj++)
596
			vecData[jj] += rhs;
597
598
	} else {
		for (unsigned int jj=0; jj<nxy; jj++) {
599
600
			if(vecData[jj]!=IOUtils::nodata)
				vecData[jj] += rhs;
601
602
		}
	}
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617

	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
618
619
620
621
622
623
	if ((rhs.nx != nx) || (rhs.ny != ny)){
		std::stringstream ss;
		ss << "Trying to substract two Array2D objects with different dimensions: ";
		ss << "(" << nx << "," << ny << ") - (" << rhs.nx << "," << rhs.ny << ")";
		throw IOException(ss.str(), AT);
	}
624
	//Substract to every single member of the Array2D<T>
625
	const unsigned int nxy = nx*ny;
626
627
628

	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nxy; jj++)
629
			vecData[jj] -= rhs(jj);
630
631
	} else {
		for (unsigned int jj=0; jj<nxy; jj++) {
632
633
			if(vecData[jj]==IOUtils::nodata || rhs(jj)==IOUtils::nodata)
				vecData[jj] = IOUtils::nodata;
634
			else
635
				vecData[jj] -= rhs(jj);
636
637
		}
	}
638
639
640
641
642
643
644
645
646
647
648
649
650
651

	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)
{
652
	*this += -rhs;
653
654
655
656
657
658
	return *this;
}

template<class T> const Array2D<T> Array2D<T>::operator-(const T& rhs)
{
	Array2D<T> result = *this;
659
	result += -rhs; //already implemented
660
661
662
663
664
665
666

	return result;
}

template<class T> Array2D<T>& Array2D<T>::operator*=(const Array2D<T>& rhs)
{
	//They have to have equal size
667
668
669
670
671
672
	if ((rhs.nx != nx) || (rhs.ny != ny)){
		std::stringstream ss;
		ss << "Trying to multiply two Array2D objects with different dimensions: ";
		ss << "(" << nx << "," << ny << ") * (" << rhs.nx << "," << rhs.ny << ")";
		throw IOException(ss.str(), AT);
	}
673
	//Add to every single member of the Array2D<T>
674
	const unsigned int nxy = nx*ny;
675
676
677

	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nxy; jj++)
678
			vecData[jj] *= rhs(jj);
679
680
	} else {
		for (unsigned int jj=0; jj<nxy; jj++) {
681
682
			if(vecData[jj]==IOUtils::nodata || rhs(jj)==IOUtils::nodata)
				vecData[jj] = IOUtils::nodata;
683
			else
684
				vecData[jj] *= rhs(jj);
685
686
		}
	}
687
688
689
690
691
692
693
694
695
696
697
698
699
700

	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)
{
701
	//Multiply to every single member of the Array2D<T>
702
	const unsigned int nxy = nx*ny;
703
704
705

	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nxy; jj++)
706
			vecData[jj] *= rhs;
707
708
	} else {
		for (unsigned int jj=0; jj<nxy; jj++) {
709
710
			if(vecData[jj]!=IOUtils::nodata)
				vecData[jj] *= rhs;
711
712
		}
	}
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727

	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
728
729
730
731
732
733
	if ((rhs.nx != nx) || (rhs.ny != ny)){
		std::stringstream ss;
		ss << "Trying to divide two Array2D objects with different dimensions: ";
		ss << "(" << nx << "," << ny << ") / (" << rhs.nx << "," << rhs.ny << ")";
		throw IOException(ss.str(), AT);
	}
734
	//Divide every single member of the Array2D<T>
735
	const unsigned int nxy = nx*ny;
736
737
738

	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nxy; jj++)
739
			vecData[jj] /= rhs(jj);
740
741
	} else {
		for (unsigned int jj=0; jj<nxy; jj++) {
742
743
			if(vecData[jj]==IOUtils::nodata || rhs(jj)==IOUtils::nodata)
				vecData[jj] = IOUtils::nodata;
744
			else
745
				vecData[jj] /= rhs(jj);
746
747
		}
	}
748
749
750
751
752
753
754
755
756
757
758
759
760
761

	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)
{
762
	*this *= (1./rhs);
763
764
765
766
767
768
	return *this;
}

template<class T> const Array2D<T> Array2D<T>::operator/(const T& rhs)
{
	Array2D<T> result = *this;
769
	result *= (1./rhs); //already implemented
770
771
772
773

	return result;
}

774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
template<class T> bool Array2D<T>::operator==(const Array2D<T>& in) const {
	unsigned int in_nx=in.getNx(), in_ny=in.getNy();

	if(nx!=in_nx || ny!=in_ny)
		return false;

	const unsigned int nxy = nx*ny;
	for(unsigned int jj=0; jj<nxy; jj++)
		if( !IOUtils::checkEpsilonEquality( vecData[jj] , in.vecData[jj], 1e-6) ) return false;

	return true;
}

template<class T> bool Array2D<T>::operator!=(const Array2D<T>& in) const {
	return !(*this==in);
}

791
792
793
} //end namespace mio

#endif