diff --git a/ApplicationLibCode/UnitTests/RifRmsWellPathReader-Test.cpp b/ApplicationLibCode/UnitTests/RifRmsWellPathReader-Test.cpp index a1b00bcfab..a265974d28 100644 --- a/ApplicationLibCode/UnitTests/RifRmsWellPathReader-Test.cpp +++ b/ApplicationLibCode/UnitTests/RifRmsWellPathReader-Test.cpp @@ -41,3 +41,21 @@ TEST( RifRmsWellPathReader, ReadFromFile ) EXPECT_EQ( 4647u, wellPath->wellPathPoints().size() ); EXPECT_EQ( 4647u, wellPath->measuredDepths().size() ); } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +TEST( RifRmsWellPathReader, ReadFromFileWithSpaceInName ) +{ + QString filePath = TEST_DATA_DIRECTORY + "well_with_space.rmswell"; + auto wellData = RifRmsWellPathReader::readWellData( filePath ); + EXPECT_EQ( wellData.m_name, QString( "55 33-1" ) ); + + auto wellPath = wellData.m_wellPathGeometry; + ASSERT_TRUE( wellPath.notNull() ); + + ASSERT_TRUE( wellPath->hasDatumElevation() ); + EXPECT_DOUBLE_EQ( 25.0, wellPath->datumElevation() ); + EXPECT_EQ( 2u, wellPath->wellPathPoints().size() ); + EXPECT_EQ( 2u, wellPath->measuredDepths().size() ); +} diff --git a/ApplicationLibCode/UnitTests/TestData/RifRmsWellPathReader/well_with_space.rmswell b/ApplicationLibCode/UnitTests/TestData/RifRmsWellPathReader/well_with_space.rmswell new file mode 100644 index 0000000000..df14efbb47 --- /dev/null +++ b/ApplicationLibCode/UnitTests/TestData/RifRmsWellPathReader/well_with_space.rmswell @@ -0,0 +1,7 @@ +1.0 +Unknown +55 33-1 462480.0 5934232.0 25.0 +1 +MDepth CONT linear +462480.0 5934232.0 -25.0 0.0 +462480.0 5934232.0 -24.5 0.5 diff --git a/ThirdParty/NRLib/nrlib/well/rmswell.cpp b/ThirdParty/NRLib/nrlib/well/rmswell.cpp index f72c694812..7d74d7c989 100644 --- a/ThirdParty/NRLib/nrlib/well/rmswell.cpp +++ b/ThirdParty/NRLib/nrlib/well/rmswell.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -45,18 +46,60 @@ RMSWell::RMSWell(const std::string& filename) getline(file,line2_); int line = 0; std::string token; - std::string wellName = ReadNext(file, line); - SetWellName(wellName); - xpos0_ = ReadNext(file, line); - ypos0_ = ReadNext(file, line); // read wellname and positions - try { - rkb_ = ReadNext(file, line); - } catch (std::exception &) { - // Ignore errors: not guaranteed to get rkb + + // Read the entire line containing well name and coordinates + // The well name may contain spaces, so we parse from the end to find coordinates first + std::string line3; + getline(file, line3); + + // Tokenize the line + std::istringstream iss(line3); + std::vector tokens; + std::string t; + while (iss >> t) { + tokens.push_back(t); } - // getline(file,dummy);// Line may contain a dummy number - //-----line shift - DiscardRestOfLine(file, line, false); + + // Need at least 3 tokens: wellname (1+), x, y (rkb is optional) + if (tokens.size() < 3) { + throw FileFormatError("Invalid RMS well format: line 3 must contain well name, x, and y coordinates"); + } + + // Parse from end to find where coordinates start + // Work backwards: try to parse as many trailing numbers as possible (up to 3: x, y, rkb) + size_t coordStart = tokens.size(); + int numCoords = 0; + + for (size_t i = tokens.size(); i > 0 && numCoords < 3; --i) { + try { + ParseType(tokens[i-1]); + coordStart = i - 1; + numCoords++; + } catch (...) { + break; // Stop when we hit a non-number (part of well name) + } + } + + // Must have at least x and y coordinates + if (numCoords < 2) { + throw FileFormatError("Invalid RMS well format: could not parse x and y coordinates"); + } + + // Well name is everything before the coordinates + std::string wellName; + for (size_t i = 0; i < coordStart; ++i) { + if (i > 0) wellName += " "; + wellName += tokens[i]; + } + SetWellName(wellName); + + // Parse coordinates (in order: x, y, [rkb]) + xpos0_ = ParseType(tokens[coordStart]); + ypos0_ = ParseType(tokens[coordStart + 1]); + if (coordStart + 2 < tokens.size()) { + rkb_ = ParseType(tokens[coordStart + 2]); + } + nlog = ReadNext(file, line); // read number of logs DiscardRestOfLine(file, line, false); lognames_.resize(nlog+3);