From bb49ce04dc1dc96ef64f76afc6c69d48b377f705 Mon Sep 17 00:00:00 2001 From: ftomei Date: Tue, 30 Jul 2024 16:42:09 +0200 Subject: [PATCH] set criteria output depth --- DATA/PROJECT/Montue/Montue.ini | 3 +- .../outputPoints/dbOutputPointsHandler.cpp | 79 ++++++------- agrolib/outputPoints/dbOutputPointsHandler.h | 16 ++- bin/CRITERIA3D/criteria3DProject.cpp | 110 ++++++++++-------- bin/CRITERIA3D/criteria3DProject.h | 3 + bin/CRITERIA3D/mainwindow.cpp | 10 -- bin/CRITERIA3D/shared/project3D.cpp | 4 +- bin/CRITERIA3D/shared/project3D.h | 2 +- 8 files changed, 122 insertions(+), 105 deletions(-) diff --git a/DATA/PROJECT/Montue/Montue.ini b/DATA/PROJECT/Montue/Montue.ini index d217ea5ea..0e2677bf9 100644 --- a/DATA/PROJECT/Montue/Montue.ini +++ b/DATA/PROJECT/Montue/Montue.ini @@ -26,4 +26,5 @@ tile_map=GOOGLE [output] waterContent=20,40,55,65,95,105,115,125,140 waterPotential=20,55,65,115,125 -factorOfSafety=20,55,65,95,105,115,125 \ No newline at end of file +factorOfSafety=20,55,65,95,105,115,125 +degreeOfSaturation=20 \ No newline at end of file diff --git a/agrolib/outputPoints/dbOutputPointsHandler.cpp b/agrolib/outputPoints/dbOutputPointsHandler.cpp index acd7db28f..ce6b0ab0f 100644 --- a/agrolib/outputPoints/dbOutputPointsHandler.cpp +++ b/agrolib/outputPoints/dbOutputPointsHandler.cpp @@ -99,7 +99,7 @@ bool Crit3DOutputPointsDbHandler::addCriteria3DColumn(const QString &tableName, // column name if (depth != NODATA) { - QString newField = variableString + "_" + QString::number(depth); + variableString += "_" + QString::number(depth); } // column exists already @@ -181,21 +181,18 @@ bool Crit3DOutputPointsDbHandler::saveHourlyMeteoData(const QString &tableName, } -// layerDepth [m] +// variableDepth [cm] bool Crit3DOutputPointsDbHandler::saveHourlyCriteria3D_Data(const QString &tableName, const QDateTime& myTime, - const std::vector& varList, - const std::vector& values, - const std::vector & layerDepth, - QString &errorStr) + const std::vector& values, + const std::vector& waterContentDepth, + const std::vector& waterPotentialDepth, + const std::vector& degreeOfSaturationDepth, + const std::vector& factorOfSafetyDepth, + QString &errorStr) { - int nrSoilLayers = int(layerDepth.size()) - 1; - if (nrSoilLayers <= 0) - { - errorStr = "Error saving values: missing soil layers."; - return false; - } + int nrValues = waterContentDepth.size() + waterPotentialDepth.size() + + degreeOfSaturationDepth.size() + factorOfSafetyDepth.size(); - int nrValues = int(varList.size()) * nrSoilLayers; if (nrValues != int(values.size())) { errorStr = "Error saving values: number of values is not as expected."; @@ -204,35 +201,16 @@ bool Crit3DOutputPointsDbHandler::saveHourlyCriteria3D_Data(const QString &table QString timeStr = myTime.toString("yyyy-MM-dd HH:mm:ss"); - // field list - QString setList = ""; - for (unsigned int i = 0; i < varList.size(); i++) - { - QString variableString = QString::fromStdString(getCriteria3DVarName(varList[i])); - if (variableString == "") - { - errorStr = "Missing variable name."; - return false; - } - - for (int layer = 1; layer <= nrSoilLayers; layer++) - { - int depth = round(layerDepth[layer] * 100); // [cm] + QList valueList; + int firstIndex = 0; + appendCriteria3DOutputValue(volumetricWaterContent, waterContentDepth, values, firstIndex, valueList); + appendCriteria3DOutputValue(waterMatricPotential, waterPotentialDepth, values, firstIndex, valueList); + appendCriteria3DOutputValue(degreeOfSaturation, degreeOfSaturationDepth, values, firstIndex, valueList); + appendCriteria3DOutputValue(factorOfSafety, factorOfSafetyDepth, values, firstIndex, valueList); - QString fieldName = variableString + "_" + QString::number(depth); - setList += "'" + fieldName + "'="; - - int index = i * nrSoilLayers + layer - 1; - QString valueStr = QString::number(values[index], 'f', 3); - setList += valueStr; - - if (index < (nrValues - 1)) - setList += ","; - } - } QSqlQuery qry(_db); - QString queryString = QString("UPDATE '%1' SET %2 WHERE DATE_TIME ='%3'").arg(tableName, setList, timeStr); + QString queryString = QString("UPDATE '%1' SET %2 WHERE DATE_TIME ='%3'").arg(tableName, valueList.join(','), timeStr); if (! qry.exec(queryString)) { errorStr = QString("Error in query: " + queryString + "\n" + qry.lastError().text()); @@ -242,3 +220,26 @@ bool Crit3DOutputPointsDbHandler::saveHourlyCriteria3D_Data(const QString &table return true; } + +void Crit3DOutputPointsDbHandler::appendCriteria3DOutputValue(criteria3DVariable myVar, const std::vector &depthList, + const std::vector& values, int &firstIndex, + QList &outputList) +{ + QString variableString = QString::fromStdString(getCriteria3DVarName(myVar)); + + for (int l = 0; l < depthList.size(); l++) + { + float depth_cm = depthList[l]; + QString fieldName = variableString + "_" + QString::number(depth_cm); + + int index = firstIndex + l; + QString valueStr = QString::number(values[index], 'f', 3); + + QString assignStr = "'" + fieldName + "'=" + valueStr; + + outputList.push_back(assignStr); + } + + firstIndex += depthList.size(); +} + diff --git a/agrolib/outputPoints/dbOutputPointsHandler.h b/agrolib/outputPoints/dbOutputPointsHandler.h index 00742d1fa..b53bf3ce8 100644 --- a/agrolib/outputPoints/dbOutputPointsHandler.h +++ b/agrolib/outputPoints/dbOutputPointsHandler.h @@ -33,12 +33,20 @@ const std::vector &varList, const std::vector &values, QString &errorStr); - bool saveHourlyCriteria3D_Data(const QString &tableName, const QDateTime &myTime, - const std::vector &varList, - const std::vector &values, - const std::vector &layerDepth, QString &errorStr); + bool saveHourlyCriteria3D_Data(const QString &tableName, const QDateTime& myTime, + const std::vector& values, + const std::vector& waterContentDepth, + const std::vector& waterPotentialDepth, + const std::vector& degreeOfSaturationDepth, + const std::vector& factorOfSafetyDepth, + QString &errorStr); + + void appendCriteria3DOutputValue(criteria3DVariable myVar, const std::vector &depthList, + const std::vector& values, int &firstIndex, + QList &outputList); private: + QSqlDatabase _db; QString errorString; }; diff --git a/bin/CRITERIA3D/criteria3DProject.cpp b/bin/CRITERIA3D/criteria3DProject.cpp index 016930c02..cedd577d3 100644 --- a/bin/CRITERIA3D/criteria3DProject.cpp +++ b/bin/CRITERIA3D/criteria3DProject.cpp @@ -367,6 +367,16 @@ bool Crit3DProject::runModels(QDateTime firstTime, QDateTime lastTime, bool isRe { if (! isRestart) { + // create tables for output points + if (isSaveOutputPoints()) + { + if (! writeOutputPointsTables()) + { + logError(); + return false; + } + } + // initialize meteo maps if (processes.computeMeteo) { @@ -1609,19 +1619,30 @@ bool Crit3DProject::writeOutputPointsTables() } if (processes.computeWater) { - for (int l = 1; l < int(layerDepth.size()); l++) + for (int l = 0; l < waterContentDepth.size(); l++) { - int depth_cm = round(layerDepth[l] * 100); + int depth_cm = waterContentDepth[l]; if (! outputPointsDbHandler->addCriteria3DColumn(tableName, volumetricWaterContent, depth_cm, errorString)) return false; } - } - if (processes.computeSlopeStability) - { - // slope stablility starts from layer 1 - for (int l = 1; l < int(layerDepth.size()); l++) + + for (int l = 0; l < waterPotentialDepth.size(); l++) + { + int depth_cm = waterPotentialDepth[l]; + if (! outputPointsDbHandler->addCriteria3DColumn(tableName, waterMatricPotential, depth_cm, errorString)) + return false; + } + + for (int l = 0; l < degreeOfSaturationDepth.size(); l++) { - int depth_cm = round(layerDepth[l] * 100); + int depth_cm = degreeOfSaturationDepth[l]; + if (! outputPointsDbHandler->addCriteria3DColumn(tableName, degreeOfSaturation, depth_cm, errorString)) + return false; + } + + for (int l = 0; l < factorOfSafetyDepth.size(); l++) + { + int depth_cm = factorOfSafetyDepth[l]; if (! outputPointsDbHandler->addCriteria3DColumn(tableName, factorOfSafety, depth_cm, errorString)) return false; } @@ -1637,7 +1658,6 @@ bool Crit3DProject::writeOutputPointsData() { QString tableName; std::vector meteoVarList; - std::vector criteria3dVarList; std::vector meteoValuesList; std::vector criteria3dValuesList; @@ -1667,14 +1687,6 @@ bool Crit3DProject::writeOutputPointsData() meteoVarList.push_back(sensibleHeat); meteoVarList.push_back(latentHeat); } - if (processes.computeWater) - { - criteria3dVarList.push_back(volumetricWaterContent); - } - if (processes.computeSlopeStability) - { - criteria3dVarList.push_back(factorOfSafety); - } for (unsigned int i = 0; i < outputPoints.size(); i++) { @@ -1713,43 +1725,21 @@ bool Crit3DProject::writeOutputPointsData() if (processes.computeWater) { int row, col; - float value; gis::getRowColFromXY((*DEM.header), x, y, &row, &col); - for (unsigned int layerIndex = 1; layerIndex < nrLayers; layerIndex++) - { - long nodeIndex = indexMap.at(layerIndex).value[row][col]; - if (nodeIndex == indexMap.at(layerIndex).header->flag) - { - value = NODATA; - } - else - { - value = getCriteria3DVar(volumetricWaterContent, nodeIndex); - } - - criteria3dValuesList.push_back(value); - } - } - if (processes.computeSlopeStability) - { - int row, col; - float value; - gis::getRowColFromXY((*DEM.header), x, y, &row, &col); - - for (unsigned int layerIndex = 1; layerIndex < nrLayers; layerIndex++) - { - value = computeFactorOfSafety(row, col, layerIndex); - criteria3dValuesList.push_back(value); - } + appendCriteria3DOutputValue(volumetricWaterContent, row, col, waterContentDepth, criteria3dValuesList); + appendCriteria3DOutputValue(waterMatricPotential, row, col, waterPotentialDepth, criteria3dValuesList); + appendCriteria3DOutputValue(degreeOfSaturation, row, col, degreeOfSaturationDepth, criteria3dValuesList); + appendCriteria3DOutputValue(factorOfSafety, row, col, factorOfSafetyDepth, criteria3dValuesList); } if (! outputPointsDbHandler->saveHourlyMeteoData(tableName, getCurrentTime(), meteoVarList, meteoValuesList, errorString)) { return false; } - if (! outputPointsDbHandler->saveHourlyCriteria3D_Data(tableName, getCurrentTime(), criteria3dVarList, - criteria3dValuesList, layerDepth, errorString)) + if (! outputPointsDbHandler->saveHourlyCriteria3D_Data(tableName, getCurrentTime(), criteria3dValuesList, + waterContentDepth, waterPotentialDepth, + degreeOfSaturationDepth, factorOfSafetyDepth, errorString)) { return false; } @@ -1760,12 +1750,38 @@ bool Crit3DProject::writeOutputPointsData() } meteoVarList.clear(); - criteria3dVarList.clear(); return true; } +void Crit3DProject::appendCriteria3DOutputValue(criteria3DVariable myVar, int row, int col, + const std::vector &depthList, std::vector &outputList) +{ + for (int l = 0; l < depthList.size(); l++) + { + float depth = depthList[l] * 0.01; // [cm] -> [m] + int layerIndex = getSoilLayerIndex(depth); + long nodeIndex = indexMap.at(layerIndex).value[row][col]; + float value = NODATA; + + if (nodeIndex != indexMap.at(layerIndex).header->flag) + { + if (myVar == factorOfSafety) + { + value = computeFactorOfSafety(row, col, layerIndex); + } + else + { + value = getCriteria3DVar(myVar, nodeIndex); + } + } + + outputList.push_back(value); + } +} + + bool Crit3DProject::writeMeteoPointsProperties(const QList &joinedPropertiesList, const QList &csvFields, const QList> &csvData) { diff --git a/bin/CRITERIA3D/criteria3DProject.h b/bin/CRITERIA3D/criteria3DProject.h index a2fc43cc9..39ae29e83 100644 --- a/bin/CRITERIA3D/criteria3DProject.h +++ b/bin/CRITERIA3D/criteria3DProject.h @@ -36,6 +36,9 @@ bool saveSnowModelState(const QString ¤tStatePath); bool saveSoilWaterState(const QString ¤tStatePath); + void appendCriteria3DOutputValue(criteria3DVariable myVar, int row, int col, + const std::vector &depthList, std::vector &outputList); + public: Crit3DGeometry* openGlGeometry; diff --git a/bin/CRITERIA3D/mainwindow.cpp b/bin/CRITERIA3D/mainwindow.cpp index 92834de11..673b269bc 100644 --- a/bin/CRITERIA3D/mainwindow.cpp +++ b/bin/CRITERIA3D/mainwindow.cpp @@ -2012,16 +2012,6 @@ bool MainWindow::startModels(QDateTime firstTime, QDateTime lastTime) } myProject.closeLogInfo(); - // output points - if (myProject.isSaveOutputPoints()) - { - if (! myProject.writeOutputPointsTables()) - { - myProject.logError(); - return false; - } - } - // set model interface myProject.modelFirstTime = firstTime; myProject.modelLastTime = lastTime; diff --git a/bin/CRITERIA3D/shared/project3D.cpp b/bin/CRITERIA3D/shared/project3D.cpp index d497ae824..1d38fb299 100644 --- a/bin/CRITERIA3D/shared/project3D.cpp +++ b/bin/CRITERIA3D/shared/project3D.cpp @@ -78,7 +78,6 @@ void Crit3DProcesses::initialize() computeMeteo = false; computeRadiation = false; computeWater = false; - computeSlopeStability = false; computeCrop = false; computeSnow = false; computeSolutes = false; @@ -120,7 +119,6 @@ void Crit3DProcesses::setComputeWater(bool value) { computeMeteo = true; computeRadiation = true; - computeSlopeStability = true; } } @@ -1389,7 +1387,7 @@ double Project3D::getSoilLayerBottom(unsigned int i) return layerDepth[i] + layerThickness[i] / 2.0; } -// soil layer index from soil depth +// soil layer index from soil depth [m] int Project3D::getSoilLayerIndex(double depth) { unsigned int i= 0; diff --git a/bin/CRITERIA3D/shared/project3D.h b/bin/CRITERIA3D/shared/project3D.h index f572abb15..81e672538 100644 --- a/bin/CRITERIA3D/shared/project3D.h +++ b/bin/CRITERIA3D/shared/project3D.h @@ -46,7 +46,7 @@ { public: - bool computeMeteo, computeRadiation, computeWater, computeSlopeStability; + bool computeMeteo, computeRadiation, computeWater; bool computeCrop, computeSnow, computeSolutes; bool computeHeat, computeAdvectiveHeat, computeLatentHeat;