Skip to content

Commit

Permalink
Merge commit '7e3e45511eb81eb66efe122830d5678cb98b6c33'
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Jan 18, 2024
2 parents 05f1593 + 7e3e455 commit d835a7d
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 68 deletions.
101 changes: 60 additions & 41 deletions agrolib/criteria1DWidget/criteria1DWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ void Criteria1DWidget::on_actionOpenProject()

openCropDB(myProject.dbCropName);
openSoilDB(myProject.dbSoilName);
openMeteoDB(myProject.dbMeteoName);
openMeteoDB(myProject.dbMeteoName, false);

this->cropListComboBox.blockSignals(false);
this->soilListComboBox.blockSignals(false);
Expand Down Expand Up @@ -861,37 +861,40 @@ void Criteria1DWidget::openCropDB(QString newDbCropName)

void Criteria1DWidget::on_actionOpenMeteoDB()
{

QString dbMeteoName = QFileDialog::getOpenFileName(this, tr("Open meteo database"), "", tr("SQLite files or XML (*.db *xml)"));
if (dbMeteoName == "")
return;
else
if (! dbMeteoName.isEmpty())
{
if (dbMeteoName.right(3) == "xml")
myProject.isXmlMeteoGrid = true;
else
myProject.isXmlMeteoGrid = false;
openMeteoDB(dbMeteoName);

openMeteoDB(dbMeteoName, true);
}
}


void Criteria1DWidget::openMeteoDB(QString dbMeteoName)
void Criteria1DWidget::openMeteoDB(QString dbMeteoName, bool isMenu)
{
QString errorStr;
QList<QString> idMeteoList;
if (myProject.isXmlMeteoGrid)
{
if (! myProject.observedMeteoGrid->parseXMLGrid(dbMeteoName, &errorStr))
{
QMessageBox::critical(nullptr, "Error XML meteo grid", errorStr);
return;
}
if (! myProject.observedMeteoGrid->openDatabase(&errorStr, "observed"))
if (isMenu)
{
QMessageBox::critical(nullptr, "Error DB Grid", errorStr);
return;
if (! myProject.observedMeteoGrid->parseXMLGrid(dbMeteoName, &errorStr))
{
QMessageBox::critical(nullptr, "Error XML meteo grid", errorStr);
return;
}
if (! myProject.observedMeteoGrid->openDatabase(&errorStr, "observed"))
{
QMessageBox::critical(nullptr, "Error DB Grid", errorStr);
return;
}
}

// check daily list
if (! myProject.observedMeteoGrid->idDailyList(&errorStr, &idMeteoList))
{
QMessageBox::critical(nullptr, "Error daily table list", errorStr);
Expand All @@ -900,10 +903,13 @@ void Criteria1DWidget::openMeteoDB(QString dbMeteoName)
}
else
{
if (! openDbMeteo(dbMeteoName, myProject.dbMeteo, errorStr))
if (isMenu)
{
QMessageBox::critical(nullptr, "Error DB meteo", errorStr);
return;
if (! openDbMeteo(dbMeteoName, myProject.dbMeteo, errorStr))
{
QMessageBox::critical(nullptr, "Error DB meteo", errorStr);
return;
}
}

// read id_meteo list
Expand Down Expand Up @@ -1226,6 +1232,7 @@ void Criteria1DWidget::on_actionChooseMeteo(QString idMeteo)
{
return;
}

// clear prev year list
this->firstYearListComboBox.blockSignals(true);
this->lastYearListComboBox.blockSignals(true);
Expand Down Expand Up @@ -1299,16 +1306,18 @@ void Criteria1DWidget::on_actionChooseMeteo(QString idMeteo)
// last year can be incomplete
for (int i = 0; i < yearList.size()-1; i++)
{
if ( !checkYearMeteoGrid(myProject.observedMeteoGrid->db(), meteoTableName, myProject.observedMeteoGrid->tableDaily().fieldTime, varCodeTmin, varCodeTmax, varCodePrec, yearList[i], &errorStr))
if (! checkYearMeteoGrid(myProject.observedMeteoGrid->db(), meteoTableName, myProject.observedMeteoGrid->tableDaily().fieldTime,
varCodeTmin, varCodeTmax, varCodePrec, yearList[i], errorStr))
{
yearList.removeAt(pos);
i = i - 1;
i--;
}
else
{
pos = pos + 1;
pos++;
}
}

// store last Date
getLastDateGrid(myProject.dbMeteo, meteoTableName, myProject.observedMeteoGrid->tableDaily().fieldTime, yearList[yearList.size()-1], &myProject.lastSimulationDate, &errorStr);
}
Expand Down Expand Up @@ -1363,19 +1372,19 @@ void Criteria1DWidget::on_actionChooseMeteo(QString idMeteo)
{
if (yearList[i].toInt() == yearList[i-1].toInt()+1)
{
this->firstYearListComboBox.addItem(yearList[i]);
firstYearListComboBox.addItem(yearList[i]);
}
}

this->lastYearListComboBox.blockSignals(false);

}


void Criteria1DWidget::on_actionChooseFirstYear(QString year)
{
this->lastYearListComboBox.blockSignals(true);
this->lastYearListComboBox.clear();

// add first year
this->lastYearListComboBox.addItem(year);
int index = yearList.indexOf(year);
Expand All @@ -1392,6 +1401,7 @@ void Criteria1DWidget::on_actionChooseFirstYear(QString year)
break;
}
}

updateMeteoPointValues();
this->lastYearListComboBox.blockSignals(false);
}
Expand All @@ -1407,17 +1417,19 @@ void Criteria1DWidget::on_actionChooseLastYear(QString year)
this->lastYearListComboBox.setCurrentText(QString::number(maxYear));
return;
}

updateMeteoPointValues();
}


