diff --git a/agrolib/dbMeteoGrid/dbMeteoGrid.cpp b/agrolib/dbMeteoGrid/dbMeteoGrid.cpp index 8189188d..c2ac990b 100644 --- a/agrolib/dbMeteoGrid/dbMeteoGrid.cpp +++ b/agrolib/dbMeteoGrid/dbMeteoGrid.cpp @@ -773,7 +773,6 @@ QString Crit3DMeteoGridDbHandler::getDailyVarField(meteoVariable meteoGridDailyV meteoVariable Crit3DMeteoGridDbHandler::getDailyVarEnum(int varCode) { - if (varCode == NODATA) { return noMeteoVar; @@ -791,6 +790,7 @@ meteoVariable Crit3DMeteoGridDbHandler::getDailyVarEnum(int varCode) return noMeteoVar; } + meteoVariable Crit3DMeteoGridDbHandler::getDailyVarFieldEnum(QString varField) { @@ -1724,64 +1724,71 @@ bool Crit3DMeteoGridDbHandler::updateGridDate(QString *myError) } return true; - } -bool Crit3DMeteoGridDbHandler::loadGridDailyData(QString &myError, QString meteoPoint, QDate first, QDate last) +bool Crit3DMeteoGridDbHandler::loadGridDailyData(QString &errorStr, const QString &meteoPoint, const QDate &first, const QDate &last) { - myError = ""; + errorStr = ""; QSqlQuery qry(_db); QString tableD = _tableDaily.prefix + meteoPoint + _tableDaily.postFix; - QDate date; - int varCode; - float value; unsigned row, col; - if ( !_meteoGrid->findMeteoPointFromId(&row, &col, meteoPoint.toStdString()) ) { - myError = "Missing MeteoPoint id"; + errorStr = "Missing MeteoPoint id"; return false; } int numberOfDays = first.daysTo(last) + 1; - _meteoGrid->meteoPointPointer(row,col)->initializeObsDataD(numberOfDays, getCrit3DDate(first)); + _meteoGrid->meteoPointPointer(row, col)->initializeObsDataD(numberOfDays, getCrit3DDate(first)); - QString statement = QString("SELECT * FROM `%1` WHERE %2 >= '%3' AND %2 <= '%4' ORDER BY %2").arg(tableD, _tableDaily.fieldTime, first.toString("yyyy-MM-dd"), last.toString("yyyy-MM-dd")); + QString statement; + if (first == last) + { + statement = QString("SELECT * FROM `%1` WHERE %2 = '%3'").arg(tableD, _tableDaily.fieldTime, first.toString("yyyy-MM-dd")); + } + else + { + statement = QString("SELECT * FROM `%1` WHERE %2 >= '%3' AND %2 <= '%4' ORDER BY %2") + .arg(tableD, _tableDaily.fieldTime, first.toString("yyyy-MM-dd"), last.toString("yyyy-MM-dd")); + } qry.prepare(statement); - if( !qry.exec()) + + if(! qry.exec()) { - myError = qry.lastError().text(); + errorStr = qry.lastError().text(); return false; } - else + + int varCode; + float value; + QDate date; + + while (qry.next()) { - while (qry.next()) + if (! getValue(qry.value(_tableDaily.fieldTime), &date)) { - if (!getValue(qry.value(_tableDaily.fieldTime), &date)) - { - myError = "Missing fieldTime"; - return false; - } + errorStr = "Missing " + _tableDaily.fieldTime; + return false; + } - if (!getValue(qry.value("VariableCode"), &varCode)) - { - myError = "Missing VariableCode"; - return false; - } + if (! getValue(qry.value("VariableCode"), &varCode)) + { + errorStr = "Missing VariableCode"; + return false; + } - if (!getValue(qry.value("Value"), &value)) - { - myError = "Missing Value"; - } + if (! getValue(qry.value("Value"), &value)) + { + // no data + } - meteoVariable variable = getDailyVarEnum(varCode); + meteoVariable variable = getDailyVarEnum(varCode); - if (! _meteoGrid->meteoPointPointer(row,col)->setMeteoPointValueD(getCrit3DDate(date), variable, value)) - return false; - } + if (! _meteoGrid->meteoPointPointer(row, col)->setMeteoPointValueD(getCrit3DDate(date), variable, value)) + return false; } return true; diff --git a/agrolib/dbMeteoGrid/dbMeteoGrid.h b/agrolib/dbMeteoGrid/dbMeteoGrid.h index 5c4f8cb2..8bbf6fb0 100644 --- a/agrolib/dbMeteoGrid/dbMeteoGrid.h +++ b/agrolib/dbMeteoGrid/dbMeteoGrid.h @@ -112,7 +112,7 @@ bool loadIdMeteoProperties(QString *myError, QString idMeteo); bool updateGridDate(QString *myError); - bool loadGridDailyData(QString &myError, QString meteoPoint, QDate first, QDate last); + bool loadGridDailyData(QString &errorStr, const QString &meteoPoint, const QDate &first, const QDate &last); bool loadGridDailyDataFixedFields(QString &myError, QString meteoPoint, QDate first, QDate last); bool loadGridDailyDataEnsemble(QString &myError, QString meteoPoint, int memberNr, QDate first, QDate last); bool loadGridHourlyData(QString &myError, QString meteoPoint, QDateTime first, QDateTime last); diff --git a/agrolib/project/project.cpp b/agrolib/project/project.cpp index 02c46514..d91082dd 100644 --- a/agrolib/project/project.cpp +++ b/agrolib/project/project.cpp @@ -1,4 +1,5 @@ #include "project.h" +#include "dbMeteoGrid.h" #include "commonConstants.h" #include "basicMath.h" #include "spatialControl.h" @@ -1503,14 +1504,14 @@ bool Project::loadMeteoGridDailyData(QDate firstDate, QDate lastDate, bool showI int memberNr = 1; if (this->meteoGridDbHandler->loadGridDailyDataEnsemble(errorString, QString::fromStdString(id), memberNr, firstDate, lastDate)) { - count = count + 1; + count++; } } else { if (this->meteoGridDbHandler->loadGridDailyData(errorString, QString::fromStdString(id), firstDate, lastDate)) { - count = count + 1; + count++; } } } @@ -1518,7 +1519,7 @@ bool Project::loadMeteoGridDailyData(QDate firstDate, QDate lastDate, bool showI { if (this->meteoGridDbHandler->loadGridDailyDataFixedFields(errorString, QString::fromStdString(id), firstDate, lastDate)) { - count = count + 1; + count++; } } }