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.

RectilinearGridReader.cpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* This file is part of Lemma, a geophysical modelling and inversion API */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. */
  6. /**
  7. @file
  8. @date 09/23/2013 11:05:48 AM
  9. @version $Id$
  10. @author Trevor Irons (ti)
  11. @email Trevor.Irons@xri-geo.com
  12. @copyright Copyright (c) 2013, XRI Geophysics, LLC
  13. @copyright Copyright (c) 2013, Trevor Irons
  14. **/
  15. #include "RectilinearGridReader.h"
  16. namespace Lemma {
  17. // ==================== FRIEND METHODS =====================
  18. std::ostream &operator << (std::ostream &stream, const RectilinearGridReader &ob) {
  19. stream << ob.Serialize() << "\n---\n"; // End of doc
  20. return stream;
  21. }
  22. // ==================== LIFECYCLE =======================
  23. //--------------------------------------------------------------------------------------
  24. // Class: RectilinearGridReader
  25. // Method: RectilinearGridReader
  26. // Description: constructor (locked)
  27. //--------------------------------------------------------------------------------------
  28. RectilinearGridReader::RectilinearGridReader ( const ctor_key& ) : GridReader( ),
  29. rGrid( nullptr ), Parser( nullptr ) {
  30. } // ----- end of method RectilinearGridReader::RectilinearGridReader (constructor) -----
  31. //--------------------------------------------------------------------------------------
  32. // Class: RectilinearGridReader
  33. // Method: RectilinearGridReader
  34. // Description: DeSerializing constructor (protected)
  35. //--------------------------------------------------------------------------------------
  36. RectilinearGridReader::RectilinearGridReader (const YAML::Node& node, const ctor_key& key) : GridReader(node) {
  37. } // ----- end of method RectilinearGridReader::RectilinearGridReader (constructor) -----
  38. //--------------------------------------------------------------------------------------
  39. // Class: RectilinearGridReader
  40. // Method: NewSP()
  41. // Description: constructor
  42. //--------------------------------------------------------------------------------------
  43. std::shared_ptr< RectilinearGridReader > RectilinearGridReader::NewSP() {
  44. return std::make_shared< RectilinearGridReader > ( ctor_key() );
  45. }
  46. //--------------------------------------------------------------------------------------
  47. // Class: RectilinearGridReader
  48. // Method: ~RectilinearGridReader
  49. // Description: destructor (protected)
  50. //--------------------------------------------------------------------------------------
  51. RectilinearGridReader::~RectilinearGridReader () {
  52. } // ----- end of method RectilinearGridReader::~RectilinearGridReader (destructor) -----
  53. //--------------------------------------------------------------------------------------
  54. // Class: ASCIIParser
  55. // Method: Serialize
  56. //--------------------------------------------------------------------------------------
  57. YAML::Node RectilinearGridReader::Serialize ( ) const {
  58. YAML::Node node = GridReader::Serialize();;
  59. node.SetTag( GetName() );
  60. //node["CommentString"] = CommentString;
  61. //node["BufferSize"] = BufferSize;
  62. return node;
  63. } // ----- end of method GridReader::Serialize -----
  64. //--------------------------------------------------------------------------------------
  65. // Class: RectilinearGridReader
  66. // Method: DeSerialize
  67. //--------------------------------------------------------------------------------------
  68. std::shared_ptr<RectilinearGridReader> RectilinearGridReader::DeSerialize ( const YAML::Node& node ) {
  69. if (node.Tag() != "RectilinearGridReader") {
  70. throw DeSerializeTypeMismatch( "RectilinearGridReader", node.Tag());
  71. }
  72. return std::make_shared< RectilinearGridReader >( node, ctor_key() ); //, ctor_key() );
  73. } // ----- end of method GridReader::DeSerialize -----
  74. //--------------------------------------------------------------------------------------
  75. // Class: RectilinearGridReader
  76. // Method: GetName
  77. // Description: Class identifier
  78. //--------------------------------------------------------------------------------------
  79. inline std::string RectilinearGridReader::GetName ( ) const {
  80. return CName;
  81. } // ----- end of method RectilinearGridReader::get_GetName -----
  82. //--------------------------------------------------------------------------------------
  83. // Class: RectilinearGridReader
  84. // Method: ReadASCIIGridFile
  85. //--------------------------------------------------------------------------------------
  86. void RectilinearGridReader::ReadASCIIGridFile ( const std::string& name ) {
  87. rGrid = RectilinearGrid::NewSP();
  88. Parser = ASCIIParser::NewSP();
  89. Parser->SetCommentString("//");
  90. Parser->Open(name);
  91. std::vector<int> vals = Parser->ReadInts(3);
  92. rGrid->SetDimensions(vals[0], vals[1], vals[2]);
  93. std::vector<Real> rvals = Parser->ReadReals(3);
  94. rGrid->SetOffset(rvals[0], rvals[1], rvals[2]);
  95. rvals.clear();
  96. rvals = Parser->ReadReals( vals[0] );
  97. VectorXr temp = VectorXr::Map(&rvals[0], vals[0]);
  98. VectorXr hx = temp;
  99. rvals.clear();
  100. rvals = Parser->ReadReals( vals[1] );
  101. VectorXr hy = VectorXr::Map(&rvals[0], vals[1]);
  102. rvals.clear();
  103. rvals = Parser->ReadReals( vals[2] );
  104. VectorXr hz = VectorXr::Map(&rvals[0], vals[2]);
  105. rGrid->SetSpacing(hx, hy, hz);
  106. // Read in model(s)/data? Where should this be done?
  107. Parser->Close();
  108. return;
  109. } // ----- end of method RectilinearGridReader::ReadSCIIGridFile -----
  110. //--------------------------------------------------------------------------------------
  111. // Class: RectilinearGridReader
  112. // Method: GetGrid
  113. //--------------------------------------------------------------------------------------
  114. std::shared_ptr<Grid> RectilinearGridReader::GetGrid ( ) {
  115. return std::static_pointer_cast<Grid> (rGrid);
  116. } // ----- end of method RectilinearGridReader::GetGrid -----
  117. } // ----- end of Lemma name -----