UFO
DrawValueFromFile.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_DRAWVALUEFROMFILE_H_
9 #define UFO_FILTERS_OBSFUNCTIONS_DRAWVALUEFROMFILE_H_
10 
11 #include <algorithm> // transform
12 #include <list>
13 #include <set>
14 #include <string>
15 #include <unordered_map>
16 #include <utility> // pair
17 #include <vector>
18 
19 #include "oops/util/parameters/OptionalParameter.h"
20 #include "oops/util/parameters/Parameter.h"
21 #include "oops/util/parameters/Parameters.h"
22 #include "oops/util/parameters/RequiredParameter.h"
24 #include "ufo/filters/Variable.h"
25 #include "ufo/filters/Variables.h"
28 
29 namespace eckit {
30  class Configuration;
31 }
32 
33 namespace ufo {
34 
35 
38  static constexpr char enumTypeName[] = "InterpMethod";
39  static constexpr util::NamedEnumerator<InterpMethod> namedValues[] = {
40  { InterpMethod::EXACT, "exact" },
41  { InterpMethod::NEAREST, "nearest" },
42  { InterpMethod::LEAST_UPPER_BOUND, "least upper bound" },
43  { InterpMethod::GREATEST_LOWER_BOUND, "greatest lower bound" },
44  { InterpMethod::LINEAR, "linear" },
45  { InterpMethod::BILINEAR, "bilinear" }
46  };
47 };
48 
49 } // namespace ufo
50 
51 
52 namespace oops {
53 
54 template <>
55 struct ParameterTraits<ufo::InterpMethod> :
56  public EnumParameterTraits<ufo::InterpMethodParameterTraitsHelper>
57 {};
58 
59 } // namespace oops
60 
61 
62 namespace ufo {
63 
64 /// \brief How to identify the relevant elements of the interpolated array along a dimension
65 /// indexed by a particular variable.
66 class InterpolationParameters : public oops::Parameters {
67  OOPS_CONCRETE_PARAMETERS(InterpolationParameters, Parameters)
68 
69  public:
70  /// Name of the indexing variable (e.g. `latitude@MetaData`).
71  oops::RequiredParameter<std::string> name{"name", this};
72 
73  /// Method used to map the value of a variable to a range of slices of the interpolated array
74  /// along the dimension indexed by that variable.
75  ///
76  /// \see InterpMethod for the list of supported methods.
77  oops::RequiredParameter<InterpMethod> method{"method", this};
78 };
79 
80 
81 /// \brief Options controlling the DrawValueFromFile ObsFunction (excluding the `group` option).
82 class DrawValueFromFileParametersWithoutGroup : public oops::Parameters {
83  OOPS_CONCRETE_PARAMETERS(DrawValueFromFileParametersWithoutGroup, Parameters)
84 
85  public:
86  /// Path to the file containing the data to interpolate.
87  oops::RequiredParameter<std::string> fpath{"file", this};
88 
89  /// List of interpolation variables and associated methods.
90  /// Note that channel numbers is handled implicitly by the "channels" (see below).
91  oops::RequiredParameter<std::vector<InterpolationParameters>> interpolation{"interpolation",
92  this};
93  /// List of channel numbers (then deriving an observation error per channel)
94  /// If this option is provided, the channel number is implicitly prepended to the list of
95  /// interpolation variables and matched exactly.
96  oops::OptionalParameter<std::set<int>> chlist{"channels", this};
97 };
98 
99 
100 /// \brief Options controlling the DrawValueFromFile ObsFunction
102  OOPS_CONCRETE_PARAMETERS(DrawValueFromFileParameters,
104 
105  public:
106  /// The file should contain exactly one variable from this group. This is the variable that
107  /// will be interpolated.
108  oops::RequiredParameter<std::string> group{"group", this};
109 };
110 
111 
112 /// \brief Produce values by interpolating an array loaded from a file, indexed by
113 /// coordinates whose names correspond to ObsSpace variables.
114 ///
115 /// \tparam T
116 /// Type of values produced by this ObsFunction. Must be `float`, `int` or `std::string`.
117 ///
118 /// \details See DataExtractor for details on the format of this file.
119 ///
120 /// ### example configurations: ###
121 ///
122 /// \code{.yaml}
123 /// - filter: Variable Assignment
124 /// assignments:
125 /// - name: interpolated_value@DerivedObsValue
126 /// function:
127 /// name: DrawValueFromFile@ObsFunction
128 /// channels: 1-3
129 /// options:
130 /// file: <filepath>
131 /// channels: 1-3
132 /// group: DerivedObsValue
133 /// interpolation:
134 /// - name: satellite_id@MetaData
135 /// method: exact
136 /// - name: processing_center@MetaData
137 /// method: exact
138 /// - name: air_pressure@MetaData
139 /// method: linear
140 /// \endcode
141 ///
142 /// Note that channel number extraction is implicit, using the channels selected and performed as
143 /// an exact match before any user defined interpolation takes place.
144 template <typename T>
146  public:
147  explicit DrawValueFromFile(const eckit::LocalConfiguration &);
148 
149  void compute(const ObsFilterData &,
150  ioda::ObsDataVector<T> &) const;
151  const ufo::Variables & requiredVariables() const;
152 
153  private:
155  std::unordered_map<std::string, InterpMethod> interpMethod_;
156  std::string fpath_;
158  std::vector<int> channels_;
159 };
160 
161 } // namespace ufo
162 
163 #endif // UFO_FILTERS_OBSFUNCTIONS_DRAWVALUEFROMFILE_H_
Produce values by interpolating an array loaded from a file, indexed by coordinates whose names corre...
const ufo::Variables & requiredVariables() const
geovals required to compute the function
std::unordered_map< std::string, InterpMethod > interpMethod_
void compute(const ObsFilterData &, ioda::ObsDataVector< T > &) const
compute the result of the function
std::vector< int > channels_
DrawValueFromFile(const eckit::LocalConfiguration &)
DrawValueFromFileParameters options_
Options controlling the DrawValueFromFile ObsFunction.
Options controlling the DrawValueFromFile ObsFunction (excluding the group option).
oops::RequiredParameter< std::vector< InterpolationParameters > > interpolation
oops::RequiredParameter< std::string > fpath
Path to the file containing the data to interpolate.
oops::OptionalParameter< std::set< int > > chlist
How to identify the relevant elements of the interpolated array along a dimension indexed by a partic...
oops::RequiredParameter< InterpMethod > method
oops::RequiredParameter< std::string > name
Name of the indexing variable (e.g. latitude@MetaData).
ObsFilterData provides access to all data related to an ObsFilter.
Forward declarations.
Definition: ObsAodExt.h:21
Definition: RunCRTM.h:27
InterpMethod
Method used by the DataExtractor to map the value of an ObsSpace variable to a range of slices of the...
@ LEAST_UPPER_BOUND
Select slices corresponding to the least value of the indexing coordinate greater than or equal to th...
@ GREATEST_LOWER_BOUND
Select slices corresponding to the greatest value of the indexing coordinate less than or equal to th...
@ LINEAR
Perform a piecewise linear interpolation along the dimension indexed by the ObsSpace variable.
@ NEAREST
Select slices where the indexing coordinate is closest to the value of the corresponding ObsSpace var...
@ BILINEAR
Perform a bilinear interpolation along two dimensions indexed by the ObsSpace variables.
@ EXACT
Select slices where the indexing coordinate matches exactly the value of the corresponding ObsSpace v...
static constexpr util::NamedEnumerator< InterpMethod > namedValues[]