Skip to content

Commit

Permalink
Merge commit '11a5ccdd50bdc1392a5220354a9c36c187f702c4'
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed May 23, 2024
2 parents 9b8bc5e + 11a5ccd commit 00fd54d
Show file tree
Hide file tree
Showing 15 changed files with 385 additions and 217 deletions.
95 changes: 95 additions & 0 deletions agrolib/dbMeteoGrid/dbMeteoGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,101 @@ bool Crit3DMeteoGridDbHandler::loadGridDailyData(QString &myError, const QString
return true;
}

bool Crit3DMeteoGridDbHandler::loadGridDailyMeteoPrec(QString &myError, const QString &meteoPointId, const QDate &firstDate, const QDate &lastDate)
{
myError = "";
QString tableD = _tableDaily.prefix + meteoPointId + _tableDaily.postFix;

unsigned row, col;
if ( !_meteoGrid->findMeteoPointFromId(&row, &col, meteoPointId.toStdString()) )
{
myError = "Missing meteoPoint id: " + meteoPointId;
return false;
}

int numberOfDays = firstDate.daysTo(lastDate) + 1;
_meteoGrid->meteoPointPointer(row, col)->initializeObsDataD(numberOfDays, getCrit3DDate(firstDate));

if (_firstDailyDate.isValid() && _lastDailyDate.isValid())
{
if (_firstDailyDate.year() != 1800 && _lastDailyDate.year() != 1800)
{
if (firstDate > _lastDailyDate || lastDate < _firstDailyDate)
{
myError = "Missing data in this time interval.";
return false;
}
}
}

QSqlQuery qry(_db);
QString statement;
bool isSingleDate = false;
QDate date;

QList<QString> varList;
varList.push_back(QString::number(getDailyVarCode(dailyAirTemperatureMin)));
varList.push_back(QString::number(getDailyVarCode(dailyAirTemperatureMax)));
varList.push_back(QString::number(getDailyVarCode(dailyAirTemperatureAvg)));
varList.push_back(QString::number(getDailyVarCode(dailyPrecipitation)));


if (firstDate == lastDate)
{
statement = QString("SELECT * FROM `%1` WHERE %2 = '%3' AND `VariableCode` IN ('%4')")
.arg(tableD, _tableDaily.fieldTime, firstDate.toString("yyyy-MM-dd"), varList.join("','"));
isSingleDate = true;
date = firstDate;
}
else
{
statement = QString("SELECT * FROM `%1` WHERE %2 >= '%3' AND %2 <= '%4' AND `VariableCode` IN ('%5') ORDER BY %2")
.arg(tableD, _tableDaily.fieldTime, firstDate.toString("yyyy-MM-dd"), lastDate.toString("yyyy-MM-dd"), varList.join("','"));
}
qry.prepare(statement);

if(! qry.exec())
{
myError = qry.lastError().text();
return false;
}

int varCode;
float value;
while (qry.next())
{
getValue(qry.value("Value"), &value);

if (value != NODATA)
{
if (! isSingleDate)
{
if (! getValue(qry.value(_tableDaily.fieldTime), &date))
{
myError = "Missing " + _tableDaily.fieldTime;
return false;
}
}

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

meteoVariable variable = getDailyVarEnum(varCode);

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

return true;
}


bool Crit3DMeteoGridDbHandler::loadGridDailyDataEnsemble(QString &myError, QString meteoPoint, int memberNr, QDate first, QDate last)
{
Expand Down
1 change: 1 addition & 0 deletions agrolib/dbMeteoGrid/dbMeteoGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
bool loadGridDailyData(QString &myError, const QString &meteoPointId, const QDate &firstDate, const QDate &lastDate);
bool loadGridDailyDataFixedFields(QString &myError, QString meteoPoint, QDate first, QDate last);
bool loadGridDailyDataEnsemble(QString &myError, QString meteoPoint, int memberNr, QDate first, QDate last);
bool loadGridDailyMeteoPrec(QString &myError, const QString &meteoPointId, const QDate &firstDate, const QDate &lastDate);
bool loadGridHourlyData(QString &myError, QString meteoPoint, QDateTime firstDate, QDateTime lastDate);
bool loadGridHourlyDataFixedFields(QString &myError, QString meteoPoint, QDateTime first, QDateTime last);
bool loadGridHourlyDataEnsemble(QString &myError, QString meteoPoint, int memberNr, QDateTime first, QDateTime last);
Expand Down
2 changes: 1 addition & 1 deletion agrolib/dbMeteoPoints/dbMeteoPointsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,7 @@ QList<QString> Crit3DMeteoPointsDbHandler::getIdListGivenDataset(QList<QString>
datasetList += "'" + dataset + "'";
}
}
QString statement = "SELECT id_point from point_properties WHERE dataset IN (" + datasetList + ")";
QString statement = "SELECT id_point from point_properties WHERE UPPER(dataset) IN (" + datasetList.toUpper() + ")";

if( !qry.exec(statement) )
{
Expand Down
2 changes: 1 addition & 1 deletion agrolib/dbMeteoPoints/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ QMap<QString, QString> Download::getArmiketIdList(QList<QString> datasetList)
qDebug() << "jsonDataset: value is not string";
else
foreach(QString item, _datasetsList)
if (jsonDataset == item)
if (jsonDataset.toString().toUpper() == item.toUpper())
{
QString idValue;
QString nameValue;
Expand Down
8 changes: 4 additions & 4 deletions agrolib/mathFunctions/furtherMathFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,7 @@ namespace interpolation
a[counterDim][counterDim] += lambda[i][k]*a[counterDim][counterDim];
for (j = counterDim+1; j < nrParametersTotal; j++)
{
a[j][i] = a[i][j];
a[j][counterDim] = a[counterDim][j];
}
counterDim++;
}
Expand Down Expand Up @@ -1664,7 +1664,7 @@ namespace interpolation
a[counterDim][counterDim] += lambda[i][k];
for (j = counterDim+1; j < nrParametersTotal; j++)
{
a[j][i] = a[i][j];
a[j][counterDim] = a[counterDim][j];
}
counterDim++;
}
Expand Down Expand Up @@ -1803,7 +1803,7 @@ namespace interpolation
a[counterDim][counterDim] += lambda[i][k]*a[counterDim][counterDim];
for (j = counterDim+1; j < nrParametersTotal; j++)
{
a[j][i] = a[i][j];
a[j][counterDim] = a[counterDim][j];
}
counterDim++;
}
Expand Down Expand Up @@ -2032,7 +2032,7 @@ namespace interpolation
a[k][k] += lambda[k]*a[k][k];
for (j = k+1; j < nrParameters; j++)
{
a[j][i] = a[i][j];
a[j][k] = a[k][j];
}
}
// linear system resolution by matrix inversion
Expand Down
8 changes: 8 additions & 0 deletions agrolib/meteo/meteoPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,14 @@ Crit3DDate Crit3DMeteoPoint::getLastDailyData()
return obsDataD[nrObsDataDaysD-1].date;
}

