///////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2018- 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 // for more details. // ///////////////////////////////////////////////////////////////////////////////// #include "RifElementPropertyReader.h" #include "RiaLogging.h" #include "cvfAssert.h" #include #include //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RifElementPropertyReader::RifElementPropertyReader( const std::vector& elementIdxToId ) : m_elementIdxToId( elementIdxToId ) { } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RifElementPropertyReader::~RifElementPropertyReader() { } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RifElementPropertyReader::addFile( const std::string& fileName ) { RifElementPropertyMetadata metaData = RifElementPropertyTableReader::readMetadata( QString::fromStdString( fileName ) ); for ( QString field : metaData.dataColumns ) { m_fieldsMetaData[field.toStdString()] = metaData; } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RifElementPropertyReader::removeFile( const std::string& fileName ) { std::map tempMetaData; for ( std::pair metaData : m_fieldsMetaData ) { if ( metaData.second.fileName.toStdString() != fileName ) { tempMetaData[metaData.first] = metaData.second; } } m_fieldsMetaData.swap( tempMetaData ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::vector RifElementPropertyReader::scalarElementFields() const { std::vector fields; for ( const std::pair& field : m_fieldsMetaData ) { fields.push_back( field.first ); } return fields; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::map> RifElementPropertyReader::readAllElementPropertiesInFileContainingField( const std::string& fieldName ) { std::map> fieldAndData; if ( m_fieldsMetaData.find( fieldName ) == m_fieldsMetaData.end() ) { return fieldAndData; } RifElementPropertyTable table; RifElementPropertyTableReader::readData( &m_fieldsMetaData[fieldName], &table ); CVF_ASSERT( m_fieldsMetaData[fieldName].dataColumns.size() == table.data.size() ); for ( size_t i = 0; i < table.data.size(); i++ ) { CVF_ASSERT( table.data[i].size() == table.elementIds.size() ); } const std::vector& elementIdsFromFile = table.elementIds; if ( elementIdsFromFile == m_elementIdxToId ) { for ( size_t i = 0; i < table.data.size(); i++ ) { const std::string& currentFieldFromFile = m_fieldsMetaData[fieldName].dataColumns[i].toStdString(); if ( currentFieldFromFile == "MODULUS" ) { const std::vector& currentColumn = table.data[i]; std::vector tempResult( currentColumn.size(), 0 ); for ( float resultItem : currentColumn ) { tempResult[i] = resultItem * 0.000000001; } fieldAndData[currentFieldFromFile].swap( tempResult ); } else { fieldAndData[currentFieldFromFile] = table.data[i]; } } } else if ( elementIdsFromFile.size() > m_elementIdxToId.size() && elementIdsFromFile.size() > m_elementIdToIdx.size() ) { RifElementPropertyReader::outputWarningAboutWrongFileData(); return fieldAndData; } else { if ( m_elementIdToIdx.empty() ) { makeElementIdToIdxMap(); } std::vector fileIdxToElementIdx; fileIdxToElementIdx.reserve( elementIdsFromFile.size() ); for ( int elementId : elementIdsFromFile ) { std::unordered_map::const_iterator it = m_elementIdToIdx.find( elementId ); if ( it == m_elementIdToIdx.end() ) { RifElementPropertyReader::outputWarningAboutWrongFileData(); return fieldAndData; } fileIdxToElementIdx.push_back( it->second ); } for ( size_t i = 0; i < table.data.size(); i++ ) { std::string currentFieldFromFile = m_fieldsMetaData[fieldName].dataColumns[i].toStdString(); const std::vector& currentColumn = table.data[i]; std::vector tempResult( m_elementIdToIdx.size(), HUGE_VAL ); if ( currentFieldFromFile == "MODULUS" ) { for ( size_t j = 0; j < currentColumn.size(); j++ ) { tempResult[fileIdxToElementIdx[j]] = currentColumn[j] * 0.000000001; } } else { for ( size_t j = 0; j < currentColumn.size(); j++ ) { tempResult[fileIdxToElementIdx[j]] = currentColumn[j]; } } fieldAndData[currentFieldFromFile].swap( tempResult ); } } return fieldAndData; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::vector RifElementPropertyReader::fieldsInFile( const std::string& fileName ) const { std::vector fields; for ( std::pair metaData : m_fieldsMetaData ) { if ( metaData.second.fileName.toStdString() == fileName ) { for ( const QString& column : metaData.second.dataColumns ) { fields.push_back( column.toStdString() ); } } } return fields; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RifElementPropertyReader::makeElementIdToIdxMap() { m_elementIdToIdx.reserve( m_elementIdxToId.size() ); for ( size_t i = 0; i < m_elementIdxToId.size(); i++ ) { m_elementIdToIdx[m_elementIdxToId[i]] = (int)i; } std::vector tempVectorForDeletion; m_elementIdxToId.swap( tempVectorForDeletion ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RifElementPropertyReader::outputWarningAboutWrongFileData() { QString warningText = QString( "The chosen result property does not fit the model" ); RiaLogging::errorInMessageBox( nullptr, "Element Property Reader", warningText ); }