Main Lemma Repository

utXMLParse.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* This file is part of Lemma, a geophysical modelling and inversion API.
  2. * More information is available at http://lemmasoftware.org
  3. */
  4. /* This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file
  10. * @date 04/03/2014 01:08:28 PM
  11. * @version $Id$
  12. * @author Trevor Irons (ti)
  13. * @email Trevor.Irons@xri-geo.com
  14. * @copyright Copyright (c) 2014, XRI Geophysics, LLC
  15. * @copyright Copyright (c) 2014, Trevor Irons
  16. */
  17. #include "Lemma"
  18. #ifdef TINYXMLSUPPORT
  19. #include "tinyxml2.h"
  20. #include <ctime>
  21. using namespace tinyxml2;
  22. using namespace Lemma;
  23. void ParseTEM( XMLElement* element );
  24. template <typename T>
  25. std::vector<T> ReadLine( const int& nr, const std::string& line ) ;
  26. int main(int argc, char **argv) {
  27. if ( argc > 1 ) {
  28. XMLDocument* doc = new XMLDocument();
  29. clock_t startTime = clock();
  30. doc->LoadFile( argv[1] );
  31. clock_t loadTime = clock();
  32. int errorID = doc->ErrorID();
  33. // Get some info
  34. //static const char* xml = "<element/>";
  35. //doc->Parse( xml );
  36. //XMLNode* titleElement = doc->FirstChild(); //Element()->FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
  37. XMLElement* titleElement = doc->FirstChildElement( ); //->FirstChildElement( "TITLE" );
  38. //const char* title = titleElement->Value(); //GetText();
  39. const char* title = titleElement->Value(); //GetText();
  40. printf( "Test file '%s' loaded. ErrorID=%d\n", argv[1], errorID );
  41. printf( "Name of FirstChildElement: %s\n", title );
  42. if (strcmp ( titleElement->Value(), "NGXFile" ) != 0) {
  43. std::cout <<"NGX file was not detected!" << std::endl;
  44. exit(EXIT_FAILURE);
  45. } else {
  46. //std::cout <<
  47. //titleElement->FirstAttribute() ;//"type");// << std::endl;;
  48. if (strcmp( titleElement->FindAttribute("type")->Value(),"tem") == 0) {
  49. // Parse TEM file
  50. ParseTEM( titleElement );
  51. } else {
  52. std::cout << "No parser for NGX filetype " << titleElement->FindAttribute("type")->Value() << std::endl;
  53. std::cout << "YOU should write one!\n";
  54. exit(EXIT_FAILURE);
  55. }
  56. }
  57. delete doc; doc = 0;
  58. clock_t deleteTime = clock();
  59. if ( !errorID ) {
  60. printf( "========================================================\n");
  61. printf( "Load time=%u\n", (unsigned)(loadTime - startTime) );
  62. printf( "Delete time=%u\n", (unsigned)(deleteTime - loadTime) );
  63. printf( "Total time=%u\n", (unsigned)(deleteTime - startTime) );
  64. }
  65. exit(0);
  66. } else {
  67. std::cout << "Enter NGX.x file to parse\n";
  68. }
  69. }
  70. void ParseTEM( XMLElement* element ) {
  71. std::cout << "This is a NGX.t file" << std::endl;
  72. if (strcmp( element->FindAttribute("version")->Value(),"0.1") == 0) {
  73. std::cout << "Version 0.1 detected" << std::endl;
  74. } else {
  75. std::cout << "Unsupported NGX.t version" << std::endl;
  76. }
  77. // system description
  78. std::cout << "System description" << std::endl;
  79. std::cout << element->FirstChildElement("system")->FirstChildElement("name")->GetText() << std::endl;
  80. XMLElement* system = element->FirstChildElement("system");
  81. int ntx = atoi(system->FirstChildElement("transmitters")->FindAttribute("num")->Value());
  82. std::cout << "numTx = " << ntx << std::endl;
  83. // OK Parse Transmitter(s)
  84. XMLElement* tx = system->FirstChildElement("transmitters")->FirstChildElement("transmitter");
  85. // std::cout << "Tx id = " << tx->FindAttribute("id")->Value() << std::endl;
  86. //std::cout << tx->FirstChildElement("loop.coordinates")->FindAttribute("npoint")->Value();//->FirstChildElement("northingPoints")->Value() << std::endl;
  87. //std::cout << tx->FirstChildElement("loop.coordinates")->FirstChildElement("northing.points")->GetText();//->FirstChildElement("northingPoints")->Value() << std::endl;
  88. for (int it=0; it<ntx; ++it) {
  89. std::cout << "\nTransmitter " << it << std::endl;
  90. if (it > 0) tx = tx->NextSiblingElement();
  91. int npoints = atoi(tx->FirstChildElement("loop.coordinates")->FindAttribute("npoint")->Value());
  92. std::cout << "points= " << npoints; // tx->FirstChildElement("loop.coordinates")->FindAttribute("npoint")->Value();//->FirstChildElement("northingPoints")->Value() << std::endl;
  93. std::vector<Real> LP = ReadLine<Real>( npoints, std::string(tx->FirstChildElement("loop.coordinates")->FirstChildElement("northing.points")->GetText()) );
  94. for (int ii=0; ii<npoints; ++ii) std::cout << "\t"<< LP[ii] ;
  95. //std::cout << tx->FirstChildElement("loop.coordinates")->FirstChildElement("northing.points")->GetText();//->FirstChildElement("northingPoints")->Value() << std::endl;
  96. //std::cout << tx->FirstChildElement("loop.coordinates")->FirstChildElement("easting.points")->GetText();//->FirstChildElement("northingPoints")->Value() << std::endl;
  97. }
  98. std::cout << "\nEND system description\n";
  99. }
  100. template <typename T>
  101. std::vector<T> ReadLine( const int& nr, const std::string& line ) {
  102. std::vector<T> lineData;
  103. std::stringstream lineStream(line);
  104. T value;
  105. while(lineStream >> value) {
  106. lineData.push_back(value);
  107. }
  108. return lineData;
  109. }
  110. #else
  111. int main() {
  112. std::cout << "you have to compile lemma with external tinyxml library to use this" << std::endl;
  113. }
  114. #endif
  115. //--------------------------------------------------------------------------------------
  116. // Class: ASCIIParser
  117. // Method: ReadInts
  118. //--------------------------------------------------------------------------------------
  119. // std::vector<int> ReadInts ( const int& nr ) {
  120. // std::string buf;
  121. // char *dump = new char[BufferSize];
  122. // std::vector<int> vals(0);
  123. // while (input >> buf) {
  124. // if (buf.substr(0, CommentString.size()) == CommentString) {
  125. // input.getline(dump, BufferSize);
  126. // } else {
  127. // vals.push_back( atoi(buf.c_str() ));
  128. // }
  129. // if (static_cast<int>(vals.size()) == nr) {
  130. // delete [] dump;
  131. // return vals;
  132. // }
  133. //
  134. // }
  135. // delete [] dump;
  136. // return vals;
  137. // } // ----- end of method ASCIIParser::ReadInts -----