FV3 Bundle
l95/test/lorenz95/ObsBias.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2009-2016 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #include <fstream>
12 #include <iostream>
13 
14 #include <boost/lexical_cast.hpp>
15 #include <boost/scoped_ptr.hpp>
16 #include <boost/test/unit_test.hpp>
17 
18 #include "./TestConfig.h"
19 #include "eckit/config/LocalConfiguration.h"
20 #include "lorenz95/ObsBias.h"
22 #include "test/TestFixture.h"
23 
24 namespace test {
25 
26 // -----------------------------------------------------------------------------
28  public:
30  biasconf_.reset(new eckit::LocalConfiguration(TestConfig::config(), "ObsBias"));
31  nobias_.reset(new eckit::LocalConfiguration());
32  covconf_.reset(new eckit::LocalConfiguration(TestConfig::config(), "Covariance"));
33  }
35  boost::scoped_ptr<const eckit::LocalConfiguration> biasconf_;
36  boost::scoped_ptr<const eckit::LocalConfiguration> nobias_;
37  boost::scoped_ptr<const eckit::LocalConfiguration> covconf_;
38 };
39 // -----------------------------------------------------------------------------
40 
41 // -----------------------------------------------------------------------------
42 BOOST_FIXTURE_TEST_SUITE(test_obsBias, ObsBiasTestFixture)
43 // -----------------------------------------------------------------------------
44  BOOST_AUTO_TEST_CASE(test_obsBias_constructor_bias) {
45  lorenz95::ObsBias ob(*biasconf_);
46  BOOST_CHECK_EQUAL(ob.value(), biasconf_->getDouble("bias"));
47  }
48 // -----------------------------------------------------------------------------
49  BOOST_AUTO_TEST_CASE(test_obsBias_constructor_no_bias) {
50  lorenz95::ObsBias ob(*nobias_);
51  BOOST_CHECK_EQUAL(ob.value(), 0.0);
52 }
53 // -----------------------------------------------------------------------------
54  BOOST_AUTO_TEST_CASE(test_obsBias_copy_constructor_config_copy) {
55  lorenz95::ObsBias ob1(*biasconf_);
56  lorenz95::ObsBias ob2(ob1, true);
57  BOOST_CHECK_EQUAL(ob2.value(), biasconf_->getDouble("bias"));
58  }
59 // -----------------------------------------------------------------------------
60  BOOST_AUTO_TEST_CASE(test_obsBias_copy_constructor_config_no_copy) {
61  lorenz95::ObsBias ob1(*biasconf_);
62  lorenz95::ObsBias ob2(ob1, false);
63 
64  // bias value is 0 because copy flag was set to false
65  BOOST_CHECK_EQUAL(ob2.value(), 0.0);
66  }
67 // -----------------------------------------------------------------------------
68  BOOST_AUTO_TEST_CASE(test_obsBias_copy_constructor_no_config_copy) {
69  lorenz95::ObsBias ob1(*nobias_);
70  lorenz95::ObsBias ob2(ob1, true);
71 
72  // bias value is 0 because an empty config was used
73  BOOST_CHECK_EQUAL(ob2.value(), 0.0);
74  }
75 // -----------------------------------------------------------------------------
76  BOOST_AUTO_TEST_CASE(test_obsBias_copy_constructor_no_config_no_copy) {
77  lorenz95::ObsBias ob1(*nobias_);
78  lorenz95::ObsBias ob2(ob1, true);
79 
80  // bias value is 0 because an empty config was used
81  BOOST_CHECK_EQUAL(ob2.value(), 0.0);
82  }
83 // -----------------------------------------------------------------------------
84  BOOST_AUTO_TEST_CASE(test_obsBias_destructor) {
85  // not yet implemented
86  }
87 // -----------------------------------------------------------------------------
88  BOOST_AUTO_TEST_CASE(test_obsBias_compound_assignment_add_active) {
89  lorenz95::ObsBias ob(*biasconf_);
90 
91  // construct an obsBiasCorrection object
92  lorenz95::ObsBiasCorrection obsBiasCorrection(*covconf_);
93  obsBiasCorrection.value() = 3.14;
94 
95  ob += obsBiasCorrection;
96 
97  BOOST_CHECK_EQUAL(ob.value(), biasconf_->getDouble("bias") + 3.14);
98  }
99 // -----------------------------------------------------------------------------
100  BOOST_AUTO_TEST_CASE(test_obsBias_compound_assignment_add_inactive) {
101  lorenz95::ObsBias ob(*nobias_);
102 
103  // construct an obsBiasCorrection object
104  lorenz95::ObsBiasCorrection obsBiasCorrection(*covconf_);
105  obsBiasCorrection.value() = 3.14;
106 
107  ob += obsBiasCorrection;
108 
109  // the bias value will be 0 because the ob had an empty config
110  BOOST_CHECK_EQUAL(ob.value(), 0.0);
111  }
112 // -----------------------------------------------------------------------------
113  BOOST_AUTO_TEST_CASE(test_obsBias_value) {
114  lorenz95::ObsBias ob(*biasconf_);
115  BOOST_CHECK_EQUAL(ob.value(), biasconf_->getDouble("bias"));
116  }
117 // -----------------------------------------------------------------------------
118  BOOST_AUTO_TEST_CASE(test_obsBias_read) {
119  // not yet implemented
120  }
121 // -----------------------------------------------------------------------------
122  BOOST_AUTO_TEST_CASE(test_obsBias_write) {
123  // not yet implemented
124  }
125 // -----------------------------------------------------------------------------
126  BOOST_AUTO_TEST_CASE(test_obsBias_stream_output) {
127  lorenz95::ObsBias ob(*biasconf_);
128 
129  // use the operator<< method to write the value to a file
130  std::filebuf fb;
131  std::string filename("ObsBiasTest.txt");
132  fb.open(filename.c_str(), std::ios::out);
133  std::ostream os(&fb);
134  os << ob;
135  fb.close();
136 
137  // then read the value that was written to the file
138  std::string inputString;
139  std::string inputBias;
140  double testBias = biasconf_->getDouble("bias");
141  double bias = 0.0;
142  int biasStartPos = 10; // length of "ObsBias = " is 10
143  std::ifstream inputFile(filename.c_str());
144  if (inputFile.is_open()) {
145  getline(inputFile, inputString);
146  getline(inputFile, inputString);
147 
148  inputBias = inputString.substr(biasStartPos);
149 
150  try {
151  bias = boost::lexical_cast<double>(inputBias);
152  }
153  catch(boost::bad_lexical_cast const&) {
154  BOOST_ERROR("operator<< incorrectly output a non-double");
155  }
156 
157  BOOST_CHECK_CLOSE(testBias, bias, 0.0001);
158  } else {
159  // if we can't open the file then we can't
160  // verify that the value was correctly written
161  BOOST_ERROR("operator<< functionality cannot be determined");
162  }
163  inputFile.close();
164  }
165 // -----------------------------------------------------------------------------
166 
167 BOOST_AUTO_TEST_SUITE_END()
168 } // namespace test
boost::scoped_ptr< const eckit::LocalConfiguration > covconf_
boost::scoped_ptr< const eckit::LocalConfiguration > nobias_
Class to handle observation bias parameters.
BOOST_AUTO_TEST_CASE(test_GomL95_constructor)
boost::scoped_ptr< const eckit::LocalConfiguration > biasconf_
static const eckit::Configuration & config()
Definition: TestConfig.h:30
const double & value() const