UFO
Conditional.h
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 #ifndef UFO_FILTERS_OBSFUNCTIONS_CONDITIONAL_H_
9 #define UFO_FILTERS_OBSFUNCTIONS_CONDITIONAL_H_
10 
11 #include <string>
12 #include <vector>
13 
14 #include "oops/util/parameters/OptionalParameter.h"
15 #include "oops/util/parameters/Parameter.h"
16 #include "oops/util/parameters/Parameters.h"
17 #include "oops/util/parameters/RequiredParameter.h"
18 
22 #include "ufo/filters/Variables.h"
23 
24 namespace ufo {
25 
26 /// Parameters for one where configuration from the cases section
27 /// of the yaml file.
28 ///
29 /// Example:
30 ///
31 /// - where:
32 /// - variable:
33 /// name: float_variable_2@MetaData
34 /// minvalue: 0
35 /// value: 0.5
36 ///
37 template <typename FunctionValue>
38 class LocalConditionalParameters : public oops::Parameters {
39  OOPS_CONCRETE_PARAMETERS(LocalConditionalParameters, Parameters)
40 
41  public:
42  /// Where clause needed for assignment. This is passed to create a ProcessWhere
43  /// object and requires the expected parameters for ufo::WhereParameters
44  oops::RequiredParameter<std::vector<WhereParameters>> where{"where", this};
45 
46  /// \brief Value to be assigned when this particular where clause is true.
47  oops::RequiredParameter<FunctionValue> value{"value", this};
48 };
49 
50 /// Parameters controlling the Conditional obs function.
51 template <typename FunctionValue>
52 class ConditionalParameters : public oops::Parameters {
53  OOPS_CONCRETE_PARAMETERS(ConditionalParameters, Parameters)
54 
55  public:
56  /// List of cases for assignment. See LocalConditionalParameters
57  /// for what each where clause requires.
58  oops::RequiredParameter<std::vector<LocalConditionalParameters<FunctionValue>>> cases{
59  "cases", this};
60 
61  /// Default value for the array to be assigned. Missing value is used
62  /// if this is not present in the yaml.
63  oops::OptionalParameter<FunctionValue> defaultvalue{"defaultvalue", this};
64 
65  /// When this flag is true a value is assigned for the first matching where case
66  /// for a location. This matches with the python case logic.
67  /// When this is false the last matching case will take precedence which
68  /// replicates a series of separate variable assignment filter calls.
69  oops::Parameter<bool> firstmatchingcase{"firstmatchingcase", true, this};
70 };
71 
72 /// \brief Creates an array with values for specified variables selected by a series of where
73 /// statements.
74 ///
75 /// The obs function has been designed primarily to work with the Variable assignment filter
76 /// to simplify the assignment of more complicated variables. Any functionality in the
77 /// processWhere class can be used with this obs function.
78 ///
79 /// This template is used to define four ObsFunctions, each producing values of a different type:
80 /// * `Conditional@ObsFunction` produces floats
81 /// * `Conditional@IntObsFunction` produces ints
82 /// * `Conditional@StringObsFunction` produces strings
83 /// * `Conditional@DateTimeObsFunction` produces datetimes.
84 ///
85 /// Example 1: Create a new floating-point variable `emissivity@ObsDerived` and assign values based
86 /// on the surface type.
87 ///
88 /// - filter: Variable Assignment
89 /// assignments:
90 /// - name: emissivity@ObsDerived
91 /// type: float
92 /// function:
93 /// name: Conditional@ObsFunction
94 /// options:
95 /// defaultvalue: 0.0 # default value - rttov to calculate.
96 /// cases:
97 /// - where:
98 /// - variable:
99 /// name: surface_type@MetaData
100 /// is_in: 1
101 /// # if necessary, further conditions could be specified in extra items
102 /// # in the 'where' list
103 /// value: 0.3
104 /// - where:
105 /// - variable:
106 /// name: surface_type@MetaData
107 /// is_in: 2
108 /// value: 0.5
109 ///
110 /// Example 2: Create a new string variable `surface_description@MetaData` and set it to `land`,
111 /// `sea` or `unknown` depending on the value of the `surface_type@MetaData` variable.
112 ///
113 /// - filter: Variable Assignment
114 /// assignments:
115 /// - name: surface_description@MetaData
116 /// type: string
117 /// function:
118 /// name: Conditional@StringObsFunction
119 /// options:
120 /// defaultvalue: unknown
121 /// cases:
122 /// - where:
123 /// - variable:
124 /// name: surface_type@MetaData
125 /// is_in: 1
126 /// value: land
127 /// - where:
128 /// - variable:
129 /// name: surface_type@MetaData
130 /// is_in: 2
131 /// value: sea
132 
133 ///
134 template <typename FunctionValue>
135 class Conditional : public ObsFunctionBase<FunctionValue> {
136  public:
137  explicit Conditional(const eckit::LocalConfiguration & = eckit::LocalConfiguration());
138  void compute(const ObsFilterData &,
140  const ufo::Variables & requiredVariables() const;
141  private:
144 };
145 
146 // -----------------------------------------------------------------------------
147 
148 } // namespace ufo
149 
150 #endif // UFO_FILTERS_OBSFUNCTIONS_CONDITIONAL_H_
Creates an array with values for specified variables selected by a series of where statements.
Definition: Conditional.h:135
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
Parameters controlling the Conditional obs function.
Definition: Conditional.h:52
oops::Parameter< bool > firstmatchingcase
Definition: Conditional.h:69
oops::OptionalParameter< FunctionValue > defaultvalue
Definition: Conditional.h:63
oops::RequiredParameter< std::vector< LocalConditionalParameters< FunctionValue > > > cases
Definition: Conditional.h:58
oops::RequiredParameter< FunctionValue > value
Value to be assigned when this particular where clause is true.
Definition: Conditional.h:47
oops::RequiredParameter< std::vector< WhereParameters > > where
Definition: Conditional.h:44
ObsFilterData provides access to all data related to an ObsFilter.
Definition: RunCRTM.h:27