Skip to content

Commit

Permalink
Merge commit '6c16de9d0a624eed3fa4b90ce03802713a1c28b0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Sep 26, 2024
2 parents 8af5588 + 6c16de9 commit e10b90f
Show file tree
Hide file tree
Showing 18 changed files with 331 additions and 226 deletions.
164 changes: 131 additions & 33 deletions agrolib/climate/climate.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <QString>
#include <QDate>
#include <QtSql>
#include <QDebug>

#include <math.h> /* ceil */

#include "commonConstants.h"
Expand All @@ -11,7 +13,7 @@
#include "statistics.h"
#include "quality.h"
#include "dbClimate.h"
#include "qdebug.h"
#include "meteo.h"

using namespace std;

Expand Down Expand Up @@ -828,6 +830,93 @@ float loadDailyVarSeries(QString *myError, Crit3DMeteoPointsDbHandler *meteoPoin
}



float loadHourlyVarSeries_SaveOutput(QString *myError, Crit3DMeteoPointsDbHandler *meteoPointsDbHandler,
Crit3DMeteoGridDbHandler *meteoGridDbHandler, Crit3DMeteoPoint* meteoPoint, bool isMeteoGrid,
meteoVariable variable, QDate firstDate, QDate lastDate, std::vector<float> &outputValues)
{
std::vector<float> hourlyValues;
QDateTime firstDateTimeDB;

if (isMeteoGrid)
{
QDateTime firstTime = QDateTime(firstDate, QTime(1,0,0,0));
QDateTime lastTime = QDateTime(lastDate.addDays(1), QTime(0,0,0,0));

if (meteoGridDbHandler->gridStructure().isFixedFields())
{
hourlyValues = meteoGridDbHandler->loadGridHourlyVarFixedFields(myError, QString::fromStdString(meteoPoint->id),
variable, firstTime, lastTime, &firstDateTimeDB);
}
else
{
hourlyValues = meteoGridDbHandler->loadGridHourlyVar(myError, QString::fromStdString(meteoPoint->id), variable,
firstTime, lastTime, &firstDateTimeDB);
}
}
else
{
// meteoPoints
hourlyValues = meteoPointsDbHandler->loadHourlyVar(myError, variable, getCrit3DDate(firstDate), getCrit3DDate(lastDate),
&firstDateTimeDB, meteoPoint);
}

// no values
if (hourlyValues.empty())
return 0;

// number of days
QDateTime lastDateTimeDB = firstDateTimeDB.addSecs((hourlyValues.size()-1) * 3600);
int nrOfDays = firstDateTimeDB.daysTo(lastDateTimeDB);
if (lastDateTimeDB.time().hour() > 0)
nrOfDays++;

Crit3DDate firstCrit3DDate = getCrit3DDate(firstDateTimeDB.date());
meteoPoint->initializeObsDataH(meteoPoint->hourlyFraction, nrOfDays, firstCrit3DDate);
meteoPoint->initializeObsDataD(nrOfDays, firstCrit3DDate);

// fill initial NODATA (if firstHour > 1)
int firstHour = firstDateTimeDB.time().hour();
if (firstHour > 1)
{
for (int h = 1; h < firstHour; h++)
{
outputValues.push_back(NODATA);
}
}

int nrRequestedValues = (firstDate.daysTo(lastDate) + 1) * 24;
int nrValidValues = 0;
Crit3DQuality qualityCheck;

Crit3DTime currentDateTime = getCrit3DTime(firstDateTimeDB);
for (unsigned int i = 0; i < hourlyValues.size(); i++)
{
quality::qualityType myQuality = qualityCheck.syntacticQualitySingleValue(variable, hourlyValues[i]);
if (myQuality == quality::accepted)
{
if (meteoPoint->setMeteoPointValueH(currentDateTime.date, currentDateTime.getHour(), currentDateTime.getMinutes(), variable, hourlyValues[i]))
{
outputValues.push_back(hourlyValues[i]);
nrValidValues = nrValidValues + 1;
}
}
else
{
if (meteoPoint->setMeteoPointValueH(currentDateTime.date, currentDateTime.getHour(), currentDateTime.getMinutes(), variable, NODATA))
{
outputValues.push_back(NODATA);
}
}

currentDateTime = currentDateTime.addSeconds(3600);
}

float percValue = float(nrValidValues) / float(nrRequestedValues);
return percValue;
}