void Criteria1DWidget::updateMeteoPointValues()
bool Criteria1DWidget::updateMeteoPointValues()
{
QString errorStr;

// init meteoPoint with all years asked
// initialize meteoPoint with all the required years
int firstYear = this->firstYearListComboBox.currentText().toInt() - 1;
int lastYear = this->lastYearListComboBox.currentText().toInt();

QDate firstDate(firstYear, 1, 1);
QDate lastDate(lastYear, 1, 1);
QDate myDate = firstDate;
Expand All @@ -1427,26 +1439,26 @@ void Criteria1DWidget::updateMeteoPointValues()
numberDays = numberDays + unsigned(myDate.daysInYear());
myDate.setDate(myDate.year()+1, 1, 1);
}

myProject.myCase.meteoPoint.initializeObsDataD(numberDays, getCrit3DDate(firstDate));

if (myProject.isXmlMeteoGrid)
{
unsigned row;
unsigned col;
if (!myProject.observedMeteoGrid->meteoGrid()->findMeteoPointFromId(&row, &col, myProject.myCase.meteoPoint.id) )
unsigned row, col;
if (! myProject.observedMeteoGrid->meteoGrid()->findMeteoPointFromId(&row, &col, myProject.myCase.meteoPoint.id))
{
errorStr = "Missing observed meteo cell";
QMessageBox::critical(nullptr, "Error!", errorStr);
return;
return false;
}

if (!myProject.observedMeteoGrid->gridStructure().isFixedFields())
if (! myProject.observedMeteoGrid->gridStructure().isFixedFields())
{
if (!myProject.observedMeteoGrid->loadGridDailyData(errorStr, QString::fromStdString(myProject.myCase.meteoPoint.id), firstDate, QDate(lastDate.year(),12,31)))
if (! myProject.observedMeteoGrid->loadGridDailyData(errorStr, QString::fromStdString(myProject.myCase.meteoPoint.id), firstDate, QDate(lastDate.year(),12,31)))
{
errorStr = "Missing observed data";
QMessageBox::critical(nullptr, "Error!", errorStr);
return;
return false;
}
}
else
Expand All @@ -1455,10 +1467,12 @@ void Criteria1DWidget::updateMeteoPointValues()
{
errorStr = "Missing observed data";
QMessageBox::critical(nullptr, "Error!", errorStr);
return;
return false;
}
}

float tmin, tmax, tavg, prec, waterDepth;

for (int i = 0; i < (firstDate.daysTo(QDate(lastDate.year(), 12, 31)) + 1); i++)
{
Crit3DDate myDate = getCrit3DDate(firstDate.addDays(i));
Expand All @@ -1481,6 +1495,7 @@ void Criteria1DWidget::updateMeteoPointValues()
waterDepth = myProject.observedMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyWaterTableDepth);
myProject.myCase.meteoPoint.setMeteoPointValueD(myDate, dailyWaterTableDepth, waterDepth);
}

if (isOnlyOneYear)
{
// copy values to prev years
Expand All @@ -1502,10 +1517,10 @@ void Criteria1DWidget::updateMeteoPointValues()
{
if (isOnlyOneYear)
{
if (!fillDailyTempPrecCriteria1D(&(myProject.dbMeteo), meteoTableName, &(myProject.myCase.meteoPoint), lastYear, &errorStr))
if (! fillDailyTempPrecCriteria1D(&(myProject.dbMeteo), meteoTableName, &(myProject.myCase.meteoPoint), lastYear, &errorStr))
{
QMessageBox::critical(nullptr, "Error! ", errorStr + " year: " + QString::number(lastYear));
return;
return false;
}
// copy values to previous year
Crit3DDate myDate = getCrit3DDate(lastDate);
Expand All @@ -1531,16 +1546,18 @@ void Criteria1DWidget::updateMeteoPointValues()
&(myProject.myCase.meteoPoint), year, &errorStr))
{
QMessageBox::critical(nullptr, "Error! ", errorStr + " year: " + QString::number(year));
return;
return false;
}
}
}
}

if (!myProject.myCase.crop.idCrop.empty())
if (! myProject.myCase.crop.idCrop.empty())
{
on_actionUpdate();
}

return true;
}


Expand Down Expand Up @@ -2159,11 +2176,13 @@ bool Criteria1DWidget::setMeteoSqlite(QString& errorStr)

void Criteria1DWidget::on_actionViewWeather()
{
QString errorStr;
if (!setMeteoSqlite(errorStr))
if (! myProject.isXmlMeteoGrid)
{
QMessageBox::critical(nullptr, "ERROR!", errorStr);
return;
if (! setMeteoSqlite(myProject.projectError))
{
QMessageBox::critical(nullptr, "ERROR!", myProject.projectError);
return;
}
}

Crit3DMeteoWidget* meteoWidgetPoint = new Crit3DMeteoWidget(myProject.isXmlMeteoGrid, myProject.path, &meteoSettings);
Expand Down
4 changes: 2 additions & 2 deletions agrolib/criteria1DWidget/criteria1DWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
void on_actionViewWeather();
void on_actionViewSoil();
bool saveCrop();
void updateMeteoPointValues();
bool updateMeteoPointValues();
void updateCropParam(QString idCrop);
bool updateCrop();
void updateTabLAI();
Expand Down Expand Up @@ -155,7 +155,7 @@
void checkCropUpdate();
void openComputationUnitsDB(QString dbComputationUnitsName);
void openCropDB(QString dbCropName);
void openMeteoDB(QString dbMeteoName);
void openMeteoDB(QString dbMeteoName, bool isMenu);
void openSoilDB(QString dbSoilName);
bool setMeteoSqlite(QString &error);
};
Expand Down
Loading

0 comments on commit d835a7d

Please sign in to comment.