CARMA C++
ArchiveReader.h
Go to the documentation of this file.
1 // $Id: ArchiveReader.h,v 1.5 2013/08/27 21:44:30 eml Exp $
2 
3 #ifndef SZA_UTIL_ARCHIVEREADER_H
4 #define SZA_UTIL_ARCHIVEREADER_H
5 
16 #include <iostream>
17 #include <vector>
18 #include <valarray>
19 
22 #include "carma/szautil/BitMask.h"
24 
25 #include "carma/szaarrayutils/arcfile.h"
26 #include "carma/szaarrayutils/arraymap.h"
27 
28 namespace sza {
29  namespace util {
30 
31  // A class for optimized reading from the archive
32 
33  class ArchiveReader {
34  public:
35 
36  //------------------------------------------------------------
37  // Define a struct that will encapsulate a data format, and
38  // its corresponding string identifier
39  //------------------------------------------------------------
40 
41  struct Format {
42  std::string format_;
43  DataType::Type type_;
44  };
45 
46  enum ArchiveTransposeType {
47  NONE = 0x0,
48  FIRST = 0x2,
49  LAST = 0x4,
50  };
51 
52  //------------------------------------------------------------
53  // Define a struct that will encapsulate a single monitor file
54  //------------------------------------------------------------
55 
56  struct ArchiveFile {
57  ArcTimeStamp ts_; // The timestamp corresponding to the start
58  // of this file
59  std::string name_; // The full name (including path) of this file
60 
61  unsigned startFrame_; // The index of the first frame to read in
62  // this file
63  unsigned stopFrame_; // The number of frames to read from this file
64 
65  unsigned currFrame_;
66 
67 
68  ArchiveFile() {
69  startFrame_ = 0;
70  stopFrame_ = 0;
71  currFrame_ = 0;
72  }
73 
74  bool atEnd() {
75  return (currFrame_ > stopFrame_);
76  }
77 
78  void incr() {
79  currFrame_++;
80  }
81  };
82 
83  //------------------------------------------------------------
84  // Define a struct that will encapsulate a register to read
85  //------------------------------------------------------------
86 
87  struct ArchiveRegister {
88 
89  // The native data type of this register
90 
91  DataType::Type type_;
92 
93  // The requested output data type for this register
94 
95  DataType::Type outputType_;
96 
97  // If printing, the width and precision with which to print
98 
99  int width_;
100  int prec_;
101 
102  ArrRegMap* arregmap_;
103  RegMapBoard* board_;
104  RegMapBlock* block_;
105 
106  // A vector of byte ranges in this register selection
107 
108  std::vector<Range<unsigned> > byteRanges_;
109 
110  // A vector of slot ranges corresponding to this register
111  // selection
112 
113  std::vector<Range<unsigned> > slotRanges_;
114 
115  // A vector of element ranges corresponding to this register
116  // selection
117 
118  std::vector<Range<unsigned> > elementRanges_;
119 
120  // The number of bytes, summed over all elements in the
121  // specified byte ranges
122 
123  unsigned nTotalBytes_;
124 
125  // The total number of elements
126 
127  unsigned nEl_;
128 
129  // The number of elements in the fastest-changing dimension of
130  // this register
131 
132  unsigned nEl1_;
133 
134  // Which aspect of this register was requested?
135 
136  RegAspect aspect_;
137 
138  // An array into which the raw bytes for all byte ranges of
139  // this register will be copied
140 
141  std::valarray<unsigned char> buf_;
142 
143  // External pointers to memory into which the calibrated
144  // values will be written
145 
146  void* cRePtr_;
147  void* cImPtr_;
148  void* args_;
149 
150  // Arrays of input/output indices
151 
152  std::valarray<unsigned int> inInds_;
153  std::valarray<unsigned int> outInds_;
154 
155  // A function for converting values read from the archive to
156  // the output type
157 
158  ARC_CONV_FN(*convFn_);
159  ARC_PRINT_FN(*printFn_);
160 
161  // An array of register calibration factors, one for each
162  // element in this register
163 
164  std::vector<RegCal::RegCalSlot> regCalSlots_;
165 
166  // If true, transpose the indices of this register
167 
168  ArchiveTransposeType transpose_;
169 
170  // A string to hold the converted value
171 
172  std::ostringstream strVal_;
173 
174  // Convert from input values to output values
175 
176  void convertVals(unsigned iFrame, unsigned nFrame);
177 
178  // Print input values
179 
180  void printVals(std::ostringstream& os);
181 
182  void printVals();
183 
184  // Copy constructors & operators to be sure we don't have
185  // undefined behavior by not explicitly defining these
186 
187  ArchiveRegister();
188 
189  ArchiveRegister(RegDescription& desc);
190 
191  void initialize(RegDescription& desc, DataType::Type outputType,
192  ArchiveTransposeType transpose, int width, int prec,
193  bool convert=true, bool read=false);
194 
195  void operator=(RegDescription& desc);
196 
197  ArchiveRegister(const ArchiveRegister& reg);
198 
199  ArchiveRegister(ArchiveRegister& reg);
200 
201  void operator=(const ArchiveRegister& reg);
202 
203  void operator=(ArchiveRegister& reg);
204 
205  // External method for setting output indices
206 
207  void setInputIndices(std::valarray<unsigned int>& inds);
208 
209  void setOutputIndices(std::valarray<unsigned int>& inds);
210 
211  void setExternalMemory(void* rePtr, void* imPtr, void* args);
212 
213  };
214 
215  friend std::ostream& operator<<(std::ostream& os,
216  ArchiveReader::ArchiveRegister& reg);
217 
218  //------------------------------------------------------------
219  // A struct to encapsulate a single byte range to read out
220  // of the archive
221  //------------------------------------------------------------
222 
223  struct ArchiveByteRange {
224  ArchiveRegister* reg_;
225  unsigned char* ptr_;
226  unsigned byteOffsetFromStartOfFrame_;
227  unsigned nByte_;
228  };
229 
230  //------------------------------------------------------------
231  // Methods of the ArchiveReader class
232  //------------------------------------------------------------
233 
237  void initialize(bool memMap, bool convert, bool read);
238 
239  ArchiveReader(bool memMap=false, bool convert=true, bool read=false);
240  ArchiveReader(std::string arcDir, std::string calFile,
241  std::string startUtc, std::string stopUtc,
242  bool memMap=false, bool convert=true, bool read=false);
243 
247  ArchiveReader(const ArchiveReader& objToBeCopied);
248 
252  ArchiveReader(ArchiveReader& objToBeCopied);
253 
257  void operator=(const ArchiveReader& objToBeAssigned);
258 
262  void operator=(ArchiveReader& objToBeAssigned);
263 
267  friend std::ostream& operator<<(std::ostream& os, ArchiveReader& obj);
268 
272  virtual ~ArchiveReader();
273 
274  //-----------------------------------------------------------------------
275  // User Methods
276  //-----------------------------------------------------------------------
277 
278  // Set the top-level archive directory. Note that this class
279  // will read files in subdirectories below the top-level
280 
281  void setArchiveDirectory(std::string arcDir);
282 
283  void getFileList();
284 
285  unsigned countFrames();
286 
287  unsigned readTimeStamps();
288 
289  void setCalFile(std::string calFile);
290 
291  void setDates(std::string startUtc, std::string stopUtc);
292 
293  bool readNextFrame();
294 
295  bool updateArrayMap();
296 
297  void updateRegisterCalibration();
298 
299  void updateRegSelection();
300 
301  void updateValidityRegister();
302 
303  void updateRegBufPtrCache(unsigned nByteRanges);
304 
305  void readFirstArrayMap();
306 
307  void printAddresses();
308 
309  bool regIsValid(ArchiveReader::ArchiveRegister& reg, unsigned iEl);
310 
311  private:
312 
313  bool convert_;
314  bool read_;
315 
316  static ArchiveReader::Format formats_[];
317  static int nFormat_;
318 
319  unsigned iFrame_;
320  unsigned nFrame_;
321 
322  ArchiveRegister* validityRegister_;
323  BitMask validityBitMask_;
324 
325  TimeVal tmpTimeVal_;
326  TimeVal tmpDiff_;
327  RegDate tmpRegDate_;
328 
329  public:
330 
331  RegCal* regCal_;
332 
333  private:
334  std::string calFile_;
335  bool haveCalFile_;
336 
337  std::vector<ArchiveByteRange> byteRanges_;
338 
339  ArrayMap* arrayMap_;
340 
341  bool memMap_;
342  std::string arcDir_;
343  bool arcDirIsSet_;
344 
345  std::string startUtc_;
346  std::string stopUtc_;
347  bool datesAreSet_;
348 
349  std::vector<ArchiveReader::ArchiveFile> fileList_;
350  std::vector<ArchiveReader::ArchiveFile>::iterator currFile_;
351  unsigned nFile_;
352 
353  ArchiveFileHandler handler_;
354 
355  // The array of registers and format specifiers input by the user
356 
357  public:
358 
359  std::vector<bool> regSpecValid_;
360  std::vector<std::string> regSpecs_;
361  std::vector<std::string> formatSpecs_;
362  std::vector<ArchiveTransposeType> transposeTypes_;
363  std::vector<int> widths_;
364  std::vector<int> precs_;
365 
366  // The array of registers and associated information derived
367  // from the above. Note, however, that these arrays are not in
368  // general of the same length as the above, since some registers
369  // may have errors in the specification, or may not exist in
370  // the register map
371 
372  void printRegsSize();
373 
374  std::vector<ArchiveRegister>& getRegs();
375  std::vector<RegDescription>& getRegDescs();
376 
377  public:
378 
379  std::vector<ArchiveRegister> regs_;
380  std::vector<DataType::Type> formatTypes_;
381  std::vector<RegDescription> regDescs_;
382 
383  private:
384 
385  static bool compArchiveFile(ArchiveReader::ArchiveFile f1,
386  ArchiveReader::ArchiveFile f2);
387 
388  static bool compArchiveByteRange(ArchiveByteRange r1, ArchiveByteRange r2);
389 
390  static DataType::Type parseFormat(std::string format);
391 
392  bool unique(ArchiveRegister& reg);
393 
394  void checkFirstFile();
395 
396  public:
397 
398  void addRegister(std::string regSpec);
399  void addRegisterOnly(std::string regSpec);
400 
401  bool stageNextFile();
402 
403  void resetToBeginning();
404 
405  bool advanceFile();
406 
407  void iterateFiles();
408 
409  void readRegs();
410  void printRegs(std::ostringstream& os, TimeVal* refVal = 0);
411 
412  // Convenience methods for external use of this object
413 
414  std::vector<RegDescription> selectedRegs();
415 
416  std::vector<DataType::Type> selectedFormats();
417 
418  }; // End class ArchiveReader
419 
420  } // End namespace util
421 } // End namespace sza
422 
423 
424 
425 #endif // End #ifndef SZA_UTIL_ARCHIVEREADER_H
Tagged: Tue Mar 6 16:39:02 PST 2012.
Tagged: Sat Jan 31 23:42:25 NZDT 2009.
Tagged: Mon Sep 27 21:37:46 UTC 2004.
Tagged: Thu Jan 29 23:10:35 NZDT 2009.