float loadDailyVarSeries_SaveOutput(QString *myError, Crit3DMeteoPointsDbHandler *meteoPointsDbHandler,
Crit3DMeteoGridDbHandler *meteoGridDbHandler, Crit3DMeteoPoint* meteoPoint, bool isMeteoGrid,
meteoVariable variable, QDate first, QDate last, std::vector<float> &outputValues)
Expand Down Expand Up @@ -857,7 +946,6 @@ float loadDailyVarSeries_SaveOutput(QString *myError, Crit3DMeteoPointsDbHandler
dailyValues = meteoPointsDbHandler->loadDailyVar(variable, getCrit3DDate(first), getCrit3DDate(last), *meteoPoint, firstDateDB);
}


if ( dailyValues.empty() )
{
return 0;
Expand Down Expand Up @@ -893,6 +981,7 @@ float loadDailyVarSeries_SaveOutput(QString *myError, Crit3DMeteoPointsDbHandler
}
}


float loadFromMp_SaveOutput(Crit3DMeteoPoint* meteoPoint,
meteoVariable variable, QDate first, QDate last, std::vector<float> &outputValues)
{
Expand Down Expand Up @@ -955,7 +1044,7 @@ float loadHourlyVarSeries(QString *myError, Crit3DMeteoPointsDbHandler* meteoPoi
if (meteoPoint->nrObsDataDaysH == 0)
{
int nrOfDays = ceil(float(hourlyValues.size()) / float(24 * meteoPoint->hourlyFraction));
meteoPoint->initializeObsDataH(meteoPoint->hourlyFraction, nrOfDays,getCrit3DDate(firstDateDB.date()));
meteoPoint->initializeObsDataH(meteoPoint->hourlyFraction, nrOfDays, getCrit3DDate(firstDateDB.date()));
meteoPoint->initializeObsDataD(nrOfDays, getCrit3DDate(firstDateDB.date()));
}

Expand Down Expand Up @@ -1944,9 +2033,9 @@ bool aggregatedHourlyToDaily(meteoVariable myVar, Crit3DMeteoPoint* meteoPoint,

}


std::vector<float> aggregatedHourlyToDailyList(meteoVariable myVar, Crit3DMeteoPoint* meteoPoint, Crit3DDate dateIni, Crit3DDate dateFin, Crit3DMeteoSettings *meteoSettings)
{

Crit3DDate date;
std::vector <float> values;
std::vector<float> dailyData;
Expand All @@ -1958,7 +2047,9 @@ std::vector<float> aggregatedHourlyToDailyList(meteoVariable myVar, Crit3DMeteoP
int nValidValues;

if (meteoPoint->nrObsDataDaysD == 0)
{
meteoPoint->initializeObsDataD(dateIni.daysTo(dateFin)+1, dateIni);
}

switch(myVar)
{
Expand Down Expand Up @@ -2028,7 +2119,8 @@ std::vector<float> aggregatedHourlyToDailyList(meteoVariable myVar, Crit3DMeteoP
break;
}

if (hourlyVar == noMeteoVar || elab == noMeteoComp) return dailyData;
if (hourlyVar == noMeteoVar || elab == noMeteoComp)
return dailyData;

for (date = dateIni; date <= dateFin; date = date.addDays(1))
{
Expand Down Expand Up @@ -2062,13 +2154,12 @@ std::vector<float> aggregatedHourlyToDailyList(meteoVariable myVar, Crit3DMeteoP
{
// todo warning
}

}

return dailyData;

}


bool preElaboration(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbHandler, Crit3DMeteoGridDbHandler* meteoGridDbHandler, Crit3DMeteoPoint* meteoPoint, bool isMeteoGrid, meteoVariable variable, meteoComputation elab1,
QDate startDate, QDate endDate, std::vector<float> &outputValues, float* percValue, Crit3DMeteoSettings* meteoSettings)
{
Expand All @@ -2078,7 +2169,6 @@ bool preElaboration(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbH

switch(variable)
{

case dailyLeafWetness:
{
if ( loadHourlyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, leafWetness, QDateTime(startDate,QTime(1,0,0),Qt::UTC), QDateTime(endDate.addDays(1),QTime(0,0,0),Qt::UTC)) > 0)
Expand Down Expand Up @@ -2120,7 +2210,6 @@ bool preElaboration(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbH
}
case dailyThomAvg: case dailyThomMax: case dailyThomHoursAbove:
{

if (loadHourlyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, airTemperature, QDateTime(startDate,QTime(1,0,0),Qt::UTC), QDateTime(endDate.addDays(1),QTime(0,0,0),Qt::UTC)) > 0)
{
if (loadHourlyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, airRelHumidity, QDateTime(startDate,QTime(1,0,0),Qt::UTC), QDateTime(endDate.addDays(1),QTime(0,0,0),Qt::UTC)) > 0)
Expand All @@ -2132,7 +2221,6 @@ bool preElaboration(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbH
}
case dailyBIC:
{

if (loadDailyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, dailyReferenceEvapotranspirationHS, startDate, endDate) > 0)
{
preElaboration = true;
Expand Down Expand Up @@ -2286,41 +2374,50 @@ bool preElaboration(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbH
break;
}

case winkler: case correctedDegreeDaysSum: case fregoni:
{
if (loadDailyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, dailyAirTemperatureMin, startDate, endDate) > 0 )
case winkler: case correctedDegreeDaysSum: case fregoni:
{
if (loadDailyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, dailyAirTemperatureMax, startDate, endDate) > 0 )
if (loadDailyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, dailyAirTemperatureMin, startDate, endDate) > 0 )
{
preElaboration = true;
if (loadDailyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, dailyAirTemperatureMax, startDate, endDate) > 0 )
{
preElaboration = true;
}
}
break;
}
break;
}

case phenology:
{
if (loadDailyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, dailyAirTemperatureMin, startDate, endDate) > 0 )
case phenology:
{
if (loadDailyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, dailyAirTemperatureMax, startDate, endDate) > 0 )
if (loadDailyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, dailyAirTemperatureMin, startDate, endDate) > 0 )
{
if (loadDailyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, dailyPrecipitation, startDate, endDate) > 0 )
if (loadDailyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, dailyAirTemperatureMax, startDate, endDate) > 0 )
{
preElaboration = true;
if (loadDailyVarSeries(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, dailyPrecipitation, startDate, endDate) > 0 )
{
preElaboration = true;
}
}
}
}
break;
}

