CARMA C++
RegCal.h
Go to the documentation of this file.
1 #ifndef SZA_UTIL_REGCAL_H
2 #define SZA_UTIL_REGCAL_H
3 
15 
16 #include "carma/szaarrayutils/regset.h"
17 #include "carma/szaarrayutils/regdata.h"
18 #include "carma/szaarrayutils/input.h"
19 
20 #include <string>
21 #include <vector>
22 
23 namespace sza {
24  namespace util {
25 
26  class MonitorDataType;
27 
28  /*
29  * This module provides facilities for converting selected 32-bit int
30  * archived data to physical double precision values. Calibration
31  * offsets and scale factors to be applied during this process are
32  * taken from a RegCal object. Such objects are initialized from text
33  * input streams. In addition, integrated registers are divided by the
34  * value of the frame.nsnap register to yield mean values per
35  * snapshot. This requires that the frame.nsnap register slot contain
36  * valid data (for this reason MonitorStream objects silently select
37  * this register if it isn't selected for monitoring).
38  *
39  * Some archive values such as bit-masks are inherently 32 bit
40  * integers and will presumably be promptly returned to that form.
41  * Assuming that the calibration file doesn't specify a non-unity
42  * scale factor or a non-zero offset, we can guarantee that the
43  * conversion to and from a double is lossless because ANSI/ISO C
44  * mandates that a double must be able to exactly represent any
45  * number of at least 10 decimal digits.
46  *
47  * The calibration of scalar registers is implemented as:
48  *
49  * reg[i] = offset + factor * reg[i].
50  *
51  * The calibration of complex registers is implemented as:
52  *
53  * real = reg[i]
54  * imag = reg[i+1]
55  * reg[i] = offset + factor * real;
56  * reg[i+1] = offset + factor * (imag/imag_gain + real*sin(phi))/cos(phi);
57  */
58  class RegCal {
59  public:
60 
61  //------------------------------------------------------------
62  // RegCal struct
63  //------------------------------------------------------------
64 
70  struct RegCalData {
71  unsigned nSlot_; // The number of elements in slots_[]
72  std::vector<double> slots_; // The array of monitored registers
73  bool empty_; // True until the first successful
74  // call to calibrateRegData()
75 
76  inline bool isEmpty() {
77  return empty_;
78  }
79 
80  // The following functions can be used to retrieve calibrated
81  // data from a RegCalData array. The reg argument should be a
82  // register specification that convers the register index
83  // range index..index+n-1.
84 
85  void getCalChar(RegDescription* reg, char* data, CoordRange* range=0);
86  void getCalUchar(RegDescription* reg, unsigned char* data, CoordRange* range=0);
87  void getCalUshort(RegDescription* reg, unsigned short* data, CoordRange* range=0);
88  void getCalShort(RegDescription* reg, short* data, CoordRange* range=0);
89  void getCalUint(RegDescription* reg, unsigned* data, CoordRange* range=0);
90  void getCalInt(RegDescription* reg, int* data, CoordRange* range=0);
91  void getCalUlong(RegDescription* reg, unsigned long *data, CoordRange* range=0);
92  void getCalLong(RegDescription* reg, long* data, CoordRange* range=0);
93  void getCalFloat(RegDescription* reg, float* data, CoordRange* range=0);
94  void getCalDouble(RegDescription* reg, double* data, CoordRange* range=0);
95  void getCalDate(RegDescription* reg, sza::util::RegDate::Data* data, CoordRange* range=0);
96  void getCalComplexFloat(RegDescription* reg,
97  sza::util::Complex<float>::Data* data,
98  CoordRange* range=0);
99 
100  void getCalFloat(RegDescription* reg, sza::util::MonitorDataType* data, sza::util::RegAxisRange& range);
101  void getCalDouble(RegDescription* reg, sza::util::MonitorDataType* data, sza::util::RegAxisRange& range);
102  void getCalUshort(RegDescription* reg, sza::util::MonitorDataType* data, sza::util::RegAxisRange& range);
103  void getCalShort(RegDescription* reg, sza::util::MonitorDataType* data, sza::util::RegAxisRange& range);
104  void getCalUint(RegDescription* reg, sza::util::MonitorDataType* data, sza::util::RegAxisRange& range);
105  void getCalInt(RegDescription* reg, sza::util::MonitorDataType* data, sza::util::RegAxisRange& range);
106  void getCalChar(RegDescription* reg, sza::util::MonitorDataType* data, sza::util::RegAxisRange& range);
107  void getCalUchar(RegDescription* reg, sza::util::MonitorDataType* data, sza::util::RegAxisRange& range);
108  void getCalUlong(RegDescription* reg, sza::util::MonitorDataType* data, sza::util::RegAxisRange& range);
109  void getCalLong(RegDescription* reg, sza::util::MonitorDataType* data, sza::util::RegAxisRange& range);
110  void getCalDate(RegDescription* reg, sza::util::MonitorDataType* data, sza::util::RegAxisRange& range);
111  void getCalComplexFloat(RegDescription* reg, sza::util::MonitorDataType* data,
112  sza::util::RegAxisRange& range);
113 
114  void getCalDouble(RegDescription* reg, double* data, RegAxisRange& iRange);
115 
116  void getCalString(RegDescription* reg, char *string, unsigned length);
117 
118  // An argument validation function used by the getCal_...()
119  // functions.
120 
121  void checkCalArgs(std::string caller, RegDescription* reg,
122  void* data, CoordRange* range=0);
123 
124  RegCalData(ArrayMap* arrayMap, bool archivedOnly=false);
125  ~RegCalData();
126  };
127 
128  //------------------------------------------------------------
129  // RegCalSlot struct
130  //------------------------------------------------------------
131 
144  struct RegCalSlot {
145 
146  double offset_; // The calibration offset of the
147  // register
148  double factor_; // The calibration multiplier of the
149  // register
150 
151  // The following are only relevant to complex registers
152 
153  double imagGain_; // The gain of the imaginary channel
154  // wrt the real
155  double sinPhi_,cosPhi_; // Sin and cos of the imaginary
156  // channel phase offset
157 
158  // Constructor
159 
160  RegCalSlot();
161 
162  // Destructor
163 
164  ~RegCalSlot();
165 
166  // Reset members to default values
167 
168  void reset();
169 
170  };
171 
172  //------------------------------------------------------------
173  // RegCal class methods
174  //------------------------------------------------------------
175 
179  RegCal(ArrayMap* arrayMap=NULL, bool archivedOnly=false);
180 
184  RegCal(RegCal& regCal);
185 
189  virtual ~RegCal();
190 
196  void loadCalFile(std::string dir, std::string name, bool doThrow=true);
197 
201  void loadCalStream(InputStream *stream, bool doThrow=true);
202 
203  /*
204  * Calibrate selected registers while copying them from a raw archive
205  * array of 32-bit unsigned integers to a parallel double precision array.
206  */
207  void calibrateRegData(RegisterSet* registerSet,
208  RegRawData* raw);
209 
210  void calibrateRegData(RegisterSet& regset,
211  ArrayDataFrameManager& fm);
212 
213  void calibrateRegData(RegSet* registerSet,
214  ArrayDataFrameManager* fm);
215 
216  /*
217  * Reset all calibration multipliers to 1.0 and zero all calibration
218  * offsets.
219  */
220  void reset();
221 
225  void printCalFactors(std::vector<RegDescription>& regs);
226 
230  inline RegCal::RegCalData* calData() {
231  return calData_;
232  }
233 
234  // Raw access to the slot pointer
235 
236  double* getSlotPtr(unsigned iSlot);
237 
238  // Raw access to the calibration information
239 
240  RegCal::RegCalSlot getRegCalSlot(unsigned iSlot);
241 
242  private:
243 
244  // An array map
245 
246  ArrayMapBase arrayMapBase_;
247  ArrayMap* arrayMap_;
248 
249  // True if this class is managing cal factors for archived
250  // register only, false if for the whole array map
251 
252  bool archivedOnly_;
253 
254  unsigned int nSlot_; // The number of register calibration slots
255  std::vector<RegCalSlot> slots_; // An array of nslot sets of
256  // calibration parameters
257  RegCalData* calData_; // The calibrated register data
258  int nsnapSlot_; // The register slot of the nsnap register
259  int nsnapByteOffset_; // The byte offset in the array map of the
260  // nsnap register
261 
266  void readCalGroup(InputStream* stream, std::vector<RegDescription>& regs);
267 
273  void skipCalGroup(InputStream* stream);
274 
279  void readCalRecord(RegMapBlock *block,
280  InputStream *stream,
281  double pars[4]);
282 
287  void installCalPars(double pars[4], RegMapBlock* blk,
288  unsigned ia, unsigned ib);
289 
290  }; // End class RegCal
291 
292  } // End namespace util
293 } // End namespace sza
294 
295 
296 
297 #endif // End #ifndef SZA_UTIL_REGCAL_H
The following structure contains a double precision array having the same dimension as an archive fra...
Definition: RegCal.h:70
Tagged: Mon Sep 27 21:37:46 UTC 2004.
The calibration of a scalar register is:
Definition: RegCal.h:144
Tagged: Wed Oct 6 11:04:54 PDT 2004.
Tagged: Wed Oct 6 11:00:37 PDT 2004.
A class for iterating over slot ranges specified in a CoordRange object.
Definition: RegAxisRange.h:27
Tagged: Fri Sep 17 15:51:07 PDT 2004.