CARMA C++
XMLHandler.h
Go to the documentation of this file.
1 
9 #ifndef XMLHANDLER_H
10 #define XMLHANDLER_H
11 
12 // Carma includes
13 #include "carma/sdp/MiriadUV.h"
16 #include "carma/pipeline/VisBrickReader.h"
18 #include "carma/util/Time.h"
20 
21 // Carma tools includes
22 #include <xercesc/sax2/Attributes.hpp>
23 #include <xercesc/sax2/DefaultHandler.hpp>
24 
25 // C++ standard includes
26 #include <iostream>
27 #include <map>
28 #include <vector>
29 #include <complex>
30 #include <string>
31 
32 // C include for strdup
33 #include <string.h>
34 
35 // Namespace using statements.
36 using namespace XERCES_CPP_NAMESPACE;
37 
38 namespace carma {
39  namespace sdp {
40 
43 std::ostream& operator<< (std::ostream& target, const XMLCh *s);
44 
49 class XMLChComp {
50  public:
51  bool operator()(const XMLCh *a, const XMLCh *b)const
52  { int islower = XMLString::compareIString(a, b);
53  return (islower < 0);
54  }
55 };
56 
57 typedef std::map<const XMLCh *, int, XMLChComp> KeywordMap;
58 class XMLHandler;
59 struct Keyword;
60 typedef void (*XMLhandlerFunc)(XMLHandler *, const XMLCh* name,
61  const Keyword *,
62  const XMLCh* type, int length,
63  const XMLCh* value);
66 typedef struct Keyword {
67  const char *name;
68  const char *type;
69  XMLhandlerFunc funcp;
70  const char *description;
71  const char *whose;
72 } Keyword;
73 
76 typedef struct CorrelationData {
77  int bits_; // Flags indicating what's been seen.
78  int ndata_; // # of elements.
79  float *data_;// correlation data.
80  int *flags_; // flags.
82 
83  typedef struct SystemTempData{
84  float LSB;
85  float USB;
86  float DSB;
87  }SystemTempData;
88 
89 int getNumberKeywords();
90 const Keyword *getKeywordEntry(unsigned int index);
91 
92 // If anything is changed here, probably should change the documentation
93 // in the MIRIAD task 'uvbflag'.
94 // See also http://www.mmarray.org/wiki/Blanking_and_flagging
95 const std::string bfReasons[] = {"A1_PHASELOCK",
96  "A2_PHASELOCK",
97  "A1_MAJOR_TRACKING",
98  "A2_MAJOR_TRACKING",
99  "A1_TSYS_BAD",
100  "A2_TSYS_BAD",
101  "A1_SHADOWED",
102  "A2_SHADOWED",
103  "A1_OFFLINE",
104  "A2_OFFLINE",
105  "A1_MINOR_TRACKING",
106  "A2_MINOR_TRACKING",
107  "A1_CALSTATE", // added May 15,2012
108  "A2_CALSTATE",
109  "UNKNOWN12",
110  "UNKNOWN13",
111  "UNKNOWN14",
112  "UNKNOWN15",
113  "UNKNOWN16",
114  "UNKNOWN17",
115  "UNKNOWN18",
116  "UNKNOWN19",
117  "UNKNOWN20",
118  "UNKNOWN21",
119  "BAND_OFFLINE",
120  "UNMAPPED_SIGNAL",
121  "MONITOR_DATA_BAD",
122  "BAD_CHANNEL_COUNT",
123  "NO_RX_IN_SIDEBAND",
124  "CORR_DATA_MISSING",
125  "CORR_DATA_INVALID",
126  "DO_NOT_USE"}; // this better be [31]
127 
131 public:
132  XMLKeywordMap();
133  const Keyword * getKeywordMapEntry(const XMLCh *index)const;
134  const Keyword * getItem(const XMLCh *key)const;
135  bool isDefined(const XMLCh *key)const;
136  void add(const XMLCh *key, unsigned int value);
137 
138  private:
139  static bool mapinitialized_;
140  static KeywordMap map_;
141 };
142 
143 
146 class XMLHandler : public DefaultHandler
147 {
148 public:
151  XMLHandler();
152 
155  ~XMLHandler();
156 
157  void setCurrentFile(std::string fileName);
158 
164  void startElement(const XMLCh* const uri,
165  const XMLCh* const localname,
166  const XMLCh* const qname, const Attributes& attrs);
167 
170  void endElement(const XMLCh *const uri, const XMLCh *const localname,
171  const XMLCh *const qname);
172 
175  void startCDATA();
176  void endCDATA();
177 
180  void resetDocument();
182 
186  void writeKeyword(const Attributes &a);
187 
191  void warning(const SAXParseException& exc);
192  void error(const SAXParseException& exc);
193  void fatalError(const SAXParseException& exc);
194  void resetErrors();
196 
199  void openMiriad(const std::string& inputAstroHdrFile,
200  const char *filename, const std::string& mode,
201  const bool writeFloats, const bool justGatherPdb);
202 
205  void closeMiriad();
206 
209  void configure(const std::string& visBrickDir,
210  const std::string& corrType);
211 
214  void selectFrameRange(const carma::util::frameType& startFrame,
215  const carma::util::frameType& endFrame);
216 
219  static int frameOffset(const double& frame);
220 
223  carma::util::frameType getLastFrame();
224 
227  void resetProjectData();
228 
231  void updateProjectDatabase();
232 
233  void addScript(std::string miriadFile);
234 
235  // Log the number of warnings gathered.
236  static void printVisBrickWarnings(bool isIncremental);
237 
241 
244  static void putUnknown(XMLHandler *h, const XMLCh *, const Keyword *,
245  const XMLCh *, int length, const XMLCh *);
248  static void putNothing(XMLHandler *h, const XMLCh *, const Keyword *,
249  const XMLCh *, int length, const XMLCh *);
252  static void putA(XMLHandler *h, const XMLCh *, const Keyword *,
253  const XMLCh *, int length, const XMLCh *);
256  static void putCv(XMLHandler *h, const XMLCh *, const Keyword *,
257  const XMLCh *, int length, const XMLCh *);
260  static void putD(XMLHandler *h, const XMLCh *, const Keyword *,
261  const XMLCh *, int length, const XMLCh *);
262  static void putDv(XMLHandler *h, const XMLCh *, const Keyword *,
263  const XMLCh *, int length, const XMLCh *);
264 
265  static void putI(XMLHandler *h, const XMLCh *, const Keyword *,
266  const XMLCh *, int length, const XMLCh *);
267  static void putIv(XMLHandler *h, const XMLCh *, const Keyword *,
268  const XMLCh *, int length, const XMLCh *);
269  static void putR(XMLHandler *h, const XMLCh *, const Keyword *,
270  const XMLCh *, int length, const XMLCh *);
271  static void putRv(XMLHandler *h, const XMLCh *, const Keyword *,
272  const XMLCh *, int length, const XMLCh *);
273 
274  static void putCoord(XMLHandler *h, const XMLCh *, const Keyword *,
275  const XMLCh *, int length, const XMLCh *);
276 
279  static void putBaseline(XMLHandler *h, const XMLCh *,
280  const Keyword *,
281  const XMLCh *, int length, const XMLCh *);
282  static void putTime(XMLHandler *h, const XMLCh *, const Keyword *,
283  const XMLCh *, int length, const XMLCh *);
284  static void putDTime(XMLHandler *h, const XMLCh *, const Keyword *,
285  const XMLCh *, int length, const XMLCh *);
286 
287  static void putCorr(XMLHandler *h, const XMLCh *, const Keyword *,
288  const XMLCh *, int length, const XMLCh *);
289  static void putFlags(XMLHandler *h, const XMLCh *, const Keyword *,
290  const XMLCh *, int length, const XMLCh *);
291  static void storeCorrelatorData(XMLHandler *h, const XMLCh *,
292  const Keyword *, const XMLCh *,
293  int length, const XMLCh *);
294  static void storeCorrInp(XMLHandler *h, const XMLCh *, const Keyword *,
295  const XMLCh *, int length, const XMLCh *);
296  static void storeNewCorrInp(XMLHandler *h, const XMLCh *, const Keyword *,
297  const XMLCh *, int length, const XMLCh *);
298  static void storeCorbit(XMLHandler *h, const XMLCh *, const Keyword *,
299  const XMLCh *, int length, const XMLCh *);
300  static void storeCoreff(XMLHandler *h, const XMLCh *, const Keyword *,
301  const XMLCh *, int length, const XMLCh *);
302  static void storeUVW(XMLHandler *h, const XMLCh *, const Keyword *,
303  const XMLCh *, int length, const XMLCh *);
304  static void storeJyperka(XMLHandler *h, const XMLCh *, const Keyword *,
305  const XMLCh *, int length, const XMLCh *);
306  static void storeBandFreq(XMLHandler *h, const XMLCh *, const Keyword *,
307  const XMLCh *, int length, const XMLCh *);
308  static void storeRestFreq(XMLHandler *h, const XMLCh *, const Keyword *,
309  const XMLCh *, int length, const XMLCh *);
310  static void storeTsys(XMLHandler *h, const XMLCh *, const Keyword *,
311  const XMLCh *, int length, const XMLCh *);
312  static void storeNewTsys(XMLHandler *h, const XMLCh *, const Keyword *,
313  const XMLCh *, int length, const XMLCh *);
314  static void storePsys(XMLHandler *h, const XMLCh *, const Keyword *,
315  const XMLCh *, int length, const XMLCh *);
316  static void storeNewPsys(XMLHandler *h, const XMLCh *, const Keyword *,
317  const XMLCh *, int length, const XMLCh *);
318  static void storePsysAttn(XMLHandler *h, const XMLCh *, const Keyword *,
319  const XMLCh *, int length, const XMLCh *);
320  static void storeNewPsysAttn(XMLHandler *h, const XMLCh *, const Keyword *,
321  const XMLCh *, int length, const XMLCh *);
322  static void storeAmbPsys(XMLHandler *h, const XMLCh *, const Keyword *,
323  const XMLCh *, int length, const XMLCh *);
324  static void storeNewAmbPsys(XMLHandler *h, const XMLCh *, const Keyword *,
325  const XMLCh *, int length, const XMLCh *);
326  static void storeAntennas(XMLHandler *h, const XMLCh *, const Keyword *,
327  const XMLCh *, int length, const XMLCh *);
328  static void storeBandGood(XMLHandler *h, const XMLCh *, const Keyword *,
329  const XMLCh *, int length, const XMLCh *);
330  static void storePolState(XMLHandler *h, const XMLCh *, const Keyword *,
331  const XMLCh *, int length, const XMLCh *);
332  static void storeVersion(XMLHandler *h, const XMLCh *, const Keyword *,
333  const XMLCh *, int length, const XMLCh *);
334  static void storeCorrtype(XMLHandler *h, const XMLCh *name,
335  const Keyword *kw, const XMLCh *type,
336  int length, const XMLCh *value);
337  static void putIntTime(XMLHandler *h, const XMLCh *, const Keyword *,
338  const XMLCh *, int length, const XMLCh *);
339  static void putPntRaDec(XMLHandler *h, const XMLCh *, const Keyword *,
340  const XMLCh *, int length, const XMLCh *);
341  enum PREAMBLE {COORD=0x1, TIME=0x2, BASELINE=0x4, DATA=0x8,
342  FLAGS=0x10, PREAMBLEBITS=0x1f, WBBITS=DATA|FLAGS};
343  enum {WCORRINDEX=0, CORRINDEX=1}; // Index into correlation struct array.
345 
346  private:
349  static int buildInt(const XMLCh *value);
350  static float buildFloat(const XMLCh *value);
351  static double buildDouble(const XMLCh *value);
352 
360  static int *buildIntv(const XMLCh *value, int &length, int *list=0);
361  static float *buildFloatv(const XMLCh *value, int &length,
362  float *list=0);
363  static double *buildDoublev(const XMLCh *value, int &length,
364  double *l=0);
365 
369  void unpackCorrelatorData
370  (carma::correlator::lib::CorrelatorData*& correlatorData,
371  std::string& corrType, const std::string& sortOrder,
372  std::map<int,int>& mapCorrInputToAntNumber,
373  int& nspect, int& nchan, std::vector<int>& ischan,
374  std::vector<int>& nschan,
375  std::vector<double>& restfreq,
376  std::vector<double>& sfreq,
377  std::vector<double>& sdf, double& freq,
378  int& nwide,
379  std::vector<float>& wfreq,
380  std::vector<float>& wwidth,
381  double& time,
382  std::map<int, std::map<int, std::vector<float> > >& corr,
383  std::map<int, std::map<int, std::vector<int> > >& flags,
384  std::map<int, std::map<int, std::vector<float> > >& wcorr,
385  std::map<int, std::map<int, std::vector<int> > >& wflags,
386  std::map<int, std::map<int, std::vector<float> > >& intTime,
387  std::map<int, std::map<int, std::vector<int> > >& flagReason);
388 
389 
390  int getPolCode(int band, int input1, int input2);
391 
392  int getPolCode(std::string polarization);
393 
394  int getAntNumber(int band, int input);
399  sideband(const carma::correlator::lib::CorrelatorSideband & sb);
400 
404  bool selectedCorrType(const std::string& corrType,
406 
409  static void printIv(const char *str, const XMLCh *name,
410  const XMLCh *type, int xmllength,
411  const XMLCh *value);
412 
413  static void printRv(const char *str, const XMLCh *name,
414  const XMLCh *type, int xmllength,
415  const XMLCh *value);
416  static void printDv(const char *str, const XMLCh *name,
417  const XMLCh *type, int xmllength,
418  const XMLCh *value);
419 
420  static std::map<int, std::map<int,SystemTempData> > reconfigureTsysMap(std::vector<float> &tempMap);
421 
422  template<class T> static std::map<int, std::map<int,T> > reconfigureMap(std::vector<T> &tempMap);
423 
424  template<class T> static void reconfigureMultiMap(std::map<int, std::map<int,T> > inMap,
425  std::map<std::string, std::map<int, std::map<int, T> > > &outMap);
426 
427  static void reconfigureAntTsys(std::vector<float> &tempMap, std::map<std::string, std::map<int, std::map<int, SystemTempData> > > &outMap);
428 
429  static void reconfigureAntAmbPsys(std::vector<float> &tempMap, std::map<std::string, std::map<int, std::map<int, float> > > &outMap);
430 
431  void writeTsys(int ant1, std::string pol1, int ant2, std::string pol2, int nspect);
432 
433  void putChi();
434 
435  template<class T> static T calculateMedian(std::vector<T> &data);
436 
437 private:
438  XMLKeywordMap map_;
439  carma::sdp::MiriadUV *muv_;
440  bool inCDATA_;
441  int recordLevel_;
442  int recordNumber_;
443  int preambleBits_;
444  double preamble_[5];
445  // Correlation data
446  CorrelationData correlationData_[2]; // 0 - wcorr, 1 - corr.
447  static XMLCh *KW_;
448  static XMLCh *UNK_;
449  static XMLCh *NAME_, *TYPE_, *LENGTH_, *VALUE_;
450  static XMLCh *INTEGRATION_;
451  static int nkws_;
452  static int nunkws_;
453  // MJD of start of current integration
454  static double currentStartFrameMJD_;
455  // Frame count of start of current integration
456  static carma::util::frameType currentStartFrame_;
457  // Current visbrick file name
458  static std::string currentVisBrickFile_;
459  // Current visbrick reader
461  currentVisBrickReader_;
462  // Current correlator data object
463  static carma::correlator::lib::CorrelatorData* currentCorrelatorData_;
464  // Current correlator input map to antenna number
465  static std::map<int, std::map<int,int> > currentNewCorrelatorInputMap_;
466  static std::map<int, int> currentCorrelatorInputMap_;
467  // Current correlator bit depth
468  static std::vector<int> currentCorbit_;
469  // Current correlator efficiency
470  static std::vector<float> currentCoreff_;
471  // Current (u,v,w) coordinates
472  static std::vector<double> currentUVW_;
473  // Current Jy/K values for each antenna
474  static std::vector<float> currentJyperka_;
475  // Current band frequencies
476  static std::vector<double> currentBandFreq_;
477  // Current rest frequencies
478  static std::vector<double> currentRestFreq_;
479  static double maxRestfreq_;
480  // Current nominal integration time
481  static double currentIntTime_;
482  // Current Tsys values
483  static std::vector<float> currentTsys_;
484  static std::map<std::string, std::map<int, std::map<int, SystemTempData> > >
485  currentNewTsys_;
486  static std::vector<float> currentPsys_;
487  static std::map<std::string, std::map<int, std::map<int, float> > >
488  currentNewPsys_;
489  static std::vector<float> currentPsysAttn_;
490  static std::map<std::string, std::map<int, std::map<int, float> > >
491  currentNewPsysAttn_;
492  static std::vector<float> currentAmbPsys_;
493  static std::map<std::string, std::map<int, std::map<int, float> > >
494  currentNewAmbPsys_;
495  // Current antenna list
496  static std::vector<int> currentAntennas_;
497  // Current online bands
498  static std::vector<int> currentGoodBands_;
499  // Current max band number (resets only when changing AH file)
500  static int currentMaxBand_;
501  static std::map<int, std::map<int, std::string> > currentPolState_;
502  static std::vector<int> currentVersion_;
503 
504  // Visbrick directory
505  static std::string visBrickDir_;
506  // Start and end frame count selection
507  static carma::util::frameType startFrame_, endFrame_;
508  // Correlation type selection
509  static std::string corrType_;
510  // Flag indicating if current integration is selected
511  static bool writeData_;
512  // Last integrations start frame processed.
513  static carma::util::frameType lastFrame_;
514  // Trial project data accumulator
515  static carma::sdp::TrialProjectData* trialProjectData_;
516  // Current input astro header file anem
517  static std::string inputAstroHdrFile_;
518  // Count of # of warnings (ie missing data).
519  static int dataNotPresentCount_;
520  static std::string dataNotPresentFile_;
521  static int maxSlCorrelatorInput_;
522  static int maxWbCorrelatorInput_;
523  static int maxSlBandIndex_;
524  static int maxWbBandIndex_;
525  static int maxABBandIndex_;
526  static int maxABCorrelatorInput_;
527  static int SlBandStart_;
528  static int WbBandStart_;
529  static bool havePolState_;
530  static int32_t visbrickVersionNumber_;
531  static int maxAntIndex_;
532  // for calculating chi
533  static double currentLatitude_;
534  static double currentLst_;
535  static double currentObsdec_;
536  static double currentObsra_;
537  static double evector_;
538  static std::map<std::string,int> lastNChan_;
539  static std::map<std::string, std::vector<int> > lastNsChan_;
540  static std::map<std::string, std::vector<int> > lastIsChan_;
541  static std::string currentFile_;
542 };
543 
544 }; // namespace carma
545 }; // namespace sdp
546 
547 #endif //XMLHANDLER_H
This class provides the comparision function for use with maps dealing with XMLCh strings...
Definition: XMLHandler.h:49
Common time functions.
unsigned int frameType
Half second frames since Jan 1, 2000.
Definition: types.h:29
Correlation data.
Definition: XMLHandler.h:76
A class to accumulate project data for an obsblock trial.
A class to accumulate project data for an obsblock trial.
Map of XML keywords.
Definition: XMLHandler.h:130
Abstract base class for access to MIRIAD uv-data.
Definition: MiriadUV.h:24
Content and error handler for the astronomical hdr SAX XML parser.
Definition: XMLHandler.h:146
Reads Correlator Data from a specifiec file.
SIDEBAND
Enumerated sideband types.
relationships between the monitor and dbms systems
Class used to represents bands of Correlator Data.
Mapping of correlator bands and sidebands to output spectral windows.
Defines the abstract class MiriadUV and the specialization MiriadUVBin which define the interface for...
XML keyword.
Definition: XMLHandler.h:66
Base class for Correlator Sideband data.