UFO
ObsErrorModelRamp.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2019 UCAR
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  */
7 
9 
10 #include <algorithm>
11 #include <cmath>
12 #include <set>
13 #include <string>
14 #include <vector>
15 
16 #include "ioda/ObsDataVector.h"
17 #include "oops/util/IntSetParser.h"
18 #include "oops/util/missingValues.h"
19 #include "ufo/filters/Variable.h"
20 #include "ufo/utils/Constants.h"
21 
22 namespace ufo {
23 
24 static ObsFunctionMaker<ObsErrorModelRamp>
25  makerRamp_("ObsErrorModelRamp");
26 
27 // -----------------------------------------------------------------------------
28 
29 ObsErrorModelRamp::ObsErrorModelRamp(const eckit::LocalConfiguration config)
30  : invars_() {
31  // Initialize options
32  options_.deserialize(config);
33 
34  // Initialize y-variable
35  eckit::LocalConfiguration yconf;
36  yconf.set("name", classname());
37  if (options_.chlist.value() != boost::none) {
38  yconf.set("channels", options_.chlist.value().get());
39  }
40  yconf.set("options", config);
41  Variable yvar(yconf);
42 
43  // Initialize x-variable
44  const Variable &xvar = options_.xvar.value();
45  ASSERT(xvar.size() == 1 ||
46  xvar.size() == yvar.size());
47  invars_ += xvar;
48 
49  // Get piece-wise parameters from options
50  const std::vector<float> &x0 = options_.x0.value();
51  const std::vector<float> &x1 = options_.x1.value();
52  const std::vector<float> &err0 = options_.err0.value();
53  const std::vector<float> &err1 = options_.err1.value();
54 
55  // Check parameter sizes/values
56  ASSERT(yvar.size() == x0.size());
57  ASSERT(yvar.size() == x1.size());
58  ASSERT(yvar.size() == err0.size());
59  ASSERT(yvar.size() == err1.size());
60  for (size_t i = 0; i < yvar.size(); ++i) {
61  ASSERT(x1[i] >= x0[i]);
62  ASSERT(err0[i] > 0.0);
63  ASSERT(err1[i] > 0.0);
64  }
65 }
66 
67 // -----------------------------------------------------------------------------
68 
70 
71 // -----------------------------------------------------------------------------
72 
74  ioda::ObsDataVector<float> & out) const {
75  const float missing = util::missingValue(missing);
76 
77  // Get piece-wise parameters from options
78  const std::vector<float> &x0 = options_.x0.value();
79  const std::vector<float> &x1 = options_.x1.value();
80  const std::vector<float> &err0 = options_.err0.value();
81  const std::vector<float> &err1 = options_.err1.value();
82 
83  // Check out size
84  ASSERT(out.nvars() == x0.size());
85 
86  // Compute x values
87  const Variable &xvar = options_.xvar.value();
89  in.get(xvar, xvals);
90 
91  // Optional save of the xfunc values
92  if (options_.save) xvals.save("ObsFunction");
93 
94  float slope;
95 
96  // Loop over selected variables
97  for (size_t jvar = 0; jvar < out.nvars(); ++jvar) {
98  size_t ivar = std::min(jvar, xvar.size() - 1);
99 
100  // Calculate slope of ramp for this variable
101  if (x1[jvar] > x0[jvar]) {
102  slope = (err1[jvar] - err0[jvar]) / (x1[jvar] - x0[jvar]);
103  } else {
104  slope = missing;
105  }
106 
107  // Calculate piece-wise function value across locations
108  for (size_t iloc = 0; iloc < in.nlocs(); ++iloc) {
109  out[jvar][iloc] = missing;
110  if (xvals[ivar][iloc] != missing) {
111  if (xvals[ivar][iloc] <= x0[jvar]) {
112  out[jvar][iloc] = err0[jvar];
113  } else if (xvals[ivar][iloc] < x1[jvar] && slope != missing) {
114  out[jvar][iloc] = err0[jvar] + slope * (xvals[ivar][iloc] - x0[jvar]);
115  } else {
116  out[jvar][iloc] = err1[jvar];
117  }
118  }
119  }
120  }
121 }
122 
123 // -----------------------------------------------------------------------------
124 
126  return invars_;
127 }
128 
129 // -----------------------------------------------------------------------------
130 
131 } // namespace ufo
ufo::ObsErrorModelRampParameters::save
oops::Parameter< bool > save
whether to save xvar values to the ObsSpace
Definition: ObsErrorModelRamp.h:51
ufo::ObsErrorModelRamp::compute
void compute(const ObsFilterData &, ioda::ObsDataVector< float > &) const
compute the result of the function
Definition: ObsErrorModelRamp.cc:73
ufo::ObsErrorModelRamp::options_
ObsErrorModelRampParameters options_
Definition: ObsErrorModelRamp.h:169
ufo::ObsErrorModelRamp::classname
static const std::string classname()
Definition: ObsErrorModelRamp.h:159
ufo::ObsFilterData::nlocs
size_t nlocs() const
Returns number of locations.
Definition: ObsFilterData.cc:66
ufo::Variables
Definition: src/ufo/filters/Variables.h:24
ufo::ObsFilterData::obsspace
ioda::ObsSpace & obsspace() const
Returns reference to ObsSpace associated with ObsFilterData.
Definition: src/ufo/filters/ObsFilterData.h:84
ufo::ObsErrorModelRampParameters::err1
oops::RequiredParameter< std::vector< float > > err1
y-coordinate of the upper ramp inflection point
Definition: ObsErrorModelRamp.h:49
ufo::Variable::size
size_t size() const
Definition: Variable.cc:79
ufo::Variable::toOopsVariables
oops::Variables toOopsVariables() const
Definition: Variable.cc:129
ufo::ObsErrorModelRampParameters::chlist
oops::OptionalParameter< std::string > chlist
Definition: ObsErrorModelRamp.h:41
ufo
Definition: RunCRTM.h:27
ufo::ObsErrorModelRamp::ObsErrorModelRamp
ObsErrorModelRamp(const eckit::LocalConfiguration)
Definition: ObsErrorModelRamp.cc:29
ufo::ObsErrorModelRamp::~ObsErrorModelRamp
~ObsErrorModelRamp()
Definition: ObsErrorModelRamp.cc:69
ufo::ObsErrorModelRampParameters::xvar
oops::RequiredParameter< Variable > xvar
x variable of the piece-wise function
Definition: ObsErrorModelRamp.h:36
ufo::ObsErrorModelRampParameters::x1
oops::RequiredParameter< std::vector< float > > x1
x-coordinate of the upper ramp inflection point
Definition: ObsErrorModelRamp.h:45
ufo::QCflags::missing
constexpr int missing
Definition: QCflags.h:15
ufo::ObsErrorModelRamp::invars_
ufo::Variables invars_
Definition: ObsErrorModelRamp.h:168
ufo::ObsErrorModelRampParameters::x0
oops::RequiredParameter< std::vector< float > > x0
x-coordinate of the lower ramp inflection point
Definition: ObsErrorModelRamp.h:43
ioda::ObsDataVector
Definition: BackgroundCheck.h:26
ufo::ObsErrorModelRampParameters::err0
oops::RequiredParameter< std::vector< float > > err0
y-coordinate of the lower ramp inflection point
Definition: ObsErrorModelRamp.h:47
ufo::makerRamp_
static ObsFunctionMaker< ObsErrorModelRamp > makerRamp_("ObsErrorModelRamp")
ufo::ObsErrorModelRamp::requiredVariables
const ufo::Variables & requiredVariables() const
geovals required to compute the function
Definition: ObsErrorModelRamp.cc:125
Constants.h
ufo::ObsFilterData::get
void get(const Variable &, std::vector< float > &) const
Gets requested data from ObsFilterData.
Definition: ObsFilterData.cc:130
ufo::Variable
Definition: Variable.h:23
ObsErrorModelRamp.h
ufo::ObsFilterData
ObsFilterData provides access to all data related to an ObsFilter.
Definition: src/ufo/filters/ObsFilterData.h:40
Variable.h