Skip to content

Commit

Permalink
fix monthly data
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Jan 4, 2024
1 parent b6a6f5c commit 5c07924
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 43 deletions.
19 changes: 10 additions & 9 deletions agrolib/climate/climate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4946,21 +4946,20 @@ bool appendXMLAnomaly(Crit3DAnomalyList *listXMLAnomaly, QString xmlFileName, QS
output << xmlDoc.toString();
outputFile.close();
return true;

}


bool monthlyAggregateDataGrid(Crit3DMeteoGridDbHandler* meteoGridDbHandler, QDate firstDate, QDate lastDate,
std::vector<meteoVariable> dailyMeteoVar,
Crit3DMeteoSettings* meteoSettings, Crit3DQuality* qualityCheck, Crit3DClimateParameters* climateParam)
Crit3DMeteoSettings* meteoSettings, Crit3DQuality* qualityCheck,
Crit3DClimateParameters* climateParam, QString &myError)
{
int nrMonths = (lastDate.year()-firstDate.year())*12+lastDate.month()-(firstDate.month()-1);
QString myError;
bool isMeteoGrid = true;
Crit3DMeteoPoint* meteoPointTemp = new Crit3DMeteoPoint;
float percValue;
std::vector<float> outputValues;
QList<meteoVariable> meteoVariableList;
bool dataSaved = false;

for (unsigned row = 0; row < unsigned(meteoGridDbHandler->meteoGrid()->gridStructure().header().nrRows); row++)
{
Expand Down Expand Up @@ -4992,20 +4991,22 @@ bool monthlyAggregateDataGrid(Crit3DMeteoGridDbHandler* meteoGridDbHandler, QDat
}
}
}
// copy meteoPoint Temp valus
// copy meteoPoint Temp values
meteoGridDbHandler->meteoGrid()->meteoPointPointer(row,col)->obsDataM = meteoPointTemp->obsDataM ;
if (!meteoVariableList.isEmpty())
if (! meteoVariableList.isEmpty())
{
if (meteoGridDbHandler->saveCellGridMonthlyData(&myError, QString::fromStdString(meteoGridDbHandler->meteoGrid()->meteoPointPointer(row,col)->id), row, col, firstDate, lastDate, meteoVariableList))
if (! meteoGridDbHandler->saveCellGridMonthlyData(&myError, QString::fromStdString(meteoGridDbHandler->meteoGrid()->meteoPointPointer(row,col)->id), row, col, firstDate, lastDate, meteoVariableList))
{
dataSaved = true;
delete meteoPointTemp;
return false;
}
}
}
}
}

delete meteoPointTemp;
return dataSaved;
return true;
}


Expand Down
6 changes: 3 additions & 3 deletions agrolib/climate/climate.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@

bool appendXMLAnomaly(Crit3DAnomalyList *listXMLAnomaly, QString xmlFileName, QString *myError);

bool monthlyAggregateDataGrid(Crit3DMeteoGridDbHandler* meteoGridDbHandler, QDate firstDate, QDate lastDate,
std::vector<meteoVariable> dailyMeteoVar, Crit3DMeteoSettings* meteoSettings,
Crit3DQuality *qualityCheck, Crit3DClimateParameters *climateParam);
bool monthlyAggregateDataGrid(Crit3DMeteoGridDbHandler* meteoGridDbHandler, QDate firstDate, QDate lastDate,
std::vector<meteoVariable> dailyMeteoVar, Crit3DMeteoSettings* meteoSettings,
Crit3DQuality *qualityCheck, Crit3DClimateParameters *climateParam, QString &myError);

