casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
asdm2MSGeneric.h
Go to the documentation of this file.
1 #ifndef TableSAXReader_h
2 #define TableSAXReader_h
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <iostream>
7 #include <set>
8 #include <libxml/tree.h>
9 #include <libxml/parser.h>
10 #include <libxml/parserInternals.h>
11 #include <casa/OS/Path.h>
12 
13 #include <memory>
14 
19 template<typename T>
20 struct negateFunctor {
21 public:
22  T operator() (const T& v) {
23  return (v < 0.0) ? v : -v;
24  }
25 };
26 
34 template<typename T>
35 std::string displaySet(const std::set<T> &aSet) {
36  std::ostringstream oss;
37  oss << "{";
38  typename std::set<T>::const_iterator iter = aSet.begin();
39  if (iter != aSet.end())
40  oss << *iter++;
41 
42  while (iter != aSet.end())
43  oss << "," << *iter++;
44  oss<< "}";
45  return oss.str();
46 }
47 
56 template<typename T>
57 std::set<T> SetAndSet(const std::set<T>& s1, const std::set<T>& s2) {
58  std::set<T> result;
59  typename std::set<T>::iterator iter1_s, iter2_s;
60  for (iter1_s = s1.begin(); iter1_s != s1.end(); iter1_s++) {
61  if ((iter2_s = s2.find(*iter1_s)) != s2.end())
62  result.insert(*iter1_s);
63  }
64  return result;
65 }
66 
67 
68 template<typename T>
70 private:
71  const std::vector<asdm::ScanRow *>& scans;
72  std::vector<T *> result;
73 
86  bool timeIntervalIntersectsAScan (T* row, const std::vector<asdm::ScanRow *>& scans) {
87  bool result = false;
88 
89  int64_t currentScanStartTime, currentScanEndTime;
90  int64_t rowStartTime, rowEndTime;
91  for (std::vector<asdm::ScanRow *>::const_iterator iter = scans.begin(); iter != scans.end(); iter++) {
92  currentScanStartTime = (*iter)->getStartTime().get();
93  currentScanEndTime = (*iter)->getEndTime().get();
94 
95  rowStartTime = row->getTimeInterval().getStart().get();
96  rowEndTime = rowStartTime + row->getTimeInterval().getDuration().get();
97  if (std::max(currentScanStartTime, rowStartTime) < std::min(currentScanEndTime, rowEndTime))
98  return true;
99  }
100  return result;
101  }
102 
103 public:
104  rowsInAScanbyTimeIntervalFunctor(const std::vector<asdm::ScanRow *>& scans): scans(scans) {};
105  const std::vector<T *> & operator() (const std::vector<T *>& rows, bool ignoreTime=false) {
106  if (ignoreTime) return rows;
107 
108  result.clear();
109  for (typename std::vector<T *>::const_iterator iter = rows.begin(); iter != rows.end(); iter++) {
110  if (timeIntervalIntersectsAScan (*iter, scans))
111  result.push_back(*iter);
112  }
113  return result;
114  }
115 };
116 
117 template<typename T>
119 private:
120  const std::vector<asdm::ScanRow *>& scans;
121  std::vector<T *> result;
122 
128  bool timeIsInAScan(T* row, const std::vector<asdm::ScanRow *>& scans) {
129  bool result = false;
130 
131  int64_t currentScanStartTime, currentScanEndTime;
132  int64_t rowTime;
133  rowTime = row->getTime().get();
134  for (std::vector<asdm::ScanRow *>::const_iterator iter = scans.begin(); iter != scans.end(); iter++) {
135  currentScanStartTime = (*iter)->getStartTime().get();
136  currentScanEndTime = (*iter)->getEndTime().get();
137  if ((currentScanStartTime <= rowTime) && (rowTime < currentScanEndTime))
138  return true;
139  }
140  return result;
141  }
142 
143 public:
144  rowsInAScanbyTimeFunctor(const std::vector<asdm::ScanRow *>& scans): scans(scans) {};
145  const std::vector<T *> & operator() (const std::vector<T *>& rows, bool ignoreTime=false) {
146  if (ignoreTime) return rows;
147 
148  result.clear();
149  for (typename std::vector<T *>::const_iterator iter = rows.begin(); iter != rows.end(); iter++) {
150  if (timeIsInAScan (*iter, scans))
151  result.push_back(*iter);
152  }
153 
154  return result;
155  }
156 };
157 
162 template<typename T>
163 struct size_lt {
164 public:
165  size_lt(unsigned int y) : y(y) {}
166  bool operator()(std::vector<T>& x) {return x.size() < y;}
167 
168 private:
169  unsigned int y;
170 };
171 
176 template<typename Enum, typename CEnum>
177 std::string stringValue(Enum literal) {
178  return CEnum::name(literal);
179 }
180 
181 
186 template<typename PhysicalQuantity, typename BasicType>
187 BasicType basicTypeValue (PhysicalQuantity value) {
188  return (BasicType) value.get();
189 }
190 
191 
192 /* ------------------------------------- TableSaxParser - beginning ------------------------------------------*/
193 
194 #if defined(__APPLE__)
195 #include <mach-o/dyld.h>
196 std::string getexepath() {
197  char path[1024];
198  uint32_t size = sizeof(path);
199  return (_NSGetExecutablePath(path, &size) == 0) ? std::string(path) : "";
200 }
201 #else
202 #include <limits.h>
203 #include <unistd.h>
204 std::string getexepath() {
205  char result[ PATH_MAX ];
206  ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
207  return std::string( result, (count > 0) ? count : 0);
208 }
209 #endif
210 
212 #include <casa/Logging/LogSink.h>
213 
214 
217 #include <alma/ASDM/ASDM.h>
219 
220 #define CONTEXT_P ((ParserContext<T, R, RFilter> *)myContext_p)
221 #define V2CTX_P(v_p) ((ParserContext<T, R, RFilter> *) v_p)
222 
223 template<class T, class R, class RFilter>
224  struct ParserContext {
225  public:
228  unsigned int maxNumberOfRowsInMem;
229  std::shared_ptr<R> row_sp;
230  std::vector<std::shared_ptr<R> > rows;
231  RFilter* rFilter_p;
233  void (*tableFiller_f_p) (const std::vector<R*>&, std::map<AtmPhaseCorrectionMod::AtmPhaseCorrection, ASDM2MSFiller*>&);
234  std::map<AtmPhaseCorrectionMod::AtmPhaseCorrection, ASDM2MSFiller*>* msFillers_m_p;
239  int depth;
240  std::string currentElement;
241  std::string currentValue;
243  bool verbose;
244  bool debug;
245  };
246 
247 template <class T, class R, class RFilter>
249 
250  typedef void (*TableFiller)(const std::vector<R*>&, std::map<AtmPhaseCorrectionMod::AtmPhaseCorrection, ASDM2MSFiller*>&);
251 
252  public:
256  TableSAXReader(bool verbose, RFilter& rFilter, TableFiller tableFiller_f_p, std::map<AtmPhaseCorrectionMod::AtmPhaseCorrection, ASDM2MSFiller*>& msFillers_m) {
257 
258  //casacore::LogSinkInterface& lsif = casacore::LogSink::globalSink();
259  static_cast<void>(casacore::LogSink::globalSink());
260 
261  // The top level element name can be derived from the template parameter T.
262  myContext.asdm_p = &asdm;
263  myContext.maxNumberOfRowsInMem = 100000;
264  myContext.rFilter_p = &rFilter;
265  myContext.tableFiller_f_p = tableFiller_f_p;
266  myContext.msFillers_m_p = &msFillers_m;
268  topLevelElement_s = T::name()+"Table";
269  myContext.topLevelElement_p = (const xmlChar *) topLevelElement_s.c_str();
270  myContext.entityElement_p = (const xmlChar *) "Entity";
271  myContext.containerEntityElement_p = (const xmlChar *)"ContainerEntity";
272  myContext.rowElement_p = (const xmlChar *) "row";
273  myContext.depth = 0;
274  myContext.verbose = verbose;
275  myContext.debug = getenv("ASDM_DEBUG") != NULL;
276  }
277 
282 
286  void operator() (const std::string& asdmDirectory, bool ignoreTime) {
287  myContext.ignoreTime = ignoreTime;
288  std::string tablePath = asdmDirectory + "/"+ T::name() + ".xml";
289  xmlSAXUserParseFile(&myHandler, &myContext, tablePath.c_str());
290  }
291 
296  static void start_element_callback(void *v_p, const xmlChar *name, const xmlChar **) {
297  const xmlChar* expectedElement = NULL;
299 
301  V2CTX_P(v_p)->depth++;
302 
303  switch (V2CTX_P(v_p)->state) {
304  // We are right before the toplevel element of the XML document.
306  expectedElement = V2CTX_P(v_p)->topLevelElement_p;
308  break;
309 
310  // We are right after the toplevel element of the XML document.
312  expectedElement = V2CTX_P(v_p)->entityElement_p;
314  break;
315 
316  // We are right after the <Entity.../> element.
318  expectedElement = V2CTX_P(v_p)->containerEntityElement_p;
320  break;
321 
322  // We are right after the <ContainerEntity.../> element or a <row>..</row> element.
325  {
326  T& tableRef = (T&) V2CTX_P(v_p)->asdm_p->getTable(T::name());
327  V2CTX_P(v_p)->row_sp = std::shared_ptr<R>(tableRef.newRow());
328  expectedElement = V2CTX_P(v_p)->rowElement_p;
330  }
331  break;
332 
333  // We are inside a <row>...</row> but outside any element contained in that element.
335  {
336  expectedElement = NULL;
337  V2CTX_P(v_p)->currentElement = (char *) name;
339  }
340  break;
341 
342  // We are inside a element contained in a <row>...</row> element.
344  break;
345 
346  // Otherwise we have a problem.
347  default :
348  std::string message = "Unexpected '" + std::string((char *) name) + "'.";
349  error(message);
350  };
351 
352  checkOpeningElement(name, expectedElement);
353  V2CTX_P(v_p)->state = nextState;
354  }
355 
360  static void end_element_callback(void *v_p, const xmlChar* name) {
362  typename ParserContext<T, R, RFilter>::StatesEnum currentState = V2CTX_P(v_p)->state;
363 
364  V2CTX_P(v_p)->depth--;
366 
367  switch (currentState) {
368  // We are right after a <row>..</row>
370  checkClosingElement(name, V2CTX_P(v_p)->topLevelElement_p);
372 
373  // Possibly write some remainings rows by applying tableFiller on the vector.
374  if (V2CTX_P(v_p)->rows.size() > 0) {
375  std::vector<R*> tmp(V2CTX_P(v_p)->rows.size());
376  for (unsigned int iR = 0; iR < tmp.size(); iR++) tmp[iR] = V2CTX_P(v_p)->rows[iR].get();
377  const std::vector<R*>& filteredRows = (*(V2CTX_P(v_p)->rFilter_p))(tmp, V2CTX_P(v_p)->ignoreTime);
378  if (filteredRows.size() > 0) {
379  (*(V2CTX_P(v_p)->tableFiller_f_p))(filteredRows , *(V2CTX_P(v_p)->msFillers_m_p));
380  //
381  // Log a minimum of information about what has just happened.
382  //
383  if (V2CTX_P(v_p)->verbose){
384  std::ostringstream oss;
385  oss.str("");
386  oss << "Appended " << filteredRows.size() << " rows to the MS SYSPOWER table." << endl;
389  }
390  }
391  //
392  // Empty the vector before reading new rows.
393  //
394  V2CTX_P(v_p)->rows.clear();
395  }
396  break;
397 
398  // We were in an <Entity/> element.
400  checkClosingElement(name, V2CTX_P(v_p)->entityElement_p);
402  break;
403 
404  // We were in a <ContainerEntity/> element.
406  checkClosingElement(name, V2CTX_P(v_p)->containerEntityElement_p);
408  break;
409 
410  // We were in a <row>..</row> element.
412  checkClosingElement(name, V2CTX_P(v_p)->rowElement_p);
414 
415  // Push the last parsed row into the vector.
416  V2CTX_P(v_p)->rows.push_back(V2CTX_P(v_p)->row_sp);
417 
418  // If the size of this vector is equal to the maximum number allowed to reside in memory
419  // then proceed by applying tableFiller on on the vector and after that clear the vector.
420  if (V2CTX_P(v_p)->rows.size() == V2CTX_P(v_p)->maxNumberOfRowsInMem) {
421  std::vector<R*> tmp(V2CTX_P(v_p)->rows.size());
422  for (unsigned int iR = 0; iR < tmp.size(); iR++) tmp[iR] = V2CTX_P(v_p)->rows[iR].get();
423  const std::vector<R*>& filteredRows = (*(V2CTX_P(v_p)->rFilter_p))(tmp, V2CTX_P(v_p)->ignoreTime);
424  if (filteredRows.size() > 0) {
425  (*(V2CTX_P(v_p)->tableFiller_f_p))(filteredRows , *(V2CTX_P(v_p)->msFillers_m_p));
426  //
427  // Log a minimum of information about what has just happened.
428  //
429  if (V2CTX_P(v_p)->verbose){
430  std::ostringstream oss;
431  oss.str("");
432  oss << "Appended " << filteredRows.size() << " rows to the MS SYSPOWER table." << endl;
435  }
436  }
437  V2CTX_P(v_p)->rows.clear();
438  }
439  break;
440 
441  // We were in an element located in a <row>..</row>, which is normally an attribute.
443  checkClosingElement(name, (const xmlChar*) V2CTX_P(v_p)->currentElement.c_str());
445  V2CTX_P(v_p)->row_sp.get()->fromText(V2CTX_P(v_p)->currentElement, V2CTX_P(v_p)->currentValue);
446  break;
447 
448  default :
449  std::string message = "Unexpected '" + std::string((char *) name) + "'.";
450  error(message);
451  }
452 
453  V2CTX_P(v_p)->state = nextState;
454  }
455 
461  static void characters_callback (void * v_p,
462  const xmlChar * ch,
463  int len) {
464  V2CTX_P(v_p)->currentValue = std::string((char * ) ch, len);
465  }
466 
467 
468  private:
469  bool verbose;
471  std::string topLevelElement_s;
473  static xmlSAXHandler myHandler;
474  static xmlSAXHandler initSAXHandler() {
475  xmlSAXHandler handler = {};
476  handler.startElement = start_element_callback;
477  handler.endElement = end_element_callback;
478  handler.characters = characters_callback;
479  return handler;
480  }
481 
482  static void error(const std::string & message) {
483  throw asdm::ConversionException(message, T::name());
484  }
485 
486  static void unexpectedOpeningElement(const xmlChar *name, const xmlChar *expectedName) {
487  std::string message = "Unexpected opening tag '" + std::string((const char *) name) + "', I was expecting '" + std::string((const char*) expectedName) +"'.";
488  error(message);
489  }
490 
491  static void unexpectedClosingElement(const xmlChar *name) {
492  std::string message = "Unexpected closing tag '" + std::string((const char *) name) + "'.";
493  error(message);
494  }
495 
496  static void checkOpeningElement(const xmlChar* name, const xmlChar* expectedName) {
497  if (expectedName && xmlStrcmp(name, expectedName)) unexpectedOpeningElement(name, expectedName);
498  }
499 
500  static void checkClosingElement(const xmlChar* name, const xmlChar* expectedName) {
501  if (xmlStrcmp(name, expectedName)) unexpectedClosingElement(name);
502  }
503 
504  static void enterElementInfo(void *v_p, const xmlChar* name) {
505  cout << "Enter '" << name << "' (depth = " << V2CTX_P(v_p)->depth << ")" << endl;
506  }
507 
508  static void exitElementInfo(void *v_p, const xmlChar* name) {
509  cout << "Exit from '" << name << "' (depth = " << V2CTX_P(v_p)->depth << ")" << endl;
510  }
511 
512 }; // end TableSAXReader
513 
514 template<class T, class R, class RFilter> xmlSAXHandler TableSAXReader<T, R, RFilter>::myHandler = TableSAXReader<T, R, RFilter>::initSAXHandler();
515 
516 /* ------------------------------------- TableSaxParser - end ------------------------------------------*/
517 #endif
rowsInAScanbyTimeIntervalFunctor(const std::vector< asdm::ScanRow * > &scans)
std::map< AtmPhaseCorrectionMod::AtmPhaseCorrection, ASDM2MSFiller * > * msFillers_m_p
Elements::const_iterator const_iterator
bool operator()(std::vector< T > &x)
#define V2CTX_P(v_p)
#define max(a, b)
Definition: hio.h:44
bool timeIsInAScan(T *row, const std::vector< asdm::ScanRow * > &scans)
A template function which checks if there is at least one element scan of the vector scans for which ...
const std::vector< asdm::ScanRow * > & scans
const xmlChar * rowElement_p
BasicType basicTypeValue(PhysicalQuantity value)
template function meant to return a value of type BasicType out of a value expected to be of one of t...
#define min(a, b)
Definition: hio.h:45
std::string currentValue
RFilter * rFilter_p
static void checkOpeningElement(const xmlChar *name, const xmlChar *expectedName)
T operator()(const T &v)
static void end_element_callback(void *v_p, const xmlChar *name)
Defines the action to perform when an event &quot;closing tag&quot; occurs.
~TableSAXReader()
The destructor.
The ASDM class is the container for all tables.
Definition: ASDM.h:273
unsigned char xmlChar
Definition: Misc.h:52
asdm::ASDM * asdm_p
static void exitElementInfo(void *v_p, const xmlChar *name)
const xmlChar * topLevelElement_p
rowsInAScanbyTimeFunctor(const std::vector< asdm::ScanRow * > &scans)
ABSTRACT CLASSES Abstract class for colors Any implementation of color should be able to provide a hexadecimal form of the if a human readable name(i.e."black").In many places throughout the plotter
Path name of a file.
Definition: Path.h:126
size_t size() const
static LogSinkInterface & globalSink()
Get/set the global sink or check if the global sink is null.
const std::vector< T * > & operator()(const std::vector< T * > &rows, bool ignoreTime=false)
std::string getexepath()
std::shared_ptr< R > row_sp
std::vector< std::shared_ptr< R > > rows
static void unexpectedOpeningElement(const xmlChar *name, const xmlChar *expectedName)
const xmlChar * entityElement_p
static void enterElementInfo(void *v_p, const xmlChar *name)
static void error(const std::string &message)
StatesEnum state
std::string path(const std::string &name)
static Bool postGlobally(const LogMessage &message)
Send message to the global sink only.
void operator()(const std::string &asdmDirectory, bool ignoreTime)
It will be used as a functor.
std::string topLevelElement_s
static void checkClosingElement(const xmlChar *name, const xmlChar *expectedName)
TableSAXReader(bool verbose, RFilter &rFilter, TableFiller tableFiller_f_p, std::map< AtmPhaseCorrectionMod::AtmPhaseCorrection, ASDM2MSFiller * > &msFillers_m)
An empty contructor.
A boolean template functor which returns the value of the expression x.size() &lt; y.
asdm::ASDM asdm
const xmlChar * containerEntityElement_p
const std::vector< T * > & operator()(const std::vector< T * > &rows, bool ignoreTime=false)
static void characters_callback(void *v_p, const xmlChar *ch, int len)
Defines the action to be performed while parsing any text outside of a tag.
#define WHERE
Definition: LogOrigin.h:211
void(* tableFiller_f_p)(const std::vector< R * > &, std::map< AtmPhaseCorrectionMod::AtmPhaseCorrection, ASDM2MSFiller * > &)
A template functor which returns -|v|.
static void start_element_callback(void *v_p, const xmlChar *name, const xmlChar **)
Defines the action to perform when an event &quot;opening tag&quot; occurs.
The ConversionException class represents an exception when an error occurs in converting a table to i...
unsigned int maxNumberOfRowsInMem
std::string stringValue(Enum literal)
A template function which returns a string from an (expectedly) enumeration and its associated helper...
std::string currentElement
bool debug
LogOrigin: The source code location of the originator of a LogMessage.
Definition: LogOrigin.h:94
const std::vector< asdm::ScanRow * > & scans
static xmlSAXHandler initSAXHandler()
std::set< T > SetAndSet(const std::set< T > &s1, const std::set< T > &s2)
Returns the intersection of two sets.
std::vector< T * > result
static void unexpectedClosingElement(const xmlChar *name)
size_lt(unsigned int y)
static xmlSAXHandler myHandler
ParserContext< T, R, RFilter > myContext
void(* TableFiller)(const std::vector< R * > &, std::map< AtmPhaseCorrectionMod::AtmPhaseCorrection, ASDM2MSFiller * > &)
Informational log messages with with time, priority, and origin.
Definition: LogMessage.h:101
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
T getenv(const char *name, const T defaultVal)
std::string displaySet(const std::set< T > &aSet)
A template function which returns a string representing the contents of a set of elements of type T...
unsigned int y
bool timeIntervalIntersectsAScan(T *row, const std::vector< asdm::ScanRow * > &scans)
A function which returns true if and only there is at least one element in the vector &#39;scans&#39; for whi...