WSL/SLF GitLab Repository

Commit ba0908dc authored by Mathias Bavay's avatar Mathias Bavay
Browse files

The Arrays now skip some unecessary tests in resize(). Since the arguments are...

The Arrays now skip some unecessary tests in resize(). Since the arguments are unsigned, there can not be any negative size! This also now allows setting size to 0. A new "operator" has been added to get the absolute value of an array. This unfortunatelly generate a warning for unsigned arrays despite that the necessary signedness test is performed. This warning will have to be disabled one way or another.

The example data set has been brought back to SMET version 1.1
parent e5d482a4
SMET 0.99 ASCII
SMET 1.1 ASCII
[HEADER]
station_id = FIR2
station_name = Schmidigen-Bidmeren
......
SMET 0.99 ASCII
SMET 1.1 ASCII
[HEADER]
station_id = FLU2
station_name = Fluela Hospiz
......
SMET 0.99 ASCII
SMET 1.1 ASCII
[HEADER]
station_id = FRA2
station_name = Efra
......
SMET 0.99 ASCII
SMET 1.1 ASCII
[HEADER]
station_id = GLA2
station_name = Guppen
......
SMET 0.99 ASCII
SMET 1.1 ASCII
[HEADER]
station_id = ILI2
station_name = Les Collines
......
SMET 0.99 ASCII
SMET 1.1 ASCII
[HEADER]
station_id = OTT2
station_name = Ottere
......
SMET 0.99 ASCII
SMET 1.1 ASCII
[HEADER]
station_id = TUJ3
station_name = Nual
......@@ -12,6 +12,7 @@ nodata = -999
tz = 1
fields = timestamp TA RH VW DW OSWR HS PSUM TSG TSS
units_multiplier = 1 1 1 1 1 1 0.01 1 1 1
units_offset = 0 0 0 0 0 0 0 0 0 0
[DATA]
2008-12-01T00:00 265.95 0.988 0.0 335 0 142.000 0.000 273.55 264.45
2008-12-01T01:00 265.95 0.987 0.0 196 0 142.000 0.000 273.55 264.25
......
......@@ -18,13 +18,13 @@
#ifndef ARRAY_H
#define ARRAY_H
#include <meteoio/IOUtils.h>
#include <meteoio/IOExceptions.h>
#include <vector>
#include <limits>
#include <iostream>
#include <meteoio/IOUtils.h>
#include <meteoio/IOExceptions.h>
#define NOSAFECHECKS
namespace mio {
......@@ -41,8 +41,16 @@ template<class T> class Array {
public:
Array(const unsigned int& asize=0);
/**
* A constructor that creates an array filled with constant values
* @param asize size of the new array
* @param init initial value to fill the array with
*/
Array(const unsigned int& asize, const T& init);
unsigned int size();
void resize(const unsigned int& asize);
void resize(const unsigned int& asize, const T& init);
void clear();
void insertAt(const int& index, T e);
void removeAt(const unsigned int& index);
......@@ -94,6 +102,8 @@ 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
......@@ -102,7 +112,10 @@ template<class T> class Array {
template<class T> Array<T>::Array(const unsigned int& asize) {
resize(asize);
nx = asize;
}
template<class T> Array<T>::Array(const unsigned int& asize, const T& init) {
resize(asize, init);
}
template<class T> unsigned int Array<T>::size() {
......@@ -110,12 +123,16 @@ template<class T> unsigned int Array<T>::size() {
}
template<class T> void Array<T>::resize(const unsigned int& asize) {
if (asize != vecData.size()) {
vecData.resize(asize);
nx = asize;
}
vecData.resize(asize);
nx = asize;
}
template<class T> void Array<T>::resize(const unsigned int& asize, const T& init) {
resize(asize);
std::fill(vecData.begin(), vecData.end(), init);
}
template<class T> T& Array<T>::operator()(const unsigned int& index) {
#ifndef NOSAFECHECKS
if (index >= nx) {
......@@ -434,6 +451,23 @@ 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
......@@ -159,6 +159,9 @@ 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;
......@@ -235,15 +238,10 @@ template<class T> Array2D<T>::Array2D(const unsigned int& anx, const unsigned in
}
template<class T> void Array2D<T>::resize(const unsigned int& anx, const unsigned int& any) {
clear();
if ((anx > 0) && (any > 0)) {
vecData.resize(anx*any);
nx = anx;
ny = any;
} else {
throw IndexOutOfBoundsException("Can not resize a 2D array to negative sizes!", AT);
}
clear(); //we won't be able to "rescue" old values, so we reset the whole vector
vecData.resize(anx*any);
nx = anx;
ny = any;
}
template<class T> void Array2D<T>::resize(const unsigned int& anx, const unsigned int& any, const T& init) {
......@@ -523,6 +521,24 @@ 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
......@@ -191,6 +191,9 @@ 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;
......@@ -276,17 +279,12 @@ template<class T> Array3D<T>::Array3D(const unsigned int& anx, const unsigned in
}
template<class T> void Array3D<T>::resize(const unsigned int& anx, const unsigned int& any, const unsigned int& anz) {
clear();
if ((anx > 0) && (any > 0) && (anz > 0)) {
vecData.resize(anx*any*anz);
nx = anx;
ny = any;
nz = anz;
nxny = nx*ny;
} else {
throw IndexOutOfBoundsException("", AT);
}
clear(); //we won't be able to "rescue" old values, so we reset the whole vector
vecData.resize(anx*any*anz);
nx = anx;
ny = any;
nz = anz;
nxny = nx*ny;
}
template<class T> void Array3D<T>::resize(const unsigned int& anx, const unsigned int& any, const unsigned int& anz, const T& init) {
......@@ -603,6 +601,24 @@ 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