CARMA C++
CobraIntegrationData.h
1 #ifndef COBRAINTEGRATIONDATA_H
2 #define COBRAINTEGRATIONDATA_H
3 
4 #include <vector>
5 #include <complex>
7 
8 namespace carma {
9  namespace correlator {
10  namespace lib {
11 
12  class CobraIntegrationData :
14  public:
15  CobraIntegrationData() :
16  timestamp_(0.0),
17  nauto_(0),
18  nauto_samples_(0),
19  ncross_(0),
20  ncross_samples_(0) {}
21  ~CobraIntegrationData(){}
22 
23  int size()
24  {
25  // Return the serialized size
26  // (since the underlying objects may not
27  // be initialized, use the nauto_ etc params).
28  //
29  // This is not that great....
30  //
31  // Integration timestamp
32  int size = sizeof(timestamp_);
33  // Auto correlation data
34  size += nauto_*(12 + nauto_samples_*sizeof(float));
35  // Cross correlation
36  size += ncross_*(12 + 4*ncross_samples_*sizeof(float));
37  return size;
38  }
39 
40  void mySerialize( char * const byteArray,
41  int * const offset ) const
42  {
43  pack(timestamp_, byteArray, offset);
44  int size = autodata_.size();
45  for (int i = 0; i < size; i++) {
46  //pack(&autodata_[i], byteArray, offset);
47  autodata_[i].serialize(byteArray, offset);
48  }
49  size = crossdata_.size();
50  for (int i = 0; i < size; i++) {
51  //pack(&crossdata_[i], byteArray, offset);
52  crossdata_[i].serialize(byteArray, offset);
53  }
54  }
55 
56  void deserializeVer0( const char * const byteArray,
57  int * const offset,
58  int byteArraySize )
59  {
60  unpack(timestamp_, byteArray, offset, byteArraySize);
61  int size = nauto_;
62  autodata_.resize(size);
63  for (int i = 0; i < size; i++) {
64  autodata_[i].setNumChannels(nauto_samples_);
65  autodata_[i].deserializeVer0(byteArray, offset, byteArraySize);
66  }
67  size = ncross_;
68  crossdata_.resize(size);
69  for (int i = 0; i < size; i++) {
70  crossdata_[i].setNumChannels(ncross_samples_);
71  crossdata_[i].deserializeVer0(byteArray, offset, byteArraySize);
72  }
73  }
74 
75  void deserializeVer1( const char * const byteArray,
76  int * const offset,
77  int byteArraySize )
78  {
79  unpack(timestamp_, byteArray, offset, byteArraySize);
80  int size = nauto_;
81  autodata_.resize(size);
82  for (int i = 0; i < size; i++) {
83  autodata_[i].setNumChannels(nauto_samples_);
84  autodata_[i].deserializeVer1(byteArray, offset, byteArraySize);
85  }
86  size = ncross_;
87  crossdata_.resize(size);
88  for (int i = 0; i < size; i++) {
89  crossdata_[i].setNumChannels(ncross_samples_);
90  crossdata_[i].deserializeVer1(byteArray, offset, byteArraySize);
91  }
92  }
93 
94  void deserializeSwapVer0( const char * const byteArray,
95  int * const offset,
96  int byteArraySize )
97  {
98  unpackSwap(timestamp_, byteArray, offset, byteArraySize);
99  int size = nauto_;
100  autodata_.resize(size);
101  for (int i = 0; i < size; i++) {
102  autodata_[i].setNumChannels(nauto_samples_);
103  autodata_[i].deserializeSwapVer0(byteArray,
104  offset,
105  byteArraySize);
106  }
107  size = ncross_;
108  crossdata_.resize(size);
109  for (int i = 0; i < size; i++) {
110  crossdata_[i].setNumChannels(ncross_samples_);
111  crossdata_[i].deserializeSwapVer0(byteArray,
112  offset,
113  byteArraySize);
114  }
115  }
116 
117  void deserializeSwapVer1( const char * const byteArray,
118  int * const offset,
119  int byteArraySize )
120  {
121  unpackSwap(timestamp_, byteArray, offset, byteArraySize);
122  int size = nauto_;
123  autodata_.resize(size);
124  for (int i = 0; i < size; i++) {
125  autodata_[i].setNumChannels(nauto_samples_);
126  autodata_[i].deserializeSwapVer1(byteArray,
127  offset,
128  byteArraySize);
129  }
130  size = ncross_;
131  crossdata_.resize(size);
132  for (int i = 0; i < size; i++) {
133  crossdata_[i].setNumChannels(ncross_samples_);
134  crossdata_[i].deserializeSwapVer1(byteArray,
135  offset,
136  byteArraySize);
137  }
138  }
139 
140  int getSizeInBytes() const {
141  int size = 0;
142  // Return the serialized size
143  // (since the underlying objects may not
144  // be initialized, use the nauto_ etc params).
145  //
146  // This is not that great....
147  //
148  // Integration timestamp
149  size += sizeof(timestamp_);
150  // Auto correlation data
151  size += nauto_*(12 + nauto_samples_*sizeof(float));
152  // Cross correlation
153  size += ncross_*(12 + 4*ncross_samples_*sizeof(float));
154  return size;
155  }
156 
157  // Equality operator (used when comparing objects)
158  bool operator== (const CobraIntegrationData &rhs)
159  {
160  if (timestamp_ != rhs.timestamp_) {
161  return false;
162  }
163  int size = autodata_.size();
164  for (int i = 0; i < size; i++) {
165  if (autodata_[i] != rhs.autodata_[i]) {
166  return false;
167  }
168  }
169  size = crossdata_.size();
170  for (int i = 0; i < size; i++) {
171  if (crossdata_[i] != rhs.crossdata_[i]) {
172  return false;
173  }
174  }
175  return true;
176  }
177  bool operator!= (const CobraIntegrationData &rhs)
178  {
179  return !(*this == rhs);
180  }
181 
182  // Member accessors
183  //
184  // Routines used by data readers, eg. after the format
185  // has been reconstructed via a call to deserialize.
186  //
187  double getTimestamp()
188  {return timestamp_;}
189  CobraAutoSpectra getAutoSpectra(int index)
190  {return autodata_[index];}
191  CobraCrossSpectra getCrossSpectra(int index)
192  {return crossdata_[index];}
193  int getNauto()
194  {return nauto_;}
195  int getNcross()
196  {return ncross_;}
197  int getNautoSamples()
198  {return nauto_samples_;}
199  int getNcrossSamples()
200  {return ncross_samples_;}
201 
202  // Routines used by data writers, eg. before the format
203  // has been deconstructed via a call to mySerialize.
204  //
205  void setTimestamp(double timestamp)
206  {timestamp_ = timestamp;}
207  void setAutoSpectra(CobraAutoSpectra data, unsigned int index)
208  {
209  if (index + 1 > autodata_.size()) {
210  autodata_.resize(index + 1);
211  }
212  autodata_.at(index) = data;
213  nauto_ = autodata_.size();
214  if (nauto_samples_ == 0) {
215  nauto_samples_ = data.getNumChannels();
216  }
217  }
218  void setCrossSpectra(CobraCrossSpectra data, unsigned int index)
219  {
220  if (index + 1 > crossdata_.size()) {
221  crossdata_.resize(index + 1);
222  }
223  crossdata_.at(index) = data;
224  ncross_ = crossdata_.size();
225  if (ncross_samples_ == 0) {
226  ncross_samples_ = data.getNumChannels();
227  }
228  }
229 
230  // IMPORTANT: the serialized data format does
231  // not include the number of auto and cross
232  // correlations, or samples so these parameters
233  // must be set before a call to deserialize.
234  void setNauto(int nauto)
235  {nauto_ = nauto;}
236  void setNcross(int ncross)
237  {ncross_ = ncross;}
238  void setNautoSamples(int nauto_samples)
239  {nauto_samples_ = nauto_samples;}
240  void setNcrossSamples(int ncross_samples)
241  {ncross_samples_ = ncross_samples;}
242  private:
243  double timestamp_;
244  unsigned int nauto_;
245  unsigned int nauto_samples_;
246  unsigned int ncross_;
247  unsigned int ncross_samples_;
248  std::vector <CobraAutoSpectra> autodata_;
249  std::vector <CobraCrossSpectra> crossdata_;
250  };
251  };
252  };
253 };
254 
255 #endif // COBRACORRELATIONDATA_H
256 
257 
Abstract Class used to allow object to serialize themselves into a byte array.
Definition: Serializable.h:32