UFO
ObsFunctionLinearCombination.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2021 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 <set>
12 #include <vector>
13 
14 #include "ioda/ObsDataVector.h"
15 #include "oops/util/IntSetParser.h"
16 #include "oops/util/missingValues.h"
17 
18 namespace ufo {
19 
20 static ObsFunctionMaker<LinearCombination>
21  makerLinearCombination_("LinearCombination");
22 
23 // -----------------------------------------------------------------------------
24 
25 LinearCombination::LinearCombination(const eckit::LocalConfiguration & conf)
26  : invars_() {
27  // Check options
28  options_.validateAndDeserialize(conf);
29 
30  // Create variable and add to invars_
31  for (const Variable & var : options_.variables.value()) {
32  invars_ += var;
33  }
34 }
35 
36 // -----------------------------------------------------------------------------
37 
39 
40 // -----------------------------------------------------------------------------
41 
43  ioda::ObsDataVector<float> & out) const {
44  // dimension
45  const size_t nlocs = in.nlocs();
46 
47  // number of input variables
48  const size_t nv = invars_.size();
49 
50  // get coefs for linear combination
51  const std::vector<float> coefs = options_.coefs.value();
52 
53  // sanity checks / initialize
54  ASSERT(coefs.size() == nv);
55  out.zero();
56 
57  // compute linear combination of input variables
58  const float missing = util::missingValue(missing);
59  for (size_t ivar = 0; ivar < nv; ++ivar) {
61  in.get(invars_[ivar], varin);
62  ASSERT(varin.nvars() == out.nvars());
63  for (size_t iloc = 0; iloc < nlocs; ++iloc) {
64  for (size_t ichan = 0; ichan < out.nvars(); ++ichan) {
65  if ( varin[ichan][iloc] == missing || out[ichan][iloc] == missing ) {
66  out[ichan][iloc] = missing;
67  } else {
68  out[ichan][iloc] += coefs[ivar] * varin[ichan][iloc];
69  }
70  } // ichan
71  } // nlocs
72  } // nvars
73 }
74 
75 // -----------------------------------------------------------------------------
76 
78  return invars_;
79 }
80 
81 // -----------------------------------------------------------------------------
82 
83 } // namespace ufo
LinearCombinationParameters options_
const ufo::Variables & requiredVariables() const
geovals required to compute the function
LinearCombination(const eckit::LocalConfiguration &)
void compute(const ObsFilterData &, ioda::ObsDataVector< float > &) const
compute the result of the function
oops::RequiredParameter< std::vector< Variable > > variables
Input variables of the linear combination.
oops::RequiredParameter< std::vector< float > > coefs
coefficient associated with the above variables
ObsFilterData provides access to all data related to an ObsFilter.
size_t nlocs() const
Returns the number of locations in the associated ObsSpace.
ioda::ObsSpace & obsspace() const
Returns reference to ObsSpace associated with ObsFilterData.
void get(const Variable &varname, std::vector< float > &values) const
Fills a std::vector with values of the specified variable.
oops::Variables toOopsVariables() const
Definition: Variables.cc:156
size_t size() const
Return the number of constituent Variable objects (some of which may contain multiple channels).
Definition: Variables.cc:92
constexpr int missing
Definition: QCflags.h:20
integer function nlocs(this)
Return the number of observational locations in this Locations object.
Definition: RunCRTM.h:27
static ObsFunctionMaker< LinearCombination > makerLinearCombination_("LinearCombination")