WSL/SLF GitLab Repository

Array4D.h 27.8 KB
Newer Older
Till Schumann's avatar
Neu  
Till Schumann committed
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
/***********************************************************************************/
/*  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 ARRAY4D_H
#define ARRAY4D_H

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

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

namespace mio {

/**
 * @class Array4D
 * @brief The template class Array4D is a 4D Array (Tensor) able to hold any type of object as datatype.
 * @date  2012-12-20
 * @author Till Schumann
 */
template<class T> class Array4D {
	public:
		Array4D();

		/**
		* A constructor that can be used to create an Array4D object that is contained in the
		* one passed as i_array4D argument. The resulting Array4D object is a by value copy of
		* a subvolume of the volume spanned by the i_array4D
		* @param i_array4D array containing to extract the values from
Till Schumann's avatar
Till Schumann committed
45
		* @param i_nw lower left corner cell W index
Till Schumann's avatar
Neu  
Till Schumann committed
46
47
48
		* @param i_nx lower left corner cell X index
		* @param i_ny lower left corner cell Y index
		* @param i_nz lower left corner cell Z index
Till Schumann's avatar
Till Schumann committed
49
50
51
52
		* @param i_sizeW length of the w dimension of the new array
		* @param i_sizeX length of the x dimension of the new array
		* @param i_sizeY length of the y dimension of the new array
		* @param i_sizeZ length of the z dimension of the new array
Till Schumann's avatar
Neu  
Till Schumann committed
53
54
55
		*/
		Array4D(const Array4D<T>& i_array4D,
		        const unsigned int& i_nw, const unsigned int& i_nx, const unsigned int& i_ny, const unsigned int& i_nz,
Till Schumann's avatar
Till Schumann committed
56
		        const unsigned int& i_sizeW, const unsigned int& i_sizeX, const unsigned int& i_sizeY, const unsigned int& i_sizeZ);
Till Schumann's avatar
Neu  
Till Schumann committed
57
58
59
60
61

		Array4D(const unsigned int& anw, const unsigned int& anx, const unsigned int& any, const unsigned int& anz);

		/**
		* A constructor that creates an array filled with constant values
Till Schumann's avatar
Till Schumann committed
62
63
64
65
		* @param i_sizeW length of the w dimension of the new array
		* @param i_sizeX length of the x dimension of the new array
		* @param i_sizeY length of the y dimension of the new array
		* @param i_sizeZ length of the z dimension of the new array
Till Schumann's avatar
Neu  
Till Schumann committed
66
67
		* @param init initial value to fill the array with
		*/
Till Schumann's avatar
Till Schumann committed
68
		Array4D(const unsigned int& i_sizeW, const unsigned int& i_sizeX, const unsigned int& i_sizeY, const unsigned int& i_sizeZ, const T& init);
Till Schumann's avatar
Neu  
Till Schumann committed
69
70
71
72
73
74

		/**
		* A method that can be used to create an Array4D object that is contained in the
		* one passed as i_array4D argument. The resulting Array4D object is a by value copy of
		* a subvolume of the volume spanned by the i_array4D
		* @param i_array4D array containing to extract the values from
Till Schumann's avatar
Till Schumann committed
75
		* @param i_nw lower left corner cell W index
Till Schumann's avatar
Neu  
Till Schumann committed
76
77
78
		* @param i_nx lower left corner cell X index
		* @param i_ny lower left corner cell Y index
		* @param i_nz lower left corner cell Z index
Till Schumann's avatar
Till Schumann committed
79
80
81
82
		* @param i_sizeW length of the w dimension of the new array
		* @param i_sizeX length of the x dimension of the new array
		* @param i_sizeY length of the y dimension of the new array
		* @param i_sizeZ length of the z dimension of the new array
Till Schumann's avatar
Neu  
Till Schumann committed
83
84
85
		*/
		void subset(const Array4D<T>& i_array4D,
		            const unsigned int& i_nw, const unsigned int& i_nx, const unsigned int& i_ny, const unsigned int& i_nz,
Till Schumann's avatar
Till Schumann committed
86
		            const unsigned int& i_sizeW, const unsigned int& i_sizeX, const unsigned int& i_sizeY, const unsigned int& i_sizeZ);
Till Schumann's avatar
Neu  
Till Schumann committed
87
88
89
90
91
92
93
94
95
96

		/**
		* @brief A method that can be used to insert a subplane into an existing Array4D object
		* that is passed as i_array4D argument. This is exactly the opposite of the subset method
		* an can be used to rebuild an array from subsets.
		* @param i_array4D array containing to extract the values from
		* @param i_nw lower left corner cell W index
		* @param i_nx lower left corner cell X index
		* @param i_ny lower left corner cell Y index
		* @param i_nz lower left corner cell Z index
Till Schumann's avatar
Till Schumann committed
97
98
99
100
		* @param i_sizeW length of the w dimension of the new array
		* @param i_sizeX length of the x dimension of the new array
		* @param i_sizeY length of the y dimension of the new array
		* @param i_sizeZ length of the z dimension of the new array
Till Schumann's avatar
Neu  
Till Schumann committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
		*/
		void fill(const Array4D<T>& i_array4D,
		          const unsigned int& i_nw, const unsigned int& i_nx, const unsigned int& i_ny, const unsigned int& i_nz,
		          const unsigned int& i_sizeW, const unsigned int& i_sizeX, const unsigned int& i_sizeY, const unsigned int& i_sizeZ);

		void fill(const Array4D<T>& i_array4D, const unsigned int& i_nw, const unsigned int& i_nx, const unsigned int& i_ny, const unsigned int& i_nz);

		/**
		* @brief set how to process nodata values (ie: as nodata or as normal numbers)
		* @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.
		*/
		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();

		void resize(const unsigned int& anw, const unsigned int& anx, const unsigned int& any, const unsigned int& anz);
		void resize(const unsigned int& anw, const unsigned int& anx, const unsigned int& any, const unsigned int& anz, const T& init);
		void size(unsigned int& anw, unsigned int& anx, unsigned int& any, unsigned int& anz) const;
		unsigned int getNw() const;
		unsigned int getNx() const;
		unsigned int getNy() const;
		unsigned int getNz() const;
		void clear();
		bool isEmpty() const;

		/**
		* @brief returns the minimum value contained in the grid
		* @return minimum value
		*/
		T getMin() const;
		/**
		* @brief returns the maximum value contained in the grid
		* @return maximum value
		*/
		T getMax() const;
		/**
		* @brief returns the mean value contained in the grid
		* @return mean value
		*/
		T getMean() const;
		/**
		* @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
		* @return count
		*/
		size_t getCount() const;
		/**
		* @brief returns the grid of the absolute value of values contained in the grid
		* @return grid of abs(grid)
		*/
		const Array4D<T> getAbs() const;
		void abs();

160
161
162
		const std::string toString() const;
		template<class P> friend std::iostream& operator<<(std::iostream& os, const Array4D<P>& array);
		template<class P> friend std::iostream& operator>>(std::iostream& is, Array4D<P>& array);
Till Schumann's avatar
Neu  
Till Schumann committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

		bool checkEpsilonEquality(const Array4D<double>& rhs, const double& epsilon) const;
		static bool checkEpsilonEquality(const Array4D<double>& rhs1, const Array4D<double>& rhs2, const double& epsilon);

		T& operator ()(const unsigned int& i);
		const T operator ()(const unsigned int& i) const;
		T& operator ()(const unsigned int& w, const unsigned int& x, const unsigned int& y, const unsigned int& z);
		const T operator ()(const unsigned int& w, const unsigned int& x, const unsigned int& y, const unsigned int& z) const;

		Array4D<T>& operator =(const Array4D<T>&);
		Array4D<T>& operator =(const T& value);

		Array4D<T>& operator+=(const T& rhs);
		const Array4D<T> operator+(const T& rhs);
		Array4D<T>& operator+=(const Array4D<T>& rhs);
		const Array4D<T> operator+(const Array4D<T>& rhs);

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

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

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

195
196
197
		bool operator==(const Array4D<T>&) const; ///<Operator that tests for equality
		bool operator!=(const Array4D<T>&) const; ///<Operator that tests for inequality

Till Schumann's avatar
Neu  
Till Schumann committed
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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
	protected:
		std::vector<T> vecData; ///< The actual objects are stored in a one-dimensional vector
		unsigned int nw;
		unsigned int nx;
		unsigned int ny;
		unsigned int nz;
		unsigned int nwnx; //xw time nx
		unsigned int nwnxny; //xw time nx times ny
		bool keep_nodata;
};