Crit3DDate Crit3DMeteoPoint::getFirstDailyData()
{
if (obsDataD.size() == 0)
return NO_DATE;

return obsDataD[0].date;
}


float Crit3DMeteoPoint::getMeteoPointValueD(const Crit3DDate &myDate, meteoVariable myVar, Crit3DMeteoSettings* meteoSettings)
{
Expand Down
1 change: 1 addition & 0 deletions agrolib/meteo/meteoPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
void initializeObsDataM(unsigned int numberOfMonths, unsigned int month, int year);

bool existDailyData(const Crit3DDate& myDate);
Crit3DDate getFirstDailyData();
Crit3DDate getLastDailyData();

float getMeteoPointValueH(const Crit3DDate& myDate, int myHour, int myMinutes, meteoVariable myVar);
Expand Down
142 changes: 136 additions & 6 deletions agrolib/project/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4602,28 +4602,158 @@ bool Project::computeSingleWell(QString idWell, int indexWell)
{
bool isMeteoGridLoaded;
QDate firstMeteoDate = wellPoints[indexWell].getFirstDate().addDays(-730); // necessari 24 mesi di dati meteo precedenti il primo dato di falda
double wellUtmX = wellPoints[indexWell].getUtmX();
double wellUtmY = wellPoints[indexWell].getUtmY();
Crit3DMeteoPoint linkedMeteoPoint;
if (this->meteoGridDbHandler != nullptr)
{
loadMeteoGridDailyData(firstMeteoDate, this->meteoGridDbHandler->getLastDailyDate(), true);
isMeteoGridLoaded = true;
}
else if (meteoPoints != nullptr)
{
loadMeteoPointsData(firstMeteoDate, this->meteoPointsDbHandler->getLastDate(daily).date(), false, true, true);
isMeteoGridLoaded = false;
}
else
{
logError(ERROR_STR_MISSING_POINT_GRID);
return false;
}

if (!assignNearestMeteoPoint(isMeteoGridLoaded, wellUtmX, wellUtmY, firstMeteoDate, &linkedMeteoPoint))
{
logError("Missing near weather data");
return false;
}
if (linkedMeteoPoint.nrObsDataDaysD == 0)
{
logError("Missing near weather data");
return false;
}
int maxNrDays = 730; // attualmente fisso
WaterTable waterTable(meteoPoints, nrMeteoPoints, meteoGridDbHandler->meteoGrid(), isMeteoGridLoaded, *meteoSettings, gisSettings);
WaterTable waterTable(&linkedMeteoPoint, *meteoSettings, gisSettings);
waterTable.computeWaterTable(wellPoints[indexWell], maxNrDays);
waterTable.viewWaterTableSeries(); // prepare series to show
waterTableList.push_back(waterTable);
return true;
}

void Project::showSingleWell(WaterTable waterTable, QString idWell)
{
DialogSummary* dialogResult = new DialogSummary(waterTable); // show results
waterTable.viewWaterTableSeries(); // prepare series to show
dialogResult->show();
WaterTableWidget* chartResult = new WaterTableWidget(idWell, waterTable.getMyDates(), waterTable.getMyHindcastSeries(), waterTable.getMyInterpolateSeries(), waterTable.getDepths());
return true;
chartResult->show();
return;
}

bool Project::assignNearestMeteoPoint(bool isMeteoGridLoaded, double wellUtmX, double wellUtmY, QDate firstMeteoDate, Crit3DMeteoPoint* linkedMeteoPoint)
{
float minimumDistance = NODATA;
bool assignNearestMeteoPoint = false;
if (isMeteoGridLoaded)
{
std::string assignNearestId;
unsigned int assignNearestRow;
unsigned int assignNearestCol;
int zoneNumber;
QDate lastDate = this->meteoGridDbHandler->getLastDailyDate();
for (unsigned row = 0; row < unsigned(meteoGridDbHandler->meteoGrid()->gridStructure().header().nrRows); row++)
{
for (unsigned col = 0; col < unsigned(meteoGridDbHandler->meteoGrid()->gridStructure().header().nrCols); col++)
{
if (meteoGridDbHandler->meteoGrid()->meteoPointPointer(row,col)->active)
{
double utmX = meteoGridDbHandler->meteoGrid()->meteoPointPointer(row,col)->point.utm.x;
double utmY = meteoGridDbHandler->meteoGrid()->meteoPointPointer(row,col)->point.utm.y;
if (utmX == NODATA || utmY == NODATA)
{
double lat = meteoGridDbHandler->meteoGrid()->meteoPointPointer(row,col)->latitude;
double lon = meteoGridDbHandler->meteoGrid()->meteoPointPointer(row,col)->longitude;
gis::latLonToUtm(lat, lon, &utmX, &utmY, &zoneNumber);
}
float myDistance = gis::computeDistance(wellUtmX, wellUtmY, utmX, utmY);
if (myDistance < MAXWELLDISTANCE )
{
if (myDistance < minimumDistance || minimumDistance == NODATA)
{
minimumDistance = myDistance;
assignNearestId = meteoGridDbHandler->meteoGrid()->meteoPointPointer(row,col)->id;
assignNearestRow = row;
assignNearestCol = col;
assignNearestMeteoPoint = true;
}
}
}
}
}
if (assignNearestMeteoPoint)
{
meteoGridDbHandler->loadGridDailyMeteoPrec(errorString, QString::fromStdString(assignNearestId), firstMeteoDate, lastDate);
if (!assignWTMeteoData(meteoGridDbHandler->meteoGrid()->meteoPointPointer(assignNearestRow,assignNearestCol), firstMeteoDate))
{
return false;
}
else
{
linkedMeteoPoint->id = meteoGridDbHandler->meteoGrid()->meteoPointPointer(assignNearestRow,assignNearestCol)->id;
linkedMeteoPoint->name = meteoGridDbHandler->meteoGrid()->meteoPointPointer(assignNearestRow,assignNearestCol)->name;
linkedMeteoPoint->latitude = meteoGridDbHandler->meteoGrid()->meteoPointPointer(assignNearestRow,assignNearestCol)->latitude;
linkedMeteoPoint->nrObsDataDaysD = meteoGridDbHandler->meteoGrid()->meteoPointPointer(assignNearestRow,assignNearestCol)->nrObsDataDaysD;
linkedMeteoPoint->obsDataD = meteoGridDbHandler->meteoGrid()->meteoPointPointer(assignNearestRow,assignNearestCol)->obsDataD;
}
}
}
else
{
int assignNearestIndex;
QDate lastDate = meteoPointsDbHandler->getLastDate(daily).date();
for (int i = 0; i < nrMeteoPoints; i++)
{

double utmX = meteoPoints[i].point.utm.x;
double utmY = meteoPoints[i].point.utm.y;
float myDistance = gis::computeDistance(wellUtmX, wellUtmY, utmX, utmY);
if (myDistance < MAXWELLDISTANCE )
{
if (myDistance < minimumDistance || minimumDistance == NODATA)
{
meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstMeteoDate), getCrit3DDate(lastDate), &(meteoPoints[i]));
if (assignWTMeteoData(&meteoPoints[i], firstMeteoDate))
{
minimumDistance = myDistance;
assignNearestMeteoPoint = true;
assignNearestIndex = i;
}
}
}
}
if (assignNearestMeteoPoint)
{
linkedMeteoPoint->id = meteoPoints[assignNearestIndex].id;
linkedMeteoPoint->name = meteoPoints[assignNearestIndex].name;
linkedMeteoPoint->latitude = meteoPoints[assignNearestIndex].latitude;
linkedMeteoPoint->nrObsDataDaysD = meteoPoints[assignNearestIndex].nrObsDataDaysD;
linkedMeteoPoint->obsDataD = meteoPoints[assignNearestIndex].obsDataD;
}
}
return assignNearestMeteoPoint;
}

