diff --git a/agrolib/climate/climate.cpp b/agrolib/climate/climate.cpp index 560933278..cb7952470 100644 --- a/agrolib/climate/climate.cpp +++ b/agrolib/climate/climate.cpp @@ -105,9 +105,32 @@ bool elaborationOnPoint(QString *myError, Crit3DMeteoPointsDbHandler* meteoPoint } return false; +} + + +bool elaborationOnPointHourly(Crit3DMeteoPointsDbHandler* meteoPointsDbHandler, Crit3DMeteoGridDbHandler* meteoGridDbHandler, + Crit3DMeteoPoint* meteoPointTemp, bool isMeteoGrid, Crit3DClimate* climate, Crit3DMeteoSettings* meteoSettings, QString &myError) +{ + meteoPointTemp->elaboration = NODATA; + + std::vector outputValues; + QDateTime firstTime = QDateTime(climate->genericPeriodDateStart(), QTime(climate->hourStart(), 0), Qt::UTC); + QDateTime lastTime = QDateTime(climate->genericPeriodDateEnd(), QTime(climate->hourEnd(), 0), Qt::UTC); + + float percValue = loadHourlyVarSeries_SaveOutput(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPointTemp, + isMeteoGrid, climate->variable(), firstTime, lastTime, outputValues); + + if ((percValue*100) < meteoSettings->getMinimumPercentage()) + return false; + meteoComputation elab = getMeteoCompFromString(MapMeteoComputation, climate->elab1().toStdString()); + float result = statisticalElab(elab, NODATA, outputValues, int(outputValues.size()), meteoSettings->getRainfallThreshold()); + meteoPointTemp->elaboration = result; + + return (result != NODATA); } + bool anomalyOnPoint(Crit3DMeteoPoint* meteoPoint, float refValue) { @@ -227,6 +250,7 @@ bool passingClimateToAnomalyGrid(QString *myError, Crit3DMeteoPoint* meteoPointT return false; } + bool climateOnPoint(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbHandler, Crit3DMeteoGridDbHandler* meteoGridDbHandler, Crit3DClimate* clima, Crit3DMeteoPoint* meteoPointTemp, std::vector &outputValues, bool isMeteoGrid, QDate startDate, QDate endDate, bool changeDataSet, Crit3DMeteoSettings* meteoSettings) { @@ -830,63 +854,46 @@ float loadDailyVarSeries(QString *myError, Crit3DMeteoPointsDbHandler *meteoPoin } -float loadHourlyVarSeries_SaveOutput(QString *myError, Crit3DMeteoPointsDbHandler *meteoPointsDbHandler, +float loadHourlyVarSeries_SaveOutput(QString &myError, Crit3DMeteoPointsDbHandler *meteoPointsDbHandler, Crit3DMeteoGridDbHandler *meteoGridDbHandler, Crit3DMeteoPoint* meteoPoint, bool isMeteoGrid, - meteoVariable variable, QDate firstDate, QDate lastDate, std::vector &outputValues) + meteoVariable variable, QDateTime firstTime, QDateTime lastTime, std::vector &outputValues) { std::vector hourlyValues; QDateTime firstDateTimeDB; + QString meteoPointId = QString::fromStdString(meteoPoint->id); + if (isMeteoGrid) { - QDateTime firstTime = QDateTime(firstDate, QTime(1,0,0,0)); - QDateTime lastTime = QDateTime(lastDate.addDays(1), QTime(0,0,0,0)); - if (meteoGridDbHandler->gridStructure().isFixedFields()) { - hourlyValues = meteoGridDbHandler->loadGridHourlyVarFixedFields(myError, QString::fromStdString(meteoPoint->id), - variable, firstTime, lastTime, &firstDateTimeDB); + hourlyValues = meteoGridDbHandler->loadGridHourlyVarFixedFields(variable, meteoPointId, firstTime, lastTime, + firstDateTimeDB, myError); } else { - hourlyValues = meteoGridDbHandler->loadGridHourlyVar(myError, QString::fromStdString(meteoPoint->id), variable, - firstTime, lastTime, &firstDateTimeDB); + hourlyValues = meteoGridDbHandler->loadGridHourlyVar(variable, meteoPointId, firstTime, lastTime, firstDateTimeDB, myError); } } else { // meteoPoints - hourlyValues = meteoPointsDbHandler->loadHourlyVar(myError, variable, getCrit3DDate(firstDate), getCrit3DDate(lastDate), - &firstDateTimeDB, meteoPoint); + hourlyValues = meteoPointsDbHandler->loadHourlyVar(variable, meteoPointId, firstTime, lastTime, firstDateTimeDB, myError); } // no values if (hourlyValues.empty()) return 0; - // number of days - QDateTime lastDateTimeDB = firstDateTimeDB.addSecs((hourlyValues.size()-1) * 3600); - int nrOfDays = firstDateTimeDB.daysTo(lastDateTimeDB); - if (lastDateTimeDB.time().hour() > 0) - nrOfDays++; - - Crit3DDate firstCrit3DDate = getCrit3DDate(firstDateTimeDB.date()); + // initialize meteopoint data + int nrOfDays = firstTime.daysTo(lastTime) + 1; + Crit3DDate firstCrit3DDate = getCrit3DDate(firstTime.date()); meteoPoint->initializeObsDataH(meteoPoint->hourlyFraction, nrOfDays, firstCrit3DDate); meteoPoint->initializeObsDataD(nrOfDays, firstCrit3DDate); - int nrRequestedValues = (firstDate.daysTo(lastDate) + 1) * 24; + int nrRequestedValues = firstTime.secsTo(lastTime) / 3600 + 1; int nrValidValues = 0; - // fill initial NODATA (if firstHour > 1) - int firstHour = firstDateTimeDB.time().hour(); - if (firstHour > 1) - { - for (int h = 1; h < firstHour; h++) - { - outputValues.push_back(NODATA); - } - } - Crit3DQuality qualityCheck; Crit3DTime currentDateTime = getCrit3DTime(firstDateTimeDB); for (unsigned int i = 0; i < hourlyValues.size(); i++) @@ -897,22 +904,18 @@ float loadHourlyVarSeries_SaveOutput(QString *myError, Crit3DMeteoPointsDbHandle if (meteoPoint->setMeteoPointValueH(currentDateTime.date, currentDateTime.getHour(), currentDateTime.getMinutes(), variable, hourlyValues[i])) { outputValues.push_back(hourlyValues[i]); - nrValidValues = nrValidValues + 1; + nrValidValues++; } } else { - if (meteoPoint->setMeteoPointValueH(currentDateTime.date, currentDateTime.getHour(), currentDateTime.getMinutes(), variable, NODATA)) - { - outputValues.push_back(NODATA); - } + outputValues.push_back(NODATA); } - currentDateTime = currentDateTime.addSeconds(3600); + currentDateTime = currentDateTime.addSeconds(3600 / meteoPoint->hourlyFraction); } - float percValue = float(nrValidValues) / float(nrRequestedValues); - return percValue; + return float(nrValidValues) / float(nrRequestedValues); } @@ -1001,33 +1004,37 @@ float loadFromMp_SaveOutput(Crit3DMeteoPoint* meteoPoint, return percValue; } -float loadHourlyVarSeries(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbHandler, + +// todo generalizzare (ora funziona solo su giorni completi) +float loadHourlyVarSeries(QString &myError, Crit3DMeteoPointsDbHandler* meteoPointsDbHandler, Crit3DMeteoGridDbHandler* meteoGridDbHandler, Crit3DMeteoPoint* meteoPoint, bool isMeteoGrid, - meteoVariable variable, QDateTime first, QDateTime last) + meteoVariable variable, const QDateTime &firstTime, const QDateTime &lastTime) { std::vector hourlyValues; QDateTime firstDateDB; Crit3DQuality qualityCheck; int nrValidValues = 0; - int nrRequestedDays = first.daysTo(last); + int nrRequestedDays = firstTime.daysTo(lastTime); int nrRequestedValues = nrRequestedDays * 24 * meteoPoint->hourlyFraction; + QString meteoPointId = QString::fromStdString(meteoPoint->id); + // meteoGrid if (isMeteoGrid) { if (meteoGridDbHandler->gridStructure().isFixedFields()) { - hourlyValues = meteoGridDbHandler->loadGridHourlyVarFixedFields(myError, QString::fromStdString(meteoPoint->id), variable, first, last, &firstDateDB); + hourlyValues = meteoGridDbHandler->loadGridHourlyVarFixedFields(variable, meteoPointId, firstTime, lastTime, firstDateDB, myError); } else { - hourlyValues = meteoGridDbHandler->loadGridHourlyVar(myError, QString::fromStdString(meteoPoint->id), variable, first, last, &firstDateDB); + hourlyValues = meteoGridDbHandler->loadGridHourlyVar(variable, meteoPointId, firstTime, lastTime, firstDateDB, myError); } } // meteoPoint else { - hourlyValues = meteoPointsDbHandler->loadHourlyVar(myError, variable, getCrit3DDate(first.date()), getCrit3DDate(last.date()), &firstDateDB, meteoPoint ); + hourlyValues = meteoPointsDbHandler->loadHourlyVar(variable, meteoPointId, firstTime, lastTime, firstDateDB, myError); } @@ -1054,13 +1061,11 @@ float loadHourlyVarSeries(QString *myError, Crit3DMeteoPointsDbHandler* meteoPoi meteoPoint->setMeteoPointValueH(Crit3DDate(firstDateDB.date().day(), firstDateDB.date().month(), firstDateDB.date().year()), firstDateDB.time().hour(), firstDateDB.time().minute(), variable, hourlyValues[i]); - firstDateDB = firstDateDB.addSecs(3600); + firstDateDB = firstDateDB.addSecs(3600 / meteoPoint->hourlyFraction); } - float percValue = float(nrValidValues) / float(nrRequestedValues); - return percValue; + return float(nrValidValues) / float(nrRequestedValues); } - } @@ -2156,8 +2161,10 @@ std::vector aggregatedHourlyToDailyList(meteoVariable myVar, Crit3DMeteoP } -bool preElaboration(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbHandler, Crit3DMeteoGridDbHandler* meteoGridDbHandler, Crit3DMeteoPoint* meteoPoint, bool isMeteoGrid, meteoVariable variable, meteoComputation elab1, - QDate startDate, QDate endDate, std::vector &outputValues, float* percValue, Crit3DMeteoSettings* meteoSettings) +bool preElaboration(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbHandler, Crit3DMeteoGridDbHandler* meteoGridDbHandler, + Crit3DMeteoPoint* meteoPoint, bool isMeteoGrid, meteoVariable variable, meteoComputation elab1, + QDate startDate, QDate endDate, std::vector &outputValues, float* percValue, + Crit3DMeteoSettings* meteoSettings) { bool preElaboration = false; bool automaticETP = meteoSettings->getAutomaticET0HS(); @@ -2167,7 +2174,7 @@ bool preElaboration(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbH { case dailyLeafWetness: { - if ( loadHourlyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, leafWetness, QDateTime(startDate,QTime(1,0,0),Qt::UTC), QDateTime(endDate.addDays(1),QTime(0,0,0),Qt::UTC)) > 0) + if ( loadHourlyVarSeries(*myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, leafWetness, QDateTime(startDate,QTime(1,0,0),Qt::UTC), QDateTime(endDate.addDays(1),QTime(0,0,0),Qt::UTC)) > 0) { preElaboration = elaborateDailyAggregatedVar(dailyLeafWetness, *meteoPoint, outputValues, percValue, meteoSettings); } @@ -2175,7 +2182,7 @@ bool preElaboration(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbH } case dailyTemperatureHoursAbove: { - if ( loadHourlyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, airTemperature, QDateTime(startDate,QTime(1,0,0),Qt::UTC), QDateTime(endDate.addDays(1),QTime(0,0,0),Qt::UTC)) > 0) + if ( loadHourlyVarSeries(*myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, airTemperature, QDateTime(startDate,QTime(1,0,0),Qt::UTC), QDateTime(endDate.addDays(1),QTime(0,0,0),Qt::UTC)) > 0) { preElaboration = elaborateDailyAggregatedVar(dailyTemperatureHoursAbove, *meteoPoint, outputValues, percValue, meteoSettings); } @@ -2206,9 +2213,9 @@ bool preElaboration(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbH } case dailyThomAvg: case dailyThomMax: case dailyThomHoursAbove: { - if (loadHourlyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, airTemperature, QDateTime(startDate,QTime(1,0,0),Qt::UTC), QDateTime(endDate.addDays(1),QTime(0,0,0),Qt::UTC)) > 0) + if (loadHourlyVarSeries(*myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, airTemperature, QDateTime(startDate,QTime(1,0,0),Qt::UTC), QDateTime(endDate.addDays(1),QTime(0,0,0),Qt::UTC)) > 0) { - if (loadHourlyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, airRelHumidity, QDateTime(startDate,QTime(1,0,0),Qt::UTC), QDateTime(endDate.addDays(1),QTime(0,0,0),Qt::UTC)) > 0) + if (loadHourlyVarSeries(*myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, airRelHumidity, QDateTime(startDate,QTime(1,0,0),Qt::UTC), QDateTime(endDate.addDays(1),QTime(0,0,0),Qt::UTC)) > 0) { preElaboration = elaborateDailyAggregatedVar(variable, *meteoPoint, outputValues, percValue, meteoSettings); } @@ -2402,8 +2409,10 @@ bool preElaboration(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbH // default variables / elaboration if (getVarFrequency(variable) == hourly) { - *percValue = loadHourlyVarSeries_SaveOutput(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, - isMeteoGrid, variable, startDate, endDate, outputValues); + QDateTime startTime = QDateTime(startDate, QTime(1,0,0), Qt::UTC); + QDateTime endTime = QDateTime(endDate.addDays(1), QTime(0,0,0), Qt::UTC); + *percValue = loadHourlyVarSeries_SaveOutput(*myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, + isMeteoGrid, variable, startTime, endTime, outputValues); } else { @@ -5486,22 +5495,21 @@ void setMpValues(Crit3DMeteoPoint meteoPointGet, Crit3DMeteoPoint* meteoPointSet } -meteoComputation getMeteoCompFromString(std::map map, std::string value) +meteoComputation getMeteoCompFromString(const std::map &map, const std::string &computationStr) { + meteoComputation myComputation = noMeteoComp; + QString computationLower = QString::fromStdString(computationStr).toLower(); std::map::const_iterator it; - meteoComputation meteoValue = noMeteoComp; - QString valueLower = QString::fromStdString(value).toLower(); - QString mapStringToLower; - for (it = map.begin(); it != map.end(); ++it) { - mapStringToLower = QString::fromStdString(it->first).toLower(); - if (mapStringToLower == valueLower) + QString mapStringToLower = QString::fromStdString(it->first).toLower(); + if (mapStringToLower == computationLower) { - meteoValue = it->second; + myComputation = it->second; break; } } - return meteoValue; + + return myComputation; } diff --git a/agrolib/climate/climate.h b/agrolib/climate/climate.h index d2c47f83c..56d29d47d 100644 --- a/agrolib/climate/climate.h +++ b/agrolib/climate/climate.h @@ -41,8 +41,14 @@ { "correctedDegreeDaysSum", 1 } }; - bool elaborationOnPoint(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbHandler, Crit3DMeteoGridDbHandler* meteoGridDbHandler, - Crit3DMeteoPoint* meteoPointTemp, Crit3DClimate* clima, bool isMeteoGrid, QDate startDate, QDate endDate, bool isAnomaly, Crit3DMeteoSettings *meteoSettings, bool dataAlreadyLoaded); + bool elaborationOnPoint(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbHandler, + Crit3DMeteoGridDbHandler* meteoGridDbHandler, Crit3DMeteoPoint* meteoPointTemp, + Crit3DClimate* clima, bool isMeteoGrid, QDate startDate, QDate endDate, + bool isAnomaly, Crit3DMeteoSettings *meteoSettings, bool dataAlreadyLoaded); + + bool elaborationOnPointHourly(Crit3DMeteoPointsDbHandler* meteoPointsDbHandler, + Crit3DMeteoGridDbHandler* meteoGridDbHandler, Crit3DMeteoPoint* meteoPointTemp, + bool isMeteoGrid, Crit3DClimate* climate, Crit3DMeteoSettings* meteoSettings, QString &myError); frequencyType getAggregationFrequency(meteoVariable myVar); @@ -104,13 +110,13 @@ Crit3DMeteoGridDbHandler *meteoGridDbHandler, Crit3DMeteoPoint &meteoPoint, bool isMeteoGrid, meteoVariable variable, QDate first, QDate last, std::vector &outputValues); - float loadHourlyVarSeries_SaveOutput(QString *myError, Crit3DMeteoPointsDbHandler *meteoPointsDbHandler, + float loadHourlyVarSeries_SaveOutput(QString &myError, Crit3DMeteoPointsDbHandler *meteoPointsDbHandler, Crit3DMeteoGridDbHandler *meteoGridDbHandler, Crit3DMeteoPoint* meteoPoint, bool isMeteoGrid, - meteoVariable variable, QDate firstDate, QDate lastDate, std::vector &outputValues); + meteoVariable variable, QDateTime firstTime, QDateTime lastTime, std::vector &outputValues); - float loadHourlyVarSeries(QString *myError, Crit3DMeteoPointsDbHandler *meteoPointsDbHandler, - Crit3DMeteoGridDbHandler *meteoGridDbHandler, Crit3DMeteoPoint* meteoPoint, - bool isMeteoGrid, meteoVariable variable, QDateTime first, QDateTime last); + float loadHourlyVarSeries(QString &myError, Crit3DMeteoPointsDbHandler *meteoPointsDbHandler, + Crit3DMeteoGridDbHandler *meteoGridDbHandler, Crit3DMeteoPoint* meteoPoint, + bool isMeteoGrid, meteoVariable variable, const QDateTime &firstTime, const QDateTime &lastTime); void extractValidValuesCC(std::vector &outputValues); @@ -168,7 +174,8 @@ meteoVariable variable, QDate first, QDate last, std::vector &outputValues); void setMpValues(Crit3DMeteoPoint meteoPointGet, Crit3DMeteoPoint* meteoPointSet, QDate myDate, meteoVariable myVar, Crit3DMeteoSettings* meteoSettings); - meteoComputation getMeteoCompFromString(std::map map, std::string value); + + meteoComputation getMeteoCompFromString(const std::map &map, const std::string &computationStr); #endif // CLIMATE_H diff --git a/agrolib/dbMeteoGrid/dbMeteoGrid.cpp b/agrolib/dbMeteoGrid/dbMeteoGrid.cpp index d95bfd62d..9e76bd73f 100644 --- a/agrolib/dbMeteoGrid/dbMeteoGrid.cpp +++ b/agrolib/dbMeteoGrid/dbMeteoGrid.cpp @@ -2708,11 +2708,12 @@ std::vector Crit3DMeteoGridDbHandler::loadGridDailyVarFixedFields(QString } -std::vector Crit3DMeteoGridDbHandler::loadGridHourlyVar(QString *errorStr, QString meteoPoint, meteoVariable variable, - QDateTime first, QDateTime last, QDateTime* firstDateDB) +std::vector Crit3DMeteoGridDbHandler::loadGridHourlyVar(meteoVariable variable, const QString& meteoPointId, + const QDateTime &firstTime, const QDateTime &lastTime, + QDateTime &firstDateDB, QString &errorStr) { QSqlQuery qry(_db); - QString tableH = _tableHourly.prefix + meteoPoint + _tableHourly.postFix; + QString tableH = _tableHourly.prefix + meteoPointId + _tableHourly.postFix; QDateTime dateTime, previousDateTime; dateTime.setTimeSpec(Qt::UTC); previousDateTime.setTimeSpec(Qt::UTC); @@ -2726,21 +2727,22 @@ std::vector Crit3DMeteoGridDbHandler::loadGridHourlyVar(QString *errorStr int varCode = getHourlyVarCode(variable); if (varCode == NODATA) { - *errorStr = "Variable not existing"; + errorStr = "Variable not existing"; return hourlyVarList; } - if (!_meteoGrid->findMeteoPointFromId(&row, &col, meteoPoint.toStdString()) ) + if (! _meteoGrid->findMeteoPointFromId(&row, &col, meteoPointId.toStdString()) ) { - *errorStr = "Missing MeteoPoint id"; + errorStr = "Missing MeteoPoint id"; return hourlyVarList; } QString statement = QString("SELECT * FROM `%1` WHERE VariableCode = '%2' AND `%3` >= '%4' AND `%3` <= '%5' ORDER BY `%3`") - .arg(tableH).arg(varCode).arg(_tableHourly.fieldTime, first.toString("yyyy-MM-dd hh:mm"), last.toString("yyyy-MM-dd hh:mm")); + .arg(tableH).arg(varCode).arg(_tableHourly.fieldTime, + firstTime.toString("yyyy-MM-dd hh:mm"), lastTime.toString("yyyy-MM-dd hh:mm")); if(! qry.exec(statement) ) { - *errorStr = qry.lastError().text(); + errorStr = qry.lastError().text(); } else { @@ -2748,25 +2750,25 @@ std::vector Crit3DMeteoGridDbHandler::loadGridHourlyVar(QString *errorStr { if (firstRow) { - if (! getValue(qry.value(_tableHourly.fieldTime), firstDateDB)) + if (! getValue(qry.value(_tableHourly.fieldTime), &firstDateDB)) { - *errorStr = "Missing fieldTime"; + errorStr = "Missing fieldTime"; return hourlyVarList; } if (! getValue(qry.value("Value"), &value)) { - *errorStr = "Missing Value"; + errorStr = "Missing Value"; } hourlyVarList.push_back(value); - previousDateTime = *firstDateDB; + previousDateTime = firstDateDB; firstRow = false; } else { if (! getValue(qry.value(_tableHourly.fieldTime), &dateTime)) { - *errorStr = "Missing fieldTime"; + errorStr = "Missing fieldTime"; return hourlyVarList; } @@ -2778,7 +2780,7 @@ std::vector Crit3DMeteoGridDbHandler::loadGridHourlyVar(QString *errorStr if (! getValue(qry.value("Value"), &value)) { - *errorStr = "Missing Value"; + errorStr = "Missing Value"; } hourlyVarList.push_back(value); @@ -2791,11 +2793,12 @@ std::vector Crit3DMeteoGridDbHandler::loadGridHourlyVar(QString *errorStr } -std::vector Crit3DMeteoGridDbHandler::loadGridHourlyVarFixedFields(QString *errorStr, QString meteoPoint, meteoVariable variable, - QDateTime first, QDateTime last, QDateTime* firstDateDB) +std::vector Crit3DMeteoGridDbHandler::loadGridHourlyVarFixedFields(meteoVariable variable, const QString &meteoPointId, + const QDateTime &firstTime, const QDateTime &lastTime, + QDateTime &firstDateDB, QString &errorStr) { QSqlQuery qry(_db); - QString tableH = _tableHourly.prefix + meteoPoint + _tableHourly.postFix; + QString tableH = _tableHourly.prefix + meteoPointId + _tableHourly.postFix; QDateTime dateTime, previousDateTime; std::vector hourlyVarList; @@ -2808,7 +2811,7 @@ std::vector Crit3DMeteoGridDbHandler::loadGridHourlyVarFixedFields(QStrin if (varCode == NODATA) { - *errorStr = "Variable not existing"; + errorStr = "Variable not existing"; return hourlyVarList; } @@ -2820,40 +2823,38 @@ std::vector Crit3DMeteoGridDbHandler::loadGridHourlyVarFixedFields(QStrin break; } } - // take also 00:00 day after - last = last.addSecs(3600); - QString statement = QString("SELECT `%1`, `%2` FROM `%3` WHERE `%1` >= '%4' AND `%1` <= '%5' ORDER BY `%1`").arg(_tableHourly.fieldTime).arg(varField).arg(tableH).arg(first.toString("yyyy-MM-dd hh:mm")).arg(last.toString("yyyy-MM-dd hh:mm")); - if( !qry.exec(statement) ) + QString statement = QString("SELECT `%1`, `%2` FROM `%3` WHERE `%1` >= '%4' AND `%1` <= '%5' ORDER BY `%1`") + .arg(_tableHourly.fieldTime).arg(varField).arg(tableH).arg(firstTime.toString("yyyy-MM-dd hh:mm")).arg(lastTime.toString("yyyy-MM-dd hh:mm")); + if( ! qry.exec(statement) ) { - *errorStr = qry.lastError().text(); + errorStr = qry.lastError().text(); } else { - while (qry.next()) { if (firstRow) { - if (!getValue(qry.value(_tableHourly.fieldTime), firstDateDB)) + if (! getValue(qry.value(_tableHourly.fieldTime), &firstDateDB)) { - *errorStr = "Missing fieldTime"; + errorStr = "Missing fieldTime"; return hourlyVarList; } - if (!getValue(qry.value(varField), &value)) + if (! getValue(qry.value(varField), &value)) { - *errorStr = "Missing Value"; + errorStr = "Missing Value"; } hourlyVarList.push_back(value); - previousDateTime = *firstDateDB; + previousDateTime = firstDateDB; firstRow = 0; } else { - if (!getValue(qry.value(_tableHourly.fieldTime), &dateTime)) + if (! getValue(qry.value(_tableHourly.fieldTime), &dateTime)) { - *errorStr = "Missing fieldTime"; + errorStr = "Missing fieldTime"; return hourlyVarList; } @@ -2865,15 +2866,13 @@ std::vector Crit3DMeteoGridDbHandler::loadGridHourlyVarFixedFields(QStrin if (!getValue(qry.value(varField), &value)) { - *errorStr = "Missing Value"; + errorStr = "Missing Value"; } + hourlyVarList.push_back(value); previousDateTime = dateTime; } - - } - } return hourlyVarList; diff --git a/agrolib/dbMeteoGrid/dbMeteoGrid.h b/agrolib/dbMeteoGrid/dbMeteoGrid.h index 463e4e5b9..79217f37b 100644 --- a/agrolib/dbMeteoGrid/dbMeteoGrid.h +++ b/agrolib/dbMeteoGrid/dbMeteoGrid.h @@ -130,8 +130,15 @@ std::vector loadGridDailyVar(QString *errorStr, QString meteoPoint, meteoVariable variable, QDate first, QDate last, QDate *firstDateDB); std::vector loadGridDailyVarFixedFields(QString *errorStr, QString meteoPoint, meteoVariable variable, QDate first, QDate last, QDate* firstDateDB); - std::vector loadGridHourlyVar(QString *errorStr, QString meteoPoint, meteoVariable variable, QDateTime first, QDateTime last, QDateTime* firstDateDB); - std::vector loadGridHourlyVarFixedFields(QString *errorStr, QString meteoPoint, meteoVariable variable, QDateTime first, QDateTime last, QDateTime* firstDateDB); + + std::vector loadGridHourlyVar(meteoVariable variable, const QString& meteoPointId, + const QDateTime &firstTime, const QDateTime &lastTime, + QDateTime &firstDateDB, QString &errorStr); + + std::vector loadGridHourlyVarFixedFields(meteoVariable variable, const QString &meteoPointId, + const QDateTime &firstTime, const QDateTime &lastTime, + QDateTime &firstDateDB, QString &errorStr); + std::vector exportAllDataVar(QString *errorStr, frequencyType freq, meteoVariable variable, QString id, QDateTime myFirstTime, QDateTime myLastTime, std::vector &dateStr); bool getYearList(QString *errorStr, QString meteoPoint, QList* yearList); diff --git a/agrolib/dbMeteoPoints/dbMeteoPointsHandler.cpp b/agrolib/dbMeteoPoints/dbMeteoPointsHandler.cpp index b1f63823e..080fe6d4c 100644 --- a/agrolib/dbMeteoPoints/dbMeteoPointsHandler.cpp +++ b/agrolib/dbMeteoPoints/dbMeteoPointsHandler.cpp @@ -776,70 +776,56 @@ std::vector Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError } -std::vector Crit3DMeteoPointsDbHandler::loadHourlyVar(QString *myError, meteoVariable variable, - Crit3DDate dateStart, Crit3DDate dateEnd, - QDateTime* firstDateDB, Crit3DMeteoPoint *meteoPoint) +std::vector Crit3DMeteoPointsDbHandler::loadHourlyVar(meteoVariable variable, const QString& meteoPointId, + const QDateTime& startTime, const QDateTime& endTime, + QDateTime &firstDateDB, QString &myError) { - QString dateStr; - QDateTime previousDate; - QDate myDate; - QTime myTime; - float value; std::vector hourlyVarList; - bool isFirstRow = true; int idVar = getIdfromMeteoVar(variable); - QString startDate = QString::fromStdString(dateStart.toISOString()); - QString endDate = QString::fromStdString(dateEnd.toISOString()); + QString tableName = meteoPointId + "_H"; - QSqlQuery qry(_db); - - QString tableName = QString::fromStdString(meteoPoint->id) + "_H"; + QString statement = QString( "SELECT * FROM `%1` WHERE `%2` = %3 AND date_time >= DATETIME('%4') AND date_time <= DATETIME('%5') ORDER BY date_time") + .arg(tableName, FIELD_METEO_VARIABLE).arg(idVar).arg(startTime.toString("yyyy-MM-dd hh:mm:ss"), endTime.toString("yyyy-MM-dd hh:mm:ss")); - QString statement = QString( "SELECT * FROM `%1` WHERE `%2` = %3 AND date_time >= DATETIME('%4 01:00:00') AND date_time <= DATETIME('%5 00:00:00', '+1 day')") - .arg(tableName, FIELD_METEO_VARIABLE).arg(idVar).arg(startDate, endDate); + QSqlQuery qry(_db); if(! qry.exec(statement)) { - *myError = qry.lastError().text(); + myError = qry.lastError().text(); return hourlyVarList; } - else - { - while (qry.next()) - { - if (isFirstRow) - { - dateStr = qry.value(0).toString(); - myDate = QDate::fromString(dateStr.mid(0,10), "yyyy-MM-dd"); - myTime = QTime::fromString(dateStr.mid(11,8), "HH:mm:ss"); - QDateTime currentDate(QDateTime(myDate, myTime, Qt::UTC)); - *firstDateDB = currentDate; - previousDate = currentDate; + bool isFirstRow = true; + QDateTime previousDate; - value = qry.value(2).toFloat(); + while (qry.next()) + { + QString dateStr = qry.value(0).toString(); + QDate myDate = QDate::fromString(dateStr.mid(0,10), "yyyy-MM-dd"); + QTime myTime = QTime::fromString(dateStr.mid(11,8), "HH:mm:ss"); + QDateTime currentDate(QDateTime(myDate, myTime, Qt::UTC)); - hourlyVarList.push_back(value); - isFirstRow = false; - } - else + if (isFirstRow) + { + firstDateDB = currentDate; + isFirstRow = false; + } + else + { + int deltaHours = (currentDate.currentSecsSinceEpoch() - previousDate.currentSecsSinceEpoch()) / 3600; + if (deltaHours > 1) { - dateStr = qry.value(0).toString(); - myDate = QDate::fromString(dateStr.mid(0,10), "yyyy-MM-dd"); - myTime = QTime::fromString(dateStr.mid(11,8), "HH:mm:ss"); - QDateTime currentDate(QDateTime(myDate, myTime, Qt::UTC)); - - int missingHours = (currentDate.currentSecsSinceEpoch() - previousDate.currentSecsSinceEpoch()) / 3600; - for (int i=1; i < missingHours; i++) + // fill missing hours + for (int i=1; i < deltaHours; i++) { hourlyVarList.push_back(NODATA); } - value = qry.value(2).toFloat(); - - hourlyVarList.push_back(value); - previousDate = currentDate; } } + + previousDate = currentDate; + float value = qry.value(2).toFloat(); + hourlyVarList.push_back(value); } return hourlyVarList; diff --git a/agrolib/dbMeteoPoints/dbMeteoPointsHandler.h b/agrolib/dbMeteoPoints/dbMeteoPointsHandler.h index 3354447b6..c57743274 100644 --- a/agrolib/dbMeteoPoints/dbMeteoPointsHandler.h +++ b/agrolib/dbMeteoPoints/dbMeteoPointsHandler.h @@ -74,9 +74,8 @@ bool loadHourlyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint &meteoPoint); - std::vector loadHourlyVar(QString *myError, meteoVariable variable, - Crit3DDate dateStart, Crit3DDate dateEnd, - QDateTime* firstDateDB, Crit3DMeteoPoint *meteoPoint); + std::vector loadHourlyVar(meteoVariable variable, const QString& meteoPointId, const QDateTime& startTime, + const QDateTime& endTime, QDateTime &firstDateDB, QString &myError); bool loadVariableProperties(); bool getFieldList(const QString &tableName, QList &fieldList); diff --git a/agrolib/pragaProject/pragaProject.cpp b/agrolib/pragaProject/pragaProject.cpp index 45dac61af..651ba63b9 100644 --- a/agrolib/pragaProject/pragaProject.cpp +++ b/agrolib/pragaProject/pragaProject.cpp @@ -776,24 +776,22 @@ bool PragaProject::computeElaboration(bool isMeteoGrid, bool isAnomaly, bool isC } -bool PragaProject::computeElaborationHourly(bool isMeteoGrid, bool showInfo) +bool PragaProject::computeElaborationHourly(bool isMeteoGrid, bool isShowInfo) { + bool result; + if (isMeteoGrid) { - if (! elaborationCycleGridHourly(showInfo)) - return false; - - meteoGridDbHandler->meteoGrid()->setIsElabValue(true); + result = elaborationCycleGridHourly(isShowInfo); + meteoGridDbHandler->meteoGrid()->setIsElabValue(result); } else { - if (! elaborationCyclePointsHourly(showInfo)) - return false; - - setIsElabMeteoPointsValue(true); + result = elaborationCyclePointsHourly(isShowInfo); + setIsElabMeteoPointsValue(result); } - return true; + return result; } @@ -1053,7 +1051,55 @@ bool PragaProject::elaborationCycleGridHourly(bool showInfo) infoStep = setProgressBar(infoStr, this->meteoGridDbHandler->gridStructure().header().nrRows); } - // TODO + for (int row = 0; row < meteoGridDbHandler->gridStructure().header().nrRows; row++) + { + if (showInfo && (row % infoStep) == 0) + updateProgressBar(row); + + for (int col = 0; col < meteoGridDbHandler->gridStructure().header().nrCols; col++) + { + if (meteoGridDbHandler->meteoGrid()->getMeteoPointActiveId(row, col, &id)) + { + Crit3DMeteoPoint* meteoPoint = meteoGridDbHandler->meteoGrid()->meteoPointPointer(row,col); + + // copy data to meteoPointTemp + Crit3DMeteoPoint* meteoPointTemp = new Crit3DMeteoPoint; + meteoPointTemp->id = meteoPoint->id; + meteoPointTemp->point.z = meteoPoint->point.z; + meteoPointTemp->latitude = meteoPoint->latitude; + + // meteoPointTemp should be init + meteoPointTemp->nrObsDataDaysH = 0; + meteoPointTemp->nrObsDataDaysD = 0; + + bool isMeteoGrid = true; + if (elaborationOnPointHourly(nullptr, meteoGridDbHandler, meteoPointTemp, + isMeteoGrid, currentElaboration, meteoSettings, errorString)) + { + nrValidCells++; + } + + // save result to MP + meteoPoint->elaboration = meteoPointTemp->elaboration; + meteoPoint->anomaly = NODATA; + meteoPoint->anomalyPercentage = NODATA; + + delete meteoPointTemp; + } + } + } + + if (showInfo) closeProgressBar(); + delete currentElaboration; + + if (nrValidCells == 0) + { + if (errorString.isEmpty()) + { + errorString = "No valid cells available."; + } + return false; + } return true; } diff --git a/agrolib/pragaProject/pragaProject.h b/agrolib/pragaProject/pragaProject.h index d68e6d0ad..b0276ef5d 100644 --- a/agrolib/pragaProject/pragaProject.h +++ b/agrolib/pragaProject/pragaProject.h @@ -125,7 +125,7 @@ bool elaborationCycleGridHourly(bool showInfo); bool elaborationCheck(bool isMeteoGrid, bool isAnomaly); bool computeElaboration(bool isMeteoGrid, bool isAnomaly, bool isClimate, bool showInfo); - bool computeElaborationHourly(bool isMeteoGrid, bool showInfo); + bool computeElaborationHourly(bool isMeteoGrid, bool isShowInfo); bool showClimateFields(bool isMeteoGrid, QList *climateDbElab, QList *climateDbVarList); void readClimate(bool isMeteoGrid, QString climateSelected, int climateIndex, bool showInfo); bool deleteClimate(bool isMeteoGrid, QString climaSelected);