HOPS
HOPS class reference
MHO_ChannelLabeler.hh
Go to the documentation of this file.
1 #ifndef MHO_ChannelLabeler_HH__
2 #define MHO_ChannelLabeler_HH__
3 
4 #include <map>
5 #include <stack>
6 #include <string>
7 
8 #include "MHO_Message.hh"
9 
10 #include "MHO_EncodeDecodeValue.hh"
11 
13 #include "MHO_TableContainer.hh"
14 #include "MHO_UnaryOperator.hh"
15 
16 namespace hops
17 {
18 
32 template< typename XArrayType > class MHO_ChannelLabeler: public MHO_UnaryOperator< XArrayType >, public MHO_ChannelIndexLabeler
33 {
34  public:
36  {
37  fIndexToChannelLabel.clear();
38  fEps = 1e-4; //tolerance when mapping freq to indices
39  fChannelLabelKey = "channel_label";
40  };
41 
42  virtual ~MHO_ChannelLabeler(){};
43 
49  void SetTolerance(double tol) { fEps = tol; }
50 
56  void SetChannelLabelToFrequencyMap(const std::map< std::string, double >& map) { fChannelLabelToFrequency = map; }
57 
58  protected:
66  virtual bool ExecuteInPlace(XArrayType* in) override
67  {
68  if(in != nullptr)
69  {
70  //need to use the user provided frequency <-> channel label map
71  auto chan_axis_ptr = &(std::get< CHANNEL_AXIS >(*in));
72  std::size_t nchans = chan_axis_ptr->GetSize();
73 
74  //grab info about DSB channels (if it exists)
75  std::vector< mho_json > dsb_labels = chan_axis_ptr->GetMatchingIntervalLabels("double_sideband");
76 
77  if(fChannelLabelToFrequency.size() == 0)
78  {
79  //apply default channel labels
80  FillDefaultMap(nchans);
81  std::size_t label_count = 0;
82  for(std::size_t i = 0; i < nchans; i++)
83  {
84  std::string ch_label = fIndexToChannelLabel[label_count];
85 
86  std::string dummy; //check if this channel was already labeled
87  bool already_labeled = chan_axis_ptr->RetrieveIndexLabelKeyValue(i, "channel_label", dummy);
88  if(!already_labeled)
89  {
90  chan_axis_ptr->InsertIndexLabelKeyValue(i, fChannelLabelKey, ch_label);
91  //check if this channel is a member of a double-sideband pair,
92  //and if so make sure its partner gets the same label
93  if(dsb_labels.size() != 0)
94  {
95  int partner_offset;
96  bool has_dsb_partner =
97  chan_axis_ptr->RetrieveIndexLabelKeyValue(i, "dsb_partner", partner_offset);
98  int partner_idx = i + partner_offset;
99  if(has_dsb_partner && (0 <= partner_offset) && (partner_offset < nchans))
100  {
101  // chan_axis_ptr->InsertIndexLabelKeyValue(i, fChannelLabelKey, ch_label);
102  // chan_axis_ptr->InsertIndexLabelKeyValue(partner_idx, fChannelLabelKey, ch_label);
103  chan_axis_ptr->InsertIndexLabelKeyValue(i, fChannelLabelKey, ch_label + "-");
104  chan_axis_ptr->InsertIndexLabelKeyValue(partner_idx, fChannelLabelKey, ch_label + "+");
105  }
106  }
107  label_count++;
108  }
109  }
110  }
111  else
112  {
113  if(fChannelLabelToFrequency.size() < nchans - dsb_labels.size())
114  {
115  msg_error("calibration", "not all channels given a user specified label, "
116  << "some channels will remain un-labelled." << eom);
117  }
118 
119  //now do a brute force search over the channel frequencies, and
120  //determine which ones match the labels we've been given
121  for(auto it = fChannelLabelToFrequency.begin(); it != fChannelLabelToFrequency.end(); it++)
122  {
123  std::string ch_label = it->first;
124  double freq = it->second;
125  for(std::size_t i = 0; i < nchans; i++)
126  {
127  double ch_freq = chan_axis_ptr->at(i);
128  if(std::abs(freq - ch_freq) < fEps)
129  {
130  chan_axis_ptr->InsertIndexLabelKeyValue(i, fChannelLabelKey, ch_label);
131  //check if this channel is a member of a double-sideband pair,
132  //and if so make sure its partner gets the same label
133  if(dsb_labels.size() != 0)
134  {
135  int partner_offset;
136  bool has_dsb_partner =
137  chan_axis_ptr->RetrieveIndexLabelKeyValue(i, "dsb_partner", partner_offset);
138  int partner_idx = i + partner_offset;
139  if(has_dsb_partner)
140  {
141  // chan_axis_ptr->InsertIndexLabelKeyValue(i, fChannelLabelKey, ch_label);
142  // chan_axis_ptr->InsertIndexLabelKeyValue(partner_idx, fChannelLabelKey, ch_label);
143  chan_axis_ptr->InsertIndexLabelKeyValue(i, fChannelLabelKey, ch_label + "-");
144  chan_axis_ptr->InsertIndexLabelKeyValue(partner_idx, fChannelLabelKey, ch_label + "+");
145  }
146  }
147  break;
148  }
149  }
150  }
151  }
152  return true;
153  }
154  return false;
155  }
156 
157  private:
163  void FillDefaultMap(std::size_t nchans)
164  {
165  fIndexToChannelLabel.clear();
166  for(std::size_t i = 0; i < nchans; i++)
167  {
168  fIndexToChannelLabel[i] = EncodeValueToLabel(i);
169  }
170  }
171 
172  //data
173  std::string fChannelLabelKey;
174 
175  //user supplied channel label to frequency map (if available)
176  std::map< std::string, double > fChannelLabelToFrequency;
177  double fEps;
178 
179  std::map< std::size_t, std::string > fIndexToChannelLabel;
180 };
181 
182 } // namespace hops
183 
184 #endif
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:238
Class MHO_ChannelIndexLabeler.
Definition: MHO_EncodeDecodeValue.hh:96
std::string EncodeValueToLabel(const uint64_t &value) const
Encodes a uint64_t value into a label string using default and extended encoding schemes.
Definition: MHO_EncodeDecodeValue.hh:130
Class MHO_ChannelLabeler.
Definition: MHO_ChannelLabeler.hh:33
void SetTolerance(double tol)
Setter for tolerance - allows channel freq association to use a (freq) difference tolerance.
Definition: MHO_ChannelLabeler.hh:49
void SetChannelLabelToFrequencyMap(const std::map< std::string, double > &map)
Setter for channel label to frequency map so if there is a user provided labeling scheme,...
Definition: MHO_ChannelLabeler.hh:56
MHO_ChannelLabeler()
Definition: MHO_ChannelLabeler.hh:35
virtual ~MHO_ChannelLabeler()
Definition: MHO_ChannelLabeler.hh:42
virtual bool ExecuteInPlace(XArrayType *in) override
Function ExecuteInPlace - attaches channel labels based on sky frequency or user specified map.
Definition: MHO_ChannelLabeler.hh:66
Class MHO_UnaryOperator.
Definition: MHO_UnaryOperator.hh:24
def dummy(fringe_data_interface)
Definition: example1.py:3
Definition: MHO_AdhocFlagging.hh:18