DP3
PreFlagger.h
Go to the documentation of this file.
1 // PreFlagger.h: DP3 step class to flag data on channel, baseline, or time
2 // Copyright (C) 2023 ASTRON (Netherlands Institute for Radio Astronomy)
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 
8 
9 #ifndef DP3_STEPS_PREFLAGGER_H_
10 #define DP3_STEPS_PREFLAGGER_H_
11 
12 #include <casacore/measures/Measures/MDirection.h>
13 
14 #include <xtensor/containers/xtensor.hpp>
15 
16 #include "base/DPBuffer.h"
17 #include "steps/Step.h"
18 
19 #include "base/BaselineSelection.h"
20 #include "base/FlagCounter.h"
21 #include "common/ParameterSet.h"
22 #include "common/Timer.h"
23 
24 namespace dp3::steps {
25 
27 
51 
52 class PreFlagger : public Step {
54  friend class TestPSet;
55 
56  public:
58 
61  PreFlagger(const common::ParameterSet&, const std::string& prefix);
62 
63  ~PreFlagger() override;
64 
65  common::Fields getRequiredFields() const override {
66  common::Fields fields = kFlagsField;
67  if (itsMode == Mode::kClearFlag || itsMode == Mode::kClearComp) {
68  fields |= kDataField | kWeightsField;
69  }
70  return fields |= itsPSet.getRequiredFields();
71  }
72 
73  common::Fields getProvidedFields() const override { return kFlagsField; }
74 
77  bool process(std::unique_ptr<base::DPBuffer> buffer) override;
78 
80  void finish() override;
81 
84  void updateInfo(const base::DPInfo&) override;
85 
87  void show(std::ostream&) const override;
88 
90  void showCounts(std::ostream&) const override;
91 
93  void showTimings(std::ostream&, double duration) const override;
94 
95  private:
98  class PSet {
100  friend class TestPSet;
101 
102  public:
105  enum Oper { OpParen = -1, OpOr = -2, OpAnd = -3, OpNot = -4 };
106 
108  typedef std::shared_ptr<PSet> ShPtr;
109 
111  PSet() {}
112 
114  PSet(const common::ParameterSet& parset, const std::string& prefix);
115 
117  common::Fields fields;
118  if (itsFlagOnAmpl || itsFlagOnPhase || itsFlagOnReal || itsFlagOnImag) {
119  fields |= kDataField;
120  }
121  if (itsFlagOnUV) {
122  fields |= kUvwField;
123  }
124  for (const std::shared_ptr<PSet>& sub_pset : itsPSets) {
125  fields |= sub_pset->getRequiredFields();
126  }
127  return fields;
128  }
129 
131  xt::xtensor<uint8_t, 2>* process(base::DPBuffer&, unsigned int timeSlot,
132  const xt::xtensor<bool, 1>& matchBL,
133  common::NSTimer& timer);
134 
137  void updateInfo(const base::DPInfo&);
138 
140  void show(std::ostream&, bool showName) const;
141 
142  private:
144  bool matchTime(double time, unsigned int timeSlot) const;
145 
147  bool matchRange(double v, const std::vector<double>& ranges) const;
148 
151  bool flagBL();
152 
155  bool flagUV(const base::DPBuffer::UvwType& uvw);
156 
159  bool flagAzEl(double time);
160 
163  void testAzEl(casacore::MDirection::Convert& converter, unsigned int blnr,
164  int ant, const std::vector<int>& ant1,
165  const std::vector<int>& ant2);
166 
168  void flagAmpl(const base::DPBuffer::DataType& data);
169 
171  void flagPhase(const base::DPBuffer::DataType& data);
172 
175  void flagReal(const base::DPBuffer::DataType& data);
176  void flagImag(const base::DPBuffer::DataType& data);
178 
180  void flagChannels();
181 
186  std::vector<double> fillTimes(const std::vector<std::string>& str,
187  bool asTime, bool canEndBeforeStart);
188 
192  double getSeconds(const std::string& str, bool asTime, bool usepm);
193 
195  void fillBLMatrix();
196 
198  void fillChannels(const base::DPInfo&);
199 
206  std::vector<float> fillValuePerCorr(const common::ParameterValue& value,
207  float defVal, bool& doFlag);
208 
211  xt::xtensor<int, 1> handleFreqRanges(const std::vector<double>& chanFreqs);
212 
215  void getValue(const std::string& str, double& value,
216  casacore::String& unit);
217 
219  double getFreqHz(double value, const casacore::String& unit);
220 
223  std::vector<std::string> exprToRpn(const std::string& expr);
224 
225  const base::DPInfo* itsInfo;
226  std::string itsName;
227  std::string itsStrExpr;
228  bool itsFlagOnTimeOnly;
229  bool itsFlagOnTime;
230  bool itsFlagOnUV;
231  bool itsFlagOnBL;
232  bool itsFlagOnAmpl;
233  bool itsFlagOnPhase;
234  bool itsFlagOnReal;
235  bool itsFlagOnImag;
236  bool itsFlagOnAzEl;
237  base::BaselineSelection itsSelBL;
238  double itsMinUV;
239  double itsMaxUV;
240  casacore::Matrix<bool> itsFlagBL;
241  std::vector<double> itsAzimuth;
242  std::vector<double> itsElevation;
243  std::vector<double> itsTimes;
244  std::vector<double> itsLST;
245  std::vector<double> itsATimes;
246  std::vector<double> itsRTimes;
247  std::vector<unsigned int> itsTimeSlot;
248  std::vector<float> itsAmplMin;
249  std::vector<float> itsAmplMax;
250  std::vector<float> itsPhaseMin;
251  std::vector<float> itsPhaseMax;
252  std::vector<float> itsRealMin;
253  std::vector<float> itsRealMax;
254  std::vector<float> itsImagMin;
255  std::vector<float> itsImagMax;
256  std::vector<unsigned int> itsChannels;
257  std::vector<std::string> itsStrChan;
258  std::vector<std::string> itsStrFreq;
259  std::vector<std::string> itsStrTime;
260  std::vector<std::string> itsStrLST;
261  std::vector<std::string>
262  itsStrATime;
263  std::vector<std::string>
264  itsStrRTime;
265  std::vector<std::string> itsStrAzim;
266  std::vector<std::string> itsStrElev;
267  std::vector<int> itsRpn;
268  std::vector<PSet::ShPtr> itsPSets;
269  xt::xtensor<bool, 1> itsChanFlags;
270  // Using 'uint8_t' instead of 'bool' in itsFlags works better with XSimd,
271  // since XSimd uses an integer representation anyway for boolean operations.
272  xt::xtensor<uint8_t, 2> itsFlags;
273  xt::xtensor<bool, 1> itsMatchBL;
274  };
275 
277  void setFlags(const xt::xtensor<uint8_t, 2>& in,
278  base::DPBuffer::FlagsType& out, bool mode);
279 
283  void clearFlags(const xt::xtensor<uint8_t, 2>& in,
284  base::DPBuffer::FlagsType& out, bool mode,
285  const base::DPBuffer::DataType& data,
286  const base::DPBuffer::WeightsType& weights);
287 
288  std::string itsName;
289  Mode itsMode;
290  common::NSTimer itsTimer;
291  PSet itsPSet;
292  unsigned int itsCount;
293  base::FlagCounter itsFlagCounter;
294 };
295 
296 } // namespace dp3::steps
297 
298 #endif
Class to handle the baseline selection.
Buffer holding the data of a timeslot/band.
Class to keep counts of nr of flagged points.
Class to hold code for virtual base class for Flaggers in DP3.
xt::xtensor< double, 2 > UvwType
Definition: DPBuffer.h:98
xt::xtensor< bool, 3 > FlagsType
Definition: DPBuffer.h:97
xt::xtensor< float, 3 > WeightsType
Definition: DPBuffer.h:96
aocommon::xt::UTensor< std::complex< float >, 3 > DataType
Definition: DPBuffer.h:95
General info about DP3 data processing attributes like averaging.
Definition: DPInfo.h:35
Definition: Fields.h:16
Implements a map of Key-Value pairs.
Definition: ParameterSet.h:31
DP3 step class to flag data on channel, baseline, or time.
Definition: PreFlagger.h:52
void show(std::ostream &) const override
Show the step parameters.
PreFlagger(const common::ParameterSet &, const std::string &prefix)
void finish() override
Finish the processing of this step and subsequent steps.
void showCounts(std::ostream &) const override
Show the flag counts.
friend class TestPSet
Make this Test class a friend, so it can access private code.
Definition: PreFlagger.h:54
common::Fields getRequiredFields() const override
Get the fields required by the current step.
Definition: PreFlagger.h:65
common::Fields getProvidedFields() const override
Definition: PreFlagger.h:73
void showTimings(std::ostream &, double duration) const override
Show the timings.
bool process(std::unique_ptr< base::DPBuffer > buffer) override
Mode
Definition: PreFlagger.h:57
void updateInfo(const base::DPInfo &) override
Abstract base class for a DP3 step.
Definition: Step.h:52
static constexpr dp3::common::Fields kWeightsField
Definition: Step.h:64
std::shared_ptr< Step > ShPtr
Definition: Step.h:54
static constexpr dp3::common::Fields kUvwField
Definition: Step.h:66
static constexpr dp3::common::Fields kDataField
Definition: Step.h:60
static constexpr dp3::common::Fields kFlagsField
Definition: Step.h:62
BaseTimer< std::chrono::steady_clock > NSTimer
Definition: Timer.h:129
Definition: AntennaFlagger.h:17