From bf5407f1ee90c67fc1fa84a5dcb06352c66c9648 Mon Sep 17 00:00:00 2001 From: giadasan Date: Tue, 30 Jul 2024 14:10:11 +0200 Subject: [PATCH] update output db writing --- .../outputPoints/dbOutputPointsHandler.cpp | 11 ++-- bin/CRITERIA3D/shared/project3D.cpp | 56 ++++++++++++++++++- bin/CRITERIA3D/shared/project3D.h | 12 ++++ 3 files changed, 72 insertions(+), 7 deletions(-) diff --git a/agrolib/outputPoints/dbOutputPointsHandler.cpp b/agrolib/outputPoints/dbOutputPointsHandler.cpp index 3e17f7b17..acd7db28f 100644 --- a/agrolib/outputPoints/dbOutputPointsHandler.cpp +++ b/agrolib/outputPoints/dbOutputPointsHandler.cpp @@ -97,23 +97,26 @@ bool Crit3DOutputPointsDbHandler::addCriteria3DColumn(const QString &tableName, } // column name + if (depth != NODATA) + { QString newField = variableString + "_" + QString::number(depth); + } // column exists already QList fieldList = getFields(&_db, tableName); - if ( fieldList.contains(newField) ) + if ( fieldList.contains(variableString) ) { return true; } // add column QString queryString = "ALTER TABLE '" + tableName + "'"; - queryString += " ADD " + newField + " REAL"; + queryString += " ADD " + variableString + " REAL"; QSqlQuery myQuery = _db.exec(queryString); if (myQuery.lastError().isValid()) { - errorStr = "Error in add column: " + newField + "\n" + myQuery.lastError().text(); + errorStr = "Error in add column: " + variableString + "\n" + myQuery.lastError().text(); return false; } @@ -193,7 +196,7 @@ bool Crit3DOutputPointsDbHandler::saveHourlyCriteria3D_Data(const QString &table } int nrValues = int(varList.size()) * nrSoilLayers; - if (nrValues != values.size()) + if (nrValues != int(values.size())) { errorStr = "Error saving values: number of values is not as expected."; return false; diff --git a/bin/CRITERIA3D/shared/project3D.cpp b/bin/CRITERIA3D/shared/project3D.cpp index 2a5554c06..416eb9869 100644 --- a/bin/CRITERIA3D/shared/project3D.cpp +++ b/bin/CRITERIA3D/shared/project3D.cpp @@ -168,6 +168,18 @@ void Project3D::initializeProject3D() totalEvaporation = 0; totalTranspiration = 0; + // specific outputs + isClimateOutput = false; + //waterDeficitDepth.clear(); + waterContentDepth.clear(); + //degreeOfSaturationDepth.clear(); + waterPotentialDepth.clear(); + //availableWaterDepth.clear(); + //fractionAvailableWaterDepth.clear(); + factorOfSafetyDepth.clear(); + //awcDepth.clear(); + + setCurrentFrequency(hourly); } @@ -250,6 +262,28 @@ bool Project3D::loadProject3DSettings() projectSettings->endGroup(); + // OUTPUT variables (optional) + QList depthList; + projectSettings->beginGroup("output"); + + depthList = projectSettings->value("waterContent").toStringList(); + if (! setVariableDepth(depthList, waterContentDepth)) + { + errorString = "Wrong water content depth in " + projectSettings->fileName(); + } + + depthList = projectSettings->value("waterPotential").toStringList(); + if (! setVariableDepth(depthList, waterPotentialDepth)) + { + errorString = "Wrong water potential depth in " + projectSettings->fileName(); + } + + depthList = projectSettings->value("factorOfSafety").toStringList(); + if (! setVariableDepth(depthList, factorOfSafetyDepth)) + { + errorString = "Wrong factor of safety depth in " + projectSettings->fileName(); + } + return true; } @@ -1961,15 +1995,15 @@ float Project3D::computeFactorOfSafety(int row, int col, unsigned int layerIndex double weightSum = 0; // [kPa] for (unsigned int layer = 1; layer <= layerIndex; layer++) { - long cuurentNode = indexMap.at(layer).value[row][col]; - if (cuurentNode != indexMap.at(layer).header->flag) + long currentNode = indexMap.at(layer).value[row][col]; + if (currentNode != indexMap.at(layer).header->flag) { int currentHorizon = soil::getHorizonIndex(soilList[unsigned(soilIndex)], layerDepth[layer]); if (currentHorizon != NODATA) { // [g cm-3] --> [Mg m-3] double bulkDensity = soilList[unsigned(soilIndex)].horizon[currentHorizon].bulkDensity; - double waterContent = soilFluxes3D::getWaterContent(cuurentNode); + double waterContent = soilFluxes3D::getWaterContent(currentNode); // [kN m-3] double unitWeight = (bulkDensity + waterContent) * GRAVITY; // [kPa] @@ -2159,3 +2193,19 @@ QString getDailyPrefixFromVar(QDate myDate, criteria3DVariable myVar) return fileName; } +bool setVariableDepth(const QList& depthList, std::vector& variableDepth) +{ + int nrDepth = depthList.size(); + if (nrDepth > 0) + { + variableDepth.resize(unsigned(nrDepth)); + for (int i = 0; i < nrDepth; i++) + { + variableDepth[unsigned(i)] = depthList[i].toInt(); + if (variableDepth[unsigned(i)] <= 0) + return false; + } + } + + return true; +} diff --git a/bin/CRITERIA3D/shared/project3D.h b/bin/CRITERIA3D/shared/project3D.h index f3daa00a7..03c19aa94 100644 --- a/bin/CRITERIA3D/shared/project3D.h +++ b/bin/CRITERIA3D/shared/project3D.h @@ -137,6 +137,17 @@ double totalEvaporation; // [m3 h-1] double totalTranspiration; // [m3 h-1] + // specific outputs + bool isClimateOutput; + std::vector waterContentDepth; + //std::vector degreeOfSaturationDepth; + std::vector waterPotentialDepth; + //std::vector waterDeficitDepth; + //std::vector awcDepth; + //std::vector availableWaterDepth; + //std::vector fractionAvailableWaterDepth; + std::vector factorOfSafetyDepth; + Project3D(); void initializeProject3D(); @@ -203,5 +214,6 @@ float readDataHourly(meteoVariable myVar, QString hourlyPath, QDateTime myTime, QString myArea, int row, int col); bool readHourlyMap(meteoVariable myVar, QString hourlyPath, QDateTime myTime, QString myArea, gis::Crit3DRasterGrid* myGrid); + bool setVariableDepth(const QList &depthList, std::vector &variableDepth); #endif // PROJECT3D_H