template<class T> inline T& Array4D<T>::operator()(const unsigned int& i) {
#ifndef NOSAFECHECKS
	return vecData.at(i);
#else
	return vecData[i];
#endif
}

template<class T> inline const T Array4D<T>::operator()(const unsigned int& i) const {
#ifndef NOSAFECHECKS
	return vecData.at(i);
#else
	return vecData[i];
#endif
}

template<class T> inline T& Array4D<T>::operator()(const unsigned int& w, const unsigned int& x, const unsigned int& y, const unsigned int& z) {
#ifndef NOSAFECHECKS
	if ((w >= nw) || (x >= nx) || (y >= ny) || (z >= nz))  {
		std::stringstream ss;
		ss << "Trying to access array(" << w << "," << x << "," << y << "," << z << ")";
		ss << " while array is ("<< nw << "," << nx << "," << ny << "," << nz << ")";
		throw IndexOutOfBoundsException(ss.str(), AT);
	}
#endif

	//ROW-MAJOR alignment of the vector: fully C-compatible memory layout
	return vecData[w + x*nw + y*nwnx + z*nwnxny];
}

template<class T> inline const T Array4D<T>::operator()(const unsigned int& w, const unsigned int& x, const unsigned int& y, const unsigned int& z) const {
#ifndef NOSAFECHECKS
	if ((w >= nw) || (x >= nx) || (y >= ny) || (z >= nz))  {
		std::stringstream ss;
		ss << "Trying to access array("<< w << "," << x << "," << y << "," << z << ")";
		ss << " while array is ("<< nw << "," << nx << "," << ny << "," << nz << ")";
		throw IndexOutOfBoundsException(ss.str(), AT);
	}
#endif
	return vecData[w + nw*x + y*nwnx + z*nwnxny];
}

