Skip to content

Commit

Permalink
Merge commit '7b888462eefce71d88d0df41159ea571993d6e5b'
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Mar 14, 2024
2 parents 9fad196 + 7b88846 commit 47a0309
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 112 deletions.
24 changes: 17 additions & 7 deletions agrolib/dbMeteoGrid/dbMeteoGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2306,15 +2306,14 @@ bool Crit3DMeteoGridDbHandler::loadGridAllMonthlyData(QString &myError, QDate fi
{
if (! _meteoGrid->findMeteoPointFromId(&row, &col, pointCode.toStdString()) )
{
myError = "Missing MeteoPoint id";
return false;
continue;
}
lastPointCode = pointCode;
}

if (! getValue(qry.value("VariableCode"), &varCode))
{
myError = "Missing VariableCode";
myError = "Missing VariableCode: " + QString::number(varCode);
return false;
}

Expand All @@ -2330,7 +2329,10 @@ bool Crit3DMeteoGridDbHandler::loadGridAllMonthlyData(QString &myError, QDate fi
}

if (! _meteoGrid->meteoPointPointer(row, col)->setMeteoPointValueM(getCrit3DDate(monthDate), variable, value))
{
myError = "Error in setMeteoPointValueM()";
return false;
}
}
}

Expand Down Expand Up @@ -2438,14 +2440,17 @@ std::vector<float> Crit3DMeteoGridDbHandler::loadGridDailyVar(QString *myError,
return dailyVarList;
}

std::vector<float> Crit3DMeteoGridDbHandler::exportAllDataVar(QString *myError, frequencyType freq, meteoVariable variable, QString id, std::vector<QString> &dateStr)
std::vector<float> Crit3DMeteoGridDbHandler::exportAllDataVar(QString *myError, frequencyType freq, meteoVariable variable, QString id, QDateTime myFirstTime, QDateTime myLastTime, std::vector<QString> &dateStr)
{
QString myDateStr;
float value;
std::vector<float> allDataVarList;

QSqlQuery myQuery(_db);
QString tableName;
QString statement;
QString startDate;
QString endDate;
int idVar;

if (freq == daily)
Expand All @@ -2457,6 +2462,10 @@ std::vector<float> Crit3DMeteoGridDbHandler::exportAllDataVar(QString *myError,
return allDataVarList;
}
tableName = _tableDaily.prefix + id + _tableDaily.postFix;
startDate = myFirstTime.date().toString("yyyy-MM-dd");
endDate = myLastTime.date().toString("yyyy-MM-dd");
statement = QString( "SELECT * FROM `%1` WHERE VariableCode = '%2' AND `%3` >= '%4' AND `%3`<= '%5'")
.arg(tableName).arg(idVar).arg(_tableDaily.fieldTime).arg(startDate).arg(endDate);
}
else if (freq == hourly)
{
Expand All @@ -2467,15 +2476,16 @@ std::vector<float> Crit3DMeteoGridDbHandler::exportAllDataVar(QString *myError,
return allDataVarList;
}
tableName = _tableHourly.prefix + id + _tableHourly.postFix;
startDate = myFirstTime.date().toString("yyyy-MM-dd") + " " + myFirstTime.time().toString("hh:mm");
endDate = myLastTime.date().toString("yyyy-MM-dd") + " " + myLastTime.time().toString("hh:mm");
statement = QString( "SELECT * FROM `%1` WHERE VariableCode = '%2' AND `%3` >= '%4' AND `%3`<= '%5'")
.arg(tableName).arg(idVar).arg(_tableHourly.fieldTime).arg(startDate).arg(endDate);
}
else
{
*myError = "Frequency should be daily or hourly";
return allDataVarList;
}

