My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
ArchiveReader.h
Go to the documentation of this file.
1 // $Id: ArchiveReader.h,v 1.5 2011/08/29 22:35:22 sjcm Exp $
2 
3 #ifndef GCP_UTIL_ARCHIVEREADER_H
4 #define GCP_UTIL_ARCHIVEREADER_H
5 
16 #include <iostream>
17 #include <vector>
18 #include <valarray>
19 
20 #include "gcp/util/common/ArchiveConvFn.h"
21 #include "gcp/util/common/ArchiveFileHandler.h"
22 #include "gcp/util/common/RegDescription.h"
23 
24 #include "gcp/control/code/unix/libunix_src/common/arcfile.h"
25 #include "gcp/control/code/unix/libunix_src/common/arraymap.h"
26 
27 namespace gcp {
28  namespace util {
29 
30  // A class for optimized reading from the archive
31 
32  class ArchiveReader {
33  public:
34 
35  //------------------------------------------------------------
36  // Define a struct that will encapsulate a data format, and
37  // its corresponding string identifier
38  //------------------------------------------------------------
39 
40  struct Format {
41  std::string format_;
42  DataType::Type type_;
43  };
44 
45  enum ArchiveTransposeType {
46  NONE = 0x0,
47  FIRST = 0x2,
48  LAST = 0x4,
49  };
50 
51  //------------------------------------------------------------
52  // Define a struct that will encapsulate a single monitor file
53  //------------------------------------------------------------
54 
55  struct ArchiveFile {
56  ArcTimeStamp ts_; // The timestamp corresponding to the start
57  // of this file
58  std::string name_; // The full name (including path) of this file
59 
60  unsigned startFrame_; // The index of the first frame to read in
61  // this file
62  unsigned stopFrame_; // The number of frames to read from this file
63 
64  unsigned currFrame_;
65 
66 
67  ArchiveFile() {
68  startFrame_ = 0;
69  stopFrame_ = 0;
70  currFrame_ = 0;
71  }
72 
73  bool atEnd() {
74  return (currFrame_ > stopFrame_);
75  }
76 
77  void incr() {
78  currFrame_++;
79  }
80  };
81 
82  //------------------------------------------------------------
83  // Define a struct that will encapsulate a register to read
84  //------------------------------------------------------------
85 
86  struct ArchiveRegister {
87 
88  // The native data type of this register
89 
90  DataType::Type type_;
91 
92  // The requested output data type for this register
93 
94  DataType::Type outputType_;
95 
96  // If printing, the width and precision with which to print
97 
98  int width_;
99  int prec_;
100 
101  ArrRegMap* arregmap_;
102  RegMapBoard* board_;
103  RegMapBlock* block_;
104 
105  // A vector of byte ranges in this register selection
106 
107  std::vector<Range<unsigned> > byteRanges_;
108 
109  // A vector of slot ranges corresponding to this register
110  // selection
111 
112  std::vector<Range<unsigned> > slotRanges_;
113 
114  // The number of bytes, summed over all elements in the
115  // specified byte ranges
116 
117  unsigned nTotalBytes_;
118 
119  // The total number of elements
120 
121  unsigned nEl_;
122 
123  // The number of elements in the fastest-changing dimension of
124  // this register
125 
126  unsigned nEl1_;
127 
128  // Which aspect of this register was requested?
129 
130  RegAspect aspect_;
131 
132  // An array into which the raw bytes for all byte ranges of
133  // this register will be copied
134 
135  std::valarray<unsigned char> buf_;
136 
137  // External pointers to memory into which the calibrated
138  // values will be written
139 
140  void* cRePtr_;
141  void* cImPtr_;
142  void* args_;
143 
144  // Arrays of input/output indices
145 
146  std::valarray<unsigned int> inInds_;
147  std::valarray<unsigned int> outInds_;
148 
149  // A function for converting values read from the archive to
150  // the output type
151 
152  ARC_CONV_FN(*convFn_);
153  ARC_PRINT_FN(*printFn_);
154 
155  // An array of register calibration factors, one for each
156  // element in this register
157 
158  std::vector<RegCal::RegCalSlot> regCalSlots_;
159 
160  // If true, transpose the indices of this register
161 
162  ArchiveTransposeType transpose_;
163 
164  // A string to hold the converted value
165 
166  std::ostringstream strVal_;
167 
168  // Convert from input values to output values
169 
170  void convertVals(unsigned iFrame, unsigned nFrame) {
171  convFn_(&buf_[0], cRePtr_, cImPtr_, args_, &regCalSlots_[0], nEl_, &inInds_[0],
172  &outInds_[0],
173  (transpose_ == ArchiveReader::LAST) ? iFrame*nEl_ :
174  (transpose_ == ArchiveReader::FIRST ? iFrame*nEl1_ : iFrame));
175  }
176 
177  // Print input values
178 
179  void printVals(std::ostringstream& os) {
180  printFn_(&buf_[0], os, args_, &regCalSlots_[0], nEl_, width_, prec_);
181  }
182 
183  void printVals() {
184  strVal_.str("");
185  printFn_(&buf_[0], strVal_, args_, &regCalSlots_[0], nEl_, width_, prec_);
186  }
187 
188  // Copy constructors & operators to be sure we don't have
189  // undefined behavior by not explicitly defining these
190 
191  ArchiveRegister() {
192  nEl_ = 0;
193  nEl1_ = 0;
194  arregmap_ = 0;
195  board_ = 0;
196  block_ = 0;
197  convFn_ = 0;
198  printFn_ = 0;
199  cRePtr_ = 0;
200  cImPtr_ = 0;
201  args_ = 0;
202  transpose_ = ArchiveReader::NONE;
203  width_ = 10;
204  prec_ = 4;
205  }
206 
208  *this = desc;
209  }
210 
211  void initialize(RegDescription& desc, DataType::Type outputType,
212  ArchiveTransposeType transpose, int width, int prec,
213  bool convert=true, bool read=false) {
214 
215  *this = desc;
216  outputType_ = outputType;
217  transpose_ = transpose;
218  width_ = width;
219  prec_ = prec;
220 
221  try {
222 
223  if(convert) {
224  ArchiveConvFn::setConvFn(type_, outputType_, &convFn_);
225 
226  if(convFn_ == 0) {
227  ReportError("No conv function for register: " << desc);
228  ThrowError("No conv function for register: " << desc);
229  }
230 
231  } else {
232  ArchiveConvFn::setPrintFn(type_, outputType_, &printFn_);
233 
234  if(printFn_ == 0) {
235  ReportError("No print function for register: " << desc);
236  ThrowError("No print function for register: " << desc);
237  }
238 
239  }
240 
241  } catch(Exception& err) {
242 
243  ReportError(err.what() << " (" << desc << ")");
244  ThrowError(err.what() << " (" << desc << ")");
245 
246  }
247  }
248 
249  void operator=(RegDescription& desc) {
250  nEl_ = desc.nEl();
251  nEl1_ = desc.axes().nEl(desc.axes().nAxis()-1);
252  arregmap_ = desc.regMap();
253  board_ = arregmap_->regmap->boards_[desc.iBoard()];
254  block_ = board_->blocks[desc.iBlock()];
255  type_ = DataType::typeOf(block_);
256  byteRanges_ = desc.getByteRanges();
257  slotRanges_ = desc.getSlotRanges();
258  nTotalBytes_ = desc.nByte();
259  aspect_ = desc.aspect();
260 
261  buf_.resize(nTotalBytes_);
262  regCalSlots_.resize(nEl_);
263  }
264 
265  ArchiveRegister(const ArchiveRegister& reg) {
266  *this = (ArchiveRegister&)reg;
267  }
268 
270  *this = reg;
271  }
272 
273  void operator=(const ArchiveRegister& reg) {
274  *this = (ArchiveRegister&)reg;
275  }
276 
277  void operator=(ArchiveRegister& reg) {
278  nEl_ = reg.nEl_;
279  nEl1_ = reg.nEl1_;
280  arregmap_ = reg.arregmap_;
281  board_ = reg.board_;
282  block_ = reg.block_;
283  convFn_ = reg.convFn_;
284  printFn_ = reg.printFn_;
285  cRePtr_ = reg.cRePtr_;
286  cImPtr_ = reg.cImPtr_;
287  args_ = reg.args_;
288 
289  type_ = reg.type_;
290  byteRanges_ = reg.byteRanges_;
291  slotRanges_ = reg.slotRanges_;
292  nTotalBytes_ = reg.nTotalBytes_;
293  aspect_ = reg.aspect_;
294  outputType_ = reg.outputType_;
295  transpose_ = reg.transpose_;
296  width_ = reg.width_;
297  prec_ = reg.prec_;
298 
299  buf_.resize(reg.buf_.size());
300  regCalSlots_.resize(nEl_);
301 
302  }
303 
304  // External method for setting output indices
305 
306  void setInputIndices(std::valarray<unsigned int>& inds) {
307  inInds_.resize(inds.size());
308 
309  for(unsigned i=0; i < inds.size(); i++)
310  inInds_[i] = inds[i];
311  }
312 
313  void setOutputIndices(std::valarray<unsigned int>& inds) {
314  outInds_.resize(inds.size());
315 
316  for(unsigned i=0; i < inds.size(); i++)
317  outInds_[i] = inds[i];
318  }
319 
320  void setExternalMemory(void* rePtr, void* imPtr, void* args) {
321  cRePtr_ = rePtr;
322  cImPtr_ = imPtr;
323  args_ = args;
324  }
325 
326  };
327 
328  friend std::ostream& gcp::util::operator<<(std::ostream& os,
330 
331  //------------------------------------------------------------
332  // A struct to encapsulate a single byte range to read out
333  // of the archive
334  //------------------------------------------------------------
335 
337  ArchiveRegister* reg_;
338  unsigned char* ptr_;
339  unsigned byteOffsetFromStartOfFrame_;
340  unsigned nByte_;
341  };
342 
343  //------------------------------------------------------------
344  // Methods of the ArchiveReader class
345  //------------------------------------------------------------
346 
350  void initialize(bool memMap, bool convert, bool read);
351 
352  ArchiveReader(bool memMap=false, bool convert=true, bool read=false);
353  ArchiveReader(std::string arcDir, std::string calFile,
354  std::string startUtc, std::string stopUtc,
355  bool memMap=false, bool convert=true, bool read=false);
356 
360  ArchiveReader(const ArchiveReader& objToBeCopied);
361 
365  ArchiveReader(ArchiveReader& objToBeCopied);
366 
370  void operator=(const ArchiveReader& objToBeAssigned);
371 
375  void operator=(ArchiveReader& objToBeAssigned);
376 
380  friend std::ostream& operator<<(std::ostream& os, ArchiveReader& obj);
381 
385  virtual ~ArchiveReader();
386 
387  //-----------------------------------------------------------------------
388  // User Methods
389  //-----------------------------------------------------------------------
390 
391  // Set the top-level archive directory. Note that this class
392  // will read files in subdirectories below the top-level
393 
394  void setArchiveDirectory(std::string arcDir);
395 
396  void getFileList();
397 
398  unsigned countFrames();
399 
400  unsigned readTimeStamps();
401 
402  void setCalFile(std::string calFile);
403 
404  void setDates(std::string startUtc, std::string stopUtc);
405 
406  bool readNextFrame();
407 
408  bool updateArrayMap();
409 
411 
412  void updateRegSelection();
413 
414  void updateRegBufPtrCache(unsigned nByteRanges);
415 
416  void readFirstArrayMap();
417 
418  void printAddresses();
419 
420  private:
421 
422  bool convert_;
423  bool read_;
424 
425  static ArchiveReader::Format formats_[];
426  static int nFormat_;
427 
428  unsigned iFrame_;
429  unsigned nFrame_;
430 
431  public:
432  RegCal* regCal_;
433  private:
434  std::string calFile_;
435  bool haveCalFile_;
436 
437  std::vector<ArchiveByteRange> byteRanges_;
438 
439  ArrayMap* arrayMap_;
440 
441  bool memMap_;
442  std::string arcDir_;
443  bool arcDirIsSet_;
444 
445  std::string startUtc_;
446  std::string stopUtc_;
447  bool datesAreSet_;
448 
449  std::vector<ArchiveReader::ArchiveFile> fileList_;
450  std::vector<ArchiveReader::ArchiveFile>::iterator currFile_;
451  unsigned nFile_;
452 
453  ArchiveFileHandler handler_;
454 
455  // The array of registers and format specifiers input by the user
456 
457  public:
458 
459  std::vector<bool> regSpecValid_;
460  std::vector<std::string> regSpecs_;
461  std::vector<std::string> formatSpecs_;
462  std::vector<ArchiveTransposeType> transposeTypes_;
463  std::vector<int> widths_;
464  std::vector<int> precs_;
465 
466  // The array of registers and associated information derived
467  // from the above. Note, however, that these arrays are not in
468  // general of the same length as the above, since some registers
469  // may have errors in the specification, or may not exist in
470  // the register map
471 
472  void printRegsSize();
473 
474  std::vector<ArchiveRegister>& getRegs();
475  std::vector<RegDescription>& getRegDescs();
476 
477  public:
478 
479  std::vector<ArchiveRegister> regs_;
480  std::vector<DataType::Type> formatTypes_;
481  std::vector<RegDescription> regDescs_;
482 
483  private:
484 
485  static bool compArchiveFile(ArchiveReader::ArchiveFile f1,
487 
488  static bool compArchiveByteRange(ArchiveByteRange r1, ArchiveByteRange r2);
489 
490  static DataType::Type parseFormat(std::string format);
491 
492  bool unique(ArchiveRegister& reg);
493 
494  void checkFirstFile();
495 
496  public:
497 
498  void addRegister(std::string regSpec);
499  void addRegisterOnly(std::string regSpec);
500 
501  bool stageNextFile();
502 
503  void resetToBeginning();
504 
505  bool advanceFile();
506 
507  void iterateFiles();
508 
509  void readRegs();
510  void printRegs(std::ostringstream& os);
511 
512  // Convenience methods for external use of this object
513 
514  std::vector<RegDescription> selectedRegs();
515 
516  std::vector<DataType::Type> selectedFormats();
517 
518  }; // End class ArchiveReader
519 
520  } // End namespace util
521 } // End namespace gcp
522 
523 
524 
525 #endif // End #ifndef GCP_UTIL_ARCHIVEREADER_H
static Type typeOf(RegMapBlock *blk)
Definition: DataType.cc:223
void setArchiveDirectory(std::string arcDir)
Definition: ArchiveReader.cc:297
Definition: ArchiveFileHandler.h:29
void addRegisterOnly(std::string regSpec)
Definition: ArchiveReader.cc:615
unsigned readTimeStamps()
Definition: ArchiveReader.cc:414
Definition: ArchiveReader.h:32
std::vector< Range< unsigned > > getSlotRanges(CoordRange *range=0)
Definition: RegDescription.cc:416
Definition: RegCal.h:58
void updateRegBufPtrCache(unsigned nByteRanges)
Definition: ArchiveReader.cc:875
CoordAxes axes()
Definition: RegDescription.h:148
void addRegister(std::string regSpec)
Definition: ArchiveReader.cc:644
void updateRegSelection()
Definition: ArchiveReader.cc:716
void readRegs()
Definition: ArchiveReader.cc:942
Definition: arraymap.h:63
bool advanceFile()
Definition: ArchiveReader.cc:530
ArrRegMap * regMap()
Definition: RegDescription.cc:573
void printRegs(std::ostringstream &os)
Definition: ArchiveReader.cc:979
void readFirstArrayMap()
Definition: ArchiveReader.cc:555
bool updateArrayMap()
Definition: ArchiveReader.cc:587
friend std::ostream & operator<<(std::ostream &os, ArchiveReader &obj)
void initialize(bool memMap, bool convert, bool read)
Definition: ArchiveReader.cc:42
unsigned nEl(CoordRange *range=0)
Definition: RegDescription.cc:474
Definition: arcfile.h:19
bool stageNextFile()
Definition: ArchiveReader.cc:496
unsigned nByte(CoordRange *range=0)
Definition: RegDescription.cc:465
virtual ~ArchiveReader()
Definition: ArchiveReader.cc:119
void operator=(const ArchiveReader &objToBeAssigned)
Definition: ArchiveReader.cc:94
std::vector< Range< unsigned > > getByteRanges(CoordRange *range=0)
Definition: RegDescription.cc:433
Definition: ArchiveReader.h:55
bool readNextFrame()
Definition: ArchiveReader.cc:437
void resetToBeginning()
Definition: ArchiveReader.cc:511
Definition: RegDescription.h:26
unsigned countFrames()
Definition: ArchiveReader.cc:306
unsigned int nEl(int axis=-1)
Definition: CoordAxes.cc:136
Definition: Exception.h:30
Definition: ArchiveReader.h:336
Definition: ArchiveReader.h:40
Definition: ArchiveReader.h:86
void updateRegisterCalibration()
Definition: ArchiveReader.cc:699
ArchiveReader(bool memMap=false, bool convert=true, bool read=false)
Definition: ArchiveReader.cc:61
Definition: arraymap.h:177
void getFileList()
Definition: ArchiveReader.cc:147
unsigned int nAxis()
Definition: CoordAxes.cc:128