template<class T> Array4D<T>::Array4D() : vecData(), nw(0), nx(0), ny(0), nz(0), nwnx(0), nwnxny(0), keep_nodata(true)
{
}

template<class T> Array4D<T>::Array4D(const Array4D<T>& i_array4D,
                                      const unsigned int& i_nw,const unsigned int& i_nx, const unsigned int& i_ny, const unsigned int& i_nz,
                                      const unsigned int& i_sizeW, const unsigned int& i_sizeX, const unsigned int& i_sizeY, const unsigned int& i_sizeZ)
                               : vecData(i_sizeW*i_sizeX*i_sizeY*i_sizeZ), nw(i_sizeW), nx(i_sizeX), ny(i_sizeY), nz(i_sizeZ), nwnx(i_sizeW*i_sizeX), nwnxny(i_sizeW*i_sizeX*i_sizeY), keep_nodata(true)
{
	subset(i_array4D, i_nw, i_nx, i_ny, i_nz, i_sizeW, i_sizeX, i_sizeY, i_sizeZ);
}

template<class T> void Array4D<T>::subset(const Array4D<T>& i_array4D,
                                     const unsigned int& i_nw, const unsigned int& i_nx, const unsigned int& i_ny, const unsigned int& i_nz,
                                     const unsigned int& i_sizeW, const unsigned int& i_sizeX, const unsigned int& i_sizeY, const unsigned int& i_sizeZ)
{

	if (((i_nw+i_sizeW) > i_array4D.nw) || ((i_nx+i_sizeX) > i_array4D.nx) || ((i_ny+i_sizeY) > i_array4D.ny) || ((i_nz+i_sizeZ) > i_array4D.nz)) {
		std::stringstream ss;
		ss << "Trying to cut an array of size ("<< nw << "," << nx << "," << ny << "," << nz << ") ";
		ss << "to size (" << i_sizeW << "," << i_sizeX << "," << i_sizeY << "," << i_sizeZ << ") ";
		ss << "starting at ("<< i_nw << "," << i_nx << "," << i_ny << "," << i_nz << ")";
		throw IndexOutOfBoundsException(ss.str(), AT);
	}

	if ((i_sizeW == 0) || (i_sizeX == 0) || (i_sizeY == 0) || (i_sizeZ == 0)) //the space has to make sense
		throw IndexOutOfBoundsException("Trying to cut an array into a null sized array!", AT);

	resize(i_sizeW, i_sizeX, i_sizeY, i_sizeZ); //create new Array4D object
	//Copy by value subspace
	for (unsigned int ii=0; ii<nz; ii++) {
		for (unsigned int jj=0; jj<ny; jj++) {
			for (unsigned int kk=0; kk<nx; kk++) {
				for (unsigned int ll=0; ll<nw; ll++)
				//Running through the vector in order of memory alignment
					operator()(ll,kk,jj,ii) = i_array4D(i_nw+ll, i_nx+kk, i_ny+jj, i_nz+ii);
			}
		}
	}
}

