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:
33  public MHO_UnaryOperator< XArrayType >,
35 {
36  public:
38  {
39  fIndexToChannelLabel.clear();
40  fEps = 1e-4; //tolerance when mapping freq to indices
41  fChannelLabelKey = "channel_label";
42  };
43 
44  virtual ~MHO_ChannelLabeler(){};
45 
51  void SetTolerance(double tol) { fEps = tol; }
52 
58  void SetChannelLabelToFrequencyMap(const std::map< std::string, double >& map) { fChannelLabelToFrequency = map; }
59 
60  protected:
68  virtual bool InitializeInPlace(XArrayType* in) override { return true; }
69 
78  virtual bool InitializeOutOfPlace(const XArrayType* , XArrayType* ) override { return true; }
79 
87  virtual bool ExecuteInPlace(XArrayType* in) override
88  {
89  if(in != nullptr)
90  {
91  //need to use the user provided frequency <-> channel label map
92  auto chan_axis_ptr = &(std::get< CHANNEL_AXIS >(*in));
93  std::size_t nchans = chan_axis_ptr->GetSize();
94 
95  //grab info about DSB channels (if it exists)
96  std::vector< mho_json > dsb_labels = chan_axis_ptr->GetMatchingIntervalLabels("double_sideband");
97 
98  if(fChannelLabelToFrequency.size() == 0)
99  {
100  //apply default channel labels
101  FillDefaultMap(nchans);
102  std::size_t label_count = 0;
103  for(std::size_t i = 0; i < nchans; i++)
104  {
105  std::string ch_label = fIndexToChannelLabel[label_count];
106 
107  std::string dummy; //check if this channel was already labeled
108  bool already_labeled = chan_axis_ptr->RetrieveIndexLabelKeyValue(i, "channel_label", dummy);
109  if(!already_labeled)
110  {
111  chan_axis_ptr->InsertIndexLabelKeyValue(i, fChannelLabelKey, ch_label);
112  //check if this channel is a member of a double-sideband pair,
113  //and if so make sure its partner gets the same label
114  if(dsb_labels.size() != 0)
115  {
116  int partner_offset;
117  bool has_dsb_partner =
118  chan_axis_ptr->RetrieveIndexLabelKeyValue(i, "dsb_partner", partner_offset);
119  int partner_idx = i + partner_offset;
120  if(has_dsb_partner && (0 <= partner_offset) && (partner_offset < nchans))
121  {
122  // chan_axis_ptr->InsertIndexLabelKeyValue(i, fChannelLabelKey, ch_label);
123  // chan_axis_ptr->InsertIndexLabelKeyValue(partner_idx, fChannelLabelKey, ch_label);
124  chan_axis_ptr->InsertIndexLabelKeyValue(i, fChannelLabelKey, ch_label + "-");
125  chan_axis_ptr->InsertIndexLabelKeyValue(partner_idx, fChannelLabelKey, ch_label + "+");
126  }
127  }
128  label_count++;
129  }
130  }
131  }
132  else
133  {
134  if(fChannelLabelToFrequency.size() < nchans - dsb_labels.size())
135  {
136  msg_error("calibration", "not all channels given a user specified label, "
137  << "some channels will remain un-labelled." << eom);
138  }
139 
140  //now do a brute force search over the channel frequencies, and
141  //determine which ones match the labels we've been given
142  for(auto it = fChannelLabelToFrequency.begin(); it != fChannelLabelToFrequency.end(); it++)
143  {
144  std::string ch_label = it->first;
145  double freq = it->second;
146  for(std::size_t i = 0; i < nchans; i++)
147  {
148  double ch_freq = chan_axis_ptr->at(i);
149  if(std::abs(freq - ch_freq) < fEps)
150  {
151  chan_axis_ptr->InsertIndexLabelKeyValue(i, fChannelLabelKey, ch_label);
152  //check if this channel is a member of a double-sideband pair,
153  //and if so make sure its partner gets the same label
154  if(dsb_labels.size() != 0)
155  {
156  int partner_offset;
157  bool has_dsb_partner =
158  chan_axis_ptr->RetrieveIndexLabelKeyValue(i, "dsb_partner", partner_offset);
159  int partner_idx = i + partner_offset;
160  if(has_dsb_partner)
161  {
162  // chan_axis_ptr->InsertIndexLabelKeyValue(i, fChannelLabelKey, ch_label);
163  // chan_axis_ptr->InsertIndexLabelKeyValue(partner_idx, fChannelLabelKey, ch_label);
164  chan_axis_ptr->InsertIndexLabelKeyValue(i, fChannelLabelKey, ch_label + "-");
165  chan_axis_ptr->InsertIndexLabelKeyValue(partner_idx, fChannelLabelKey, ch_label + "+");
166  }
167  }
168  break;
169  }
170  }
171  }
172  }
173  return true;
174  }
175  return false;
176  }
177 
186  virtual bool ExecuteOutOfPlace(const XArrayType* in, XArrayType* out) override
187  {
188  out->Copy(*in);
189  return ExecuteInPlace(out);
190  }
191 
192  private:
198  void FillDefaultMap(std::size_t nchans)
199  {
200  fIndexToChannelLabel.clear();
201  for(std::size_t i = 0; i < nchans; i++)
202  {
203  fIndexToChannelLabel[i] = EncodeValueToLabel(i);
204  }
205  }
206 
207  //data
208  std::string fChannelLabelKey;
209 
210  //user supplied channel label to frequency map (if available)
211  std::map< std::string, double > fChannelLabelToFrequency;
212  double fEps;
213 
214  std::map< std::size_t, std::string > fIndexToChannelLabel;
215 };
216 
217 } // namespace hops
218 
219 #endif
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:244
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:35
virtual bool InitializeInPlace(XArrayType *in) override
Initializes XArrayType in-place and returns success.
Definition: MHO_ChannelLabeler.hh:68
void SetTolerance(double tol)
Setter for tolerance - allows channel freq association to use a (freq) difference tolerance.
Definition: MHO_ChannelLabeler.hh:51
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:58
virtual bool InitializeOutOfPlace(const XArrayType *, XArrayType *) override
Initializes output array out-of-place from input array.
Definition: MHO_ChannelLabeler.hh:78
virtual bool ExecuteOutOfPlace(const XArrayType *in, XArrayType *out) override
Copies input array to output and executes in-place operation on output.
Definition: MHO_ChannelLabeler.hh:186
MHO_ChannelLabeler()
Definition: MHO_ChannelLabeler.hh:37
virtual ~MHO_ChannelLabeler()
Definition: MHO_ChannelLabeler.hh:44
virtual bool ExecuteInPlace(XArrayType *in) override
Function ExecuteInPlace - attaches channel labels based on sky frequency or user specified map.
Definition: MHO_ChannelLabeler.hh:87
Class MHO_UnaryOperator.
Definition: MHO_UnaryOperator.hh:24
def dummy(fringe_data_interface)
Definition: example1.py:3
Definition: MHO_ChannelLabeler.hh:17