mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#11437 Propagate detected date format to line based parsing
Add try/catch to avoid crash Remove flag to avoid eternal loop
This commit is contained in:
parent
d1d3ee129f
commit
c7fbb5374c
@ -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();
|
||||
|
@ -88,7 +88,7 @@ bool RifCsvUserDataParser::parse( const RifAsciiDataParseOptions&
|
||||
const std::map<QString, QString>& nameMapping,
|
||||
const std::map<QString, std::pair<QString, double>>& 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ private:
|
||||
bool parseColumnBasedData( const RifAsciiDataParseOptions& parseOptions,
|
||||
const std::map<QString, QString>& nameMapping = {},
|
||||
const std::map<QString, std::pair<QString, double>>& unitMapping = {} );
|
||||
bool parseLineBasedData();
|
||||
bool parseLineBasedData( const RifAsciiDataParseOptions& parseOptions );
|
||||
static QDateTime tryParseDateTime( const std::string& colData, const QString& format );
|
||||
|
||||
private:
|
||||
|
Loading…
Reference in New Issue
Block a user