template<class T> void Array4D<T>::fill(const Array4D<T>& i_array4D, const unsigned int& i_nw, const unsigned int& i_nx, const unsigned int& i_ny, const unsigned int& i_nz)
{
	unsigned int i_sizeW, i_sizeX, i_sizeY, i_sizeZ;
	i_array4D.size(i_sizeW, i_sizeX, i_sizeY, i_sizeZ);
	fill(i_array4D, i_nw, i_nx, i_ny, i_nz, i_sizeW, i_sizeX, i_sizeY, i_sizeZ);
}

template<class T> void Array4D<T>::fill(const Array4D<T>& i_array4D,
                                     const unsigned int& i_nw, const unsigned int& i_nx, const unsigned int& i_ny, const unsigned int& i_nz,
                                     const unsigned int& i_sizeW, const unsigned int& i_sizeX, const unsigned int& i_sizeY, const unsigned int& i_sizeZ)
{

	if (((i_nw+i_sizeW) > i_array4D.nw) || ((i_nx+i_sizeX) > i_array4D.nx) || ((i_ny+i_sizeY) > i_array4D.ny) || ((i_nz+i_sizeZ) > i_array4D.nz)) {
		std::stringstream ss;
		ss << "Filling an array of size (" << nw << "," << nx << "," << ny << "," << nz << ") ";
		ss << "with an array of size (" << i_sizeW << "," << i_sizeX << "," << i_sizeY << "," << i_sizeZ << ") ";
		ss << "starting at (" << i_nw << "," << i_nx << "," << i_ny << "," << i_nz << ")";
		throw IndexOutOfBoundsException(ss.str(), AT);
	}

	if ((i_sizeW == 0) || (i_sizeX == 0) || (i_sizeY == 0) || (i_sizeZ == 0)) //the space has to make sense
		throw IndexOutOfBoundsException("Filling an array with a null sized array!", AT);

	//Copy by value subspace
	for (unsigned int ii=i_nz; ii<(i_nz+i_sizeZ); ii++) {
		for (unsigned int jj=i_ny; jj<(i_ny+i_sizeY); jj++) {
			for (unsigned int kk=i_nx; kk<(i_nx+i_sizeX); kk++) {
				for (unsigned int ll=i_nw; ll<(i_nw+i_sizeW); ll++) {
					const unsigned int iw = ll-i_nw;
					const unsigned int ix = kk-i_nx;
					const unsigned int iy = jj-i_ny;
					const unsigned int iz = ii-i_nz;
					operator()(ll,kk,jj,ii) = i_array4D(iw,ix, iy, iz);
				}
			}
		}
	}
}

template<class T> Array4D<T>::Array4D(const unsigned int& anw, const unsigned int& anx, const unsigned int& any, const unsigned int& anz)
                             : vecData(anw*anx*any*anz), nw(anw), nx(anx), ny(any), nz(anz), nwnx(anw*anx), nwnxny(anw*anx*any),  keep_nodata(true)
{
	//resize(anw, anx, any, anz);
}

template<class T> Array4D<T>::Array4D(const unsigned int& anw, const unsigned int& anx, const unsigned int& any, const unsigned int& anz, const T& init)
                             : vecData(anw*anx*any*anz, init), nw(anw), nx(anx), ny(any), nz(anz), nwnx(anw*anx), nwnxny(anw*anx*any),  keep_nodata(true)
{
	//resize(anw, anx, any, anz, init);
}