bool Project::assignWTMeteoData(Crit3DMeteoPoint* linkedMeteoPoint, QDate firstMeteoDate)
{
QDate lastMeteoDate;
lastMeteoDate.setDate(linkedMeteoPoint->getLastDailyData().year, linkedMeteoPoint->getLastDailyData().month, linkedMeteoPoint->getLastDailyData().day); // ultimo dato disponibile
float precPerc = linkedMeteoPoint->getPercValueVariable(Crit3DDate(firstMeteoDate.day(), firstMeteoDate.month(), firstMeteoDate.year()) , Crit3DDate(lastMeteoDate.day(), lastMeteoDate.month(), lastMeteoDate.year()), dailyPrecipitation);
float tMinPerc = linkedMeteoPoint->getPercValueVariable(Crit3DDate(firstMeteoDate.day(), firstMeteoDate.month(), firstMeteoDate.year()) , Crit3DDate(lastMeteoDate.day(), lastMeteoDate.month(), lastMeteoDate.year()), dailyAirTemperatureMin);
float tMaxPerc = linkedMeteoPoint->getPercValueVariable(Crit3DDate(firstMeteoDate.day(), firstMeteoDate.month(), firstMeteoDate.year()) , Crit3DDate(lastMeteoDate.day(), lastMeteoDate.month(), lastMeteoDate.year()), dailyAirTemperatureMax);

float minPercentage = meteoSettings->getMinimumPercentage();
if (precPerc > minPercentage/100 && tMinPerc > minPercentage/100 && tMaxPerc > minPercentage/100)
{
return true;
}
else
{
errorString = "Not enough meteo data to analyze watertable period. Try to decrease the required percentage";
return false;
}
}
3 changes: 3 additions & 0 deletions agrolib/project/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@
bool waterTableImportLocation(QString csvFileName);
bool waterTableImportDepths(QString csvDepths);
bool computeSingleWell(QString idWell, int indexWell);
void showSingleWell(WaterTable waterTable, QString idWell);
bool assignNearestMeteoPoint(bool isMeteoGridLoaded, double wellUtmX, double wellUtmY, QDate firstMeteoDate, Crit3DMeteoPoint* linkedMeteoPoint);
bool assignWTMeteoData(Crit3DMeteoPoint* linkedMeteoPoint, QDate firstMeteoDate);

private slots:
void deleteMeteoWidgetPoint(int id);
Expand Down
Loading

0 comments on commit 00fd54d

Please sign in to comment.