UFO
Conditional.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2021 Met Office UK
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 
8 #include "ioda/ObsDataVector.h"
9 #include "oops/util/missingValues.h"
10 #include "oops/util/parameters/Parameter.h"
12 #include "ufo/filters/Variable.h"
13 
14 namespace ufo {
15 
20 
21 // -----------------------------------------------------------------------------
22 template <typename FunctionValue>
23 Conditional<FunctionValue>::Conditional(const eckit::LocalConfiguration & conf)
24  : invars_() {
25  // Initialize options
26  options_.validateAndDeserialize(conf);
27 
28  // Populate invars_
29  for (const LocalConditionalParameters<FunctionValue> &lcp : options_.cases.value())
30  invars_ += getAllWhereVariables(lcp.where);
31 }
32 
33 // -----------------------------------------------------------------------------
34 template <typename FunctionValue>
37  // Assign default value to array
38  const FunctionValue missing = util::missingValue(FunctionValue());
39  for (size_t ivar = 0; ivar < out.nvars(); ++ivar) {
40  std::fill(out[ivar].begin(), out[ivar].end(), options_.defaultvalue.value().value_or(missing));
41  } // ivar
42 
43  // Assign values based on the where clauses from the configuration.
44  // if firstmatchingcase is true, the first case that is true assigns the value.
45  // if firstmatchingcase is false, the last matching case will assign the value.
46  std::vector<bool> applied(out.nlocs(), false);
47  for (const LocalConditionalParameters<FunctionValue> &lcp : options_.cases.value()) {
48  std::vector<bool> apply = processWhere(lcp.where, in);
49  for (size_t iloc = 0; iloc < out.nlocs(); ++iloc) {
50  if (apply[iloc] && applied[iloc] == false) {
51  for (size_t ivar = 0; ivar < out.nvars(); ++ivar)
52  out[ivar][iloc] = lcp.value.value();
53  if (options_.firstmatchingcase.value()) applied[iloc] = true;
54  } // if apply
55  } // iloc
56  } // lcp
57 } // compute
58 
59 // -----------------------------------------------------------------------------
60 template <typename FunctionValue>
62  return invars_;
63 }
64 
65 // -----------------------------------------------------------------------------
66 
67 } // namespace ufo
ufo::Variables invars_
Definition: Conditional.h:142
Conditional(const eckit::LocalConfiguration &=eckit::LocalConfiguration())
Definition: Conditional.cc:23
void compute(const ObsFilterData &, ioda::ObsDataVector< FunctionValue > &) const
compute the result of the function
Definition: Conditional.cc:35
const ufo::Variables & requiredVariables() const
geovals required to compute the function
Definition: Conditional.cc:61
ConditionalParameters< FunctionValue > options_
Definition: Conditional.h:143
oops::RequiredParameter< FunctionValue > value
Value to be assigned when this particular where clause is true.
Definition: Conditional.h:47
ObsFilterData provides access to all data related to an ObsFilter.
constexpr int missing
Definition: QCflags.h:20
Definition: RunCRTM.h:27
static ObsFunctionMaker< Conditional< std::string > > stringMaker("Conditional")
static ObsFunctionMaker< Conditional< util::DateTime > > dateTimeMaker("Conditional")
static ObsFunctionMaker< Conditional< float > > floatMaker("Conditional")
ufo::Variables getAllWhereVariables(const std::vector< WhereParameters > &params)
Definition: processWhere.cc:29
std::vector< bool > processWhere(const std::vector< WhereParameters > &params, const ObsFilterData &filterdata)
static ObsFunctionMaker< Conditional< int > > intMaker("Conditional")