Debian Codegraph @

src/atlas/interpolation/nonlinear/Missing.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/interpolation/nonlinear/Missing.h"
14
15 #include "eckit/types/FloatCompare.h"
16
17 #include "atlas/field/MissingValue.h"
18 #include "atlas/util/DataType.h"
19 #include "atlas/util/Metadata.h"
20
21
22 namespace atlas {
23 namespace interpolation {
24 namespace nonlinear {
25
26 // Factory builders
27 static NonLinearFactoryBuilder<MissingIfAllMissing> __nl1(MissingIfAllMissing::static_type());
28 static NonLinearFactoryBuilder<MissingIfAnyMissing> __nl2(MissingIfAnyMissing::static_type());
29 static NonLinearFactoryBuilder<MissingIfHeaviestMissing> __nl3(MissingIfHeaviestMissing::static_type());
30
31 // Deprecated factory entries with "-real32" and "-real64" suffix for backwards compatibility.
32 // static NonLinearFactoryBuilder<MissingIfAllMissing> __nl1_real32(MissingIfAllMissing::static_type()+"-real32");
33 // static NonLinearFactoryBuilder<MissingIfAnyMissing> __nl2_real32(MissingIfAnyMissing::static_type()+"-real32");
34 // static NonLinearFactoryBuilder<MissingIfHeaviestMissing> __nl3_real32(MissingIfHeaviestMissing::static_type()+"-real32");
35 // static NonLinearFactoryBuilder<MissingIfAllMissing> __nl1_real64(MissingIfAllMissing::static_type()+"-real64");
36 // static NonLinearFactoryBuilder<MissingIfAnyMissing> __nl2_real64(MissingIfAnyMissing::static_type()+"-real64");
37 // static NonLinearFactoryBuilder<MissingIfHeaviestMissing> __nl3_real64(MissingIfHeaviestMissing::static_type()+"-real64");
38
39 namespace {
40 struct force_link {
41 template <typename M>
42 void load_builder() {
43 NonLinearFactoryBuilder<M>("tmp");
44 }
45 force_link() {
46 load_builder<MissingIfAllMissing>();
47 load_builder<MissingIfAnyMissing>();
48 load_builder<MissingIfHeaviestMissing>();
49 }
50 };
51
52 std::string missing_value_type(const DataType& datatype, const field::MissingValue::Config& c) {
53 std::string result;
54 if (c.get("missing_value_type", result)) {
55 result += "-" + datatype.str();
56 }
57 return result;
58 }
59
60 } // namespace
61 void force_link_missing() {
62 static force_link static_linking;
63 }
64
65 bool Missing::applicable(const Field& f) const {
66 return field::MissingValue(f);
67 }
68
69 bool MissingIfAllMissing::execute(NonLinear::Matrix& W, const Field& field) const {
70 return execute(W, field.array(), field.metadata());
71 }
72
73 bool MissingIfAllMissing::execute(NonLinear::Matrix& W, const array::Array& array, const Config& config) const {
74 switch(array.datatype().kind()) {
75 case (DataType::kind<double>()): return executeT<double>(W, array, config);
76 case (DataType::kind<float>()): return executeT<float>(W, array, config);
77 case (DataType::kind<int>()): return executeT<int>(W, array, config);
78 case (DataType::kind<long>()): return executeT<long>(W, array, config);
79 case (DataType::kind<unsigned long>()): return executeT<unsigned long>(W, array, config);
80 default: ATLAS_NOTIMPLEMENTED;
81 }
82 }
83
84 template<typename T>
85 bool MissingIfAllMissing::executeT(NonLinear::Matrix& W, const array::Array& array, const Config& config) const {
86 field::MissingValue mv(missing_value_type(array.datatype(), config), config);
87
88 auto& missingValue = mv.ref();
89
90 ATLAS_ASSERT(array.rank() == 1);
91
92 // NOTE only for scalars (for now)
93 auto values = make_view_array_values<T, 1>(array);
94 ATLAS_ASSERT(idx_t(W.cols()) == values.shape(0));
95
96 auto data = const_cast<Scalar*>(W.data());
97 bool modif = false;
98 bool zeros = false;
99
100 Size i = 0;
101 Matrix::iterator it(W);
102 for (Size r = 0; r < W.rows(); ++r) {
103 const Matrix::iterator end = W.end(r);
104
105 // count missing values, accumulate weights (disregarding missing values)
106 size_t i_missing = i;
107 size_t N_missing = 0;
108 size_t N_entries = 0;
109 Scalar sum = 0.;
110
111 Matrix::iterator kt(it);
112 Size k = i;
113 for (; it != end; ++it, ++i, ++N_entries) {
114 const bool miss = missingValue(values[it.col()]);
115
116 if (miss) {
117 ++N_missing;
118 i_missing = i;
119 }
120 else {
121 sum += *it;
122 }
123 }
124
125 // weights redistribution: zero-weight all missing values, linear re-weighting for the others;
126 // the result is missing value if all values in row are missing
127 if (N_missing > 0) {
128 if (N_missing == N_entries || eckit::types::is_approximately_equal(sum, 0.)) {
129 for (Size j = k; j < k + N_entries; ++j) {
130 data[j] = j == i_missing ? 1. : 0.;
131 }
132 }
133 else {
134 const Scalar factor = 1. / sum;
135 for (Size j = k; j < k + N_entries; ++j, ++kt) {
136 if (missingValue(values[kt.col()])) {
137 data[j] = 0.;
138 zeros = true;
139 }
140 else {
141 data[j] *= factor;
142 }
143 }
144 }
145 modif = true;
146 }
147 }
148
149 if (zeros && missingValue.isnan()) {
150 W.prune(0.);
151 }
152
153 return modif;
154 }
155
156 bool MissingIfAnyMissing::execute(NonLinear::Matrix& W, const Field& field) const {
157 return execute(W, field.array(), field.metadata());
158 }
159
160 bool MissingIfAnyMissing::execute(NonLinear::Matrix& W, const array::Array& array, const Config& config) const {
161 switch(array.datatype().kind()) {
162 case (DataType::kind<double>()): return executeT<double>(W, array, config);
163 case (DataType::kind<float>()): return executeT<float>(W, array, config);
164 case (DataType::kind<int>()): return executeT<int>(W, array, config);
165 case (DataType::kind<long>()): return executeT<long>(W, array, config);
166 case (DataType::kind<unsigned long>()): return executeT<unsigned long>(W, array, config);
167 default: ATLAS_NOTIMPLEMENTED;
168 }
169 }
170
171
172 template<typename T>
173 bool MissingIfAnyMissing::executeT(NonLinear::Matrix& W, const array::Array& array, const Config& config) const {
174 field::MissingValue mv(missing_value_type(array.datatype(), config), config);
175
176 auto& missingValue = mv.ref();
177
178 // NOTE only for scalars (for now)
179 auto values = make_view_array_values<T, 1>(array);
180 ATLAS_ASSERT(idx_t(W.cols()) == values.size());
181
182 auto data = const_cast<Scalar*>(W.data());
183 bool modif = false;
184 bool zeros = false;
185
186 Size i = 0;
187 Matrix::iterator it(W);
188 for (Size r = 0; r < W.rows(); ++r) {
189 const Matrix::iterator end = W.end(r);
190
191 // count missing values, accumulate weights (disregarding missing values)
192 size_t i_missing = i;
193 size_t N_missing = 0;
194 size_t N_entries = 0;
195
196 Matrix::iterator kt(it);
197 Size k = i;
198 for (; it != end; ++it, ++i, ++N_entries) {
199 const bool miss = missingValue(values[it.col()]);
200
201 if (miss) {
202 ++N_missing;
203 i_missing = i;
204 }
205 }
206
207 // if any values in row are missing, force missing value
208 if (N_missing > 0) {
209 for (Size j = k; j < k + N_entries; ++j) {
210 if (j == i_missing) {
211 data[j] = 1.;
212 }
213 else {
214 data[j] = 0.;
215 zeros = true;
216 }
217 }
218 modif = true;
219 }
220 }
221
222 if (zeros && missingValue.isnan()) {
223 W.prune(0.);
224 }
225
226 return modif;
227 }
228
229 bool MissingIfHeaviestMissing::execute(NonLinear::Matrix& W, const Field& field) const {
230 return execute(W, field.array(), field.metadata());
231 }
232
233 bool MissingIfHeaviestMissing::execute(NonLinear::Matrix& W, const array::Array& array, const Config& config) const {
234 switch(array.datatype().kind()) {
235 case (DataType::kind<double>()): return executeT<double>(W, array, config);
236 case (DataType::kind<float>()): return executeT<float>(W, array, config);
237 case (DataType::kind<int>()): return executeT<int>(W, array, config);
238 case (DataType::kind<long>()): return executeT<long>(W, array, config);
239 case (DataType::kind<unsigned long>()): return executeT<unsigned long>(W, array, config);
240 default: ATLAS_NOTIMPLEMENTED;
241 }
242 }
243
244 template<typename T>
245 bool MissingIfHeaviestMissing::executeT(NonLinear::Matrix& W, const array::Array& array, const Config& config) const {
246 field::MissingValue mv(missing_value_type(array.datatype(), config), config);
247 auto& missingValue = mv.ref();
248
249 // NOTE only for scalars (for now)
250 auto values = make_view_array_values<T, 1>(array);
251 ATLAS_ASSERT(idx_t(W.cols()) == values.size());
252
253 auto data = const_cast<Scalar*>(W.data());
254 bool modif = false;
255 bool zeros = false;
256
257 Size i = 0;
258 Matrix::iterator it(W);
259 for (Size r = 0; r < W.rows(); ++r) {
260 const Matrix::iterator end = W.end(r);
261
262 // count missing values, accumulate weights (disregarding missing values) and find maximum weight in row
263 size_t i_missing = i;
264 size_t N_missing = 0;
265 size_t N_entries = 0;
266 Scalar sum = 0.;
267 Scalar heaviest = -1.;
268 bool heaviest_is_missing = false;
269
270 Matrix::iterator kt(it);
271 Size k = i;
272 for (; it != end; ++it, ++i, ++N_entries) {
273 const bool miss = missingValue(values[it.col()]);
274
275 if (miss) {
276 ++N_missing;
277 i_missing = i;
278 }
279 else {
280 sum += *it;
281 }
282
283 if (heaviest < data[i]) {
284 heaviest = data[i];
285 heaviest_is_missing = miss;
286 }
287 }
288
289 // weights redistribution: zero-weight all missing values, linear re-weighting for the others;
290 // if all values are missing, or the closest value is missing, force missing value
291 if (N_missing > 0) {
292 if (N_missing == N_entries || heaviest_is_missing || eckit::types::is_approximately_equal(sum, 0.)) {
293 for (Size j = k; j < k + N_entries; ++j) {
294 data[j] = j == i_missing ? 1. : 0.;
295 }
296 }
297 else {
298 const Scalar factor = 1. / sum;
299 for (Size j = k; j < k + N_entries; ++j, ++kt) {
300 if (missingValue(values[kt.col()])) {
301 data[j] = 0.;
302 zeros = true;
303 }
304 else {
305 data[j] *= factor;
306 }
307 }
308 }
309 modif = true;
310 }
311 }
312
313 if (zeros && missingValue.isnan()) {
314 W.prune(0.);
315 }
316
317 return modif;
318 }
319
320
321 } // namespace nonlinear
322 } // namespace interpolation
323 } // namespace atlas