QString statement = QString( "SELECT * FROM `%1` WHERE VariableCode = '%2'")
.arg(tableName).arg(idVar);
QDateTime dateTime;
QDate date;
if( !myQuery.exec(statement) )
Expand Down
2 changes: 1 addition & 1 deletion agrolib/dbMeteoGrid/dbMeteoGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
std::vector<float> loadGridDailyVarFixedFields(QString *myError, QString meteoPoint, meteoVariable variable, QDate first, QDate last, QDate* firstDateDB);
std::vector<float> loadGridHourlyVar(QString *myError, QString meteoPoint, meteoVariable variable, QDateTime first, QDateTime last, QDateTime* firstDateDB);
std::vector<float> loadGridHourlyVarFixedFields(QString *myError, QString meteoPoint, meteoVariable variable, QDateTime first, QDateTime last, QDateTime* firstDateDB);
std::vector<float> exportAllDataVar(QString *myError, frequencyType freq, meteoVariable variable, QString id, std::vector<QString> &dateStr);
std::vector<float> exportAllDataVar(QString *myError, frequencyType freq, meteoVariable variable, QString id, QDateTime myFirstTime, QDateTime myLastTime, std::vector<QString> &dateStr);
bool getYearList(QString *myError, QString meteoPoint, QList<QString>* yearList);
bool idDailyList(QString *myError, QList<QString>* idMeteoList);

Expand Down
17 changes: 13 additions & 4 deletions agrolib/dbMeteoPoints/dbMeteoPointsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ std::vector<float> Crit3DMeteoPointsDbHandler::loadDailyVar(QString *myError, me
return dailyVarList;
}