default:
{
*percValue = loadDailyVarSeries_SaveOutput(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint, isMeteoGrid, variable, startDate, endDate, outputValues);
break;
}

preElaboration = ((*percValue) > 0);
break;
}
default:
{
// default variables / elaboration
if (getVarFrequency(variable) == hourly)
{
*percValue = loadHourlyVarSeries_SaveOutput(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint,
isMeteoGrid, variable, startDate, endDate, outputValues);
}
else
{
*percValue = loadDailyVarSeries_SaveOutput(myError, meteoPointsDbHandler, meteoGridDbHandler, meteoPoint,
isMeteoGrid, variable, startDate, endDate, outputValues);
}

preElaboration = ((*percValue) > 0);
break;
}
}
break;
}
Expand All @@ -2329,6 +2426,7 @@ bool preElaboration(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbH
return preElaboration;
}


bool preElaborationWithoutLoad(Crit3DMeteoPoint* meteoPoint, meteoVariable variable, QDate startDate, QDate endDate, std::vector<float> &outputValues, float* percValue, Crit3DMeteoSettings* meteoSettings)
{

Expand Down
4 changes: 4 additions & 0 deletions agrolib/climate/climate.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
Crit3DMeteoGridDbHandler *meteoGridDbHandler, Crit3DMeteoPoint &meteoPoint, bool isMeteoGrid,
meteoVariable variable, QDate first, QDate last, std::vector<float> &outputValues);

float loadHourlyVarSeries_SaveOutput(QString *myError, Crit3DMeteoPointsDbHandler *meteoPointsDbHandler,
Crit3DMeteoGridDbHandler *meteoGridDbHandler, Crit3DMeteoPoint* meteoPoint, bool isMeteoGrid,
meteoVariable variable, QDate firstDate, QDate lastDate, std::vector<float> &outputValues);

float loadHourlyVarSeries(QString *myError, Crit3DMeteoPointsDbHandler *meteoPointsDbHandler,
Crit3DMeteoGridDbHandler *meteoGridDbHandler, Crit3DMeteoPoint* meteoPoint,
bool isMeteoGrid, meteoVariable variable, QDateTime first, QDateTime last);
Expand Down
Loading

0 comments on commit e10b90f

Please sign in to comment.