Skip to content

Commit

Permalink
Adds testing of -c accumulate
Browse files Browse the repository at this point in the history
  • Loading branch information
tnipen committed Jun 23, 2015
1 parent 455ae48 commit 1a4e72f
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 15 deletions.
26 changes: 14 additions & 12 deletions src/Calibrator/Accumulate.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
#include "Accumulate.h"
#include "../Util.h"
#include "../File/File.h"
CalibratorAccumulate::CalibratorAccumulate(Variable::Type iVariable) :
CalibratorAccumulate::CalibratorAccumulate(Variable::Type iVariable, const Options& iOptions) :
Calibrator(),
mVariable(iVariable) {
mInputVariable(iVariable),
mOutputVariable(Variable::PrecipAcc) {
std::string outputVariable;
if(iOptions.getValue("outputVariable", outputVariable)) {
mOutputVariable = Variable::getType(outputVariable);
}
}
bool CalibratorAccumulate::calibrateCore(File& iFile) const {
int nLat = iFile.getNumLat();
int nLon = iFile.getNumLon();
int nEns = iFile.getNumEns();
int nTime = iFile.getNumTime();
if(!iFile.hasVariable(mInputVariable)) {
Util::error("File '" + iFile.getFilename() + "' does not have variable '" + Variable::getTypeName(mInputVariable) + "'-");
}

// Get all fields
std::vector<FieldPtr> fields(nTime);
std::vector<FieldPtr> fieldsAcc(nTime);
for(int t = 0; t < nTime; t++) {
fields[t] = iFile.getField(mVariable, t);
fields[t] = iFile.getField(mInputVariable, t);
fieldsAcc[t] = iFile.getEmptyField();
}

Expand All @@ -41,19 +49,13 @@ bool CalibratorAccumulate::calibrateCore(File& iFile) const {
}
}

Variable::Type variableAcc;
if(mVariable == Variable::Precip) {
variableAcc = Variable::PrecipAcc;
}
else {
Util::error("Cannot accumulate " + Variable::getTypeName(mVariable));
}
iFile.addField(fieldsAcc[t], variableAcc, t);
iFile.addField(fieldsAcc[t], mOutputVariable, t);
}
return true;
}
std::string CalibratorAccumulate::description() {
std::stringstream ss;
ss << Util::formatDescription("-c accumulate","") << std::endl;
ss << Util::formatDescription("-c accumulate","Accumlates a value over time. Used to accumulate precipitation. It is assumed that the raw value for time t is the precip on the interval [t-1,t]. After accumulation, the value for time t is then the sum over the interval [0,t]. Thus, the accumulated value for the first timestep will be missing and the raw value for time t=0 will never be used.") << std::endl;
ss << Util::formatDescription(" outputVariable=PrecipAcc","Which variable should the accumulate be written to?") << std::endl;
return ss.str();
}
5 changes: 3 additions & 2 deletions src/Calibrator/Accumulate.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
// Accumlates a certain variable
class CalibratorAccumulate : public Calibrator {
public:
CalibratorAccumulate(Variable::Type iVariable);
CalibratorAccumulate(Variable::Type iVariable, const Options& iOptions);
static std::string description();
std::string name() const {return "accumulate";};
private:
bool calibrateCore(File& iFile) const;
Variable::Type mVariable;
Variable::Type mInputVariable;
Variable::Type mOutputVariable;
};
#endif
2 changes: 1 addition & 1 deletion src/Calibrator/Calibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Calibrator* Calibrator::getScheme(std::string iName, const Options& iOptions) {
if(!iOptions.getValue("variable", variable)) {
Util::error("Calibrator 'accumulate' needs variable");
}
CalibratorAccumulate* c = new CalibratorAccumulate(Variable::getType(variable));
CalibratorAccumulate* c = new CalibratorAccumulate(Variable::getType(variable), iOptions);
return c;
}
else if(iName == "neighbourhood") {
Expand Down
69 changes: 69 additions & 0 deletions src/Testing/CalibratorAccumulate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "../File/Fake.h"
#include "../Util.h"
#include "../ParameterFile.h"
#include "../Calibrator/Accumulate.h"
#include <gtest/gtest.h>

namespace {
class TestCalibratorAccumlate : public ::testing::Test {
protected:
TestCalibratorAccumlate() {
}
virtual ~TestCalibratorAccumlate() {
}
virtual void SetUp() {
}
virtual void TearDown() {
}
};

TEST_F(TestCalibratorAccumlate, 1x1) {
Variable::Type var = Variable::T;
Variable::Type varAcc = Variable::T;
FileArome from("testing/files/1x1.nc");
CalibratorAccumulate cal = CalibratorAccumulate(var, Options("outputVariable=T"));
cal.calibrate(from);

EXPECT_FLOAT_EQ(0, (*from.getField(varAcc, 0))(0,0,0));
EXPECT_FLOAT_EQ(20, (*from.getField(varAcc, 1))(0,0,0));
EXPECT_FLOAT_EQ(35, (*from.getField(varAcc, 2))(0,0,0));
EXPECT_FLOAT_EQ(56, (*from.getField(varAcc, 3))(0,0,0));
EXPECT_FLOAT_EQ(70, (*from.getField(varAcc, 4))(0,0,0));
EXPECT_FLOAT_EQ(100, (*from.getField(varAcc, 5))(0,0,0));
EXPECT_FLOAT_EQ(121, (*from.getField(varAcc, 6))(0,0,0));
EXPECT_FLOAT_EQ(140, (*from.getField(varAcc, 7))(0,0,0));
EXPECT_FLOAT_EQ(Util::MV, (*from.getField(varAcc, 8))(0,0,0));
EXPECT_FLOAT_EQ(Util::MV, (*from.getField(varAcc, 9))(0,0,0));
}
TEST_F(TestCalibratorAccumlate, 10x10) {
Variable::Type var = Variable::Precip;
Variable::Type varAcc = Variable::PrecipAcc;
FileArome from("testing/files/10x10.nc");
CalibratorAccumulate cal = CalibratorAccumulate(var, Options(""));
cal.calibrate(from);

EXPECT_FLOAT_EQ(0, (*from.getField(varAcc, 0))(5,2,0));
EXPECT_FLOAT_EQ(0.539526, (*from.getField(varAcc, 1))(5,2,0));
EXPECT_FLOAT_EQ(0, (*from.getField(varAcc, 0))(5,9,0));
EXPECT_FLOAT_EQ(6.929162, (*from.getField(varAcc, 1))(5,9,0));
EXPECT_FLOAT_EQ(0, (*from.getField(varAcc, 0))(0,9,0));
EXPECT_FLOAT_EQ(5.442121, (*from.getField(varAcc, 1))(0,9,0));
}
TEST_F(TestCalibratorAccumlate, invalid) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
Util::setShowError(false);
FileArome from("testing/files/1x1.nc");
// File does not contain QNH so it should not be possible to accumulate
CalibratorAccumulate cal = CalibratorAccumulate(Variable::QNH, Options(""));
EXPECT_DEATH(cal.calibrate(from), ".*");
CalibratorAccumulate cal2 = CalibratorAccumulate(Variable::QNH, Options("outputVariable=T"));
EXPECT_DEATH(cal2.calibrate(from), ".*");
}
TEST_F(TestCalibratorAccumlate, description) {
CalibratorAccumulate::description();
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
19 changes: 19 additions & 0 deletions src/Testing/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ namespace {
File* f = File::getScheme("missingfilename", Options());
EXPECT_EQ(NULL, f);
}
TEST_F(FileTest, deaccumulate) {
// Create accumulation field
FileArome from("testing/files/1x1.nc");
Variable::Type var = Variable::Precip;

// Accumulated 0, 3, 4, 4, 5.5, 10, _,12,12,20
// Deaccumulated _, 3, 1, 0, 1.5, 4.5, _, _, 0, 8

EXPECT_FLOAT_EQ(Util::MV, (*from.getField(var, 0))(0,0,0));
EXPECT_FLOAT_EQ(3, (*from.getField(var, 1))(0,0,0));
EXPECT_FLOAT_EQ(1, (*from.getField(var, 2))(0,0,0));
EXPECT_FLOAT_EQ(0, (*from.getField(var, 3))(0,0,0));
EXPECT_FLOAT_EQ(1.5, (*from.getField(var, 4))(0,0,0));
EXPECT_FLOAT_EQ(4.5, (*from.getField(var, 5))(0,0,0));
EXPECT_FLOAT_EQ(Util::MV, (*from.getField(var, 6))(0,0,0));
EXPECT_FLOAT_EQ(Util::MV, (*from.getField(var, 7))(0,0,0));
EXPECT_FLOAT_EQ(0, (*from.getField(var, 8))(0,0,0));
EXPECT_FLOAT_EQ(8, (*from.getField(var, 9))(0,0,0));
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
Expand Down
Binary file added testing/files/1x1.nc
Binary file not shown.

0 comments on commit 1a4e72f

Please sign in to comment.