HOPS
HOPS class reference
MHO_SamplerLabeler.hh
Go to the documentation of this file.
1 #ifndef MHO_SamplerLabeler_HH__
2 #define MHO_SamplerLabeler_HH__
3 
4 #include <map>
5 #include <stack>
6 #include <string>
7 
8 #include "MHO_Message.hh"
9 #include "MHO_Tokenizer.hh"
10 
12 #include "MHO_TableContainer.hh"
13 #include "MHO_UnaryOperator.hh"
14 
15 namespace hops
16 {
17 
34 template< typename XArrayType > class MHO_SamplerLabeler: public MHO_UnaryOperator< XArrayType >
35 {
36  public:
38  {
39  fComma = ",";
40  fChannelLabelKey = "channel_label";
41  fRefSamplerIndexKey = "ref_sampler_index";
42  fRemSamplerIndexKey = "rem_sampler_index";
43  };
44 
45  virtual ~MHO_SamplerLabeler(){};
46 
52  void SetReferenceStationSamplerChannelSets(const std::vector< std::string >& channel_sets)
53  {
54  fRefSamplerChanSets = channel_sets;
55  }
56 
62  void SetRemoteStationSamplerChannelSets(const std::vector< std::string >& channel_sets)
63  {
64  fRemSamplerChanSets = channel_sets;
65  }
66 
67  protected:
75  virtual bool InitializeInPlace(XArrayType* in) override { return true; }
76 
85  virtual bool InitializeOutOfPlace(const XArrayType* , XArrayType* ) override { return true; }
86 
95  virtual bool ExecuteInPlace(XArrayType* in) override
96  {
97  //map channel label (e.g. 'a', 'b', etc.) to sampler index for both reference and remote stations
98  ConstructChannelToSamplerIDMap(fRefSamplerChanSets, fRefChanToSamplerID);
99  ConstructChannelToSamplerIDMap(fRemSamplerChanSets, fRemChanToSamplerID);
100 
101  if(in != nullptr)
102  {
103  //need to retrieve the labels of each channel, then look up the
104  //sampler delay index using the map, and then insert that key:value pair
105  auto chan_axis_ptr = &(std::get< CHANNEL_AXIS >(*in));
106  std::size_t nchans = chan_axis_ptr->GetSize();
107 
108  for(std::size_t ch = 0; ch < nchans; ch++)
109  {
110  std::string chan_label = "";
111  chan_axis_ptr->RetrieveIndexLabelKeyValue(ch, fChannelLabelKey, chan_label);
112  //add sampler labels
113  if(fRefChanToSamplerID.find(chan_label) != fRefChanToSamplerID.end())
114  {
115  int ref_id = fRefChanToSamplerID[chan_label];
116  chan_axis_ptr->InsertIndexLabelKeyValue(ch, fRefSamplerIndexKey, ref_id);
117  }
118  if(fRemChanToSamplerID.find(chan_label) != fRemChanToSamplerID.end())
119  {
120  int rem_id = fRemChanToSamplerID[chan_label];
121  chan_axis_ptr->InsertIndexLabelKeyValue(ch, fRemSamplerIndexKey, rem_id);
122  }
123  }
124  return true;
125  }
126  return false;
127  }
128 
137  virtual bool ExecuteOutOfPlace(const XArrayType* in, XArrayType* out) override
138  {
139  out->Copy(*in);
140  return ExecuteInPlace(out);
141  }
142 
143  private:
144  //data maps channels to sampler
145  std::vector< std::string > fRefSamplerChanSets;
146  std::vector< std::string > fRemSamplerChanSets;
147  std::map< std::string, int > fRefChanToSamplerID;
148  std::map< std::string, int > fRemChanToSamplerID;
149 
150  std::string fComma;
151  std::string fChannelLabelKey;
152  std::string fRefSamplerIndexKey;
153  std::string fRemSamplerIndexKey;
154  MHO_Tokenizer fTokenizer;
155 
162  void ConstructChannelToSamplerIDMap(std::vector< std::string >& chan_set, std::map< std::string, int >& chan2id)
163  {
164  //figure out which sampler index each channel has been assigned to
165  for(std::size_t sampler_id = 0; sampler_id < chan_set.size(); sampler_id++)
166  {
167  std::string chans = chan_set[sampler_id];
168  std::vector< std::string > split_chans = SplitChannelLabels(chans);
169  for(auto it = split_chans.begin(); it != split_chans.end(); it++)
170  {
171  chan2id[*it] = (int)sampler_id;
172  }
173  }
174  }
175 
182  std::vector< std::string > SplitChannelLabels(std::string channels)
183  {
184  std::vector< std::string > split_chans;
185  //we have two possibilities, either channels are delimited by ',' or
186  //they are all single-char labels that are smashed together into a single string
187 
188  if((channels.find(',') != std::string::npos))
189  {
190  //found a comma, need to use the tokenizer
191  fTokenizer.SetDelimiter(fComma);
194  fTokenizer.SetIncludeEmptyTokensFalse();
195  fTokenizer.SetString(&channels);
196  fTokenizer.GetTokens(&split_chans);
197  }
198  else
199  {
200  //just split up the channels into individual characters
201  for(std::size_t i = 0; i < channels.size(); i++)
202  {
203  split_chans.push_back(std::string(1, channels[i]));
204  }
205  }
206  return split_chans;
207  }
208 };
209 
210 } // namespace hops
211 
212 #endif
Class MHO_SamplerLabeler.
Definition: MHO_SamplerLabeler.hh:35
virtual bool InitializeInPlace(XArrayType *in) override
Initializes in-place mapping for channel labels to sampler indices.
Definition: MHO_SamplerLabeler.hh:75
virtual bool ExecuteInPlace(XArrayType *in) override
Function ExecuteInPlace - actual implementation, map channel label (e.g. 'a', 'b',...
Definition: MHO_SamplerLabeler.hh:95
virtual bool ExecuteOutOfPlace(const XArrayType *in, XArrayType *out) override
Copies input array and executes in-place operation on output.
Definition: MHO_SamplerLabeler.hh:137
void SetReferenceStationSamplerChannelSets(const std::vector< std::string > &channel_sets)
Setter for reference station sampler channel sets.
Definition: MHO_SamplerLabeler.hh:52
virtual ~MHO_SamplerLabeler()
Definition: MHO_SamplerLabeler.hh:45
MHO_SamplerLabeler()
Definition: MHO_SamplerLabeler.hh:37
virtual bool InitializeOutOfPlace(const XArrayType *, XArrayType *) override
Initializes output array in-place from input array.
Definition: MHO_SamplerLabeler.hh:85
void SetRemoteStationSamplerChannelSets(const std::vector< std::string > &channel_sets)
Setter for remote station sampler channel sets.
Definition: MHO_SamplerLabeler.hh:62
Class MHO_Tokenizer.
Definition: MHO_Tokenizer.hh:24
void SetUseMulticharacterDelimiterFalse()
Definition: MHO_Tokenizer.cc:40
void SetIncludeEmptyTokensFalse()
Definition: MHO_Tokenizer.cc:30
void SetString(const std::string *aString)
Definition: MHO_Tokenizer.cc:65
void GetTokens(std::vector< std::string > *tokens)
Definition: MHO_Tokenizer.cc:75
void SetRemoveLeadingTrailingWhitespaceTrue()
Setter for remove leading trailing whitespace true.
Definition: MHO_Tokenizer.cc:55
void SetDelimiter(const std::string &aDelim)
Definition: MHO_Tokenizer.cc:70
Class MHO_UnaryOperator.
Definition: MHO_UnaryOperator.hh:24
Definition: MHO_ChannelLabeler.hh:17