mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#2018 CSV import. Fix decimal point/locale
This commit is contained in:
parent
11100fb6e0
commit
28db4c6e22
@ -32,15 +32,17 @@ std::string RiaStdStringTools::trimString(const std::string& s)
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaStdStringTools::isNumber(const std::string& s)
|
||||
bool RiaStdStringTools::isNumber(const std::string& s, char decimalPoint)
|
||||
{
|
||||
if (s.size() == 0) return false;
|
||||
if (findCharMatchCount(s, '.') > 1) return false;
|
||||
if (findCharMatchCount(s, decimalPoint) > 1) return false;
|
||||
if (findCharMatchCount(s, '-') > 1) return false;
|
||||
if (findCharMatchCount(s, 'e') > 1) return false;
|
||||
if (findCharMatchCount(s, 'E') > 1) return false;
|
||||
|
||||
return (s.find_first_not_of("0123456789.eE-") == std::string::npos);
|
||||
std::string matchChars("0123456789eE-");
|
||||
matchChars.append(1, decimalPoint);
|
||||
return (s.find_first_not_of(matchChars) == std::string::npos);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -31,7 +31,7 @@ class RiaStdStringTools
|
||||
{
|
||||
public:
|
||||
static std::string trimString(const std::string& s);
|
||||
static bool isNumber(const std::string& s);
|
||||
static bool isNumber(const std::string& s, char decimalPoint);
|
||||
|
||||
static int toInt(const std::string& s);
|
||||
static double toDouble(const std::string& s);
|
||||
|
@ -162,7 +162,7 @@ std::vector<RimAsciiDataCurve*> RicPasteAsciiDataToSummaryPlotFeature::parseCurv
|
||||
{
|
||||
std::vector<RimAsciiDataCurve*> curves;
|
||||
const AsciiDataParseOptions& parseOptions = settings.parseOptions();
|
||||
RifColumnBasedAsciiParser parser = RifColumnBasedAsciiParser(data, parseOptions.dateTimeFormat(), parseOptions.decimalSeparator, parseOptions.cellSeparator);
|
||||
RifColumnBasedAsciiParser parser = RifColumnBasedAsciiParser(data, parseOptions.dateTimeFormat(), parseOptions.locale, parseOptions.cellSeparator);
|
||||
|
||||
if (parser.headers().empty())
|
||||
{
|
||||
|
@ -148,18 +148,22 @@ const AsciiDataParseOptions RicPasteAsciiDataToSummaryPlotFeatureUi::parseOption
|
||||
|
||||
{
|
||||
QString separator;
|
||||
QLocale locale;
|
||||
switch (m_decimalSeparator())
|
||||
{
|
||||
case DECIMAL_COMMA:
|
||||
separator = ",";
|
||||
locale = QLocale::Norwegian;
|
||||
break;
|
||||
case DECIMAL_DOT:
|
||||
default:
|
||||
separator = ".";
|
||||
locale = QLocale::c();
|
||||
break;
|
||||
}
|
||||
|
||||
parseOptions.decimalSeparator = separator;
|
||||
parseOptions.locale = locale;
|
||||
}
|
||||
|
||||
{
|
||||
|
@ -36,6 +36,7 @@ public:
|
||||
QString plotTitle;
|
||||
QString curvePrefix;
|
||||
QString decimalSeparator;
|
||||
QLocale locale;
|
||||
QString dateFormat;
|
||||
QString timeFormat;
|
||||
QString cellSeparator;
|
||||
|
@ -59,8 +59,7 @@ bool RifCsvUserData::parse(const QString& fileName, const AsciiDataParseOptions&
|
||||
m_mapFromAddressToResultIndex.clear();
|
||||
|
||||
m_parser = std::unique_ptr<RifCsvUserDataFileParser>(new RifCsvUserDataFileParser(fileName, errorText));
|
||||
m_parser->parse(parseOptions);
|
||||
if (!m_parser)
|
||||
if (!m_parser->parse(parseOptions))
|
||||
{
|
||||
RiaLogging::error(QString("Failed to parse file"));
|
||||
return false;
|
||||
|
@ -168,9 +168,14 @@ bool RifCsvUserDataParser::parseData(const AsciiDataParseOptions& parseOptions)
|
||||
|
||||
for (int iCol = 0; iCol < colCount; iCol++)
|
||||
{
|
||||
std::string colName = columnNames[iCol].toStdString();
|
||||
RifEclipseSummaryAddress addr = RifEclipseSummaryAddress::importedAddress(colName);
|
||||
QString colName = columnNames[iCol];
|
||||
RifEclipseSummaryAddress addr = RifEclipseSummaryAddress::importedAddress(colName.toStdString());
|
||||
ColumnInfo col = ColumnInfo::createColumnInfoFromCsvData(addr, "");
|
||||
|
||||
if (colName == parseOptions.timeSeriesColumnName)
|
||||
{
|
||||
col.dataType = ColumnInfo::DATETIME;
|
||||
}
|
||||
cols.push_back(col);
|
||||
}
|
||||
}
|
||||
@ -180,7 +185,7 @@ bool RifCsvUserDataParser::parseData(const AsciiDataParseOptions& parseOptions)
|
||||
return false;
|
||||
}
|
||||
|
||||
while (!dataStream->atEnd())
|
||||
while (!dataStream->atEnd() && !errors)
|
||||
{
|
||||
QString line = dataStream->readLine();
|
||||
if(line.trimmed().isEmpty()) continue;
|
||||
@ -200,19 +205,17 @@ bool RifCsvUserDataParser::parseData(const AsciiDataParseOptions& parseOptions)
|
||||
std::string colData = lineColumns[iCol].toStdString();
|
||||
ColumnInfo& col = cols[iCol];
|
||||
|
||||
// Check if text column
|
||||
if (RiaStdStringTools::isNumber(colData))
|
||||
// Determine column data type
|
||||
if (col.dataType == ColumnInfo::NONE)
|
||||
{
|
||||
col.dataType = ColumnInfo::NUMERIC;
|
||||
}
|
||||
else if (tryParseDateTime(colData, parseOptions.dateTimeFormat()).isValid() ||
|
||||
tryParseDateTime(colData, parseOptions.dateFormat).isValid())
|
||||
{
|
||||
col.dataType = ColumnInfo::DATETIME;
|
||||
}
|
||||
else
|
||||
{
|
||||
col.dataType = ColumnInfo::TEXT;
|
||||
if (RiaStdStringTools::isNumber(colData, parseOptions.locale.decimalPoint().toAscii()))
|
||||
{
|
||||
col.dataType = ColumnInfo::NUMERIC;
|
||||
}
|
||||
else
|
||||
{
|
||||
col.dataType = ColumnInfo::TEXT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -223,25 +226,25 @@ bool RifCsvUserDataParser::parseData(const AsciiDataParseOptions& parseOptions)
|
||||
{
|
||||
for (int iCol = 0; iCol < colCount; iCol++)
|
||||
{
|
||||
std::string colData = lineColumns[iCol].toStdString();
|
||||
QString& colData = lineColumns[iCol];
|
||||
ColumnInfo& col = cols[iCol];
|
||||
|
||||
try
|
||||
{
|
||||
if (col.dataType == ColumnInfo::NUMERIC)
|
||||
{
|
||||
col.values.push_back(RiaStdStringTools::toDouble(colData));
|
||||
col.values.push_back(parseOptions.locale.toDouble(colData));
|
||||
}
|
||||
else if (col.dataType == ColumnInfo::TEXT)
|
||||
{
|
||||
col.textValues.push_back(colData);
|
||||
col.textValues.push_back(colData.toStdString());
|
||||
}
|
||||
else if (col.dataType == ColumnInfo::DATETIME)
|
||||
{
|
||||
QDateTime dt = tryParseDateTime(colData, parseOptions.dateTimeFormat());
|
||||
QDateTime dt = tryParseDateTime(colData.toStdString(), parseOptions.dateTimeFormat());
|
||||
if (!dt.isValid())
|
||||
{
|
||||
dt = tryParseDateTime(colData, parseOptions.dateFormat);
|
||||
dt = tryParseDateTime(colData.toStdString(), parseOptions.dateFormat);
|
||||
}
|
||||
if (!dt.isValid()) throw 0;
|
||||
col.dateTimeValues.push_back(dt);
|
||||
|
Loading…
Reference in New Issue
Block a user