CARMA C++
CobraCorrelationData.h
1 #ifndef COBRACORRELATIONDATA_H
2 #define COBRACORRELATIONDATA_H
3 
4 #include <vector>
5 #include <complex>
7 
8 namespace carma {
9  namespace correlator {
10  namespace lib {
11 
12  class CobraCorrelationData :
14  public:
15  CobraCorrelationData() :
16  status_(0), mode_(0), nint_(0) {}
17  ~CobraCorrelationData(){}
18 
19  int size() const
20  {
21  // Return the serialized size
22  return (3 * 4);
23  }
24 
25  int getSizeInBytes() const {
26  return size();
27  }
28 
29  void mySerialize( char * const byteArray,
30  int * const offset ) const
31  {
32  pack(status_, byteArray, offset);
33  pack(mode_, byteArray, offset);
34  pack(nint_, byteArray, offset);
35  }
36 
37  void deserializeVer0( const char * const byteArray,
38  int * const offset,
39  int byteArraySize )
40  {
41  unpack(status_, byteArray, offset, byteArraySize);
42  unpack(mode_, byteArray, offset, byteArraySize);
43  unpack(nint_, byteArray, offset, byteArraySize);
44  }
45 
46  void deserializeVer1( const char * const byteArray,
47  int * const offset,
48  int byteArraySize )
49  {
50  unpack(status_, byteArray, offset, byteArraySize);
51  unpack(mode_, byteArray, offset, byteArraySize);
52  unpack(nint_, byteArray, offset, byteArraySize);
53  }
54 
55  void deserializeSwapVer0( const char * const byteArray,
56  int * const offset,
57  int byteArraySize )
58  {
59  unpackSwap(status_, byteArray, offset, byteArraySize);
60  unpackSwap(mode_, byteArray, offset, byteArraySize);
61  unpackSwap(nint_, byteArray, offset, byteArraySize);
62  }
63 
64  void deserializeSwapVer1( const char * const byteArray,
65  int * const offset,
66  int byteArraySize )
67  {
68  unpackSwap(status_, byteArray, offset, byteArraySize);
69  unpackSwap(mode_, byteArray, offset, byteArraySize);
70  unpackSwap(nint_, byteArray, offset, byteArraySize);
71  }
72 
73  // Equality operator (used when comparing objects)
74  bool operator== (const CobraCorrelationData &rhs)
75  {
76  return ((status_ == rhs.status_) &&
77  (mode_ == rhs.mode_) &&
78  (nint_ == rhs.nint_));
79  }
80  bool operator!= (const CobraCorrelationData &rhs)
81  {
82  return !(*this == rhs);
83  }
84 
85  // Member accessors
86  //
87  // Routines used by data readers, eg. after the format
88  // has been reconstructed via a call to deserialize.
89  //
90  int getStatus()
91  {return status_;}
92  int getMode()
93  {return mode_;}
94  int getNint()
95  {return nint_;}
96 
97  // Routines used by data writers, eg. before the format
98  // has been deconstructed via a call to mySerialize.
99  //
100  void setStatus(int status)
101  {status_ = status;}
102  void setMode(int mode)
103  {mode_ = mode;}
104  void setNint(int nint)
105  {nint_ = nint;}
106  private:
107  int status_;
108  int mode_;
109  int nint_;
110  };
111 
112  // Auto and cross data is stored differently, so
113  // they have different deserialize methods, and
114  // hence separate classes are required.
115  //
116  class CobraAutoSpectra :
117  public CobraCorrelationData {
118  public:
119  CobraAutoSpectra() : nchannels_(0) {}
120  ~CobraAutoSpectra(){}
121 
122  int size () const
123  {
124  // Return the serialized size
125  int size = CobraCorrelationData::size();
126  size += nchannels_*sizeof(float);
127  return size;
128  }
129 
130  int getSizeInBytes() const {
131  return size();
132  }
133 
134  void mySerialize( char * const byteArray,
135  int * const offset ) const
136  {
137  // Serialize the baseclass
138  CobraCorrelationData::mySerialize(byteArray, offset);
139 
140  // Then the data
141  pack(data_, byteArray, offset);
142  }
143 
144  void deserializeVer0( const char * const byteArray,
145  int * const offset,
146  int byteArraySize )
147  {
148  // Deserialize the baseclass
149  CobraCorrelationData::deserializeVer0(byteArray,
150  offset,
151  byteArraySize);
152 
153  // TODO: modify the serialized object to include
154  // the vector size prior to the data.
155  data_.resize(nchannels_);
156  unpack(data_, byteArray, offset, byteArraySize);
157  }
158 
159  void deserializeVer1( const char * const byteArray,
160  int * const offset,
161  int byteArraySize )
162  {
163  // Deserialize the baseclass
164  CobraCorrelationData::deserializeVer1(byteArray,
165  offset,
166  byteArraySize);
167 
168  // TODO: modify the serialized object to include
169  // the vector size prior to the data.
170  data_.resize(nchannels_);
171  unpack(data_, byteArray, offset, byteArraySize);
172  }
173 
174  void deserializeSwapVer0( const char * const byteArray,
175  int * const offset,
176  int byteArraySize )
177  {
178  // Deserialize the baseclass
179  CobraCorrelationData::deserializeSwapVer0(byteArray,
180  offset,
181  byteArraySize);
182 
183  // TODO: modify the serialized object to include
184  // the vector size prior to the data.
185  data_.resize(nchannels_);
186  unpackSwap(data_, byteArray, offset, byteArraySize);
187  }
188 
189  void deserializeSwapVer1( const char * const byteArray,
190  int * const offset,
191  int byteArraySize )
192  {
193  // Deserialize the baseclass
194  CobraCorrelationData::deserializeSwapVer1(byteArray,
195  offset,
196  byteArraySize);
197 
198  // TODO: modify the serialized object to include
199  // the vector size prior to the data.
200  data_.resize(nchannels_);
201  unpackSwap(data_, byteArray, offset, byteArraySize);
202  }
203 
204  // Equality operator (used when comparing objects)
205  bool operator== (const CobraAutoSpectra &rhs)
206  {
207  // Check the baseclass
208  if (CobraCorrelationData::operator==(rhs) == false) {
209  return false;
210  }
211  return (data_ == rhs.data_);
212  }
213  bool operator!= (const CobraAutoSpectra &rhs)
214  {
215  return !(*this == rhs);
216  }
217 
218  // Member accessors
219  //
220  // Routines used by data readers, eg. after the format
221  // has been reconstructed via a call to deserialize.
222  //
223  std::vector<float> getData()
224  {return data_;}
225 
226  int getNumChannels()
227  {return nchannels_;}
228 
229  // Routines used by data writers, eg. before the format
230  // has been deconstructed via a call to mySerialize.
231  //
232  void setData(std::vector<float> data)
233  {
234  data_ = data;
235  if (nchannels_ == 0) {
236  nchannels_ = data_.size();
237  }
238  }
239 
240  // IMPORTANT: the current serialized data format
241  // does not include the vector length in the serialized
242  // format, so this function needs to be called prior
243  // to deserialization.
244  void setNumChannels(int num)
245  {nchannels_ = num;}
246 
247  private:
248  int nchannels_;
249  std::vector<float> data_;
250  };
251 
252  class CobraCrossSpectra :
253  public CobraCorrelationData {
254  public:
255  CobraCrossSpectra() : nchannels_(0) {}
256  ~CobraCrossSpectra(){}
257 
258  int size () const
259  {
260  // Return the serialized size
261  // (two sidebands, complex data)
262  int size = CobraCorrelationData::size();
263  size += 2*nchannels_*2*sizeof(float);
264  return size;
265  }
266 
267  int getSizeInBytes() const {
268  return size();
269  }
270 
271  void mySerialize( char * const byteArray,
272  int * const offset ) const
273  {
274  // Serialize the baseclass
275  CobraCorrelationData::mySerialize(byteArray, offset);
276 
277  // Then the LSB and USB
278  for (int i = 0; i < 2; i++) {
279  pack(data_[i], byteArray, offset);
280  }
281  }
282 
283  void deserializeVer0( const char * const byteArray,
284  int * const offset,
285  int byteArraySize)
286  {
287  // Deserialize the baseclass
288  CobraCorrelationData::deserializeVer0(byteArray,
289  offset,
290  byteArraySize);
291 
292  data_.resize(2);
293  for (int i = 0; i < 2; i++) {
294  data_[i].resize(nchannels_); // vector length
295  unpack(data_[i], byteArray, offset, byteArraySize);
296  }
297  }
298 
299  void deserializeVer1( const char * const byteArray,
300  int * const offset,
301  int byteArraySize)
302  {
303  // Deserialize the baseclass
304  CobraCorrelationData::deserializeVer1(byteArray,
305  offset,
306  byteArraySize);
307 
308  data_.resize(2);
309  for (int i = 0; i < 2; i++) {
310  data_[i].resize(nchannels_); // vector length
311  unpack(data_[i], byteArray, offset, byteArraySize);
312  }
313  }
314 
315  void deserializeSwapVer0( const char * const byteArray,
316  int * const offset,
317  int byteArraySize)
318  {
319  // Deserialize the baseclass
320  CobraCorrelationData::deserializeSwapVer0(byteArray,
321  offset,
322  byteArraySize);
323 
324  data_.resize(2);
325  for (int i = 0; i < 2; i++) {
326  data_[i].resize(nchannels_); // vector length
327  unpackSwap(data_[i], byteArray, offset, byteArraySize);
328  }
329  }
330 
331  void deserializeSwapVer1( const char * const byteArray,
332  int * const offset,
333  int byteArraySize)
334  {
335  // Deserialize the baseclass
336  CobraCorrelationData::deserializeSwapVer1(byteArray,
337  offset,
338  byteArraySize);
339 
340  data_.resize(2);
341  for (int i = 0; i < 2; i++) {
342  data_[i].resize(nchannels_); // vector length
343  unpackSwap(data_[i], byteArray, offset, byteArraySize);
344  }
345  }
346 
347  // Equality operator (used when comparing objects)
348  bool operator== (const CobraCrossSpectra &rhs)
349  {
350  // Check the baseclass
351  if (CobraCorrelationData::operator==(rhs) == false) {
352  return false;
353  }
354  return (data_ == rhs.data_);
355  }
356  bool operator!= (const CobraCrossSpectra &rhs)
357  {
358  return !(*this == rhs);
359  }
360 
361  // Member accessors
362  //
363  // Routines used by data readers, eg. after the format
364  // has been reconstructed via a call to deserialize.
365  //
366  std::vector<std::complex<float> > getData(unsigned int num)
367  {
368  // Let the vector do the range checking
369  return data_.at(num);
370  }
371 
372  int getNumChannels()
373  {return nchannels_;}
374 
375  // Routines used by data writers, eg. before the format
376  // has been deconstructed via a call to mySerialize.
377  //
378  void setData(
379  std::vector<std::complex<float> > data,
380  unsigned int num)
381  {
382  // Only support LSB and USB (0 and 1)
383  if (num > 1) {
384  return;
385  }
386  if (num + 1 > data_.size()) {
387  data_.resize(num + 1);
388  }
389  data_.at(num) = data;
390  if (nchannels_ == 0) {
391  nchannels_ = data.size();
392  }
393  }
394 
395  // IMPORTANT: the current serialized data format
396  // does not include the vector length in the serialized
397  // format, so this function needs to be called prior
398  // to deserialization.
399  void setNumChannels(int num)
400  {nchannels_ = num;}
401 
402  private:
403  int nchannels_;
404 
405  // Two sidebands of complex data
406  std::vector<std::vector<std::complex<float> > > data_;
407  };
408  };
409  };
410 };
411 
412 #endif // COBRACORRELATIONDATA_H
413 
414 
Abstract Class used to allow object to serialize themselves into a byte array.
Definition: Serializable.h:32
modeType getMode(idType id)
Extract the address mode from a 29 bit can id.