template<class T> void Array4D<T>::setKeepNodata(const bool i_keep_nodata) {
	keep_nodata = i_keep_nodata;
}

template<class T> bool Array4D<T>::getKeepNodata() {
	return keep_nodata;
}

template<class T> void Array4D<T>::resize(const unsigned int& anw, const unsigned int& anx, const unsigned int& any, const unsigned int& anz) {
	clear();  //we won't be able to "rescue" old values, so we reset the whole vector
	vecData.resize(anw*anx*any*anz);
	nw = anw;
	nx = anx;
	ny = any;
	nz = anz;
	nwnx = nw*nx;
	nwnxny = nw*nx*ny;
}

template<class T> void Array4D<T>::resize(const unsigned int& anw, const unsigned int& anx, const unsigned int& any, const unsigned int& anz, const T& init) {
	clear();  //we won't be able to "rescue" old values, so we reset the whole vector
	vecData.resize(anw*anx*any*anz, init);
	nw = anw;
	nx = anx;
	ny = any;
	nz = anz;
	nwnx = nw*nx;
	nwnxny = nw*nx*ny;
}

template<class T> void Array4D<T>::size(unsigned int& anw, unsigned int& anx, unsigned int& any, unsigned int& anz) const {
	anw=nw;
	anx=nx;
	any=ny;
	anz=nz;
}

template<class T> unsigned int Array4D<T>::getNw() const {
	return nw;
}

template<class T> unsigned int Array4D<T>::getNx() const {
	return nx;
}

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

template<class T> unsigned int Array4D<T>::getNz() const {
	return nz;
}

template<class T> void Array4D<T>::clear() {
	vecData.clear();
	nw = nx = ny = nz = nwnx = nwnxny = 0;
}

template<class T> bool Array4D<T>::isEmpty() const {
	return (nw==0 && nx==0 && ny==0 && nz==0);
}

405
406
template<class T> const std::string Array4D<T>::toString() const {
	std::stringstream os;
Till Schumann's avatar
Neu  
Till Schumann committed
407
	os << "<array4d>\n";
408
409
410
411
412
413
414
415
416
	for (unsigned int ll=0; ll<nw; ll++) {
		os << "dim4[" << ll << "]\n";
		for (unsigned int kk=0; kk<nz; kk++) {
			os << "depth[" << kk << "]\n";
			for(unsigned int ii=0; ii<nx; ii++) {
				for (unsigned int jj=0; jj<ny; jj++) {
					os << operator()(ii,jj,kk,ll) << " ";
				}
				os << "\n";
Till Schumann's avatar
Neu  
Till Schumann committed
417
418
419
420
			}
		}
	}
	os << "</array4d>\n";
421
422
423
424
425
426
427
428
429
430
	return os.str();
}

template<class P> std::iostream& operator<<(std::iostream& os, const Array4D<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.nz), sizeof(array.nz));
	os.write(reinterpret_cast<const char*>(&array.nw), sizeof(array.nw));
	os.write(reinterpret_cast<const char*>(&array.vecData[0]), array.nx*array.ny*array.nz*array.nw*sizeof(P));
Till Schumann's avatar
Neu  
Till Schumann committed
431
432
	return os;
}
433
434
435
436
437
438
439
440
441

template<class P> std::iostream& operator>>(std::iostream& is, Array4D<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));
	is.read(reinterpret_cast<char*>(&array.nz), sizeof(array.nz));
	is.read(reinterpret_cast<char*>(&array.nw), sizeof(array.nw));
	array.vecData.resize(array.nx*array.ny*array.nz*array.nw);
	is.read(reinterpret_cast<char*>(&array.vecData[0]), array.nx*array.ny*array.nz*array.nw*sizeof(P)); //30 times faster than assign() or copy()
442
	return is;
443
444
}

