Skip to content

Commit

Permalink
Merge commit '1854bd7f978043c7c2c7b8e4dbd7456bdd57447f'
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Nov 30, 2023
2 parents cadd4b8 + 1854bd7 commit b5ca081
Show file tree
Hide file tree
Showing 16 changed files with 301 additions and 124 deletions.
2 changes: 1 addition & 1 deletion agrolib/dbMeteoGrid/dbMeteoGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@ std::vector<float> Crit3DMeteoGridDbHandler::loadGridHourlyVar(QString *myError,
QString tableH = _tableHourly.prefix + meteoPoint + _tableHourly.postFix;
QDateTime dateTime, previousDateTime;
dateTime.setTimeSpec(Qt::UTC);
previousDateTime.setTimeSpec((Qt::UTC));
previousDateTime.setTimeSpec(Qt::UTC);

std::vector<float> hourlyVarList;

Expand Down
50 changes: 27 additions & 23 deletions agrolib/dbMeteoPoints/dbMeteoPointsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,12 @@ bool Crit3DMeteoPointsDbHandler::deleteAllPointsFromDataset(QList<QString> datas
}


bool Crit3DMeteoPointsDbHandler::loadDailyData(Crit3DDate firstDate, Crit3DDate lastDate, Crit3DMeteoPoint *meteoPoint)
bool Crit3DMeteoPointsDbHandler::loadDailyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint *meteoPoint)
{
// check dates
if (firstDate > lastDate)
{
this->errorStr = "wrong dates: first > last";
errorStr = "wrong dates: first > last";
return false;
}

Expand Down Expand Up @@ -545,57 +545,61 @@ bool Crit3DMeteoPointsDbHandler::loadDailyData(Crit3DDate firstDate, Crit3DDate
}


bool Crit3DMeteoPointsDbHandler::loadHourlyData(Crit3DDate dateStart, Crit3DDate dateEnd, Crit3DMeteoPoint *meteoPoint)
bool Crit3DMeteoPointsDbHandler::loadHourlyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint *meteoPoint)
{
meteoVariable variable;
int idVar;
float value;
// check dates
if (firstDate > lastDate)
{
errorStr = "wrong dates: first > last";
return false;
}

int numberOfDays = difference(dateStart, dateEnd)+1;
// initialize obs data
int numberOfDays = difference(firstDate, lastDate) + 1;
int myHourlyFraction = 1;
QString startDate = QString::fromStdString(dateStart.toStdString());
QString endDate = QString::fromStdString(dateEnd.toStdString());

QSqlQuery qry(_db);

meteoPoint->initializeObsDataH(myHourlyFraction, numberOfDays, dateStart);
meteoPoint->initializeObsDataH(myHourlyFraction, numberOfDays, firstDate);

QString startDateStr = QString::fromStdString(firstDate.toStdString());
QString endDateStr = QString::fromStdString(lastDate.toStdString());
QString tableName = QString::fromStdString(meteoPoint->id) + "_H";

QString statement = QString( "SELECT * FROM `%1` WHERE date_time >= DATETIME('%2 01:00:00') AND date_time <= DATETIME('%3 00:00:00', '+1 day')")
.arg(tableName, startDate, endDate);
if( !qry.exec(statement) )
.arg(tableName, startDateStr, endDateStr);
QSqlQuery qry(_db);
if(! qry.exec(statement) )
{
qDebug() << qry.lastError();
errorStr = qry.lastError().text();
return false;
}
else
{
meteoVariable variable;
while (qry.next())
{
QDateTime d = qry.value(0).toDateTime();
Crit3DDate myDate = Crit3DDate(d.date().day(), d.date().month(), d.date().year());
//myDate = QDate::fromString(dateStr.mid(0,10), "yyyy-MM-dd");
//myTime = QTime::fromString(dateStr.mid(11,8), "HH:mm:ss");
//QDateTime d(QDateTime(myDate, myTime, Qt::UTC));

idVar = qry.value(1).toInt();
try {
int idVar = qry.value(1).toInt();
try
{
variable = _mapIdMeteoVar.at(idVar);
}
catch (const std::out_of_range& ) {
catch (const std::out_of_range& )
{
variable = noMeteoVar;
}

if (variable != noMeteoVar)
{
value = qry.value(2).toFloat();
float value = qry.value(2).toFloat();
meteoPoint->setMeteoPointValueH(myDate, d.time().hour(), d.time().minute(), variable, value);

// copy scalar intensity to vector intensity (instantaneous values are equivalent, following WMO)
// should be removed when hourly averages are available
if (variable == windScalarIntensity)
{
meteoPoint->setMeteoPointValueH(myDate, d.time().hour(), d.time().minute(), windVectorIntensity, value);
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions agrolib/dbMeteoPoints/dbMeteoPointsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@
const gis::Crit3DGisSettings& gisSettings, QString& errorString);
bool getPropertiesGivenId(QString id, Crit3DMeteoPoint* meteoPoint,
const gis::Crit3DGisSettings& gisSettings, QString& errorString);
bool loadDailyData(Crit3DDate firstDate, Crit3DDate lastDate, Crit3DMeteoPoint *meteoPoint);
bool loadDailyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint *meteoPoint);
std::vector<float> loadDailyVar(QString *myError, meteoVariable variable,
Crit3DDate dateStart, Crit3DDate dateEnd,
QDate* firstDateDB, Crit3DMeteoPoint *meteoPoint);
bool loadHourlyData(Crit3DDate dateStart, Crit3DDate dateEnd, Crit3DMeteoPoint *meteoPoint);

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

std::vector<float> loadHourlyVar(QString *myError, meteoVariable variable,
Crit3DDate dateStart, Crit3DDate dateEnd,
QDateTime* firstDateDB, Crit3DMeteoPoint *meteoPoint);
Expand Down
2 changes: 2 additions & 0 deletions agrolib/gis/gis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,15 @@ namespace gis
}


// clean the grid (all NO DATA)
void Crit3DRasterGrid::emptyGrid()
{
for (int row = 0; row < header->nrRows; row++)
for (int col = 0; col < header->nrCols; col++)
value[row][col] = header->flag;
}


Crit3DRasterGrid::~Crit3DRasterGrid()
{
clear();
Expand Down
38 changes: 17 additions & 21 deletions agrolib/interpolation/interpolation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "meteoPoint.h"
#include "gis.h"
#include "spatialControl.h"
#include "interpolationPoint.h"
#include "interpolation.h"
#include <functional>

Expand Down Expand Up @@ -1048,25 +1049,18 @@ void localSelection(vector <Crit3DInterpolationDataPoint> &inputPoints, vector <
mySettings.setLocalRadius(r1);
}

bool checkPrecipitationZero(std::vector <Crit3DInterpolationDataPoint> &myPoints, float precThreshold, int* nrPrecNotNull, bool* flatPrecipitation)

bool checkPrecipitationZero(const std::vector<Crit3DInterpolationDataPoint> &myPoints, float precThreshold, int &nrNotNull)
{
*flatPrecipitation = true;
*nrPrecNotNull = 0;
float myValue = NODATA;
nrNotNull = 0;

for (unsigned int i = 0; i < myPoints.size(); i++)
if (myPoints[i].isActive)
if (int(myPoints[i].value) != int(NODATA))
if (myPoints[i].value >= float(precThreshold))
{
if (*nrPrecNotNull > 0 && myPoints[i].value != myValue)
*flatPrecipitation = false;
if (! isEqual(myPoints[i].value, NODATA))
if (myPoints[i].value >= precThreshold)
nrNotNull++;

myValue = myPoints[i].value;
(*nrPrecNotNull)++;
}

return (*nrPrecNotNull == 0);
return (nrNotNull == 0);
}


Expand Down Expand Up @@ -1643,8 +1637,7 @@ bool preInterpolation(std::vector <Crit3DInterpolationDataPoint> &myPoints, Crit
if (myVar == precipitation || myVar == dailyPrecipitation)
{
int nrPrecNotNull;
bool isFlatPrecipitation;
if (checkPrecipitationZero(myPoints, meteoSettings->getRainfallThreshold(), &nrPrecNotNull, &isFlatPrecipitation))
if (checkPrecipitationZero(myPoints, meteoSettings->getRainfallThreshold(), nrPrecNotNull))
{
mySettings->setPrecipitationAllZero(true);
return true;
Expand Down Expand Up @@ -1727,15 +1720,16 @@ float interpolate(vector <Crit3DInterpolationDataPoint> &myPoints, Crit3DInterpo
myResult = MAXVALUE(myResult, 0);

return myResult;

}

bool getActiveProxyValues(Crit3DInterpolationSettings* mySettings, std::vector <double> & allProxyValues, std::vector <double> & activeProxyValues)

bool getActiveProxyValues(Crit3DInterpolationSettings *mySettings, const std::vector<double> &allProxyValues, std::vector<double> &activeProxyValues)
{
Crit3DProxyCombination myCombination = mySettings->getCurrentCombination();
std::vector <double> myValues;

if (allProxyValues.size() != mySettings->getProxyNr()) return false;
if (allProxyValues.size() != mySettings->getProxyNr())
return false;

activeProxyValues.clear();

bool isComplete = true;
Expand All @@ -1744,12 +1738,14 @@ bool getActiveProxyValues(Crit3DInterpolationSettings* mySettings, std::vector <
if (myCombination.getValue(i) && mySettings->getProxy(i)->getIsSignificant())
{
activeProxyValues.push_back(allProxyValues[i]);
if (allProxyValues[i] == NODATA) isComplete = false;
if (allProxyValues[i] == NODATA)
isComplete = false;
}

return isComplete;
}


void getProxyValuesXY(float x, float y, Crit3DInterpolationSettings* mySettings, std::vector<double> &myValues)
{
float myValue;
Expand Down
4 changes: 3 additions & 1 deletion agrolib/interpolation/interpolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@
bool krigLinearPrep(double *mySlope, double *myNugget, int nrPointData);

void clearInterpolationPoints();
bool checkPrecipitationZero(const std::vector<Crit3DInterpolationDataPoint> &myPoints, float precThreshold, int &nrNotNull);

bool neighbourhoodVariability(meteoVariable myVar, std::vector<Crit3DInterpolationDataPoint> &myInterpolationPoints, Crit3DInterpolationSettings *mySettings, float x, float y, float z, int nMax,
float* devSt, float* avgDeltaZ, float* minDistance);

float interpolate(std::vector<Crit3DInterpolationDataPoint> &myPoints, Crit3DInterpolationSettings *mySettings, Crit3DMeteoSettings *meteoSettings, meteoVariable myVar, float myX, float myY, float myZ, std::vector<double> myProxyValues, bool excludeSupplemental);
void getProxyValuesXY(float x, float y, Crit3DInterpolationSettings* mySettings, std::vector<double> &myValues);
bool getActiveProxyValues(Crit3DInterpolationSettings* mySettings, std::vector<double> &allProxyValues, std::vector<double> &activeProxyValues);

bool getActiveProxyValues(Crit3DInterpolationSettings *mySettings, const std::vector<double> &allProxyValues, std::vector<double> &activeProxyValues);

void detrending(std::vector <Crit3DInterpolationDataPoint> &myPoints,
Crit3DProxyCombination myCombination, Crit3DInterpolationSettings *mySettings, Crit3DClimateParameters *myClimate,
Expand Down
9 changes: 9 additions & 0 deletions agrolib/pragaProject/pragaMeteoMaps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ void PragaHourlyMeteoMaps::clear()
}


void PragaHourlyMeteoMaps::initialize()
{
mapHourlyWindVectorInt->emptyGrid();
mapHourlyWindVectorDir->emptyGrid();
mapHourlyWindVectorX->emptyGrid();
mapHourlyWindVectorY->emptyGrid();
}


gis::Crit3DRasterGrid* PragaHourlyMeteoMaps::getMapFromVar(meteoVariable myVar)
{
if (myVar == windVectorIntensity)
Expand Down
2 changes: 2 additions & 0 deletions agrolib/pragaProject/pragaMeteoMaps.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
~PragaHourlyMeteoMaps();

void clear();
void initialize();

gis::Crit3DRasterGrid* getMapFromVar(meteoVariable myVar);
bool computeWindVector();
};
Expand Down
Loading

0 comments on commit b5ca081

Please sign in to comment.