WSL/SLF GitLab Repository

Commit 5d982c00 authored by Mathias Bavay's avatar Mathias Bavay
Browse files

The Array::getAbs() and abs() methods were not offering nodata handling, this...

The Array::getAbs() and abs() methods were not offering nodata handling, this has been fixed. A new getSum() method has been implemented for arrays (with proper choice of nodata handling)
parent f504046f
......@@ -72,6 +72,20 @@ template<class T> class Array {
* @return mean value
*/
T getMean(const IOUtils::nodata_handling flag_nodata=IOUtils::PARSE_NODATA) const;
/**
* @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 Array<T> getAbs(const IOUtils::nodata_handling flag_nodata=IOUtils::PARSE_NODATA) const;
void abs(const IOUtils::nodata_handling flag_nodata=IOUtils::PARSE_NODATA);
template<class P> friend std::ostream& operator<<(std::ostream& os, const Array<P>& array);
T& operator [](const unsigned int& index);
......@@ -102,9 +116,6 @@ template<class T> class Array {
Array<T>& operator/=(const Array<T>& rhs);
const Array<T> operator/(const Array<T>& rhs);
void abs();
const Array<T> getAbs() const;
protected:
std::vector<T> vecData; ///<the actual data structure, that holds the objects of type T
unsigned int nx; ///<this is introduced to omit the costly vecData.size()
......@@ -276,6 +287,57 @@ template<class T> T Array<T>::getMean(const IOUtils::nodata_handling flag_nodata
}
}
template<class T> T Array<T>::getSum(const IOUtils::nodata_handling flag_nodata) const {
T sum = 0;
if(flag_nodata==IOUtils::RAW_NODATA) {
for (unsigned int ii=0; ii<nx; ii++) {
sum += vecData[ii];
}
return sum;
} else if(flag_nodata==IOUtils::PARSE_NODATA) {
bool empty=true;
for (unsigned int ii=0; ii<nx; 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 Array<T>::abs(const IOUtils::nodata_handling flag_nodata) {
if(std::numeric_limits<T>::is_signed) {
if(flag_nodata==IOUtils::RAW_NODATA) {
for (unsigned int ii=0; ii<nx; ii++) {
T& val = operator()(ii);
if(val<0) val=-val;
}
} else if(flag_nodata==IOUtils::PARSE_NODATA) {
for (unsigned int ii=0; ii<nx; 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 Array<T> Array<T>::getAbs(const IOUtils::nodata_handling flag_nodata) const {
Array<T> result = *this; //make a copy
result.abs(flag_nodata); //already implemented
return result;
}
//arithmetic operators
template<class T> Array<T>& Array<T>::operator=(const Array<T>& source) {
if(this != &source) {
......@@ -451,23 +513,6 @@ template<class T> const Array<T> Array<T>::operator/(const T& rhs)
return result;
}
template<class T> void Array<T>::abs() {
if(std::numeric_limits<T>::is_signed) {
for (unsigned int ii=0; ii<nx; ii++) {
T& val = operator()(ii);
if(val<0) val=-val;
}
}
}
template<class T> const Array<T> Array<T>::getAbs() const {
Array<T> result = *this; //make a copy
result.abs(); //already implemented
return result;
}
} //end namespace mio
#endif
......@@ -126,6 +126,20 @@ template<class T> class Array2D {
* @return mean value
*/
T getMean(const IOUtils::nodata_handling flag_nodata=IOUtils::PARSE_NODATA) const;
/**
* @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);
template<class P> friend std::ostream& operator<<(std::ostream& os, const Array2D<P>& array);
......@@ -159,9 +173,6 @@ template<class T> class Array2D {
Array2D<T>& operator/=(const Array2D<T>& rhs);
const Array2D<T> operator/(const Array2D<T>& rhs);
void abs();
const Array2D<T> getAbs() const;
protected:
std::vector<T> vecData;
unsigned int nx;
......@@ -346,6 +357,59 @@ template<class T> T Array2D<T>::getMean(const IOUtils::nodata_handling flag_noda
}
}
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;
}
//arithmetic operators
template<class T> Array2D<T>& Array2D<T>::operator=(const Array2D<T>& source) {
if(this != &source) {
......@@ -521,24 +585,6 @@ template<class T> const Array2D<T> Array2D<T>::operator/(const T& rhs)
return result;
}
template<class T> void Array2D<T>::abs() {
if(std::numeric_limits<T>::is_signed) {
const unsigned int nxy = nx*ny;
for (unsigned int ii=0; ii<nxy; ii++) {
T& val = operator()(ii);
if(val<0) val=-val;
}
}
}
template<class T> const Array2D<T> Array2D<T>::getAbs() const {
Array2D<T> result = *this; //make a copy
result.abs(); //already implemented
return result;
}
} //end namespace mio
#endif
......@@ -159,6 +159,19 @@ template<class T> class Array3D {
* @return mean value
*/
T getMean(const IOUtils::nodata_handling flag_nodata=IOUtils::PARSE_NODATA) const;
/**
* @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 Array3D<T> getAbs(const IOUtils::nodata_handling flag_nodata=IOUtils::PARSE_NODATA) const;
void abs(const IOUtils::nodata_handling flag_nodata=IOUtils::PARSE_NODATA);
template<class P> friend std::ostream& operator<<(std::ostream& os, const Array3D<P>& array);
......@@ -191,9 +204,6 @@ template<class T> class Array3D {
Array3D<T>& operator/=(const Array3D<T>& rhs);
const Array3D<T> operator/(const Array3D<T>& rhs);
void abs();
const Array3D<T> getAbs() const;
protected:
std::vector<T> vecData; ///< The actual objects are stored in a one-dimensional vector
unsigned int nx;
......@@ -392,6 +402,59 @@ template<class T> T Array3D<T>::getMean(const IOUtils::nodata_handling flag_noda
}
}
template<class T> T Array3D<T>::getSum(const IOUtils::nodata_handling flag_nodata) const {
T sum = 0;
const unsigned int nxyz = nx*ny*nz;
if(flag_nodata==IOUtils::RAW_NODATA) {
for (unsigned int ii=0; ii<nxyz; ii++) {
sum += vecData[ii];
}
return sum;
} else if(flag_nodata==IOUtils::PARSE_NODATA) {
bool empty=true;
for (unsigned int ii=0; ii<nxyz; 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 Array3D<T>::abs(const IOUtils::nodata_handling flag_nodata) {
if(std::numeric_limits<T>::is_signed) {
const unsigned int nxyz = nx*ny*nz;
if(flag_nodata==IOUtils::RAW_NODATA) {
for (unsigned int ii=0; ii<nxyz; ii++) {
T& val = operator()(ii);
if(val<0) val=-val;
}
} else if(flag_nodata==IOUtils::PARSE_NODATA) {
for (unsigned int ii=0; ii<nxyz; 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 Array3D<T> Array3D<T>::getAbs(const IOUtils::nodata_handling flag_nodata) const {
Array3D<T> result = *this; //make a copy
result.abs(flag_nodata); //already implemented
return result;
}
//arithmetic operators
template<class T> Array3D<T>& Array3D<T>::operator=(const Array3D<T>& source) {
if(this != &source) {
......@@ -601,24 +664,6 @@ template<class T> const Array3D<T> Array3D<T>::operator/(const T& rhs)
return result;
}
template<class T> void Array3D<T>::abs() {
if(std::numeric_limits<T>::is_signed) {
const unsigned int nxyz = nx*ny*nz;
for (unsigned int ii=0; ii<nxyz; ii++) {
T& val = operator()(ii);
if(val<0) val=-val;
}
}
}
template<class T> const Array3D<T> Array3D<T>::getAbs() const {
Array3D<T> result = *this; //make a copy
result.abs(); //already implemented
return result;
}
} //end namespace mio
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment