32namespace mio {
template <
class T>
class Array2D; }
43template <
class T>
class Array2DProxy {
45 friend class Array2D<T>;
46 T& operator[](
const size_t& j) {
47 return array2D(anx, j);
51 Array2DProxy(Array2D<T>& i_array2D,
const size_t& i_anx) : array2D(i_array2D), anx(i_anx){}
74 Array2D(
const size_t& anx,
const size_t& any);
82 Array2D(
const size_t& anx,
const size_t& any,
const T& init);
95 const size_t& i_ncols,
const size_t& i_nrows);
115 const size_t& i_ncols,
const size_t& i_nrows);
128 const size_t& i_ncols,
const size_t& i_nrows);
145 void resize(
const size_t& anx,
const size_t& any);
146 void resize(
const size_t& anx,
const size_t& any,
const T& init);
147 void size(
size_t& anx,
size_t& any)
const;
231 return vecData.at(i);
239 return vecData.at(i);
246 if ((x >= nx) || (y >= ny)) {
247 std::stringstream ss;
248 ss <<
"Trying to access array(" << x <<
"," << y <<
")";
249 ss <<
" while array is (" << nx <<
"," << ny <<
")";
254 return vecData[x + y*nx];
259 if ((x >= nx) || (y >= ny)) {
260 std::stringstream ss;
261 ss <<
"Trying to access array(" << x <<
"," << y <<
")";
262 ss <<
" while array is (" << nx <<
"," << ny <<
")";
266 return vecData[x + y*nx];
270 return Array2DProxy<T>(*
this, i);
278 const size_t& i_ncols,
const size_t& i_nrows) :
279 vecData(i_ncols*i_nrows), nx(i_ncols), ny(i_nrows), keep_nodata(true)
281 subset(i_array2D, i_nx, i_ny, i_ncols, i_nrows);
285 : vecData(array3D.getNx()*array3D.getNy()), nx(array3D.getNx()), ny(array3D.getNy()), keep_nodata(array3D.getKeepNodata())
288 for (
size_t jj=0; jj<
ny; jj++) {
289 for (
size_t ii=0; ii<
nx; ii++) {
296 const size_t& i_ncols,
const size_t& i_nrows)
298 if (((i_nx+i_ncols) > i_array2D.
nx) || ((i_ny+i_nrows) > i_array2D.
ny)) {
299 std::stringstream ss;
300 ss <<
"Trying to cut an array of size (" << i_array2D.
nx <<
"," << i_array2D.
ny <<
") ";
301 ss <<
"to size (" << i_ncols <<
"," << i_nrows <<
") starting at (" << i_nx <<
"," << i_ny <<
")";
305 if ((i_ncols == 0) || (i_nrows == 0))
308 resize(i_ncols, i_nrows);
310 for (
size_t jj=0; jj<ny; jj++) {
311 for (
size_t ii=0; ii<nx; ii++) {
312 operator()(ii,jj) = i_array2D(i_nx+ii, i_ny+jj);
319 size_t i_ncols, i_nrows;
320 i_array2D.
size(i_ncols, i_nrows);
321 fill(i_array2D, i_nx, i_ny, i_ncols, i_nrows);
325 const size_t& i_ncols,
const size_t& i_nrows)
327 if (((i_nx+i_ncols) > nx) || ((i_ny+i_nrows) > ny)) {
328 std::stringstream ss;
329 ss <<
"Filling an array of size (" << nx <<
"," << ny <<
") ";
330 ss <<
"with an array of size (" << i_ncols <<
"," << i_nrows <<
") ";
331 ss <<
"starting at (" << i_nx <<
"," << i_ny <<
")";
335 if ((i_ncols == 0) || (i_nrows == 0))
338 for (
size_t jj=i_ny; jj<(i_ny+i_nrows); jj++) {
339 const size_t iy = jj-i_ny;
340 for (
size_t ii=i_nx; ii<(i_nx+i_ncols); ii++) {
341 const size_t ix = ii-i_nx;
342 operator()(ii,jj) = i_array2D(ix, iy);
348 vecData(anx*any, init), nx(anx), ny(any), keep_nodata(true) {}
351 vecData(anx*any), nx(anx), ny(any), keep_nodata(true) {}
354 keep_nodata = i_keep_nodata;
363 vecData.resize(anx*any);
370 vecData.resize(anx*any, init);
398 return (nx==0 && ny==0);
402 std::ostringstream os;
404 for (
size_t jj=0; jj<ny; jj++) {
405 const size_t jnx = jj*nx;
406 for (
size_t ii=0; ii<nx; ii++) {
407 os << vecData[ii+jnx] <<
" ";
411 os <<
"</array2d>\n";
417 os.write(
reinterpret_cast<const char*
>(&array.
nx),
sizeof(array.
nx));
418 os.write(
reinterpret_cast<const char*
>(&array.
ny),
sizeof(array.
ny));
419 os.write(
reinterpret_cast<const char*
>(&array.
vecData[0]),
static_cast<std::streamsize
>(array.
nx*array.
ny*
sizeof(P)));
425 is.read(
reinterpret_cast<char*
>(&array.
nx),
sizeof(array.
nx));
426 is.read(
reinterpret_cast<char*
>(&array.
ny),
sizeof(array.
ny));
428 is.read(
reinterpret_cast<char*
>(&array.
vecData[0]),
static_cast<std::streamsize
>(array.
nx*array.
ny*
sizeof(P)));
434 T min = std::numeric_limits<T>::max();
436 const size_t nxy = ny*nx;
437 if (keep_nodata==
false) {
438 min = *min_element(vecData.begin(), vecData.end());
439 if (min!=std::numeric_limits<T>::max())
return min;
442 for (
size_t jj=0; jj<nxy; jj++) {
443 const T val = vecData[jj];
446 if (min!=std::numeric_limits<T>::max())
return min;
453 T max = -std::numeric_limits<T>::max();
455 const size_t nxy = ny*nx;
456 if (keep_nodata==
false) {
457 max = *max_element(vecData.begin(), vecData.end());
458 if (max!=-std::numeric_limits<T>::max())
return max;
461 for (
size_t jj=0; jj<nxy; jj++) {
462 const T val = vecData[jj];
465 if (max!=-std::numeric_limits<T>::max())
return max;
473 const size_t nxy = nx*ny;
475 if (keep_nodata==
false) {
476 if (nxy>0)
return std::accumulate(vecData.begin(), vecData.end(), 0.) / (T)(nxy);
480 for (
size_t jj=0; jj<nxy; jj++) {
481 const T val = vecData[jj];
494 const size_t nxy = nx*ny;
496 if (keep_nodata==
false) {
500 for (
size_t ii=0; ii<nxy; ii++) {
508 if (std::numeric_limits<T>::is_signed) {
509 const size_t nxy = nx*ny;
510 if (keep_nodata==
false) {
511 for (
size_t ii=0; ii<nxy; ii++) {
512 T& val = vecData[ii];
516 for (
size_t ii=0; ii<nxy; ii++) {
517 T& val = vecData[ii];
534 if (nx!=rhs.
nx || ny!=rhs.
ny)
return false;
536 const size_t nxy = nx*ny;
537 for (
size_t jj=0; jj<nxy; jj++)
548 std::fill(vecData.begin(), vecData.end(), value);
555 if ((rhs.
nx != nx) || (rhs.
ny != ny)) {
556 std::stringstream ss;
557 ss <<
"Trying to add two Array2D objects with different dimensions: ";
558 ss <<
"(" << nx <<
"," << ny <<
") + (" << rhs.
nx <<
"," << rhs.
ny <<
")";
562 const size_t nxy = nx*ny;
564 if (keep_nodata==
false) {
565 for (
size_t jj=0; jj<nxy; jj++)
566 vecData[jj] += rhs(jj);
568 for (
size_t jj=0; jj<nxy; jj++) {
572 vecData[jj] += rhs(jj);
589 if (rhs==0.)
return *
this;
592 const size_t nxy = nx*ny;
594 if (keep_nodata==
false) {
595 for (
size_t jj=0; jj<nxy; jj++)
598 for (
size_t jj=0; jj<nxy; jj++) {
618 if ((rhs.
nx != nx) || (rhs.
ny != ny)){
619 std::stringstream ss;
620 ss <<
"Trying to substract two Array2D objects with different dimensions: ";
621 ss <<
"(" << nx <<
"," << ny <<
") - (" << rhs.
nx <<
"," << rhs.
ny <<
")";
625 const size_t nxy = nx*ny;
627 if (keep_nodata==
false) {
628 for (
size_t jj=0; jj<nxy; jj++)
629 vecData[jj] -= rhs(jj);
631 for (
size_t jj=0; jj<nxy; jj++) {
635 vecData[jj] -= rhs(jj);
667 if ((rhs.
nx != nx) || (rhs.
ny != ny)){
668 std::stringstream ss;
669 ss <<
"Trying to multiply two Array2D objects with different dimensions: ";
670 ss <<
"(" << nx <<
"," << ny <<
") * (" << rhs.
nx <<
"," << rhs.
ny <<
")";
674 const size_t nxy = nx*ny;
676 if (keep_nodata==
false) {
677 for (
size_t jj=0; jj<nxy; jj++)
678 vecData[jj] *= rhs(jj);
680 for (
size_t jj=0; jj<nxy; jj++) {
684 vecData[jj] *= rhs(jj);
701 if (rhs==1.)
return *
this;
704 const size_t nxy = nx*ny;
706 if (keep_nodata==
false) {
707 for (
size_t jj=0; jj<nxy; jj++)
710 for (
size_t jj=0; jj<nxy; jj++) {
730 if ((rhs.
nx != nx) || (rhs.
ny != ny)){
731 std::stringstream ss;
732 ss <<
"Trying to divide two Array2D objects with different dimensions: ";
733 ss <<
"(" << nx <<
"," << ny <<
") / (" << rhs.
nx <<
"," << rhs.
ny <<
")";
737 const size_t nxy = nx*ny;
739 if (keep_nodata==
false) {
740 for (
size_t jj=0; jj<nxy; jj++)
741 vecData[jj] /= rhs(jj);
743 for (
size_t jj=0; jj<nxy; jj++) {
747 vecData[jj] /= rhs(jj);
777 const size_t in_nx=in.
getNx(), in_ny=in.
getNy();
779 if (nx!=in_nx || ny!=in_ny)
782 const size_t nxy = nx*ny;
783 for (
size_t jj=0; jj<nxy; jj++)
#define AT
Definition: IOExceptions.h:28
The template class Array2D is a 2D Array (Matrix) able to hold any type of object as datatype....
Definition: Array2D.h:65
const Array2D< T > operator/(const T &rhs) const
Definition: Array2D.h:768
const Array2D< T > getAbs() const
returns the grid of the absolute value of values contained in the grid
Definition: Array2D.h:524
static bool checkEpsilonEquality(const Array2D< double > &rhs1, const Array2D< double > &rhs2, const double &epsilon)
Definition: Array2D.h:543
Array2D< T > & operator*=(const T &rhs)
Definition: Array2D.h:699
size_t getCount() const
returns the number of points contained in the grid. If setNodataHandling(IOUtils::RAW_NODATA),...
Definition: Array2D.h:492
void fill(const Array2D< T > &i_array2D, const size_t &i_nx, const size_t &i_ny, const size_t &i_ncols, const size_t &i_nrows)
A method that can be used to insert a subplane into an existing Array2D object that is passed as i_ar...
Definition: Array2D.h:324
void resize(const size_t &anx, const size_t &any)
Definition: Array2D.h:361
Array2D(const Array2D< T > &i_array2D, const size_t &i_nx, const size_t &i_ny, const size_t &i_ncols, const size_t &i_nrows)
Definition: Array2D.h:277
T getMin() const
returns the minimum value contained in the grid
Definition: Array2D.h:432
Array2D< T > & operator*=(const Array2D< T > &rhs)
Definition: Array2D.h:664
const Array2D< T > operator*(const Array2D< T > &rhs) const
Definition: Array2D.h:691
const Array2D< T > operator-(const T &rhs) const
Definition: Array2D.h:656
Array2D< T > & operator/=(const Array2D< T > &rhs)
Definition: Array2D.h:727
std::vector< T > vecData
Definition: Array2D.h:223
const Array2D< T > operator-(const Array2D< T > &rhs) const
Definition: Array2D.h:642
const Array2D< T > operator+(const T &rhs) const
Definition: Array2D.h:607
size_t getNx() const
Definition: Array2D.h:384
void abs()
Definition: Array2D.h:507
Array2DProxy< T > operator[](const size_t &i)
Definition: Array2D.h:269
void setKeepNodata(const bool i_keep_nodata)
set how to process nodata values (ie: as nodata or as normal numbers)
Definition: Array2D.h:353
size_t ny
Definition: Array2D.h:225
bool checkEpsilonEquality(const Array2D< double > &rhs, const double &epsilon) const
Definition: Array2D.h:533
friend std::ostream & operator<<(std::ostream &os, const Array2D< P > &array)
Definition: Array2D.h:415
size_t getNy() const
Definition: Array2D.h:388
T getMax() const
returns the maximum value contained in the grid
Definition: Array2D.h:451
bool getKeepNodata() const
get how to process nodata values (ie: as nodata or as normal numbers)
Definition: Array2D.h:357
Array2D< T > & operator-=(const Array2D< T > &rhs)
Definition: Array2D.h:615
friend std::istream & operator>>(std::istream &is, Array2D< P > &array)
Definition: Array2D.h:423
bool operator==(const Array2D< T > &) const
Operator that tests for equality.
Definition: Array2D.h:776
Array2D< T > & operator+=(const T &rhs)
Definition: Array2D.h:587
bool operator!=(const Array2D< T > &) const
Operator that tests for inequality.
Definition: Array2D.h:789
size_t nx
Definition: Array2D.h:224
const std::string toString() const
Definition: Array2D.h:401
Array2D< T > & operator/=(const T &rhs)
Definition: Array2D.h:762
T getMean() const
returns the mean value contained in the grid
Definition: Array2D.h:470
Array2D()
Definition: Array2D.h:273
Array2D< T > & operator=(const T &value)
Definition: Array2D.h:547
T & operator()(const size_t &x, const size_t &y)
Definition: Array2D.h:244
Array2D(const size_t &anx, const size_t &any)
Definition: Array2D.h:350
Array2D(const Array3D< T > &array3D, const size_t &depth)
Definition: Array2D.h:284
bool keep_nodata
Definition: Array2D.h:226
const Array2D< T > operator/(const Array2D< T > &rhs) const
Definition: Array2D.h:754
void subset(const Array2D< T > &i_array2D, const size_t &i_nx, const size_t &i_ny, const size_t &i_ncols, const size_t &i_nrows)
A method that can be used to cut out a subplane of an existing Array2D object that is passed as i_arr...
Definition: Array2D.h:295
size_t size() const
Definition: Array2D.h:380
Array2D< T > & operator-=(const T &rhs)
Definition: Array2D.h:650
void resize(const size_t &anx, const size_t &any, const T &init)
Definition: Array2D.h:368
Array2D(const size_t &anx, const size_t &any, const T &init)
Definition: Array2D.h:347
void clear()
Definition: Array2D.h:392
void size(size_t &anx, size_t &any) const
Definition: Array2D.h:375
bool empty() const
Definition: Array2D.h:397
const Array2D< T > operator+(const Array2D< T > &rhs) const
Definition: Array2D.h:579
void fill(const Array2D< T > &i_array2D, const size_t &i_nx, const size_t &i_ny)
Definition: Array2D.h:317
const Array2D< T > operator*(const T &rhs) const
Definition: Array2D.h:719
Array2D< T > & operator+=(const Array2D< T > &rhs)
Definition: Array2D.h:552
The template class Array3D is a 3D Array (Tensor) able to hold any type of object as datatype....
Definition: Array3D.h:87
The basic exception class adjusted for the needs of SLF software.
Definition: IOExceptions.h:40
thrown when an index is out of bounds
Definition: IOExceptions.h:106
static const double e
Definition: Meteoconst.h:68
const double nodata
This is the internal nodata value.
Definition: IOUtils.h:75
size_t count(const std::string &input, const std::string &search)
count how many times a substring appears in a string
Definition: IOUtils.cc:257
bool checkEpsilonEquality(const double &val1, const double &val2, const double &epsilon)
Check whether two values are equal regarding a certain epsilon environment (within certain radius of ...
Definition: IOUtils.h:121
std::istream & operator>>(std::istream &is, Config &cfg)
Definition: Config.cc:480
std::ostream & operator<<(std::ostream &os, const Config &cfg)
Definition: Config.cc:445