Skip to content

Commit

Permalink
set criteria output depth
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Jul 30, 2024
1 parent 46bfc53 commit bb49ce0
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 105 deletions.
3 changes: 2 additions & 1 deletion DATA/PROJECT/Montue/Montue.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
factorOfSafety=20,55,65,95,105,115,125
degreeOfSaturation=20
79 changes: 40 additions & 39 deletions agrolib/outputPoints/dbOutputPointsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<criteria3DVariable>& varList,
const std::vector<float>& values,
const std::vector <double>& layerDepth,
QString &errorStr)
const std::vector<float>& values,
const std::vector<int>& waterContentDepth,
const std::vector<int>& waterPotentialDepth,
const std::vector<int>& degreeOfSaturationDepth,
const std::vector<int>& 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.";
Expand All @@ -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<QString> 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());
Expand All @@ -242,3 +220,26 @@ bool Crit3DOutputPointsDbHandler::saveHourlyCriteria3D_Data(const QString &table
return true;
}


void Crit3DOutputPointsDbHandler::appendCriteria3DOutputValue(criteria3DVariable myVar, const std::vector<int> &depthList,
const std::vector<float>& values, int &firstIndex,
QList<QString> &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();
}

16 changes: 12 additions & 4 deletions agrolib/outputPoints/dbOutputPointsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@
const std::vector<meteoVariable> &varList,
const std::vector<float> &values, QString &errorStr);

bool saveHourlyCriteria3D_Data(const QString &tableName, const QDateTime &myTime,
const std::vector<criteria3DVariable> &varList,
const std::vector<float> &values,
const std::vector <double> &layerDepth, QString &errorStr);
bool saveHourlyCriteria3D_Data(const QString &tableName, const QDateTime& myTime,
const std::vector<float>& values,
const std::vector<int>& waterContentDepth,
const std::vector<int>& waterPotentialDepth,
const std::vector<int>& degreeOfSaturationDepth,
const std::vector<int>& factorOfSafetyDepth,
QString &errorStr);

void appendCriteria3DOutputValue(criteria3DVariable myVar, const std::vector<int> &depthList,
const std::vector<float>& values, int &firstIndex,
QList<QString> &outputList);

private:

QSqlDatabase _db;
QString errorString;
};
Expand Down
110 changes: 63 additions & 47 deletions bin/CRITERIA3D/criteria3DProject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -1637,7 +1658,6 @@ bool Crit3DProject::writeOutputPointsData()
{
QString tableName;
std::vector<meteoVariable> meteoVarList;
std::vector<criteria3DVariable> criteria3dVarList;
std::vector<float> meteoValuesList;
std::vector<float> criteria3dValuesList;

Expand Down Expand Up @@ -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++)
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<int> &depthList, std::vector<float> &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<QString> &joinedPropertiesList, const QList<QString> &csvFields,
const QList<QList<QString>> &csvData)
{
Expand Down
3 changes: 3 additions & 0 deletions bin/CRITERIA3D/criteria3DProject.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
bool saveSnowModelState(const QString &currentStatePath);
bool saveSoilWaterState(const QString &currentStatePath);

void appendCriteria3DOutputValue(criteria3DVariable myVar, int row, int col,
const std::vector<int> &depthList, std::vector<float> &outputList);

public:
Crit3DGeometry* openGlGeometry;

Expand Down
10 changes: 0 additions & 10 deletions bin/CRITERIA3D/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions bin/CRITERIA3D/shared/project3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ void Crit3DProcesses::initialize()
computeMeteo = false;
computeRadiation = false;
computeWater = false;
computeSlopeStability = false;
computeCrop = false;
computeSnow = false;
computeSolutes = false;
Expand Down Expand Up @@ -120,7 +119,6 @@ void Crit3DProcesses::setComputeWater(bool value)
{
computeMeteo = true;
computeRadiation = true;
computeSlopeStability = true;
}
}

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion bin/CRITERIA3D/shared/project3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
{
public:

bool computeMeteo, computeRadiation, computeWater, computeSlopeStability;
bool computeMeteo, computeRadiation, computeWater;
bool computeCrop, computeSnow, computeSolutes;
bool computeHeat, computeAdvectiveHeat, computeLatentHeat;

Expand Down

0 comments on commit bb49ce0

Please sign in to comment.