|
11 | 11 | #include "multio/action/scale/Scale.h"
|
12 | 12 |
|
13 | 13 | #include "eckit/config/LocalConfiguration.h"
|
14 |
| -#include "eckit/exception/Exceptions.h" |
15 | 14 |
|
16 | 15 | #include "multio/LibMultio.h"
|
17 | 16 | #include "multio/datamod/ContainerInterop.h"
|
|
21 | 20 |
|
22 | 21 | namespace multio::action::scale {
|
23 | 22 |
|
24 |
| -const Mappings getMappings(const std::string& preset) { |
25 |
| - if (preset != "local-to-wmo" && preset != "wmo-to-local") { |
26 |
| - throw eckit::UserError("Preset " + preset + " does not exist!", Here()); |
27 |
| - } |
| 23 | +ScaleConfig parseConfig(const ComponentConfiguration& compConf) { |
| 24 | + dm::ParseOptions opts; |
| 25 | + opts.allowAdditionalKeys = false; |
| 26 | + |
| 27 | + // TODO(pgeier) Fix after refactoring action - need to remove keys "type" and "next" |
| 28 | + auto conf = compConf.parsedConfig(); |
| 29 | + conf.remove("type"); |
| 30 | + conf.remove("next"); |
28 | 31 |
|
| 32 | + return dm::readRecordByValue<ScaleConfig>(conf, opts); |
| 33 | +} |
| 34 | + |
| 35 | +const std::vector<ScaleMappingConfig> getPresetMappings(const Preset& preset) { |
29 | 36 | // Load the mapping file
|
30 | 37 | eckit::LocalConfiguration mappingConf{eckit::YAMLConfiguration{eckit::PathName{
|
31 | 38 | multio::LibMultio::instance().libraryHome() + "/share/multio/mappings/local-to-wmo.yaml"
|
32 | 39 | }}};
|
33 | 40 |
|
34 |
| - // Read the mappings and put them into the map |
35 |
| - // We use the same mapping file for local-to-wmo and wmo-to-local, the |
36 |
| - // second is just the reverse mapping of the first! |
37 |
| - Mappings mappings; |
38 |
| - for (auto& mapping : mappingConf.getSubConfigurations()) { |
39 |
| - const auto paramIn = mapping.getInt64("param-in"); |
40 |
| - const auto paramOut = mapping.getInt64("param-out"); |
41 |
| - const auto scaling = mapping.getDouble("scaling"); |
42 |
| - if (preset == "local-to-wmo") { |
43 |
| - mappings[paramIn] = {paramOut, scaling}; |
44 |
| - } |
45 |
| - else { |
46 |
| - mappings[paramOut] = {paramIn, 1.0 / scaling}; |
| 41 | + auto mappings = datamod::RecordMapper<std::vector<ScaleMappingConfig>>::parse(mappingConf.getSubConfigurations()); |
| 42 | + |
| 43 | + if (preset == Preset::WmoToLocal) { |
| 44 | + for (auto& mapping : mappings) { |
| 45 | + std::swap(mapping.paramIn, mapping.paramOut); |
| 46 | + mapping.scaling.set(1.0 / mapping.scaling.get()); |
47 | 47 | }
|
48 | 48 | }
|
| 49 | + |
49 | 50 | return mappings;
|
50 | 51 | }
|
51 | 52 |
|
52 |
| -Mappings getMappings(const eckit::LocalConfiguration& config) { |
| 53 | +Mappings getMappings(const ScaleConfig& config) { |
53 | 54 | Mappings mappings;
|
54 | 55 |
|
55 | 56 | // Read the preset mapping from a mappings file
|
56 |
| - if (config.has("preset-mappings")) { |
57 |
| - ASSERT(config.isString("preset-mappings")); |
58 |
| - mappings = getMappings(config.getString("preset-mappings")); |
| 57 | + if (config.presetMappings.isSet()) { |
| 58 | + for (auto& mapping : getPresetMappings(config.presetMappings.get())) { |
| 59 | + mappings[mapping.paramIn.get()] = {mapping.paramOut.get(), mapping.scaling.get()}; |
| 60 | + } |
59 | 61 | }
|
60 | 62 |
|
61 | 63 | // Read any user defined mappings from the action configuration
|
62 |
| - if (config.has("custom-mappings")) { |
63 |
| - ASSERT(config.isSubConfigurationList("custom-mappings")); |
64 |
| - for (auto& mapping : config.getSubConfigurations("custom-mappings")) { |
65 |
| - const auto paramIn = mapping.getInt64("param-in"); |
66 |
| - const auto paramOut = mapping.getInt64("param-out"); |
67 |
| - const auto scaling = mapping.getDouble("scaling"); |
68 |
| - ASSERT(mappings.find(paramIn) == mappings.end()); |
69 |
| - mappings[paramIn] = {paramOut, scaling}; |
| 64 | + if (config.customMappings.isSet()) { |
| 65 | + for (auto& mapping : config.customMappings.get()) { |
| 66 | + mappings[mapping.paramIn.get()] = {mapping.paramOut.get(), mapping.scaling.get()}; |
70 | 67 | }
|
71 | 68 | }
|
72 | 69 |
|
73 |
| - if (mappings.empty()) { |
74 |
| - throw eckit::UserError("No scale mapping was found, set 'preset' or 'mappings' in action configuration!", Here()); |
75 |
| - } |
76 | 70 | return mappings;
|
77 | 71 | }
|
78 | 72 |
|
79 | 73 | Scale::Scale(const ComponentConfiguration& compConf) :
|
80 |
| - ChainedAction(compConf), mappings_{getMappings(compConf.parsedConfig())} {} |
| 74 | + ChainedAction(compConf), mappings_{getMappings(parseConfig(compConf))} {} |
81 | 75 |
|
82 | 76 | void Scale::executeImpl(message::Message msg) {
|
83 | 77 | // Skip non-field messages
|
@@ -133,3 +127,21 @@ void Scale::print(std::ostream& os) const {
|
133 | 127 | static ActionBuilder<Scale> ScaleBuilder("scale");
|
134 | 128 |
|
135 | 129 | } // namespace multio::action::scale
|
| 130 | + |
| 131 | + |
| 132 | +namespace multio::datamod { |
| 133 | + |
| 134 | +action::scale::Preset ParseType<action::scale::Preset>::parse(const std::string& val) { |
| 135 | + if (val == "local-to-wmo") { |
| 136 | + return action::scale::Preset::LocalToWmo; |
| 137 | + } |
| 138 | + if (val == "wmo-to-local") { |
| 139 | + return action::scale::Preset::WmoToLocal; |
| 140 | + } |
| 141 | + throw DataModellingException( |
| 142 | + std::string("ParseType<PresetMappings>::parse Unknown value for PresetMappings: ") + val, |
| 143 | + Here() |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +} // namespace multio::datamod |
0 commit comments