UFO
ParameterTraitsVariable.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2019 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_UTILS_PARAMETERS_PARAMETERTRAITSVARIABLE_H_
9 #define UFO_UTILS_PARAMETERS_PARAMETERTRAITSVARIABLE_H_
10 
11 #include <string>
12 #include <vector>
13 
14 #include "eckit/exception/Exceptions.h"
15 #include "oops/util/CompositePath.h"
16 #include "oops/util/parameters/ParameterTraits.h"
17 #include "oops/util/stringFunctions.h"
18 #include "ufo/filters/Variable.h"
19 
20 /// \file ParameterTraitsVariable.h
21 /// This file needs to be included before any uses of Parameter<ufo::Variable> or
22 /// OptionalParameter<ufo::Variable>.
23 
24 namespace oops {
25 
26 template <>
27 struct ParameterTraits<ufo::Variable> {
28  static boost::optional<ufo::Variable> get(util::CompositePath &path,
29  const eckit::Configuration &config,
30  const std::string& name) {
31  if (config.has(name)) {
32  eckit::LocalConfiguration varConf(config, name);
33  if (!varConf.has("name")) {
34  // TODO(wsmigaj): shouldn't ufo::Variable itself throw an exception if
35  // the 'name' property is not specified?
36  throw eckit::BadParameter(path.path() + ": No variable name specified", Here());
37  }
38  return ufo::Variable(varConf);
39  } else {
40  return boost::none;
41  }
42  }
43 
44  static void set(eckit::LocalConfiguration &config,
45  const std::string &name,
46  const ufo::Variable &value) {
47  eckit::LocalConfiguration subConfig;
48  subConfig.set("name", value.variable() + "@" + value.group());
49  const std::vector<int> &channels = value.channels();
50  if (!channels.empty()) {
51  const std::string channelsAsString = util::stringfunctions::join(
52  ",", channels.begin(), channels.end(), [](int n) { return std::to_string(n); });
53  subConfig.set("channels", channelsAsString);
54  }
55  if (!value.options().keys().empty())
56  subConfig.set("options", value.options());
57  config.set(name, subConfig);
58  }
59 
60  static ObjectJsonSchema jsonSchema(const std::string &name) {
61  ObjectJsonSchema nestedSchema({{"name", {{"type", "\"string\""}}},
62  {"options", {{"type", "\"object\""}}},
63  {"channels", {{"type", "[\"string\", \"integer\"]"}}}});
64  return ObjectJsonSchema({{name, nestedSchema.toPropertyJsonSchema()}});
65  }
66 };
67 
68 } // namespace oops
69 
70 #endif // UFO_UTILS_PARAMETERS_PARAMETERTRAITSVARIABLE_H_
oops
Definition: GaussianThinningParameters.h:42
ufo::Variable::channels
const std::vector< int > & channels() const
Definition: Variable.cc:123
oops::ParameterTraits< ufo::Variable >::get
static boost::optional< ufo::Variable > get(util::CompositePath &path, const eckit::Configuration &config, const std::string &name)
Definition: ParameterTraitsVariable.h:28
ufo::Variable::options
const eckit::LocalConfiguration & options() const
Definition: Variable.h:43
ufo
Definition: RunCRTM.h:27
oops::ParameterTraits< ufo::Variable >::jsonSchema
static ObjectJsonSchema jsonSchema(const std::string &name)
Definition: ParameterTraitsVariable.h:60
oops::ParameterTraits< ufo::Variable >::set
static void set(eckit::LocalConfiguration &config, const std::string &name, const ufo::Variable &value)
Definition: ParameterTraitsVariable.h:44
ufo::Variable::group
const std::string & group() const
Definition: Variable.cc:117
ufo::Variable::variable
const std::string & variable() const
Definition: Variable.cc:100
ufo::Variable
Definition: Variable.h:23
Variable.h