DP3
SagecalPredict.h
Go to the documentation of this file.
1 // Copyright (C) 2023 ASTRON (Netherlands Institute for Radio Astronomy)
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 
6 
7 #ifndef DP3_STEPS_SAGECALPREDICT_H_
8 #define DP3_STEPS_SAGECALPREDICT_H_
9 
10 #include <Dirac_radio.h>
11 // Remove 'complex' def here as we do not need it afterwards
12 #undef complex
13 
14 #include "base/DP3.h"
15 #include "base/DPBuffer.h"
16 #include "base/ComponentInfo.h"
17 #include "base/ModelComponent.h"
18 #include "base/PredictBuffer.h"
19 #include "common/ParameterSet.h"
20 #include "model/Patch.h"
21 #include "model/SourceDBUtil.h"
22 #include "steps/Step.h"
23 #include "ApplyCal.h"
24 #include "ResultStep.h"
25 
26 namespace dp3 {
27 namespace steps {
28 
29 class SagecalPredict : public ModelDataStep {
30  struct IOData {
31  std::vector<double> data;
32  std::vector<double> u;
33  std::vector<double> v;
34  std::vector<double> w;
35  std::vector<double> frequencies;
36  std::vector<clus_source_t> cluster_arr;
37  std::vector<baseline_t> baseline_arr;
38 
39  size_t n_baselines;
40  size_t n_stations;
41  size_t n_channels;
42  double f0;
43  double fdelta;
44  };
45 
46  // The following struct is unique to each instance of SagecalPredict
47  // and will be updated during each process() call.
48  struct BeamData {
49  // The following are for beam calculation, but will be updated
50  // at each process() call, hence they belong here
51  // time coord UTC (s), convert from MJD (s) to JD (days)
52  std::vector<double> time_utc;
53  bool sources_are_precessed = false;
54  // LOFAR HBA tile beam pointing center
55  double tile_ra;
56  double tile_dec;
57  // pointing center of beams (only one) (can be different from phase center)
58  double reference_ra;
59  double reference_dec;
60  };
61 
62  // The following struct is a singleton.
63  // Only updated during updateInfo(), and reused (read only) by all
64  // SagecalPredict instances.
65  struct BeamDataSingle {
66  public:
67  BeamDataSingle(BeamDataSingle const&) = delete;
68  BeamDataSingle& operator=(BeamDataSingle const&) = delete;
69  ~BeamDataSingle();
70 
71  static BeamDataSingle* get_instance() {
72  static BeamDataSingle instance;
73  return &instance;
74  }
75 
76  void update_metadata(const base::DPInfo& _info, const double freq_f0,
77  const size_t n_channels,
78  std::vector<double>& freq_channel,
79  const int beam_mode);
80 
81  /* number of elements in each station, size Nx1 */
82  std::vector<int> n_elem;
83  /* position (ITRF) of stations (m)
84  later changed to longitude,latitude,height (rad,rad,m) */
85  std::vector<double> sx; /* x: size Nx1 */
86  std::vector<double> sy; /* y: ... */
87  /* x,y,z coords of elements, projected, converted to ITRF (m) */
88  std::vector<std::vector<double>> xx;
89  std::vector<std::vector<double>> yy;
90  std::vector<std::vector<double>> zz;
91 
92  /* pointers the xx, yy and zz data, since libdirac has double** arguments */
93  std::vector<double*> xx_ptr;
94  std::vector<double*> yy_ptr;
95  std::vector<double*> zz_ptr;
96 
97  /* beamformer type STAT_NONE, STAT_SINGLE or STAT_TILE */
98  int beamformer_type;
99 
100  /* LOFAR HBA tile beam pointing center */
101  double tile_ra;
102  double tile_dec;
103 
104  /* pointing center of beams (only one) (could be different from phase
105  * center) */
106  double reference_ra;
107  double reference_dec;
108 
109  int beam_mode{DOBEAM_NONE};
110  elementcoeff ecoeff;
111 
112  private:
113  explicit BeamDataSingle() : is_updated_(false) {}
114  std::mutex mutex_;
115  bool is_updated_;
116  };
117 
118  // Singleton to open and read H5 file only by one thread
119  // But H5Parm itself is assumed thread-safe
120  class H5ParmSingle {
121  public:
122  H5ParmSingle(H5ParmSingle const&) = delete;
123  H5ParmSingle& operator=(H5ParmSingle const&) = delete;
124  ~H5ParmSingle(){};
125 
126  static H5ParmSingle& GetInstance() {
127  static H5ParmSingle instance;
128  return instance;
129  }
130 
131  schaapcommon::h5parm::H5Parm& OpenFile(const std::string& h5_name);
132 
133  private:
134  explicit H5ParmSingle() {}
135 
137  std::mutex mutex_;
138 
139  // Do not use a plain H5Parm object, since its implicitly declared
140  // assignment operator of H5Parm is deprecated,
141  // Using a (unique) pointer does allow re-assigning to this variable.
142  std::unique_ptr<schaapcommon::h5parm::H5Parm> h5_parm_;
143  };
144 
145  public:
146  SagecalPredict(const common::ParameterSet&, const std::string& prefix,
147  MsType input_type = MsType::kRegular);
148  // The following constructor is to be used within DDECal
149  SagecalPredict(const common::ParameterSet&, const std::string& prefix,
150  const std::vector<std::string>& given_source_patterns,
151  MsType input_type = MsType::kRegular);
152 
153  ~SagecalPredict() override;
154 
156  // We need UVW and data
157  common::Fields fields = kUvwField;
158  if ((operation_ == Operation::kAdd) ||
159  (operation_ == Operation::kSubtract)) {
160  fields |= kDataField;
161  }
162  return fields;
163  }
164 
166  // We only modify the output data
167  common::Fields fields = kDataField;
168  return fields;
169  }
170 
173  bool process(std::unique_ptr<dp3::base::DPBuffer> buffer) override;
174 
176  void updateInfo(const base::DPInfo&) override;
177 
178  void finish() override;
179 
180  void show(std::ostream&) const override;
181 
182  void showTimings(std::ostream& os, double duration) const override;
183 
185 
186  private:
187  enum class Operation { kReplace, kAdd, kSubtract };
188 
189  const MsType ms_type_;
190 
191  void SetOperation(const std::string& operation);
192 
193  unsigned int nPol(const std::string& parmName);
194 
195  void SetCorrectType(schaapcommon::h5parm::H5Parm& h5_parm,
196  const std::vector<std::string>& sol_tabs);
197 
198  void updateFromH5(const double startTime);
199 
201  void init(const common::ParameterSet&, const std::string& prefix,
202  const std::vector<std::string>& source_patterns);
203 
204  void LoadData(const dp3::base::DPBuffer& buffer);
205  void WriteData(dp3::base::DPBuffer& buffer) const;
206  void ReadAuxData(const base::DPInfo&);
207 
208  std::string name_;
209  Operation operation_;
210 
211  std::string h5_name_;
212  std::vector<std::string>
213  directions_list_;
214  std::vector<std::shared_ptr<model::Patch>> patch_list_;
215  std::string source_db_name_;
216  std::vector<std::pair<std::shared_ptr<base::ModelComponent>,
217  std::shared_ptr<model::Patch>>>
218  source_list_;
219  bool any_orientation_is_absolute_{false};
221 
222  // H5 solutions handling
223  std::string solset_name_;
224  std::string soltab_name_;
225  bool invert_{false};
226  bool parm_on_disk_{false};
227  bool use_amp_phase_{false};
228  schaapcommon::h5parm::GainType gain_type_;
229  schaapcommon::h5parm::JonesParameters::InterpolationType interp_type_;
230  schaapcommon::h5parm::JonesParameters::MissingAntennaBehavior
231  missing_ant_behavior_;
233  schaapcommon::h5parm::SolTab* sol_tab_;
235  schaapcommon::h5parm::SolTab* sol_tab2_;
236  std::vector<casacore::String> parm_expressions_;
237  unsigned int timeslots_per_parmupdate_;
238  unsigned int timestep_;
239  double time_interval_;
240  double time_last_;
241  std::vector<std::string> soltab_names_;
242 
243  std::vector<double> params_;
244 
245  base::Direction phase_ref_;
246 
247  common::NSTimer timer_;
248 
249  IOData iodata_;
250  BeamData runtime_beam_data_;
251  BeamDataSingle* beam_data_{nullptr};
252  int beam_mode{DOBEAM_NONE};
253  std::vector<int> ignore_list_;
254 };
255 
256 } // namespace steps
257 } // namespace dp3
258 
259 #endif // header guard
DP3 step class to apply multiple calibration solutions.
Buffer holding the data of a timeslot/band.
Class to hold code for virtual base class for Flaggers in DP3.
Buffer holding the data of a timeslot/band.
Definition: DPBuffer.h:92
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
Common interface for steps that produce model data.
Definition: Step.h:172
Definition: SagecalPredict.h:29
void finish() override
Finish the processing of this step and subsequent steps.
common::Fields getProvidedFields() const override
Definition: SagecalPredict.h:165
bool process(std::unique_ptr< dp3::base::DPBuffer > buffer) override
buffer, copies the buffer and change its data.
void show(std::ostream &) const override
Show the step parameters.
common::Fields getRequiredFields() const override
Get the fields required by the current step.
Definition: SagecalPredict.h:155
void showTimings(std::ostream &os, double duration) const override
base::Direction GetFirstDirection() const override
SagecalPredict(const common::ParameterSet &, const std::string &prefix, const std::vector< std::string > &given_source_patterns, MsType input_type=MsType::kRegular)
void updateInfo(const base::DPInfo &) override
Update the general info.
SagecalPredict(const common::ParameterSet &, const std::string &prefix, MsType input_type=MsType::kRegular)
static constexpr dp3::common::Fields kUvwField
Definition: Step.h:66
MsType
To check compatibility between steps before running.
Definition: Step.h:57
static constexpr dp3::common::Fields kDataField
Definition: Step.h:60
BaseTimer< std::chrono::steady_clock > NSTimer
Definition: Timer.h:129
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53
A direction on the celestial sphere.
Definition: Direction.h:15