Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ARPA-SIMC/CRITERIA1D
Browse files Browse the repository at this point in the history
  • Loading branch information
giadasan committed Dec 4, 2024
2 parents 0ca61b1 + 8767b89 commit f65248c
Show file tree
Hide file tree
Showing 47 changed files with 3,530 additions and 819 deletions.
1 change: 1 addition & 0 deletions .moncic-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ images:
- name: rocky:9
- name: fedora:36
- name: fedora:38
- name: fedora:40
168 changes: 131 additions & 37 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,92 @@ 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);

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

// 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);
}
}

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,17 +945,13 @@ float loadDailyVarSeries_SaveOutput(QString *myError, Crit3DMeteoPointsDbHandler
dailyValues = meteoPointsDbHandler->loadDailyVar(variable, getCrit3DDate(first), getCrit3DDate(last), *meteoPoint, firstDateDB);
}


if ( dailyValues.empty() )
{
return 0;
}
else
{
if (meteoPoint->nrObsDataDaysD == 0)
{
meteoPoint->initializeObsDataD(int(dailyValues.size()), getCrit3DDate(firstDateDB));
}
meteoPoint->initializeObsDataD(int(dailyValues.size()), getCrit3DDate(firstDateDB));

Crit3DDate currentDate = getCrit3DDate(firstDateDB);
for (unsigned int i = 0; i < dailyValues.size(); i++)
Expand All @@ -893,6 +977,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 +1040,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 +2029,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 +2043,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 +2115,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 +2150,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 +2165,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 +2206,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 +2217,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 +2370,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 +2422,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
32 changes: 16 additions & 16 deletions agrolib/climate/crit3dClimateList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void Crit3DClimateList::parserElaboration(QString &errorStr)
QString periodTypeStr = words[pos];
_listPeriodType.push_back(getPeriodTypeFromString(periodTypeStr));

pos = pos + 1; // pos = 3
pos = pos + 1;

if (words.size() == pos)
{
Expand All @@ -295,17 +295,17 @@ void Crit3DClimateList::parserElaboration(QString &errorStr)
continue;
}

if ( (_listPeriodType[i] == genericPeriod) && ( (words[pos].at(0)).isDigit() ))
if ( (_listPeriodType[i] == genericPeriod) && (words[pos].at(0)).isDigit() )
{
_listPeriodStr.push_back(words[pos]);
parserGenericPeriodString(i);
pos = pos + 1; // pos = 4
pos = pos + 1;
}
else
{
_listPeriodStr.push_back(periodTypeStr);
_listGenericPeriodDateStart.push_back( QDate(0, 0, 0) );
_listGenericPeriodDateEnd.push_back( QDate(0, 0, 0) );
_listGenericPeriodDateStart.push_back(QDate(0, 0, 0));
_listGenericPeriodDateEnd.push_back(QDate(0, 0, 0));
_listNYears.push_back(0);
}

Expand Down Expand Up @@ -447,28 +447,28 @@ void Crit3DClimateList::parserElaboration(QString &errorStr)

bool Crit3DClimateList::parserGenericPeriodString(int index)
{

QString periodString = _listPeriodStr.at(index);

if ( periodString.isEmpty())
if (periodString.isEmpty())
{
return false;
}

QString day = periodString.mid(0,2);
QString month = periodString.mid(3,2);
QString day = periodString.mid(0, 2);
QString month = periodString.mid(4, 2);
int year = 2000;
_listGenericPeriodDateStart.push_back( QDate(year, month.toInt(), day.toInt()) );
_listGenericPeriodDateStart.push_back(QDate(year, month.toInt(), day.toInt()));

day = periodString.mid(6,2);
month = periodString.mid(9,2);
day = periodString.mid(7, 2);
month = periodString.mid(11, 2);

_listGenericPeriodDateEnd.push_back( QDate(year, month.toInt(), day.toInt()) );
_listGenericPeriodDateEnd.push_back(QDate(year, month.toInt(), day.toInt()));

if ( periodString.size() > 11 )
if (periodString.size() > 13)
{
_listNYears.push_back((periodString.mid(13,2)).toInt());
QString nrYearsStr = periodString.mid(15, 2);
_listNYears.push_back(nrYearsStr.toInt());
}
return true;

return true;
}
Loading

0 comments on commit f65248c

Please sign in to comment.