Skip to content

Commit

Permalink
Merge commit '83af3463ca643be1cbe00f1d911fc67359c1661e'
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Mar 11, 2024
2 parents 74bc596 + 83af346 commit dfe6b1b
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 83 deletions.
40 changes: 20 additions & 20 deletions agrolib/climate/climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3068,7 +3068,7 @@ bool parseXMLElaboration(Crit3DElabList *listXMLElab, Crit3DAnomalyList *listXML
if (myTag == "PERIOD")
{
periodPresent = true;
if (parseXMLPeriodTag(child, listXMLElab, listXMLAnomaly, false, false, period, firstYear, myError) == false)
if (parseXMLPeriodTag(child, listXMLElab, listXMLAnomaly, false, false, period, myError) == false)
{
listXMLElab->eraseElement(nElab);
qDebug() << "parseXMLPeriodTag ";
Expand Down Expand Up @@ -3349,7 +3349,7 @@ bool parseXMLElaboration(Crit3DElabList *listXMLElab, Crit3DAnomalyList *listXML

if (myTag == "PERIOD")
{
if (parseXMLPeriodTag(child, listXMLElab, listXMLAnomaly, true, false, period, firstYear, myError) == false)
if (parseXMLPeriodTag(child, listXMLElab, listXMLAnomaly, true, false, period, myError) == false)
{
listXMLAnomaly->eraseElement(nAnomaly);
errorAnomaly = true;
Expand All @@ -3359,7 +3359,7 @@ bool parseXMLElaboration(Crit3DElabList *listXMLElab, Crit3DAnomalyList *listXML
{
if (myTag == "REFPERIOD")
{
if (parseXMLPeriodTag(child, listXMLElab, listXMLAnomaly, true, true, refPeriod, refFirstYear, myError) == false)
if (parseXMLPeriodTag(child, listXMLElab, listXMLAnomaly, true, true, refPeriod, myError) == false)
{
listXMLAnomaly->eraseElement(nAnomaly);
errorAnomaly = true;
Expand Down Expand Up @@ -4061,7 +4061,7 @@ bool parseXMLPeriodType(QDomNode ancestor, QString attributePeriod, Crit3DElabLi
}

bool parseXMLPeriodTag(QDomNode child, Crit3DElabList *listXMLElab, Crit3DAnomalyList *listXMLAnomaly, bool isAnomaly, bool isRefPeriod,
QString period, QString firstYear, QString *myError)
QString period, QString *myError)
{
QDate dateStart;
QDate dateEnd;
Expand All @@ -4070,8 +4070,8 @@ bool parseXMLPeriodTag(QDomNode child, Crit3DElabList *listXMLElab, Crit3DAnomal
{
QString periodEnd = child.toElement().attribute("fin");
QString periodStart = child.toElement().attribute("ini");
periodEnd = periodEnd+"/"+firstYear;
periodStart = periodStart+"/"+firstYear;
periodEnd = periodEnd+"/2000";
periodStart = periodStart+"/2000";
dateStart = QDate::fromString(periodStart, "dd/MM/yyyy");
dateEnd = QDate::fromString(periodEnd, "dd/MM/yyyy");
nYears = child.toElement().attribute("nyears");
Expand Down Expand Up @@ -4120,7 +4120,7 @@ bool parseXMLPeriodTag(QDomNode child, Crit3DElabList *listXMLElab, Crit3DAnomal
if (period == "Daily")
{
int dayOfYear = child.toElement().attribute("doy").toInt();
dateStart = QDate(firstYear.toInt(), 1, 1).addDays(dayOfYear - 1);
dateStart = QDate(2000, 1, 1).addDays(dayOfYear - 1);
dateEnd = dateStart;

if (isAnomaly)
Expand Down Expand Up @@ -4167,9 +4167,9 @@ bool parseXMLPeriodTag(QDomNode child, Crit3DElabList *listXMLElab, Crit3DAnomal
int dayEnd;
int month;

intervalDecade(decade, firstYear.toInt(), &dayStart, &dayEnd, &month);
dateStart.setDate(firstYear.toInt(), month, dayStart);
dateEnd.setDate(firstYear.toInt(), month, dayEnd);
intervalDecade(decade, 2000, &dayStart, &dayEnd, &month);
dateStart.setDate(2000, month, dayStart);
dateEnd.setDate(2000, month, dayEnd);

if (isAnomaly)
{
Expand Down Expand Up @@ -4210,9 +4210,9 @@ bool parseXMLPeriodTag(QDomNode child, Crit3DElabList *listXMLElab, Crit3DAnomal
if (period == "Monthly")
{
int month = child.toElement().attribute("month").toInt();
dateStart.setDate(firstYear.toInt(), month, 1);
dateStart.setDate(2000, month, 1);
dateEnd = dateStart;
dateEnd.setDate(firstYear.toInt(), month, dateEnd.daysInMonth());
dateEnd.setDate(2000, month, dateEnd.daysInMonth());

if (isAnomaly)
{
Expand Down Expand Up @@ -4257,15 +4257,15 @@ bool parseXMLPeriodTag(QDomNode child, Crit3DElabList *listXMLElab, Crit3DAnomal
int season = getSeasonFromString(seasonString);
if (season == 4)
{
dateStart.setDate(firstYear.toInt()-1, 12, 1);
QDate temp(firstYear.toInt(), 2, 1);
dateEnd.setDate(firstYear.toInt(), 2, temp.daysInMonth());
dateStart.setDate(1999, 12, 1);
QDate temp(2000, 2, 1);
dateEnd.setDate(2000, 2, temp.daysInMonth());
}
else
{
dateStart.setDate(firstYear.toInt(), season*3, 1);
QDate temp(firstYear.toInt(), season*3+2, 1);
dateEnd.setDate(firstYear.toInt(), season*3+2, temp.daysInMonth());
dateStart.setDate(2000, season*3, 1);
QDate temp(2000, season*3+2, 1);
dateEnd.setDate(2000, season*3+2, temp.daysInMonth());
}

if (isAnomaly)
Expand Down Expand Up @@ -4306,8 +4306,8 @@ bool parseXMLPeriodTag(QDomNode child, Crit3DElabList *listXMLElab, Crit3DAnomal
}
if (period == "Annual")
{
dateStart.setDate(firstYear.toInt(), 1, 1);
dateEnd.setDate(firstYear.toInt(), 12, 31);
dateStart.setDate(2000, 1, 1);
dateEnd.setDate(2000, 12, 31);

if (isAnomaly)
{
Expand Down
4 changes: 2 additions & 2 deletions agrolib/climate/climate.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@
bool parseXMLPeriodType(QDomNode ancestor, QString attributePeriod, Crit3DElabList *listXMLElab, Crit3DAnomalyList *listXMLAnomaly,
bool isAnomaly, bool isRefPeriod, QString* period, QString *myError);

bool parseXMLPeriodTag(QDomNode child, Crit3DElabList *listXMLElab, Crit3DAnomalyList *listXMLAnomaly, bool isAnomaly, bool isRefPeriod,
QString period, QString firstYear, QString *myError);
bool parseXMLPeriodTag(QDomNode child, Crit3DElabList *listXMLElab, Crit3DAnomalyList *listXMLAnomaly, bool isAnomaly, bool isRefPeriod,
QString period, QString *myError);

bool checkYears(QString firstYear, QString lastYear);

Expand Down
4 changes: 2 additions & 2 deletions agrolib/climate/crit3dAnomalyList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ bool Crit3DAnomalyList::addAnomaly(unsigned int index)


QString anomalyAdded = yearStart + "-" + yearEnd + "_" + variable + "_" + period;
anomalyAdded = anomalyAdded + "_" + periodStartDay + ":" + periodStartMonth + "-" + periodEndDay + ":" + periodEndMonth;
anomalyAdded = anomalyAdded + "_" + periodStartDay + "of" + periodStartMonth + "-" + periodEndDay + "of" + periodEndMonth;
if (nYear != "0")
{
anomalyAdded = anomalyAdded + "-+" + nYear + "y";
Expand Down Expand Up @@ -717,7 +717,7 @@ bool Crit3DAnomalyList::addAnomaly(unsigned int index)
else
{
anomalyAdded = anomalyAdded + refYearStart + "-" + refYearEnd + "_" + refPeriod;
anomalyAdded = anomalyAdded + "_" + refPeriodStartDay + ":" + refPeriodStartMonth + "-" + refPeriodEndDay + ":" + refPeriodEndMonth;
anomalyAdded = anomalyAdded + "_" + refPeriodStartDay + "of" + refPeriodStartMonth + "-" + refPeriodEndDay + "of" + refPeriodEndMonth;
if (refNYear != "0")
{
anomalyAdded = anomalyAdded + "-+" + refNYear + "y";
Expand Down
2 changes: 1 addition & 1 deletion agrolib/climate/crit3dElabList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ bool Crit3DElabList::addElab(unsigned int index)
QString elabAdded = yearStart + "-" + yearEnd + "_" + variable + "_" + period;
if (periodStartDay != "0" && periodStartMonth != "0" && periodEndDay != "0" && periodEndMonth != "0")
{
elabAdded = elabAdded + "_" + periodStartDay + ":" + periodStartMonth + "-" + periodEndDay + ":" + periodEndMonth;
elabAdded = elabAdded + "_" + periodStartDay + "of" + periodStartMonth + "-" + periodEndDay + "of" + periodEndMonth;
}
if (nYear != "0")
{
Expand Down
52 changes: 31 additions & 21 deletions agrolib/dbMeteoGrid/dbMeteoGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "basicMath.h"
#include "utilities.h"
#include "meteoPoint.h"
#include "meteo.h"
#include "commonConstants.h"

#include <iostream>
Expand Down Expand Up @@ -2233,6 +2234,7 @@ bool Crit3DMeteoGridDbHandler::loadGridMonthlyData(QString &myError, QString met
return true;
}


bool Crit3DMeteoGridDbHandler::loadGridAllMonthlyData(QString &myError, QDate firstDate, QDate lastDate)
{
myError = "";
Expand All @@ -2258,13 +2260,15 @@ bool Crit3DMeteoGridDbHandler::loadGridAllMonthlyData(QString &myError, QDate fi
}

QSqlQuery qry(_db);
QDate date;
QDate monthDate;
unsigned row, col;
int year, month, varCode;
int lastVarCode = NODATA;
meteoVariable variable = noMeteoVar;
QString pointCode, lastPointCode;
float value;
QString pointCode;
QString prevCode = "NODATA";
QString statement = QString("SELECT * FROM `%1` WHERE `PragaYear` BETWEEN %2 AND %3 ORDER BY `PragaYear`").arg(table).arg(firstDate.year()).arg(lastDate.year());

QString statement = QString("SELECT * FROM `%1` WHERE `PragaYear` BETWEEN %2 AND %3 ORDER BY `PointCode`").arg(table).arg(firstDate.year()).arg(lastDate.year());
if( !qry.exec(statement) )
{
myError = qry.lastError().text();
Expand All @@ -2274,53 +2278,59 @@ bool Crit3DMeteoGridDbHandler::loadGridAllMonthlyData(QString &myError, QDate fi
{
while (qry.next())
{
if (!getValue(qry.value("PragaYear"), &year))
if (! getValue(qry.value("PragaYear"), &year))
{
myError = "Missing PragaYear";
return false;
}

if (!getValue(qry.value("PragaMonth"), &month))
if (! getValue(qry.value("PragaMonth"), &month))
{
myError = "Missing PragaMonth";
return false;
}

date.setDate(year,month, 1);
if (date < firstDate || date > lastDate)
monthDate.setDate(year, month, 1);
if (monthDate < firstDate || monthDate > lastDate)
{
continue;
}

if (!getValue(qry.value("PointCode"), &pointCode))
if (! getValue(qry.value("PointCode"), &pointCode))
{
myError = "Missing PointCode";
return false;
}

if (!getValue(qry.value("VariableCode"), &varCode))
if (pointCode != lastPointCode) // new point
{
if (! _meteoGrid->findMeteoPointFromId(&row, &col, pointCode.toStdString()) )
{
myError = "Missing MeteoPoint id";
return false;
}
lastPointCode = pointCode;
}

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

if (!getValue(qry.value("Value"), &value))
if (varCode != lastVarCode) // new var
{
myError = "Missing Value";
variable = getMonthlyVarEnum(varCode);
lastVarCode = varCode;
}

meteoVariable variable = getMonthlyVarEnum(varCode);
if (prevCode != pointCode) // new point
if (! getValue(qry.value("Value"), &value))
{
if (!_meteoGrid->findMeteoPointFromId(&row, &col, pointCode.toStdString()) )
{
myError = "Missing MeteoPoint id";
return false;
}
myError = "Missing Value";
}
if (! _meteoGrid->meteoPointPointer(row,col)->setMeteoPointValueM(getCrit3DDate(date), variable, value))

if (! _meteoGrid->meteoPointPointer(row, col)->setMeteoPointValueM(getCrit3DDate(monthDate), variable, value))
return false;
prevCode = pointCode;
}
}

Expand Down
14 changes: 8 additions & 6 deletions agrolib/interpolation/interpolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
#include <vector>
#include <algorithm>

#include <fstream>
/*#include <fstream>
#include <chrono>
#include <iostream>
*/

#include "commonConstants.h"
#include "basicMath.h"
Expand Down Expand Up @@ -1516,15 +1517,16 @@ bool multipleDetrending(std::vector <Crit3DInterpolationDataPoint> &myPoints, Cr
std::vector<std::function<double(double, std::vector<double>&)>> myFunc;
setFittingParameters(myCombination, mySettings, myFunc, parametersMin, parametersMax, parametersDelta, parameters);

auto start2 = std::chrono::high_resolution_clock::now();
//auto start2 = std::chrono::high_resolution_clock::now();

// multiple non linear fitting
/*interpolation::bestFittingMarquardt_nDimension(&functionSum, myFunc, 4, 3, parametersMin, parametersMax, parameters, parametersDelta,
20, 0.05, 0.02, predictors, predictands, false, weights);*/
interpolation::bestFittingMarquardt_nDimension(&functionSum, myFunc, 10000, 5, parametersMin, parametersMax, parameters, parametersDelta,
100, 0.001, 0.01, predictors, predictands, false, weights);

auto end2 = std::chrono::high_resolution_clock::now();
interpolation::bestFittingMarquardt_nDimension(&functionSum, myFunc, 500, 5, parametersMin, parametersMax, parameters, parametersDelta,
100, 0.005, 0.01, predictors, predictands, false, weights);

/*auto end2 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> tempo2 = end2 - start2;
std::ofstream csvfile("C:/Github/tempoBestFit.csv", std::ios::app);
Expand All @@ -1533,7 +1535,7 @@ bool multipleDetrending(std::vector <Crit3DInterpolationDataPoint> &myPoints, Cr
}
csvfile << tempo2.count() << std::endl;
csvfile.close();

*/
mySettings->setFittingFunction(myFunc);
mySettings->setFittingParameters(parameters);

Expand Down
2 changes: 1 addition & 1 deletion agrolib/mathFunctions/commonConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
#define PI 3.141592653589793238462643383
#endif
#ifndef EPSILON
#define EPSILON 0.000001
#define EPSILON 0.0000001
#endif
#define EULER 2.718281828459
#define DEG_TO_RAD 0.0174532925
Expand Down
11 changes: 9 additions & 2 deletions agrolib/mathFunctions/furtherMathFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include <iostream>
#include <fstream>
#include <cmath>

double lapseRateRotatedSigmoid(double x, std::vector <double> par)
{
Expand Down Expand Up @@ -1124,14 +1125,14 @@ namespace interpolation
counter++;
} while( (counter < nrTrials) && (R2 < (1 - EPSILON)) && (fabs(R2Previous[0]-R2Previous[nrMinima-1]) > deltaR2) );

std::ofstream csvfile("C:/Github/counterR2.csv", std::ios::app);
/* std::ofstream csvfile("C:/Github/counterR2.csv", std::ios::app);

if (!csvfile.is_open()) {
std::cerr << "Errore apertura file\n";
}
csvfile << counter << "," << R2 << std::endl;
csvfile.close();

*/
for (i=0;i<nrPredictors;i++)
{
for (j=0; j<nrParameters[i]; j++)
Expand Down Expand Up @@ -1348,6 +1349,12 @@ namespace interpolation

parametersChange[nrPredictors - 1][nrParameters[nrPredictors-1]-1] = g[nrParametersTotal - 1] / a[nrParametersTotal - 1][nrParametersTotal - 1];

/*debugging
if (std::isnan(parametersChange[nrPredictors - 1][nrParameters[nrPredictors-1]-1]))
{
printf("nan trovato\n");
}*/

for (i = nrParametersTotal - 2; i >= 0; i--)
{
top = g[i];
Expand Down
Loading

0 comments on commit dfe6b1b

Please sign in to comment.