DP3
GainCal.h
Go to the documentation of this file.
1 // GainCal.h: DP3 step class to calibrate (direction independent) gains
2 // Copyright (C) 2022 ASTRON (Netherlands Institute for Radio Astronomy)
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 
8 
9 #ifndef DP3_STEPS_GAINCAL_H_
10 #define DP3_STEPS_GAINCAL_H_
11 
12 #include <complex>
13 #include <vector>
14 
15 #include <xtensor/containers/xtensor.hpp>
16 
17 #include <schaapcommon/h5parm/h5parm.h>
18 
20 #define H5Acreate_vers 2
21 #define H5Tarray_create_vers 2
22 #define H5Dcreate_vers 2
23 #define H5Gcreate_vers 2
24 #include <H5Cpp.h>
25 
26 #include "base/BaselineSelection.h"
27 #include "base/CalType.h"
28 #include "base/GainCalAlgorithm.h"
29 #include "base/PhaseFitter.h"
30 #include "common/ParameterSet.h"
31 #include "parmdb/ParmDB.h"
32 
33 #include "InputStep.h"
34 #include "Predict.h"
35 #include "ResultStep.h"
36 #include "UVWFlagger.h"
37 
38 namespace dp3 {
39 namespace steps {
40 
43 class GainCal final : public Step {
44  public:
47  GainCal(const common::ParameterSet& parset, const std::string& prefix);
48 
49  common::Fields getRequiredFields() const override {
51  itsUVWFlagStep.getRequiredFields() | itsSubRequiredFields;
52  }
53 
54  common::Fields getProvidedFields() const override {
55  common::Fields fields;
56  if (itsApplySolution) fields |= kDataField | kFlagsField;
57  return fields;
58  }
59 
62  bool process(std::unique_ptr<base::DPBuffer>) override;
63 
64  void finish() override;
65 
66  void updateInfo(const base::DPInfo&) override;
67 
68  void show(std::ostream&) const override;
69 
70  void showTimings(std::ostream&, double duration) const override;
71 
73  static std::vector<schaapcommon::h5parm::SolTab> makeSolTab(
74  schaapcommon::h5parm::H5Parm& h5parm, base::CalType caltype,
75  std::vector<schaapcommon::h5parm::AxisInfo>& axes);
76 
77  private:
79  void calibrate();
80 
82  static bool scalarMode(base::CalType caltype);
83 
85  static bool diagonalMode(base::CalType caltype);
86 
88  void applySolution(base::DPBuffer& buf,
89  const xt::xtensor<std::complex<float>, 3>& invsol);
90 
92  xt::xtensor<std::complex<float>, 3> invertSol(
93  const xt::xtensor<std::complex<float>, 3>& sol);
94 
96  void fillMatrices(const base::DPBuffer::DataType& model,
97  const base::DPBuffer::DataType& data,
98  const base::DPBuffer::WeightsType& weights,
99  const base::DPBuffer::FlagsType& flags);
100 
102  void initParmDB();
103 
105  std::string parmName();
106 
108  void setAntennaUsed();
109 
112  void writeSolutionsParmDB(double startTime);
113 
116  void writeSolutionsH5Parm(double startTime);
117 
118  std::string itsName;
121  std::vector<std::unique_ptr<base::DPBuffer>> itsBuffers;
122  bool itsUseModelColumn;
123  std::string itsModelColumnName;
124  std::string itsParmDBName;
125  bool itsUseH5Parm;
126  std::shared_ptr<parmdb::ParmDB> itsParmDB;
127  std::string itsParsetString;
128 
129  base::CalType itsMode;
130 
131  unsigned int itsDebugLevel;
132  bool itsDetectStalling;
133 
134  bool itsApplySolution;
135 
137  std::vector<xt::xtensor<std::complex<float>, 3>> itsSols;
139  std::vector<xt::xtensor<double, 2>> itsTECSols;
140  std::vector<double> itsFreqData;
141 
142  std::vector<std::unique_ptr<PhaseFitter>> itsPhaseFitters;
143 
144  std::vector<base::GainCalAlgorithm> algorithms_;
145 
146  UVWFlagger itsUVWFlagStep;
147  std::shared_ptr<ResultStep>
148  itsDataResultStep;
149 
153  std::shared_ptr<Step> itsFirstSubStep;
154  std::shared_ptr<ResultStep> itsResultStep;
156  common::Fields itsSubRequiredFields;
157  bool itsApplyBeamToModelColumn;
158 
159  base::BaselineSelection itsBaselineSelection;
160  casacore::Vector<bool> itsSelectedBL;
162  casacore::Vector<bool> itsAntennaUsed;
164 
165  std::map<std::string, int> itsParmIdMap;
166 
167  unsigned int itsMaxIter;
168  double itsTolerance;
169  bool itsPropagateSolutions;
170  unsigned int itsSolInt;
171  unsigned int itsNChan;
172  unsigned int itsNFreqCells;
173 
174  unsigned int itsTimeSlotsPerParmUpdate;
175  unsigned int itsConverged;
176  unsigned int itsNonconverged;
177  unsigned int itsFailed;
178  unsigned int itsStalled;
179  std::vector<unsigned int>
180  itsNIter;
182  unsigned int itsStepInParmUpdate;
183  double itsChunkStartTime;
184  unsigned int itsStepInSolInt;
185 
187  xt::xtensor<std::complex<double>, 6> itsAllSolutions;
188 
189  base::FlagCounter itsFlagCounter;
190 
194  std::string itsModelDataName;
195 
196  common::NSTimer itsTimer;
197  common::NSTimer itsTimerPredict;
198  common::NSTimer itsTimerSolve;
199  common::NSTimer itsTimerPhaseFit;
200  common::NSTimer itsTimerWrite;
201  common::NSTimer itsTimerFill;
202 };
203 
204 } // namespace steps
205 } // namespace dp3
206 
207 #endif
Class to handle the baseline selection.
DPPP step class to apply a calibration correction to the data.
Abstract base class for a Step generating input.
Base class for a table holding parameters.
DP3 step class to flag data on UVW coordinates.
Class containing a few static functions to parse a baseline selection string.
Definition: BaselineSelection.h:47
Buffer holding the data of a timeslot/band.
Definition: DPBuffer.h:92
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
Class to keep counts of nr of flagged points.
Definition: FlagCounter.h:35
Definition: Fields.h:16
Implements a map of Key-Value pairs.
Definition: ParameterSet.h:31
This class is a Step class to calibrate (direction independent) gains.
Definition: GainCal.h:43
common::Fields getRequiredFields() const override
Get the fields required by the current step.
Definition: GainCal.h:49
void finish() override
Finish the processing of this step and subsequent steps.
void updateInfo(const base::DPInfo &) override
void showTimings(std::ostream &, double duration) const override
common::Fields getProvidedFields() const override
Definition: GainCal.h:54
GainCal(const common::ParameterSet &parset, const std::string &prefix)
static std::vector< schaapcommon::h5parm::SolTab > makeSolTab(schaapcommon::h5parm::H5Parm &h5parm, base::CalType caltype, std::vector< schaapcommon::h5parm::AxisInfo > &axes)
Make a soltab with the given type.
bool process(std::unique_ptr< base::DPBuffer >) override
void show(std::ostream &) const override
Show the step parameters.
Abstract base class for a DP3 step.
Definition: Step.h:52
static constexpr dp3::common::Fields kWeightsField
Definition: Step.h:64
static constexpr dp3::common::Fields kDataField
Definition: Step.h:60
static constexpr dp3::common::Fields kFlagsField
Definition: Step.h:62
DP3 step class to flag data on UVW coordinates.
Definition: UVWFlagger.h:38
common::Fields getRequiredFields() const override
Get the fields required by the current step.
Definition: UVWFlagger.h:50
CalType
Definition: CalType.h:11
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53