DP3
OnePredict.h
Go to the documentation of this file.
1 // OnePredict.h: DP3 step class to predict visibilities from a source model
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_ONEPREDICT_H_
10 #define DP3_STEPS_ONEPREDICT_H_
11 
12 #include <atomic>
13 
14 #include <xtensor/containers/xtensor.hpp>
15 
16 #include "ApplyBeam.h"
17 #include "ApplyCal.h"
18 #include "ResultStep.h"
19 #include "base/DP3.h"
20 #include "base/DPBuffer.h"
21 
22 #include "base/ModelComponent.h"
23 #include "base/PredictBuffer.h"
24 #include "base/PredictModel.h"
25 
26 #include "model/Patch.h"
27 #include "model/SourceDBUtil.h"
28 
29 namespace dp3 {
30 namespace common {
31 class ParameterSet;
32 }
33 
34 namespace steps {
35 
39 class OnePredict : public ModelDataStep {
40  public:
47  OnePredict(const common::ParameterSet&, const std::string& prefix,
48  const std::vector<std::string>& source_patterns);
49 
50  ~OnePredict() override;
51 
52  common::Fields getRequiredFields() const override {
53  common::Fields fields = kUvwField;
54  if (operation_ == Operation::kAdd || operation_ == Operation::kSubtract) {
55  fields |= kDataField;
56  }
57  if (apply_cal_step_) {
58  fields |= base::GetChainRequiredFields(apply_cal_step_);
59  }
60  return fields;
61  }
62 
63  common::Fields getProvidedFields() const override {
64  // When operation_ == "replace", the output of apply_cal_step_ is passed to
65  // the next step. For all other operations, the predicted visibility data
66  // is the only provided output.
67  common::Fields fields;
68 
69  if (output_data_name_.empty()) {
70  // The predicted visibilities go to the main data buffer
71  fields |= kDataField;
72  } else {
73  // TODO(AST-1241): Handle these dependencies using Fields.
74  }
75 
76  if (operation_ == Operation::kReplace && apply_cal_step_) {
77  std::shared_ptr<Step> step = apply_cal_step_;
78  do {
79  fields |= step->getProvidedFields();
80  step = step->getNextStep();
81  } while (step);
82  }
83  return fields;
84  }
85 
87  void SetApplyCal(const common::ParameterSet&, const std::string& prefix);
88 
90  void SetOperation(const std::string& type);
91 
97  void SetThreadData(std::mutex* measures_mutex) {
98  measures_mutex_ = measures_mutex;
99  }
100 
104  bool process(std::unique_ptr<base::DPBuffer>) override;
105 
107  void finish() override;
108 
110  void updateInfo(const base::DPInfo&) override;
111 
113  void show(std::ostream&) const override;
114 
116  void showTimings(std::ostream&, double duration) const override;
117 
119  void setSources(const std::vector<std::string>& sourcePatterns);
120 
123 
124  private:
125  enum class Operation { kReplace, kAdd, kSubtract };
126 
128  void init(const common::ParameterSet&, const std::string& prefix,
129  const std::vector<std::string>& sourcePatterns);
130 
131  void initializeThreadData();
132  everybeam::vector3r_t dir2Itrf(const casacore::MDirection& dir,
133  casacore::MDirection::Convert& measConverter);
134 
135  void addBeamToData(const model::Patch& patch, size_t buffer_index,
136  aocommon::xt::UTensor<std::complex<double>, 3>& model_data,
137  double time, bool update_beam, size_t thread,
138  aocommon::xt::UTensor<std::complex<double>, 3>& data0,
139  bool stokesIOnly);
140 
141  void addBeamToDataRange(
142  const model::Patch& patch,
143  aocommon::xt::UTensor<std::complex<double>, 3>& model_data, double time,
144  size_t thread, aocommon::xt::UTensor<std::complex<double>, 3>& data0,
145  const std::pair<size_t, size_t>& baseline_range,
146  const std::pair<size_t, size_t>& station_range, std::barrier<>& barrier,
147  bool stokesIOnly);
148 
149  void PredictWithSourceParallelization(base::DPBuffer::DataType& destination,
150  double time);
151  void PredictSourceRange(
152  aocommon::xt::UTensor<std::complex<double>, 3>& result, size_t start,
153  size_t end, size_t thread_index, std::mutex& mutex, double time,
154  bool update_beam);
155 
158  void CopyPredictBufferToData(
159  base::DPBuffer::DataType& destination,
160  const aocommon::xt::UTensor<std::complex<double>, 3>& buffer);
161 
162  std::string name_;
166  xt::xtensor<std::complex<float>, 3> input_data_;
167  std::string source_db_name_;
168  bool correct_time_smearing_ = false;
169  bool correct_freq_smearing_ = false;
170  Operation operation_;
171  std::string output_data_name_;
172  bool apply_beam_ = false;
173  std::string coefficients_path_;
174  bool use_channel_freq_ = false;
175  bool one_beam_per_patch_ = false;
176  bool thread_over_baselines_ = false;
180  double beam_proximity_limit_ = 0.0;
181  double beam_evaluation_interval_ = 0.0;
182  double previous_beam_time_ = 0.0;
183  bool stokes_i_only_ = false;
184  bool any_orientation_is_absolute_ = false;
186  base::Direction phase_ref_;
187  bool moving_phase_ref_ = false;
188 
189  std::shared_ptr<ApplyCal> apply_cal_step_;
190  std::shared_ptr<ResultStep> result_step_;
191 
192  unsigned int debug_level_ = 0;
193 
194  std::vector<double> scaled_ncp_uvw_;
195 
196  std::vector<std::pair<size_t, size_t>> baselines_;
197 
199  std::vector<int> uvw_split_index_;
200 
202  xt::xtensor<double, 2> station_uvw_;
203 
205  std::shared_ptr<std::vector<base::PredictBuffer>> predict_buffers_;
206  everybeam::BeamMode beam_mode_ = everybeam::BeamMode::kNone;
207  everybeam::ElementResponseModel element_response_model_ =
208  everybeam::ElementResponseModel::kDefault;
209  std::vector<casacore::MeasFrame> meas_frame_;
210  std::vector<casacore::MDirection::Convert> meas_convertors_;
211  std::shared_ptr<everybeam::telescope::Telescope> telescope_;
212 
213  std::string direction_str_;
214  std::vector<std::shared_ptr<model::Patch>> patch_list_;
215 
216  std::vector<std::pair<std::shared_ptr<base::ModelComponent>,
217  std::shared_ptr<model::Patch>>>
218  source_list_;
219 
220  common::NSTimer timer_;
221 
233  std::atomic<int64_t> predict_time_ = 0;
239  std::atomic<int64_t> apply_beam_time_ = 0;
240 
241  std::mutex* measures_mutex_;
242  std::mutex mutex_;
243 };
244 
245 } // namespace steps
246 } // namespace dp3
247 
248 #endif
DP3 step class to apply the beam model (optionally inverted)
DP3 step class to apply multiple calibration solutions.
Buffer holding the data of a timeslot/band.
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
A set of sources for which direction dependent effects are assumed to be equal.
Definition: Patch.h:19
Common interface for steps that produce model data.
Definition: Step.h:172
Step class that predicts visibilities with optionally beam. The Predict class uses one or more instan...
Definition: OnePredict.h:39
void finish() override
Finish the processing of this step and subsequent steps.
void SetOperation(const std::string &type)
Set the operation type.
common::Fields getRequiredFields() const override
Get the fields required by the current step.
Definition: OnePredict.h:52
void show(std::ostream &) const override
Show the step parameters.
void SetThreadData(std::mutex *measures_mutex)
Definition: OnePredict.h:97
bool process(std::unique_ptr< base::DPBuffer >) override
void showTimings(std::ostream &, double duration) const override
Show the timings.
OnePredict(const common::ParameterSet &, const std::string &prefix, const std::vector< std::string > &source_patterns)
common::Fields getProvidedFields() const override
Definition: OnePredict.h:63
void setSources(const std::vector< std::string > &sourcePatterns)
Prepare the sources.
base::Direction GetFirstDirection() const override
Return the direction of the first patch.
void SetApplyCal(const common::ParameterSet &, const std::string &prefix)
Set the ApplyCal substep and connect it to a ResultStep.
void updateInfo(const base::DPInfo &) override
Update the general info.
static constexpr dp3::common::Fields kUvwField
Definition: Step.h:66
static constexpr dp3::common::Fields kDataField
Definition: Step.h:60
dp3::common::Fields GetChainRequiredFields(std::shared_ptr< steps::Step > first_step)
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53
A direction on the celestial sphere.
Definition: Direction.h:15