Till Schumann's avatar
Neu  
Till Schumann committed
445
446
447
448
449
450
451
template<class T> T Array4D<T>::getMin() const {

	T min = std::numeric_limits<T>::max();
	const unsigned int nwyz = nwnxny*nz;

	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nwyz; jj++) {
452
			const T val = vecData[jj];
Till Schumann's avatar
Neu  
Till Schumann committed
453
454
455
456
457
			if(val<min) min=val;
		}
		return min;
	} else {
		for (unsigned int jj=0; jj<nwyz; jj++) {
458
			const T val = vecData[jj];
Till Schumann's avatar
Neu  
Till Schumann committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
			if(val!=IOUtils::nodata && val<min) min=val;
		}
		if(min!=std::numeric_limits<T>::max()) return min;
		else return (T)IOUtils::nodata;
	}
}

template<class T> T Array4D<T>::getMax() const {

	T max = -std::numeric_limits<T>::max();
	const unsigned int nwyz = nwnxny*nz;

	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nwyz; jj++) {
473
			const T val = vecData[jj];
Till Schumann's avatar
Neu  
Till Schumann committed
474
475
476
477
478
			if(val>max) max=val;
		}
		return max;
	} else {
		for (unsigned int jj=0; jj<nwyz; jj++) {
479
			const T val = vecData[jj];
Till Schumann's avatar
Neu  
Till Schumann committed
480
481
482
483
484
485
486
487
488
489
490
491
492
493
			if(val!=IOUtils::nodata && val>max) max=val;
		}
		if(max!=-std::numeric_limits<T>::max()) return max;
		else return (T)IOUtils::nodata;
	}
}

template<class T> T Array4D<T>::getMean() const {

	T mean = 0;
	const unsigned int nwyz = nwnxny*nz;

	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nwyz; jj++) {
494
			const T val = vecData[jj];
Till Schumann's avatar
Neu  
Till Schumann committed
495
496
497
498
499
500
501
			mean += val;
		}
		if(nwyz>0) return mean/(T)(nwyz);
		else return (T)0;
	} else {
		unsigned int count = 0;
		for (unsigned int jj=0; jj<nwyz; jj++) {
502
			const T val = vecData[jj];
Till Schumann's avatar
Neu  
Till Schumann committed
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
			if(val!=IOUtils::nodata) {
				mean += val;
				count++;
			}
		}
		if(count>0) return mean/(T)(count);
		else return (T)IOUtils::nodata;
	}
}

template<class T> size_t Array4D<T>::getCount() const
{
	const unsigned int nwyz = nwnxny*nz;

	if(keep_nodata==false) {
		return (size_t)nwyz;
	} else {
		size_t count = 0;
		for (unsigned int ii=0; ii<nwyz; ii++) {
			if(vecData[ii]!=IOUtils::nodata) count++;
		}
		return count;
	}
}

template<class T> void Array4D<T>::abs() {
	if(std::numeric_limits<T>::is_signed) {
		const unsigned int nwyz = nwnxny*nz;
		if(keep_nodata==false) {
532
			for (unsigned int jj=0; jj<nwyz; jj++) {
533
				T& val = vecData[jj];
Till Schumann's avatar
Neu  
Till Schumann committed
534
535
536
				if(val<0) val=-val;
			}
		} else {
537
			for (unsigned int jj=0; jj<nwyz; jj++) {
538
				T& val = vecData[jj];
Till Schumann's avatar
Neu  
Till Schumann committed
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
				if(val<0 && val!=IOUtils::nodata) val=-val;
			}
		}
	}
}


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

	return result;
}

//arithmetic operators
template<class T> bool Array4D<T>::checkEpsilonEquality(const Array4D<double>& rhs, const double& epsilon) const {
	if(nw!=rhs.nw || nx!=rhs.nx || ny!=rhs.ny || nz!=rhs.nz) return false;

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

	return true;
}

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

template<class T> Array4D<T>& Array4D<T>::operator=(const Array4D<T>& source) {
	if(this != &source) {
		keep_nodata = source.keep_nodata;
		nw = source.nw;
		nx = source.nx;
		ny = source.ny;
		nz = source.nz;
		nwnx = source.nwnx;
		nwnxny = source.nwnxny;
		vecData = source.vecData;
	}
	return *this;
}

template<class T> Array4D<T>& Array4D<T>::operator=(const T& value) {
	std::fill(vecData.begin(), vecData.end(), value);
	return *this;
}

