Lemma is an Electromagnetics API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AEMSurveyReader.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 09/24/2013 04:36:14 PM
  11. * @version $Id$
  12. * @author Trevor Irons (ti)
  13. * @email Trevor.Irons@xri-geo.com
  14. * @copyright Copyright (c) 2013, XRI Geophysics, LLC
  15. * @copyright Copyright (c) 2013, Trevor Irons
  16. */
  17. #include "AEMSurveyReader.h"
  18. namespace Lemma {
  19. // ==================== FRIEND METHODS =====================
  20. std::ostream &operator<<(std::ostream &stream, const AEMSurveyReader &ob) {
  21. stream << ob.Serialize() << "\n";
  22. return stream;
  23. }
  24. // ==================== LIFECYCLE =======================
  25. //--------------------------------------------------------------------------------------
  26. // Class: AEMSurveyReader
  27. // Method: AEMSurveyReader
  28. // Description: constructor (protected)
  29. //--------------------------------------------------------------------------------------
  30. AEMSurveyReader::AEMSurveyReader (const ctor_key& key) : LemmaObject(key), Survey(nullptr) {
  31. } // ----- end of method AEMSurveyReader::AEMSurveyReader (constructor) -----
  32. //--------------------------------------------------------------------------------------
  33. // Class: AEMSurveyReader
  34. // Method: New()
  35. // Description: public constructor
  36. //--------------------------------------------------------------------------------------
  37. std::shared_ptr<AEMSurveyReader> AEMSurveyReader::NewSP() {
  38. return std::make_shared<AEMSurveyReader>(ctor_key());
  39. }
  40. //--------------------------------------------------------------------------------------
  41. // Class: AEMSurveyReader
  42. // Method: ~AEMSurveyReader
  43. // Description: destructor (protected)
  44. //--------------------------------------------------------------------------------------
  45. AEMSurveyReader::~AEMSurveyReader () {
  46. } // ----- end of method AEMSurveyReader::~AEMSurveyReader (destructor) -----
  47. //--------------------------------------------------------------------------------------
  48. // Class: AEMSurveyReader
  49. // Method: GetSurvey
  50. //--------------------------------------------------------------------------------------
  51. std::shared_ptr<AEMSurvey> AEMSurveyReader::GetSurvey ( ) {
  52. return Survey;
  53. } // ----- end of method AEMSurveyReader::GetSurvey -----
  54. //--------------------------------------------------------------------------------------
  55. // Class: AEMSurveyReader
  56. // Method: ReadASCIIAEMFile
  57. //--------------------------------------------------------------------------------------
  58. void AEMSurveyReader::ReadASCIIAEMFile ( const std::string& fname ) {
  59. Survey = AEMSurvey::NewSP();
  60. auto Parser = ASCIIParser::NewSP();
  61. Parser->SetCommentString("//");
  62. Parser->Open(fname);
  63. // Sanity check
  64. std::vector<int> ivals = Parser->ReadInts(1);
  65. std::vector<Real> freqs;
  66. for (int isc=0; isc<ivals[0]; ++isc) {
  67. Real df = Parser->ReadReals(1)[0]; // frequency Hz
  68. bool unique = true;
  69. for (unsigned int ifreq=0; ifreq<freqs.size(); ++ifreq) {
  70. if (df - freqs[ifreq] < 1e-2) {
  71. unique = false;
  72. }
  73. }
  74. if (unique) freqs.push_back(df);
  75. std::string DT = Parser->ReadStrings(1)[0];
  76. if (DT == "MD" || DT == "ED") {
  77. } else {
  78. std::cerr << "In AEMSurveyReader::ReadASCIIAEMFile. The source type: "
  79. << DT << " is not supported.\n";
  80. std::exit(EXIT_FAILURE);
  81. }
  82. Parser->ReadReals(4); // position and moment
  83. }
  84. Survey->Freqs = VectorXr::Map(&freqs[0], freqs.size());
  85. // OK, now get cracking, those sources at every location
  86. int nb = Parser->ReadInts(1)[0]; // number of locations
  87. for (int ib=0; ib<nb; ++ib) {
  88. std::vector<Real> rvals = Parser->ReadReals(6); // position and moment
  89. std::streamoff bp = Parser->GetFileLocation( );
  90. Parser->JumpToLocation( 0 );
  91. //std::vector<int> ivals = Parser->ReadInts(1); // number of frequencies
  92. int nf = Parser->ReadInts(1)[0]; // number of frequencies
  93. for (int isc=0; isc<nf; ++isc) {
  94. Survey->Sources.push_back(DipoleSource::NewSP());
  95. size_t cnt = Survey->Sources.size() - 1; //
  96. // and now set it
  97. Survey->Sources[cnt]->SetNumberOfFrequencies(1);
  98. Survey->Sources[cnt]->SetFrequency(0, Parser->ReadReals(1)[0]);
  99. std::string DT = Parser->ReadStrings(1)[0];
  100. if (DT == "MD") {
  101. Survey->Sources[cnt]->SetType(MAGNETICDIPOLE);
  102. } else if (DT == "ED") {
  103. Survey->Sources[cnt]->SetType(UNGROUNDEDELECTRICDIPOLE);
  104. } else {
  105. std::cerr << "In AEMSurveyReader::ReadASCIIAEMFile. The source type: "
  106. << DT << " is not supported.\n";
  107. std::exit(EXIT_FAILURE);
  108. }
  109. std::vector<Real> irvals = Parser->ReadReals(4); // position and moment
  110. Survey->Sources[cnt]->SetMoment(irvals[3]);
  111. Survey->Sources[cnt]->SetLocation(rvals[0] + irvals[0], rvals[1] + irvals[1],
  112. rvals[2] + irvals[2]);
  113. Survey->Sources[cnt]->SetPolarisation(rvals[3], rvals[4], rvals[5]);
  114. }
  115. Parser->JumpToLocation(bp);
  116. }
  117. return ;
  118. } // ----- end of method AEMSurveyReader::ReadASCIIAEMFile -----
  119. } // ----- end of Lemma name -----