Commit 3a7ca41a authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

arrayWeightedMean: changed interface to vector.

parent 705382fb
......@@ -488,10 +488,10 @@ arrayMeanMV(const size_t len, const double *restrict array, const double missval
}
double
arrayWeightedMean(const size_t len, const double *restrict array, const double *restrict w, const double missval)
arrayWeightedMean(const size_t len, const std::vector<double> &array, const std::vector<double> &w, const double missval)
{
assert(array != nullptr);
assert(w != nullptr);
assert(array.size() >= len);
assert(w.size() >= len);
double sum = 0, sumw = 0;
......@@ -505,12 +505,12 @@ arrayWeightedMean(const size_t len, const double *restrict array, const double *
}
double
arrayWeightedMeanMV(const size_t len, const double *restrict array, const double *restrict w, const double missval)
arrayWeightedMeanMV(const size_t len, const std::vector<double> &array, const std::vector<double> &w, const double missval)
{
assert(array != nullptr);
assert(w != nullptr);
assert(array.size() >= len);
assert(w.size() >= len);
double missval1 = missval, missval2 = missval;
const double missval1 = missval, missval2 = missval;
double sum = 0, sumw = 0;
for (size_t i = 0; i < len; ++i)
......@@ -541,12 +541,12 @@ arrayAvgMV(const size_t len, const double *restrict array, const double missval)
}
double
arrayWeightedAvgMV(const size_t len, const double *restrict array, const double *restrict w, const double missval)
arrayWeightedAvgMV(const size_t len, const std::vector<double> &array, const std::vector<double> &w, const double missval)
{
assert(array != nullptr);
assert(w != nullptr);
assert(array.size() >= len);
assert(w.size() >= len);
double missval1 = missval, missval2 = missval;
const double missval1 = missval, missval2 = missval;
double sum = 0, sumw = 0;
for (size_t i = 0; i < len; ++i)
......
......@@ -17,7 +17,7 @@
#ifndef ARRAY_H
#define ARRAY_H
//#include <vector>
#include <vector>
#include <cstddef>
#include "compare.h"
......@@ -138,10 +138,10 @@ double arraySumMV(const size_t len, const double *array, const double missval);
double arrayMean(const size_t len, const double *array);
double arrayMeanMV(const size_t len, const double *array, const double missval);
double arrayWeightedMean(const size_t len, const double *array, const double *w, const double missval);
double arrayWeightedMeanMV(const size_t len, const double *array, const double *w, const double missval);
double arrayWeightedMean(const size_t len, const std::vector<double> &array, const std::vector<double> &w, const double missval);
double arrayWeightedMeanMV(const size_t len, const std::vector<double> &array, const std::vector<double> &w, const double missval);
double arrayAvgMV(const size_t len, const double *array, double missval);
double arrayWeightedAvgMV(const size_t len, const double *array, const double *w, const double missval);
double arrayWeightedAvgMV(const size_t len, const std::vector<double> &array, const std::vector<double> &w, const double missval);
#endif // ARRAY_H
......@@ -55,8 +55,8 @@ vfldmean(const Field &field)
double
vfldmeanw(const Field &field)
{
return field.nmiss ? arrayWeightedMeanMV(field.gridsize, field.vec.data(), field.weightv.data(), field.missval)
: arrayWeightedMean(field.gridsize, field.vec.data(), field.weightv.data(), field.missval);
return field.nmiss ? arrayWeightedMeanMV(field.gridsize, field.vec, field.weightv, field.missval)
: arrayWeightedMean(field.gridsize, field.vec, field.weightv, field.missval);
}
double
......@@ -68,8 +68,8 @@ vfldavg(const Field &field)
double
vfldavgw(const Field &field)
{
return field.nmiss ? arrayWeightedAvgMV(field.gridsize, field.vec.data(), field.weightv.data(), field.missval)
: arrayWeightedMean(field.gridsize, field.vec.data(), field.weightv.data(), field.missval);
return field.nmiss ? arrayWeightedAvgMV(field.gridsize, field.vec, field.weightv, field.missval)
: arrayWeightedMean(field.gridsize, field.vec, field.weightv, field.missval);
}
static void
......
......@@ -167,30 +167,21 @@ mersum(const Field &field1, Field &field2)
void
mermeanw(const Field &field1, Field &field2)
{
size_t rnmiss = 0;
double rmean = 0;
const double missval = field1.missval;
const size_t nx = gridInqXsize(field1.grid);
const size_t ny = gridInqYsize(field1.grid);
std::vector<double> v(ny);
std::vector<double> w(ny);
std::vector<double> v(ny), w(ny);
size_t rnmiss = 0;
for (size_t i = 0; i < nx; ++i)
{
for (size_t j = 0; j < ny; j++) v[j] = field1.vec[j * nx + i];
for (size_t j = 0; j < ny; j++) w[j] = field1.weightv[j * nx + i];
if (field1.nmiss)
{
rmean = arrayWeightedMeanMV(ny, &v[0], &w[0], field1.missval);
}
else
{
rmean = arrayWeightedMean(ny, &v[0], &w[0], field1.missval);
}
const double rmean = field1.nmiss ? arrayWeightedMeanMV(ny, v, w, missval) : arrayWeightedMean(ny, v, w, missval);
if (DBL_IS_EQUAL(rmean, field1.missval)) rnmiss++;
if (DBL_IS_EQUAL(rmean, missval)) rnmiss++;
field2.vec[i] = rmean;
}
......@@ -201,30 +192,21 @@ mermeanw(const Field &field1, Field &field2)
void
meravgw(const Field &field1, Field &field2)
{
size_t rnmiss = 0;
double ravg = 0;
const double missval = field1.missval;
const size_t nx = gridInqXsize(field1.grid);
const size_t ny = gridInqYsize(field1.grid);
std::vector<double> v(ny);
std::vector<double> w(ny);
std::vector<double> v(ny), w(ny);
size_t rnmiss = 0;
for (size_t i = 0; i < nx; ++i)
{
for (size_t j = 0; j < ny; j++) v[j] = field1.vec[j * nx + i];
for (size_t j = 0; j < ny; j++) w[j] = field1.weightv[j * nx + i];
if (field1.nmiss)
{
ravg = arrayWeightedAvgMV(ny, &v[0], &w[0], field1.missval);
}
else
{
ravg = arrayWeightedMean(ny, &v[0], &w[0], field1.missval);
}
const double ravg = field1.nmiss ? arrayWeightedAvgMV(ny, v, w, missval) : arrayWeightedMean(ny, v, w, missval);
if (DBL_IS_EQUAL(ravg, field1.missval)) rnmiss++;
if (DBL_IS_EQUAL(ravg, missval)) rnmiss++;
field2.vec[i] = ravg;
}
......
Markdown is supported
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