template<class T> Array4D<T>& Array4D<T>::operator+=(const Array4D<T>& rhs)
{
	//They have to have equal size
	if ((rhs.nw != nw) || (rhs.nx != nx) || (rhs.ny != ny) || (rhs.nz != nz)) {
		std::stringstream ss;
		ss << "Trying to add two Array4D objects with different dimensions: ";
		ss << "(" << nw << "," << nx << "," << ny << "," << nz << ") + (" << rhs.nw << "," << rhs.nx << "," << rhs.ny << "," << rhs.nz << ")";
		throw IOException(ss.str(), AT);
	}
	//Add to every single member of the Array4D<T>
	const unsigned int nwyz = nwnxny*nz;
	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nwyz; jj++) {
600
			vecData[jj] += rhs(jj);
Till Schumann's avatar
Neu  
Till Schumann committed
601
602
603
		}
	} else {
		for (unsigned int jj=0; jj<nwyz; jj++) {
604
605
			if(vecData[jj]==IOUtils::nodata || rhs(jj)==IOUtils::nodata)
				vecData[jj] = IOUtils::nodata;
Till Schumann's avatar
Neu  
Till Schumann committed
606
			else
607
				vecData[jj] += rhs(jj);
Till Schumann's avatar
Neu  
Till Schumann committed
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
		}
	}

	return *this;
}

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

	return result;
}

template<class T> Array4D<T>& Array4D<T>::operator+=(const T& rhs)
{
	//Add to every single member of the Array4D<T>
	const unsigned int nwyz = nwnxny*nz;
	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nwyz; jj++) {
628
			vecData[jj] += rhs;
Till Schumann's avatar
Neu  
Till Schumann committed
629
630
631
		}
	} else {
		for (unsigned int jj=0; jj<nwyz; jj++) {
632
633
			if(vecData[jj]!=IOUtils::nodata)
				vecData[jj] += rhs;
Till Schumann's avatar
Neu  
Till Schumann committed
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
		}
	}

	return *this;
}

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

	return result;
}

template<class T> Array4D<T>& Array4D<T>::operator-=(const Array4D<T>& rhs)
{
	//They have to have equal size
	if ((rhs.nw != nw) || (rhs.nx != nx) || (rhs.ny != ny) || (rhs.nz != nz)) {
		std::stringstream ss;
		ss << "Trying to substract two Array4D objects with different dimensions: ";
		ss << "(" << nw << "," << nx << "," << ny << "," << nz << ") - (" << rhs.nw << "," << rhs.nx << "," << rhs.ny << "," << rhs.nz << ")";
		throw IOException(ss.str(), AT);
	}
	//Substract to every single member of the Array4D<T>
	const unsigned int nwyz = nwnxny*nz;
	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nwyz; jj++) {
661
			vecData[jj] -= rhs(jj);
Till Schumann's avatar
Neu  
Till Schumann committed
662
663
664
		}
	} else {
		for (unsigned int jj=0; jj<nwyz; jj++) {
665
666
			if(vecData[jj]==IOUtils::nodata || rhs(jj)==IOUtils::nodata)
				vecData[jj] = IOUtils::nodata;
Till Schumann's avatar
Neu  
Till Schumann committed
667
			else
668
				vecData[jj] -= rhs(jj);
Till Schumann's avatar
Neu  
Till Schumann committed
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
		}
	}

	return *this;
}

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

	return result;
}

template<class T> Array4D<T>& Array4D<T>::operator-=(const T& rhs)
{
685
	*this += -rhs;
Till Schumann's avatar
Neu  
Till Schumann committed
686
687
688
689
690
691
	return *this;
}

template<class T> const Array4D<T> Array4D<T>::operator-(const T& rhs)
{
	Array4D<T> result = *this;
692
	result += -rhs; //already implemented
Till Schumann's avatar
Neu  
Till Schumann committed
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709

	return result;
}

