Skip to content

Commit

Permalink
#9773 Add summary reader for Reveal csv data.
Browse files Browse the repository at this point in the history
  • Loading branch information
kriben committed Mar 13, 2023
1 parent 836822c commit 19d9bb6
Show file tree
Hide file tree
Showing 11 changed files with 1,266 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ApplicationLibCode/FileInterface/CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RifPressureDepthTextFileReader.h
${CMAKE_CURRENT_LIST_DIR}/RifReaderPressureDepthData.h
${CMAKE_CURRENT_LIST_DIR}/RifOpmGridTools.h
${CMAKE_CURRENT_LIST_DIR}/RifRevealSummaryCsvReader.h
${CMAKE_CURRENT_LIST_DIR}/RifRevealCsvSectionSummaryReader.h
)

set(SOURCE_GROUP_SOURCE_FILES
Expand Down Expand Up @@ -157,6 +159,8 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RifPressureDepthTextFileReader.cpp
${CMAKE_CURRENT_LIST_DIR}/RifReaderPressureDepthData.cpp
${CMAKE_CURRENT_LIST_DIR}/RifOpmGridTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RifRevealCsvSummaryReader.cpp
${CMAKE_CURRENT_LIST_DIR}/RifRevealCsvSectionSummaryReader.cpp
)

