You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
1.7 KiB

#pragma once
2 years ago
#include "comdefs.h"
#include <vector>
2 years ago
using michlib::real;
class BaseParameters
{
public:
virtual ~BaseParameters();
};
2 years ago
class BaseData
{
protected:
static constexpr real fillval = 1.0e10;
std::vector<real> data;
std::vector<real> lons, lats;
2 years ago
BaseData(size_t n): data(n), lons(n), lats(n) {}
2 years ago
template<class Lon, class Lat> BaseData(size_t n, Lon genlon, Lat genlat): data(n), lons(n), lats(n)
{
for(size_t i = 0; i < n; i++)
{
lons[i] = genlon(i);
lats[i] = genlat(i);
}
};
2 years ago
public:
BaseData() = default;
2 years ago
const real& V(size_t i) const { return data[i]; }
real& V(size_t i) { return data[i]; }
const real& operator()(size_t i) const { return data[i]; }
real& operator()(size_t i) { return data[i]; }
real Lon(size_t i) const { return lons[i]; }
real Lat(size_t i) const { return lats[i]; }
2 years ago
size_t N() const { return data.size(); }
static real Fillval() { return fillval; }
explicit operator bool() const { return N() != 0; }
2 years ago
};
template<class Data> class Averager: public Data
{
std::vector<size_t> count;
public:
void Add(const Data& d)
{
if(!d) return;
if(!*this)
{
*static_cast<Data*>(this) = d;
count.resize(Data::N());
for(size_t i = 0; i < Data::N(); i++) count[i] = (Data::V(i) == Data::Fillval()) ? 0 : 1;
return;
}
if(Data::N() != d.N()) return;
for(size_t i = 0; i < Data::N(); i++)
if(d(i) != Data::Fillval())
{
if(Data::V(i) == Data::Fillval())
{
Data::V(i) = d(i);
count[i] = 1;
}
else
{
Data::V(i) += d(i);
count[i]++;
}
}
}
Averager& Div()
{
if(!*this) return *this;
for(size_t i = 0; i < Data::N(); i++)
if(count[i] != 0) Data::V(i) /= count[i];
return *this;
}
};