int computeAnnualSeriesOnPointFromDaily(QString *myError, Crit3DMeteoPointsDbHandler* meteoPointsDbHandler, Crit3DMeteoGridDbHandler* meteoGridDbHandler,
Crit3DMeteoPoint* meteoPointTemp, Crit3DClimate* clima, bool isMeteoGrid, bool isAnomaly,
Expand Down
6 changes: 3 additions & 3 deletions agrolib/dbMeteoGrid/dbMeteoGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@ bool Crit3DMeteoGridDbHandler::updateMeteoGridDate(QString &myError)
{
QString table = "MonthlyData";
QString statement = QString("CREATE TABLE IF NOT EXISTS `%1`"
"(PragaYear smallint(4) UNSIGNED, PragaMonth tinyint(2) UNSIGNED, PointCode CHAR(5), "
"(PragaYear smallint(4) UNSIGNED, PragaMonth tinyint(2) UNSIGNED, PointCode VARCHAR(6), "
"VariableCode tinyint(3) UNSIGNED, Value float(6,1), PRIMARY KEY(PragaYear,PragaMonth,PointCode,VariableCode))").arg(table);

if(! qry.exec(statement) )
Expand Down Expand Up @@ -3049,7 +3049,7 @@ bool Crit3DMeteoGridDbHandler::saveCellGridMonthlyData(QString *myError, QString
QString table = "MonthlyData";

QString statement = QString("CREATE TABLE IF NOT EXISTS `%1`"
"(PragaYear smallint(4) UNSIGNED, PragaMonth tinyint(2) UNSIGNED, PointCode CHAR(5), "
"(PragaYear smallint(4) UNSIGNED, PragaMonth tinyint(2) UNSIGNED, PointCode VARCHAR(6), "
"VariableCode tinyint(3) UNSIGNED, Value float(6,1), PRIMARY KEY(PragaYear,PragaMonth,PointCode,VariableCode))").arg(table);

if( !qry.exec(statement) )
Expand Down Expand Up @@ -3082,7 +3082,7 @@ bool Crit3DMeteoGridDbHandler::saveCellGridMonthlyData(QString *myError, QString

statement = statement.left(statement.length() - 1);

if( !qry.exec(statement) )
if( ! qry.exec(statement) )
{
*myError = qry.lastError().text();
return false;
Expand Down
17 changes: 11 additions & 6 deletions agrolib/pragaProject/pragaProject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3239,9 +3239,8 @@ bool PragaProject::loadXMLImportData(QString fileName)
}


bool PragaProject::monthlyVariablesGrid(QDate first, QDate last, QList <meteoVariable> variables)
bool PragaProject::monthlyAggregateVariablesGrid(const QDate &firstDate, const QDate &lastDate, QList<meteoVariable> &variablesList)
{

// check meteo grid
if (! meteoGridLoaded)
{
Expand All @@ -3250,24 +3249,30 @@ bool PragaProject::monthlyVariablesGrid(QDate first, QDate last, QList <meteoVar
}

// check dates
if (first.isNull() || last.isNull() || first > last)
if (firstDate.isNull() || lastDate.isNull() || firstDate > lastDate)
{
logError("Wrong period");
return false;
}

std::vector <meteoVariable> dailyMeteoVar;
for (int i = 0; i < variables.size(); i++)
for (int i = 0; i < variablesList.size(); i++)
{
meteoVariable dailyVar = updateMeteoVariable(variables[i], daily);
meteoVariable dailyVar = updateMeteoVariable(variablesList[i], daily);
if (dailyVar != noMeteoVar)
{
dailyMeteoVar.push_back(dailyVar);
}
}
return monthlyAggregateDataGrid(meteoGridDbHandler, first, last, dailyMeteoVar, meteoSettings, quality, &climateParameters);

if (! monthlyAggregateDataGrid(meteoGridDbHandler, firstDate, lastDate, dailyMeteoVar, meteoSettings,
quality, &climateParameters, errorString))
return false;

meteoGridDbHandler->updateMeteoGridDate(errorString);
}


bool PragaProject::computeDroughtIndexAll(droughtIndex index, int firstYear, int lastYear, QDate date, int timescale, meteoVariable myVar)
{
// check meteo grid
Expand Down
2 changes: 1 addition & 1 deletion agrolib/pragaProject/pragaProject.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
int executePragaCommand(QList<QString> argumentList, bool* isCommandFound);
bool parserXMLImportData(QString xmlName, bool isGrid);
bool loadXMLImportData(QString fileName);
bool monthlyVariablesGrid(QDate first, QDate last, QList <meteoVariable> variables);
bool monthlyAggregateVariablesGrid(const QDate &firstDate, const QDate &lastDate, QList <meteoVariable> &variablesList);
bool computeDroughtIndexAll(droughtIndex index, int firstYear, int lastYear, QDate date, int timescale, meteoVariable myVar);
bool computeDroughtIndexPoint(droughtIndex index, int timescale, int refYearStart, int refYearEnd);
void showPointStatisticsWidgetPoint(std::string idMeteoPoint);
Expand Down
5 changes: 4 additions & 1 deletion agrolib/pragaProject/pragaShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,11 @@ int cmdMonthlyIntegrationVariablesGrid(PragaProject* myProject, QList<QString> a
return PRAGA_INVALID_COMMAND;
}

if (! myProject->monthlyVariablesGrid(first, last, variables))
if (! myProject->monthlyAggregateVariablesGrid(first, last, variables))
{
myProject->logError();
return PRAGA_ERROR;
}

return PRAGA_OK;
}
Expand Down
42 changes: 22 additions & 20 deletions src/mainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5192,34 +5192,36 @@ void MainWindow::on_actionCompute_monthly_data_from_daily_triggered()
QDate myDateTo = myProject.meteoGridDbHandler->lastDate();

DialogComputeData computeMonthlyDialog(myDateFrom, myDateTo, isGrid, allPoints);
if (computeMonthlyDialog.result() == QDialog::Accepted)
if (computeMonthlyDialog.result() != QDialog::Accepted)
return;

QList <meteoVariable> varToCompute = computeMonthlyDialog.getVariables();
QDate firstDate = computeMonthlyDialog.getDateFrom();
QDate lastDate = computeMonthlyDialog.getDateTo();
if (myProject.meteoGridDbHandler->getFirsMonthlytDate().isValid() && myProject.meteoGridDbHandler->getLastMonthlyDate().isValid())
{
QList <meteoVariable> varToCompute = computeMonthlyDialog.getVariables();
QDate firstDate = computeMonthlyDialog.getDateFrom();
QDate lastDate = computeMonthlyDialog.getDateTo();
if (myProject.meteoGridDbHandler->getFirsMonthlytDate().isValid() && myProject.meteoGridDbHandler->getLastMonthlyDate().isValid())
if (firstDate >= myProject.meteoGridDbHandler->getFirsMonthlytDate() || lastDate <= myProject.meteoGridDbHandler->getLastMonthlyDate())
{
if (firstDate >= myProject.meteoGridDbHandler->getFirsMonthlytDate() || lastDate <= myProject.meteoGridDbHandler->getLastMonthlyDate())
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Are you sure?" ,
" monthly data will be overwritten ",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::No)
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Are you sure?" ,
" monthly data will be overwritten ",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::No)
{
return;
}
return;
}
}
if (! myProject.monthlyVariablesGrid(firstDate, lastDate, varToCompute))
{
myProject.logError("Failed to compute monthly data");
}
}
else

myProject.logInfoGUI("Compute monthly data...");
bool isOk = myProject.monthlyAggregateVariablesGrid(firstDate, lastDate, varToCompute);
myProject.closeLogInfo();

if (! isOk)
{
return;
myProject.logError("Failed to compute monthly data.\n" + myProject.errorString);
}

QDate date = myProject.getCurrentDate();
myProject.loadMeteoGridData(date, date, true);

Expand Down

0 comments on commit 5c07924

Please sign in to comment.