Debian Codegraph @

src/atlas/field/detail/MissingValue.cc

raw
1 /*
2 * (C) Copyright 1996- ECMWF.
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 * In applying this licence, ECMWF does not waive the privileges and immunities
8 * granted to it by virtue of its status as an intergovernmental organisation nor
9 * does it submit to any jurisdiction.
10 */
11
12
13 #include "atlas/field/detail/MissingValue.h"
14
15 #include <cmath>
16 #include <type_traits>
17
18 #include "eckit/types/FloatCompare.h"
19
20 #include "atlas/array/DataType.h"
21 #include "atlas/field/Field.h"
22 #include "atlas/util/Metadata.h"
23
24
25 namespace atlas {
26 namespace field {
27 namespace detail {
28
29
30 namespace {
31 static const std::string type_key = "missing_value_type";
32 static const std::string value_key = "missing_value";
33 static const std::string epsilon_key = "missing_value_epsilon";
34
35
36 template <typename T>
37 T config_value(const MissingValue::Config& c) {
38 T value;
39 ATLAS_ASSERT(c.get(value_key, value));
40 return value;
41 }
42
43
44 template <typename T>
45 T config_epsilon(const MissingValue::Config& c) {
46 T value = 0.;
47 c.get(epsilon_key, value);
48 return value;
49 }
50 } // namespace
51
52
53 /**
54 * @brief Missing value if NaN
55 */
56 template <typename T>
57 struct MissingValueNaN : MissingValue {
58 MissingValueNaN(const Config&) { ATLAS_ASSERT(std::is_floating_point<T>::value); }
59 using MissingValue::operator();
60 bool operator()(const T& value) const override { return std::isnan(value); }
61 bool isnan() const override { return true; }
62 void metadata(Field& field) const override { field.metadata().set(type_key, static_type()); }
63 static std::string static_type() { return "nan"; }
64 };
65
66
67 /**
68 * @brief Missing value if comparing equally to pre-defined value
69 */
70 template <typename T>
71 struct MissingValueEquals : MissingValue {
72 MissingValueEquals(const Config& config): MissingValueEquals(config_value<T>(config)) {}
73
74 MissingValueEquals(T missingValue): missingValue_(missingValue), missingValue2_(missingValue_) {
75 ATLAS_ASSERT(missingValue_ == missingValue2_); // FIXME this succeeds
76 ATLAS_ASSERT(!std::isnan(missingValue2_));
77 }
78
79 using MissingValue::operator();
80 bool operator()(const T& value) const override {
81 // ATLAS_ASSERT(missingValue_ == missingValue2_); // FIXME this fails (copy ellision problem on POD!?)
82 return value == missingValue2_;
83 }
84
85 bool isnan() const override { return false; }
86
87 void metadata(Field& field) const override {
88 field.metadata().set(type_key, static_type());
89 field.metadata().set(value_key, missingValue2_);
90 }
91
92 static std::string static_type() { return "equals"; }
93
94 const T missingValue_;
95 const T missingValue2_;
96 };
97
98
99 /**
100 * @brief Missing value if comparing approximately to pre-defined value
101 */
102 template <typename T>
103 struct MissingValueApprox : MissingValue {
104 MissingValueApprox(const Config& config): MissingValueApprox(config_value<T>(config), config_epsilon<T>(config)) {}
105
106 MissingValueApprox(T missingValue, T epsilon): missingValue_(missingValue), epsilon_(epsilon) {
107 ATLAS_ASSERT(!std::isnan(missingValue_));
108 ATLAS_ASSERT(std::is_floating_point<T>::value);
109 ATLAS_ASSERT(epsilon_ >= 0.);
110 }
111
112 using MissingValue::operator();
113 bool operator()(const T& value) const override {
114 return eckit::types::is_approximately_equal(value, missingValue_, epsilon_);
115 }
116
117 bool isnan() const override { return false; }
118
119 void metadata(Field& field) const override {
120 field.metadata().set(type_key, static_type());
121 field.metadata().set(value_key, missingValue_);
122 field.metadata().set(epsilon_key, epsilon_);
123 }
124
125 static std::string static_type() { return "approximately-equals"; }
126
127 const T missingValue_;
128 const T epsilon_;
129 };
130
131
132 const MissingValue* MissingValueFactory::build(const std::string& builder, const Config& config) {
133 return has(builder) ? get(builder)->make(config) : nullptr;
134 }
135
136
137 #define B MissingValueFactoryBuilder
138 #define M1 MissingValueNaN
139 #define M2 MissingValueEquals
140 #define M3 MissingValueApprox
141 #define T array::DataType::str
142
143
144 static B<M1<double>> __mv1(M1<double>::static_type());
145 static B<M1<double>> __mv2(M1<double>::static_type() + "-" + T<double>());
146 static B<M1<float>> __mv3(M1<float>::static_type() + "-" + T<float>());
147
148 static B<M2<double>> __mv4(M2<double>::static_type());
149 static B<M2<double>> __mv5(M2<double>::static_type() + "-" + T<double>());
150 static B<M2<float>> __mv6(M2<float>::static_type() + "-" + T<float>());
151 static B<M2<int>> __mv7(M2<int>::static_type() + "-" + T<int>());
152 static B<M2<long>> __mv8(M2<long>::static_type() + "-" + T<long>());
153 static B<M2<unsigned long>> __mv9(M2<unsigned long>::static_type() + "-" + T<unsigned long>());
154
155 static B<M3<double>> __mv10(M3<double>::static_type());
156 static B<M3<double>> __mv11(M3<double>::static_type() + "-" + T<double>());
157 static B<M3<float>> __mv12(M3<float>::static_type() + "-" + T<float>());
158
159
160 #undef T
161 #undef M3
162 #undef M2
163 #undef M1
164 #undef B
165
166
167 } // namespace detail
168 } // namespace field
169 } // namespace atlas