std::vector<float> Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError, frequencyType freq, meteoVariable variable, QString id, std::vector<QString> &dateStr)
std::vector<float> Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError, frequencyType freq, meteoVariable variable, QString id, QDateTime myFirstTime, QDateTime myLastTime, std::vector<QString> &dateStr)
{
QString myDateStr;
QDateTime dateTime;
Expand All @@ -699,24 +699,33 @@ std::vector<float> Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError

QSqlQuery myQuery(_db);
QString tableName;
QString startDate;
QString endDate;
QString statement;

if (freq == daily)
{
tableName = id + "_D";
startDate = myFirstTime.date().toString("yyyy-MM-dd");
endDate = myLastTime.date().toString("yyyy-MM-dd");
statement = QString( "SELECT * FROM `%1` WHERE `%2` = %3 AND date_time >= DATE('%4') AND date_time <= DATE('%5')")
.arg(tableName).arg(FIELD_METEO_VARIABLE).arg(idVar).arg(startDate).arg(endDate);

}
else if (freq == hourly)
{
tableName = id + "_H";
startDate = myFirstTime.date().toString("yyyy-MM-dd") + " " + myFirstTime.time().toString("hh:mm");
endDate = myLastTime.date().toString("yyyy-MM-dd") + " " + myLastTime.time().toString("hh:mm");
statement = QString( "SELECT * FROM `%1` WHERE `%2` = %3 AND date_time >= DATETIME('%4') AND date_time <= DATETIME('%5')")
.arg(tableName).arg(FIELD_METEO_VARIABLE).arg(idVar).arg(startDate).arg(endDate);
}
else
{
*myError = "Frequency should be daily or hourly";
return allDataVarList;
}

QString statement = QString( "SELECT * FROM `%1` WHERE `%2` = %3")
.arg(tableName).arg(FIELD_METEO_VARIABLE).arg(idVar);

if( !myQuery.exec(statement) )
{
*myError = myQuery.lastError().text();
Expand Down
2 changes: 1 addition & 1 deletion agrolib/dbMeteoPoints/dbMeteoPointsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
std::vector<float> loadDailyVar(QString *myError, meteoVariable variable,
Crit3DDate dateStart, Crit3DDate dateEnd,
QDate* firstDateDB, Crit3DMeteoPoint *meteoPoint);
std::vector<float> exportAllDataVar(QString *myError, frequencyType freq, meteoVariable variable, QString id, std::vector<QString> &dateStr);
std::vector<float> exportAllDataVar(QString *myError, frequencyType freq, meteoVariable variable, QString id, QDateTime myFirstTime, QDateTime myLastTime, std::vector<QString> &dateStr);

bool loadHourlyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint *meteoPoint);

Expand Down
19 changes: 12 additions & 7 deletions agrolib/drought/drought.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
#include "commonConstants.h"
#include "basicMath.h"
#include "gammaFunction.h"
#include "meteo.h"

#include <algorithm>


Drought::Drought(droughtIndex index, int firstYear, int lastYear, Crit3DDate date, Crit3DMeteoPoint* meteoPoint, Crit3DMeteoSettings* meteoSettings)
{
this->index = index;
Expand Down Expand Up @@ -79,28 +82,28 @@ void Drought::setComputeAll(bool value)
computeAll = value;
}


float Drought::computeDroughtIndex()
{

timeScale = timeScale - 1; // index start from 0
if (index == INDEX_SPI)
{
if (!computeSpiParameters())
if (! computeSpiParameters())
{
return NODATA;
}
}
else if (index == INDEX_SPEI)
{
if (!computeSpeiParameters())
if (! computeSpeiParameters())
{
return NODATA;
}
}
int start;
int end;

int start, end;
std::vector<float> mySum(meteoPoint->nrObsDataDaysM);
for (int i = 0; i<meteoPoint->nrObsDataDaysM; i++)
for (int i = 0; i < meteoPoint->nrObsDataDaysM; i++)
{
droughtResults.push_back(NODATA);
}
Expand Down Expand Up @@ -133,7 +136,7 @@ float Drought::computeDroughtIndex()
for (int j = start; j <= end; j++)
{
mySum[j] = 0;
for(int i = 0; i<=timeScale; i++)
for(int i = 0; i <= timeScale; i++)
{
if ((j-i)>=0 && j < meteoPoint->nrObsDataDaysM)
{
Expand Down Expand Up @@ -193,9 +196,11 @@ float Drought::computeDroughtIndex()
}
}
}

return droughtResults[end];
}


bool Drought::computeSpiParameters()
{
if (meteoPoint->nrObsDataDaysM == 0)
Expand Down
27 changes: 17 additions & 10 deletions agrolib/interpolation/interpolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1431,25 +1431,32 @@ bool multipleDetrending(std::vector <Crit3DInterpolationDataPoint> &myPoints, Cr

// exclude points with incomplete proxies
unsigned i;
std::vector <Crit3DInterpolationDataPoint> finalPoints;
//std::vector <Crit3DInterpolationDataPoint> finalPoints;
bool isValid;
float proxyValue;
vector<Crit3DInterpolationDataPoint>::iterator it = myPoints.begin();

for (i=0; i < myPoints.size(); i++)
while (it != myPoints.end())
{
isValid = true;
for (pos=0; pos < mySettings->getProxyNr(); pos++)
if (mySettings->getProxy(pos)->getIsSignificant())
{
proxyValue = myPoints[i].getProxyValue(pos);
proxyValue = it->getProxyValue(pos);
if (proxyValue == NODATA)
{
isValid = false;
break;
}
}

if (isValid) finalPoints.push_back(myPoints[i]);
if (! isValid)
{
it = myPoints.erase(it);
}
else {
it++;
}
}

// proxy spatial variability (2nd step)
Expand All @@ -1459,7 +1466,7 @@ bool multipleDetrending(std::vector <Crit3DInterpolationDataPoint> &myPoints, Cr
validNr = 0;
for (pos=0; pos < int(mySettings->getProxyNr()); pos++)
{
if (myCombination.getValue(pos) && proxyValidity(finalPoints, pos, mySettings->getProxy(pos)->getStdDevThreshold(), &avg, &stdDev))
if (myCombination.getValue(pos) && proxyValidity(myPoints, pos, mySettings->getProxy(pos)->getStdDevThreshold(), &avg, &stdDev))
{
avgs.push_back(avg);
stdDevs.push_back(stdDev);
Expand All @@ -1479,24 +1486,24 @@ bool multipleDetrending(std::vector <Crit3DInterpolationDataPoint> &myPoints, Cr
std::vector <double> predictands;
std::vector <double> weights;

for (i=0; i < finalPoints.size(); i++)
for (i=0; i < myPoints.size(); i++)
{
rowPredictors.clear();
for (pos=0; pos < mySettings->getProxyNr(); pos++)
if ((mySettings->getProxy(pos)->getIsSignificant()))
{
proxyValue = finalPoints[i].getProxyValue(pos);
proxyValue = myPoints[i].getProxyValue(pos);
//rowPredictors.push_back((proxyValue - avgs[index]) / stdDevs[index]);
rowPredictors.push_back(proxyValue);
}

//predictorsNorm.push_back(rowPredictors);
predictors.push_back(rowPredictors);
predictands.push_back(finalPoints[i].value);
weights.push_back(finalPoints[i].regressionWeight);
predictands.push_back(myPoints[i].value);
weights.push_back(myPoints[i].regressionWeight);
}

if (finalPoints.size() < mySettings->getMinPointsLocalDetrending())
if (myPoints.size() < mySettings->getMinPointsLocalDetrending())
{
for (pos = 0; pos < mySettings->getProxyNr(); pos++)
mySettings->getProxy(pos)->setIsSignificant(false);
Expand Down
14 changes: 10 additions & 4 deletions agrolib/mathFunctions/furtherMathFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1065,19 +1065,25 @@ namespace interpolation

//int iRandom = 0;
int counter = 0;
srand (unsigned(time(nullptr)));
//std::random_device rd;
//std::mt19937 gen(rd());
//srand (unsigned(time(nullptr)));
std::random_device rd;
std::mt19937 gen(rd());
//std::uniform_real_distribution<double> dis(0.0, 1.0);
std::normal_distribution<double> normal_dis(0.5, 0.2);
double truncNormal;
double randomNumber;
do
{
for (i=0; i<nrPredictors; i++)
{
for (j=0; j<nrParameters[i]; j++)
{
parameters[i][j] = parametersMin[i][j] + ((double) rand() / (RAND_MAX))*(parametersMax[i][j]-parametersMin[i][j]);
//parameters[i][j] = parametersMin[i][j] + ((double) rand() / (RAND_MAX))*(parametersMax[i][j]-parametersMin[i][j]);
//parameters[i][j] = parametersMin[i][j] + (dis(gen))*(parametersMax[i][j]-parametersMin[i][j]);
do {
truncNormal = normal_dis(gen);
} while(truncNormal <= 0.0 || truncNormal >= 1.0);
parameters[i][j] = parametersMin[i][j] + (truncNormal)*(parametersMax[i][j]-parametersMin[i][j]);
}
}
fittingMarquardt_nDimension(func,myFunc,parametersMin, parametersMax,
Expand Down
6 changes: 4 additions & 2 deletions agrolib/meteo/meteo.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@
{ dailyCoolingDegreeDays, "DAILY_DEGREEDAYS_COOLING" },
{ dailyWaterTableDepth, "DAILY_WATER_TABLE_DEPTH" },
{ elaboration, "ELABORATION" },
{ anomaly, "ANOMALY" }
{ anomaly, "ANOMALY" },
{ noMeteoVar, "NO_VARIABLE" }
};

const std::map<std::string, meteoVariable> MapHourlyMeteoVar = {
Expand Down Expand Up @@ -226,7 +227,8 @@
{ snowSurfaceEnergy, "SNOW_SURF_ENERGY"},
{ snowInternalEnergy, "SNOW_INT_ENERGY"},
{ sensibleHeat, "SENSIBLE_HEAT"},
{ latentHeat, "LATENT_HEAT"}
{ latentHeat, "LATENT_HEAT"},
{ noMeteoVar, "NO_VARIABLE" }
};

const std::map<std::string, meteoVariable> MapMonthlyMeteoVar = {
Expand Down
3 changes: 2 additions & 1 deletion agrolib/meteo/meteoPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1298,11 +1298,12 @@ bool Crit3DMeteoPoint::computeMonthlyAggregate(Crit3DDate firstDate, Crit3DDate
currentMonth = actualDate.addDays(1).month;
nrDays = getDaysInMonth(currentMonth, actualDate.year);
}

}

return aggregateDailyInMonthly;
}


TObsDataH *Crit3DMeteoPoint::getObsDataH() const
{
return obsDataH;
Expand Down
Loading

0 comments on commit 47a0309

Please sign in to comment.