list(APPEND CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})
Expand Down
14 changes: 13 additions & 1 deletion ApplicationLibCode/FileInterface/RifCsvUserDataParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,19 @@ bool RifCsvUserDataParser::parseColumnInfo( QTextStream* dataStream, const Ascii
while ( !headerFound )
{
QString line = dataStream->readLine();
if ( line.trimmed().isEmpty() ) continue;
if ( line.trimmed().isEmpty() )
{
if ( !headerFound && dataStream->atEnd() )
{
// Handle empty stream
return false;
}
else
{
// Empty lines are skipped.
continue;
}
}

QStringList columnHeaders = RifFileParseTools::splitLineAndTrim( line, parseOptions.cellSeparator );

Expand Down
175 changes: 175 additions & 0 deletions ApplicationLibCode/FileInterface/RifRevealCsvSectionSummaryReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#include "RifRevealCsvSectionSummaryReader.h"

#include "RiaDateStringParser.h"
#include "RiaLogging.h"
#include "RiaQDateTimeTools.h"

#include "RifCsvUserDataParser.h"
#include "RifEclipseUserDataKeywordTools.h"
#include "RifEclipseUserDataParserTools.h"

#include "cafUtils.h"

#include <QDateTime>
#include <QFile>
#include <QTextStream>

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifRevealCsvSectionSummaryReader::RifRevealCsvSectionSummaryReader()
{
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifRevealCsvSectionSummaryReader::~RifRevealCsvSectionSummaryReader()
{
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifRevealCsvSectionSummaryReader::parse( const QString& text, QString* errorText )
{
m_allResultAddresses.clear();
m_mapFromAddressToResultIndex.clear();

AsciiDataParseOptions parseOptions;
parseOptions.useCustomDateTimeFormat = true;
parseOptions.dateTimeFormat = "dd.MM.yyyy hh:mm:ss";
parseOptions.fallbackDateTimeFormat = "dd.MM.yyyy";
parseOptions.cellSeparator = ",";
parseOptions.decimalSeparator = ".";
parseOptions.timeSeriesColumnName = "Date";

m_parser = std::unique_ptr<RifCsvUserDataPastedTextParser>( new RifCsvUserDataPastedTextParser( text, errorText ) );
if ( !m_parser->parse( parseOptions ) )
{
RiaLogging::error( QString( "Failed to parse file" ) );
return false;
}

buildTimeStepsAndMappings();

return true;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifRevealCsvSectionSummaryReader::values( const RifEclipseSummaryAddress& resultAddress, std::vector<double>* values ) const
{
auto search = m_mapFromAddressToResultIndex.find( resultAddress );
if ( search != m_mapFromAddressToResultIndex.end() )
{
size_t columnIndex = search->second;

const Column* ci = m_parser->columnInfo( columnIndex );
if ( !ci ) return false;

values->clear();
values->reserve( ci->values.size() );
for ( double val : ci->values )
{
values->push_back( val );
}
}

return true;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<time_t> RifRevealCsvSectionSummaryReader::timeSteps( const RifEclipseSummaryAddress& resultAddress ) const
{
// First, check whether date time values exist for the current address
auto search = m_mapFromAddressToResultIndex.find( resultAddress );
if ( search != m_mapFromAddressToResultIndex.end() )
{
size_t index = m_mapFromAddressToResultIndex.at( resultAddress );
if ( !m_parser->tableData().columnInfos()[index].dateTimeValues.empty() )
{
return m_parser->tableData().columnInfos()[index].dateTimeValues;
}
}

// Then check for a separate date time column
int index = m_parser->tableData().dateTimeColumnIndex();
if ( index >= 0 )
{
return m_parser->tableData().columnInfos()[index].dateTimeValues;
}

return {};
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::string RifRevealCsvSectionSummaryReader::unitName( const RifEclipseSummaryAddress& resultAddress ) const
{
auto search = m_mapFromAddressToResultIndex.find( resultAddress );
if ( search != m_mapFromAddressToResultIndex.end() )
{
size_t columnIndex = search->second;

const Column* ci = m_parser->columnInfo( columnIndex );
if ( ci )
{
return ci->unitName;
}
}

return "";
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaDefines::EclipseUnitSystem RifRevealCsvSectionSummaryReader::unitSystem() const
{
return RiaDefines::EclipseUnitSystem::UNITS_UNKNOWN;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifRevealCsvSectionSummaryReader::buildTimeStepsAndMappings()
{
auto tableData = m_parser->tableData();

for ( size_t columnIndex = 0; columnIndex < tableData.columnInfos().size(); columnIndex++ )
{
const Column& ci = tableData.columnInfos()[columnIndex];
if ( ci.dataType == Column::NUMERIC )
{
RifEclipseSummaryAddress sumAddress = ci.summaryAddress;

m_allResultAddresses.insert( sumAddress );
if ( sumAddress.isErrorResult() ) m_allErrorAddresses.insert( sumAddress );

m_mapFromAddressToResultIndex[sumAddress] = columnIndex;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "RifSummaryReaderInterface.h"

#include "SummaryPlotCommands/RicPasteAsciiDataToSummaryPlotFeatureUi.h"

#include <map>
#include <memory>
#include <vector>

class QString;

class RifCsvUserDataParser;
class RifEclipseSummaryAddress;

//==================================================================================================
//
//
//==================================================================================================
class RifRevealCsvSectionSummaryReader : public RifSummaryReaderInterface
{
public:
RifRevealCsvSectionSummaryReader();
~RifRevealCsvSectionSummaryReader() override;

bool parse( const QString& fileName, QString* errorText = nullptr );

std::vector<time_t> timeSteps( const RifEclipseSummaryAddress& resultAddress ) const override;

bool values( const RifEclipseSummaryAddress& resultAddress, std::vector<double>* values ) const override;

std::string unitName( const RifEclipseSummaryAddress& resultAddress ) const override;

RiaDefines::EclipseUnitSystem unitSystem() const override;

private:
void buildTimeStepsAndMappings();

private:
std::unique_ptr<RifCsvUserDataParser> m_parser;

std::map<RifEclipseSummaryAddress, size_t> m_mapFromAddressToResultIndex;
};
75 changes: 75 additions & 0 deletions ApplicationLibCode/FileInterface/RifRevealCsvSummaryReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#include "RifRevealCsvSummaryReader.h"

#include "RifCsvUserDataParser.h"
#include "RifRevealCsvSectionSummaryReader.h"

#include <QFile>
#include <QTextStream>
#include <Qt>

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifRevealCsvSummaryReader::RifRevealCsvSummaryReader()
{
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifRevealCsvSummaryReader::~RifRevealCsvSummaryReader()
{
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifRevealCsvSummaryReader::parse( const QString& fileName, QString* errorText )
{
QFile file( fileName );

if ( !file.open( QFile::ReadOnly | QFile::Text ) ) return false;

QTextStream in( &file );

// Skip first line
in.readLine();

// Split files on strange header line (starts with ",Date").
QString fileContents = in.readAll();
QStringList parts = fileContents.split( ",Date", QString::SkipEmptyParts );

// Parse each section separately
for ( auto p : parts )
{
p.prepend( "Name,Date" );
cvf::ref<RifRevealCsvSectionSummaryReader> sectionReader = new RifRevealCsvSectionSummaryReader;

addReader( sectionReader.p() );
QString errorMessage;
if ( !sectionReader->parse( p, &errorMessage ) )
{
return false;
}
}

return true;
}
36 changes: 36 additions & 0 deletions ApplicationLibCode/FileInterface/RifRevealCsvSummaryReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "RifMultipleSummaryReaders.h"

class QString;

//==================================================================================================
//
//
//==================================================================================================
class RifRevealCsvSummaryReader : public RifMultipleSummaryReaders
{
public:
RifRevealCsvSummaryReader();
~RifRevealCsvSummaryReader() override;

bool parse( const QString& fileName, QString* errorText = nullptr );
};
2 changes: 2 additions & 0 deletions ApplicationLibCode/UnitTests/CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaSummaryStringTools-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaVariableMapper-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifPressureDepthTextFileReader-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifRevealCsvSectionSummaryReader-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifRevealCsvSummaryReader-Test.cpp
)

if(RESINSIGHT_ENABLE_GRPC)
Expand Down
Loading

0 comments on commit 19d9bb6

Please sign in to comment.