diff --git a/ApplicationLibCode/Commands/RicImportObservedDataFeature.cpp b/ApplicationLibCode/Commands/RicImportObservedDataFeature.cpp index d770ca5bc5..4118607d6f 100644 --- a/ApplicationLibCode/Commands/RicImportObservedDataFeature.cpp +++ b/ApplicationLibCode/Commands/RicImportObservedDataFeature.cpp @@ -60,36 +60,28 @@ void RicImportObservedDataFeature::selectObservedDataFileInDialog() for ( const QString& fileName : fileNames ) { - bool retryImport = false; + QString errorText; - do + if ( fileName.endsWith( ".rsm", Qt::CaseInsensitive ) ) { - QString errorText; + observedData = observedDataCollection->createAndAddRsmObservedSummaryDataFromFile( fileName, &errorText ); + } + else if ( fileName.endsWith( ".txt", Qt::CaseInsensitive ) || fileName.endsWith( ".csv", Qt::CaseInsensitive ) ) + { + bool useSavedFieldValuesInDialog = false; + observedData = + observedDataCollection->createAndAddCvsObservedSummaryDataFromFile( fileName, useSavedFieldValuesInDialog, &errorText ); + } + else + { + errorText = "Not able to import file. Make sure '*.rsm' is used as extension if data is in RMS format " + "or '*.txt' or '*.csv' if data is in CSV format."; + } - if ( fileName.endsWith( ".rsm", Qt::CaseInsensitive ) ) - { - observedData = observedDataCollection->createAndAddRsmObservedSummaryDataFromFile( fileName, &errorText ); - retryImport = false; - } - else if ( fileName.endsWith( ".txt", Qt::CaseInsensitive ) || fileName.endsWith( ".csv", Qt::CaseInsensitive ) ) - { - bool useSavedFieldValuesInDialog = retryImport; - observedData = - observedDataCollection->createAndAddCvsObservedSummaryDataFromFile( fileName, useSavedFieldValuesInDialog, &errorText ); - retryImport = !errorText.isEmpty(); - } - else - { - errorText = "Not able to import file. Make sure '*.rsm' is used as extension if data is in RMS format " - "or '*.txt' or '*.csv' if data is in CSV format."; - retryImport = false; - } - - if ( !errorText.isEmpty() ) - { - RiaLogging::errorInMessageBox( nullptr, "Errors detected during import", errorText ); - } - } while ( retryImport ); + if ( !errorText.isEmpty() ) + { + RiaLogging::errorInMessageBox( nullptr, "Errors detected during import", errorText ); + } } RiuPlotMainWindowTools::showPlotMainWindow(); diff --git a/ApplicationLibCode/FileInterface/RifCsvUserDataParser.cpp b/ApplicationLibCode/FileInterface/RifCsvUserDataParser.cpp index dcea1a840d..14277573fe 100644 --- a/ApplicationLibCode/FileInterface/RifCsvUserDataParser.cpp +++ b/ApplicationLibCode/FileInterface/RifCsvUserDataParser.cpp @@ -88,7 +88,7 @@ bool RifCsvUserDataParser::parse( const RifAsciiDataParseOptions& const std::map& nameMapping, const std::map>& unitMapping ) { - if ( determineCsvLayout() == LineBased ) return parseLineBasedData(); + if ( determineCsvLayout() == LineBased ) return parseLineBasedData( parseOptions ); return parseColumnBasedData( parseOptions, nameMapping, unitMapping ); } @@ -617,7 +617,7 @@ bool RifCsvUserDataParser::parseColumnBasedData( const RifAsciiDataParseOptions& //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -bool RifCsvUserDataParser::parseLineBasedData() +bool RifCsvUserDataParser::parseLineBasedData( const RifAsciiDataParseOptions& parseOptions ) { QTextStream* dataStream = openDataStream(); if ( !dataStream ) @@ -682,54 +682,62 @@ bool RifCsvUserDataParser::parseLineBasedData() } } - // DATE - QDateTime dateTime; + try { - auto dateText = dataItems[colIndexes[(size_t)CsvLineBasedColumnType::DATE]].toStdString(); - - dateTime = tryParseDateTime( dateText, ISO_DATE_FORMAT ); - if ( !dateTime.isValid() ) + // DATE + QDateTime dateTime; { - // Try to match date and time - dateTime = tryParseDateTime( dateText, QString( ISO_DATE_FORMAT ) + " " + TIME_FORMAT ); + auto dateText = dataItems[colIndexes[(size_t)CsvLineBasedColumnType::DATE]].toStdString(); + + const auto formats = { parseOptions.dateFormat, QString( ISO_DATE_FORMAT ), QString( ISO_DATE_FORMAT ) + " " + TIME_FORMAT }; + for ( const auto& format : formats ) + { + dateTime = tryParseDateTime( dateText, format ); + if ( dateTime.isValid() ) break; + } + + if ( !dateTime.isValid() ) + { + if ( m_errorText ) + m_errorText->append( + QString( "CSV import: Failed to parse date time value in line %1" ).arg( QString::number( lineCount ) ) ); + throw 0; + } } - if ( !dateTime.isValid() ) + // VALUE { - if ( m_errorText ) - m_errorText->append( - QString( "CSV import: Failed to parse date time value in line %1" ).arg( QString::number( lineCount ) ) ); - throw 0; + bool parseOk = true; + double value = QLocale::c().toDouble( dataItems[colIndexes[(size_t)CsvLineBasedColumnType::VALUE]], &parseOk ); + + if ( !parseOk ) + { + if ( m_errorText ) + m_errorText->append( + QString( "CSV import: Failed to parse numeric value in line %1\n" ).arg( QString::number( lineCount ) ) ); + throw 0; + } + + auto& samples = addressesAndData[addr]; + samples.push_back( std::make_pair( dateTime.toSecsSinceEpoch(), value ) ); + } + + // ERROR VALUE + if ( expectErrorValue ) + { + bool parseOk = true; + double value = QLocale::c().toDouble( dataItems[colIndexes[(size_t)CsvLineBasedColumnType::ERROR_VALUE]], &parseOk ); + + if ( !parseOk ) value = DOUBLE_INF; + + auto& samples = addressesAndData[errAddr]; + samples.push_back( std::make_pair( dateTime.toSecsSinceEpoch(), value ) ); } } - - // VALUE + catch ( ... ) { - bool parseOk = true; - double value = QLocale::c().toDouble( dataItems[colIndexes[(size_t)CsvLineBasedColumnType::VALUE]], &parseOk ); - - if ( !parseOk ) - { - if ( m_errorText ) - m_errorText->append( - QString( "CSV import: Failed to parse numeric value in line %1\n" ).arg( QString::number( lineCount ) ) ); - throw 0; - } - - auto& samples = addressesAndData[addr]; - samples.push_back( std::make_pair( dateTime.toSecsSinceEpoch(), value ) ); - } - - // ERROR VALUE - if ( expectErrorValue ) - { - bool parseOk = true; - double value = QLocale::c().toDouble( dataItems[colIndexes[(size_t)CsvLineBasedColumnType::ERROR_VALUE]], &parseOk ); - - if ( !parseOk ) value = DOUBLE_INF; - - auto& samples = addressesAndData[errAddr]; - samples.push_back( std::make_pair( dateTime.toSecsSinceEpoch(), value ) ); + closeDataStream(); + return false; } } } diff --git a/ApplicationLibCode/FileInterface/RifCsvUserDataParser.h b/ApplicationLibCode/FileInterface/RifCsvUserDataParser.h index f59da377b9..fdbdb84013 100644 --- a/ApplicationLibCode/FileInterface/RifCsvUserDataParser.h +++ b/ApplicationLibCode/FileInterface/RifCsvUserDataParser.h @@ -83,7 +83,7 @@ private: bool parseColumnBasedData( const RifAsciiDataParseOptions& parseOptions, const std::map& nameMapping = {}, const std::map>& unitMapping = {} ); - bool parseLineBasedData(); + bool parseLineBasedData( const RifAsciiDataParseOptions& parseOptions ); static QDateTime tryParseDateTime( const std::string& colData, const QString& format ); private: