IODA Bundle
BoundingFilter.cpp
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2020 NOAA/NWS/NCEP/EMC
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 #include "BoundingFilter.h"
9 
10 #include <ostream>
11 
12 #include "eckit/exception/Exceptions.h"
13 
14 #include "../RowSlice.h"
15 
16 namespace Ingester
17 {
19  std::shared_ptr<float> lowerBound,
20  std::shared_ptr<float> upperBound) :
21  mnemonic_(mnemonic),
22  lowerBound_(lowerBound),
23  upperBound_(upperBound)
24  {
25  if (!upperBound && !lowerBound)
26  {
27  std::stringstream errStr;
28  errStr << "BoundingFilter must contain either upperBound, lowerBound or both.";
29  throw eckit::BadParameter(errStr.str());
30  }
31 
32  if (upperBound && lowerBound && (*upperBound < *lowerBound))
33  {
34  std::stringstream errStr;
35  errStr << "BoundingFilter upperBound must be greater or equal to lowerBound";
36  throw eckit::BadParameter(errStr.str());
37  }
38  }
39 
41  {
42  std::vector<size_t> validRows;
43  if (dataMap.find(mnemonic_) == dataMap.end())
44  {
45  std::ostringstream errStr;
46  errStr << "Unknown mnemonic " << mnemonic_ << " found in bounding filter.";
47  throw eckit::BadParameter(errStr.str());
48  }
49 
50  const auto& array = dataMap.at(mnemonic_);
51  for (size_t rowIdx = 0; rowIdx < static_cast<size_t>(array.rows()); rowIdx++)
52  {
53  if (lowerBound_ && upperBound_)
54  {
55  if ((array.row(rowIdx) >= *lowerBound_).all() &&
56  (array.row(rowIdx) <= *upperBound_).all())
57  {
58  validRows.push_back(rowIdx);
59  }
60  }
61  else
62  {
63  if ((lowerBound_ && (array.row(rowIdx) >= *lowerBound_).all()) ||
64  (upperBound_ && (array.row(rowIdx) <= *upperBound_).all()))
65  {
66  validRows.push_back(rowIdx);
67  }
68  }
69  }
70 
71  if (validRows.size() != static_cast<size_t>(array.rows()))
72  {
73  for (const auto& dataPair : dataMap)
74  {
75  dataMap[dataPair.first] = rowSlice(dataPair.second, validRows);
76  }
77  }
78  }
79 } // namespace Ingester
EigenType rowSlice(const EigenType &arr, const EigenIdxType &idxVec)
Collection of template methods that are used to slice array and vector data.
Definition: RowSlice.h:19
const std::shared_ptr< float > lowerBound_
BoundingFilter(const std::string &mnemonic, std::shared_ptr< float > lowerBound, std::shared_ptr< float > upperBound)
Constructor.
const std::shared_ptr< float > upperBound_
const std::string mnemonic_
void apply(BufrDataMap &dataMap) final
Apply the filter to the data.
IngesterArrayMap BufrDataMap
Definition: BufrTypes.h:21