DP3
AntennaConstraint.h
Go to the documentation of this file.
1 // Copyright (C) 2021 ASTRON (Netherlands Institute for Radio Astronomy)
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 
4 #ifndef DP3_DDECAL_ANTENNA_CONSTRAINT_H_
5 #define DP3_DDECAL_ANTENNA_CONSTRAINT_H_
6 
7 #include "Constraint.h"
8 
9 #include <set>
10 #include <vector>
11 
12 #include <xtensor/misc/xcomplex.hpp>
13 #include <xtensor/views/xmasked_view.hpp>
14 #include <xtensor/views/xview.hpp>
15 
16 namespace dp3 {
17 namespace ddecal {
18 
29 class AntennaConstraint final : public Constraint {
30  public:
31  void SetAntennaSets(std::vector<std::set<size_t>>&& antenna_sets) {
32  antenna_sets_ = std::move(antenna_sets);
33  }
34 
35  const std::vector<std::set<size_t>>& GetAntennaSets() const {
36  return antenna_sets_;
37  }
38 
39  void Apply(SolutionSpan& solutions, double time) override {
40  const size_t n_channels = solutions.shape(0);
41  const size_t n_directions = solutions.shape(2);
42  const size_t n_polarizations = solutions.shape(3);
43  for (size_t ch = 0; ch < n_channels; ++ch) {
44  for (const std::set<size_t>& antenna_set : antenna_sets_) {
45  xt::xtensor<dcomplex, 2> solution_per_set =
46  xt::zeros<dcomplex>({n_directions, n_polarizations});
47  xt::xtensor<size_t, 2> solution_count_per_set =
48  xt::zeros<size_t>({n_directions, n_polarizations});
49 
50  // Declaring these variables outside the loop allows reusing memory.
51  xt::xtensor<dcomplex, 2> solution_set;
52  xt::xtensor<std::size_t, 2> solution_set_is_finite;
53 
54  // Calculate the sum of solutions over the set of stations
55  for (size_t antenna_index : antenna_set) {
56  solution_set =
57  xt::view(solutions, ch, antenna_index, xt::all(), xt::all());
58  solution_set_is_finite = xt::isfinite(solution_set);
59  xt::masked_view(solution_set, !solution_set_is_finite)
60  .fill(std::complex<double>(0.0, 0.0));
61 
62  solution_per_set += solution_set;
63  solution_count_per_set += solution_set_is_finite;
64  }
65 
66  // Divide by nr of core stations to get the mean solution
67  solution_per_set /= xt::cast<double>(solution_count_per_set);
68 
69  // Assign all core stations to the mean solution
70  for (size_t antenna_index : antenna_set) {
71  xt::view(solutions, ch, antenna_index, xt::all(), xt::all()) =
72  solution_per_set;
73  }
74  }
75  }
76  }
77 
78  private:
79  std::vector<std::set<size_t>> antenna_sets_;
80 };
81 
82 } // namespace ddecal
83 } // namespace dp3
84 
85 #endif
This constraint averages the solutions of several groups of antennas, so that antennas within the sam...
Definition: AntennaConstraint.h:29
void Apply(SolutionSpan &solutions, double time) override
Definition: AntennaConstraint.h:39
void SetAntennaSets(std::vector< std::set< size_t >> &&antenna_sets)
Definition: AntennaConstraint.h:31
const std::vector< std::set< size_t > > & GetAntennaSets() const
Definition: AntennaConstraint.h:35
This class is the base class for classes that implement a constraint on calibration solutions....
Definition: Constraint.h:27
aocommon::xt::Span< std::complex< double >, 4 > SolutionSpan
Definition: Solutions.h:20
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53