template<class T> Array4D<T>& Array4D<T>::operator*=(const Array4D<T>& rhs)
{
	//They have to have equal size
	if ((rhs.nw != nw) || (rhs.nx != nx) || (rhs.ny != ny) || (rhs.nz != nz)) {
		std::stringstream ss;
		ss << "Trying to multiply two Array4D objects with different dimensions: ";
		ss << "("<< nw << "," << nx << "," << ny << "," << nz << ") * (" << rhs.nw << "," << rhs.nx << "," << rhs.ny << "," << rhs.nz << ")";
		throw IOException(ss.str(), AT);
	}
	//Multiply every single member of the Array4D<T>
	const unsigned int nwxyz = nwnxny*nz;
	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nwxyz; jj++) {
710
			vecData[jj] *= rhs(jj);
Till Schumann's avatar
Neu  
Till Schumann committed
711
712
713
		}
	} else {
		for (unsigned int jj=0; jj<nwxyz; jj++) {
714
715
			if(vecData[jj]==IOUtils::nodata || rhs(jj)==IOUtils::nodata)
				vecData[jj] = IOUtils::nodata;
Till Schumann's avatar
Neu  
Till Schumann committed
716
			else
717
				vecData[jj] *= rhs(jj);
Till Schumann's avatar
Neu  
Till Schumann committed
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
		}
	}

	return *this;
}

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

	return result;
}

template<class T> Array4D<T>& Array4D<T>::operator*=(const T& rhs)
{
	//Multiply every single member of the Array4D<T>
	const unsigned int nwxyz = nwnxny*nz;
	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nwxyz; jj++) {
738
			vecData[jj] *= rhs;
Till Schumann's avatar
Neu  
Till Schumann committed
739
740
741
		}
	} else {
		for (unsigned int jj=0; jj<nwxyz; jj++) {
742
743
			if(vecData[jj]!=IOUtils::nodata)
				vecData[jj] *= rhs;
Till Schumann's avatar
Neu  
Till Schumann committed
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
		}
	}

	return *this;
}

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

	return result;
}

template<class T> Array4D<T>& Array4D<T>::operator/=(const Array4D<T>& rhs)
{
	//They have to have equal size
	if ((rhs.nw != nw) || (rhs.nx != nx) || (rhs.ny != ny) || (rhs.nz != nz)) {
		std::stringstream ss;
		ss << "Trying to divide two Array4D objects with different dimensions: ";
		ss << "(" << nw << "," << nx << "," << ny << "," << nz << ") / (" << rhs.nw << "," << rhs.nx << "," << rhs.ny << "," << rhs.nz << ")";
		throw IOException(ss.str(), AT);
	}
	//Divide every single member of the Array4D<T>
	const unsigned int nwxyz = nwnxny*nz;
	if(keep_nodata==false) {
		for (unsigned int jj=0; jj<nwxyz; jj++) {
771
			vecData[jj] /= rhs(jj);
Till Schumann's avatar
Neu  
Till Schumann committed
772
773
774
		}
	} else {
		for (unsigned int jj=0; jj<nwxyz; jj++) {
775
776
			if(vecData[jj]==IOUtils::nodata || rhs(jj)==IOUtils::nodata)
				vecData[jj] = IOUtils::nodata;
Till Schumann's avatar
Neu  
Till Schumann committed
777
			else
778
				vecData[jj] /= rhs(jj);
Till Schumann's avatar
Neu  
Till Schumann committed
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
		}
	}

	return *this;
}

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

	return result;
}

template<class T> Array4D<T>& Array4D<T>::operator/=(const T& rhs)
{
795
	*this *= (1./rhs);
Till Schumann's avatar
Neu  
Till Schumann committed
796
797
798
799
800
801
	return *this;
}

template<class T> const Array4D<T> Array4D<T>::operator/(const T& rhs)
{
	Array4D<T> result = *this;
802
	result *= (1./rhs); //already implemented
Till Schumann's avatar
Neu  
Till Schumann committed
803
804
805
806

	return result;
}

807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
template<class T> bool Array4D<T>::operator==(const Array4D<T>& in) const {
	unsigned int in_nx=in.getNx(), in_ny=in.getNy(), in_nz=in.getNz(), in_nw=in.getNw();

	if(nx!=in_nx || ny!=in_ny || nz!=in_nz || nw!=in_nw)
		return false;

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

	return true;
}

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

Till Schumann's avatar
Neu  
Till Schumann committed
824
825
826
} //end namespace mio

#endif