Skip to content

Commit

Permalink
Merge commit '0d5dd053abb530c5ce5c4a833987aed20ab9a07d'
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Apr 2, 2024
2 parents 7db8ed9 + 0d5dd05 commit d3c8450
Show file tree
Hide file tree
Showing 30 changed files with 1,511 additions and 568 deletions.
3 changes: 2 additions & 1 deletion agrolib/criteria1DWidget/criteria1DWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "criteria1DMeteo.h"
#include "utilities.h"
#include "basicMath.h"
#include "root.h"

#include <QFileInfo>
#include <QFileDialog>
Expand Down Expand Up @@ -1026,7 +1027,7 @@ void Criteria1DWidget::on_actionExecuteCase()
return;
}

if (!myProject.computeUnit(myProject.myCase.unit))
if (! myProject.computeUnit(myProject.myCase.unit))
{
QMessageBox::critical(nullptr, "Error!", myProject.projectError);
}
Expand Down
129 changes: 85 additions & 44 deletions agrolib/criteriaModel/criteria1DProject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,52 +100,70 @@ bool Crit1DProject::readSettings()
// PROJECT
projectSettings->beginGroup("project");

path += projectSettings->value("path","").toString();
path += projectSettings->value("path", "").toString();
projectName = projectSettings->value("name", "CRITERIA1D").toString();

dbCropName = projectSettings->value("db_crop","").toString();
dbCropName = projectSettings->value("db_crop", "").toString();
if (dbCropName.left(1) == ".")
dbCropName = path + dbCropName;
{
dbCropName = QDir::cleanPath(path + dbCropName);
}

dbSoilName = projectSettings->value("db_soil","").toString();
dbSoilName = projectSettings->value("db_soil", "").toString();
if (dbSoilName.left(1) == ".")
dbSoilName = path + dbSoilName;
{
dbSoilName = QDir::cleanPath(path + dbSoilName);
}

dbMeteoName = projectSettings->value("db_meteo","").toString();
dbMeteoName = projectSettings->value("db_meteo", "").toString();
if (dbMeteoName.left(1) == ".")
dbMeteoName = path + dbMeteoName;
if (dbMeteoName.right(3) == "xml")
{
dbMeteoName = QDir::cleanPath(path + dbMeteoName);
}
if (dbMeteoName.right(3).toUpper() == "XML")
{
isXmlMeteoGrid = true;
}

dbForecastName = projectSettings->value("db_forecast","").toString();
dbForecastName = projectSettings->value("db_forecast", "").toString();
if (dbForecastName.left(1) == ".")
dbForecastName = path + dbForecastName;
{
dbForecastName = QDir::cleanPath(path + dbForecastName);
}

// computational units db
dbComputationUnitsName = projectSettings->value("db_comp_units","").toString();
dbComputationUnitsName = projectSettings->value("db_comp_units", "").toString();
if (dbComputationUnitsName == "")
{
// check old name
dbComputationUnitsName = projectSettings->value("db_units","").toString();
dbComputationUnitsName = projectSettings->value("db_units", "").toString();
}
if (dbComputationUnitsName == "")
{
projectError = "Missing information on computational units";
return false;
}
if (dbComputationUnitsName.left(1) == ".")
dbComputationUnitsName = path + dbComputationUnitsName;
{
dbComputationUnitsName = QDir::cleanPath(path + dbComputationUnitsName);
}

// output db
dbOutputName = projectSettings->value("db_output","").toString();
dbOutputName = projectSettings->value("db_output", "").toString();
if (dbOutputName.left(1) == ".")
dbOutputName = path + dbOutputName;
{
dbOutputName = QDir::cleanPath(path + dbOutputName);
}

// date
if (firstSimulationDate == QDate(1800,1,1))
{
firstSimulationDate = projectSettings->value("firstDate",0).toDate();
if (! firstSimulationDate.isValid())
{
firstSimulationDate = projectSettings->value("first_date",0).toDate();
}
if (! firstSimulationDate.isValid())
{
firstSimulationDate = QDate(1800,1,1);
}
Expand All @@ -155,6 +173,10 @@ bool Crit1DProject::readSettings()
{
lastSimulationDate = projectSettings->value("lastDate",0).toDate();
if (! lastSimulationDate.isValid())
{
lastSimulationDate = projectSettings->value("last_date",0).toDate();
}
if (! lastSimulationDate.isValid())
{
lastSimulationDate = QDate(1800,1,1);
}
Expand Down Expand Up @@ -358,7 +380,7 @@ int Crit1DProject::initializeProject(QString settingsFileName)
return ERROR_SETTINGS_WRONGFILENAME;
}

if (!readSettings())
if (! readSettings())
return ERROR_SETTINGS_MISSINGDATA;

logger.setLog(path, projectName, addDateTimeLogFile);
Expand Down Expand Up @@ -468,26 +490,38 @@ bool Crit1DProject::setSoil(QString soilCode, QString &errorStr)

bool Crit1DProject::setMeteoXmlGrid(QString idMeteo, QString idForecast, unsigned int memberNr)
{
unsigned row, col;
// check date
QDate NODATE = QDate(1800, 1, 1);
if (! firstSimulationDate.isValid() || firstSimulationDate == NODATE )
{
projectError = "Missing first simulation date.";
return false;
}
if (! lastSimulationDate.isValid() || lastSimulationDate == NODATE )
{
projectError = "Missing last simulation date.";
return false;
}
unsigned nrDays = unsigned(firstSimulationDate.daysTo(lastSimulationDate)) + 1;

if (!observedMeteoGrid->meteoGrid()->findMeteoPointFromId(&row, &col, idMeteo.toStdString()) )
unsigned row, col;
if (! observedMeteoGrid->meteoGrid()->findMeteoPointFromId(&row, &col, idMeteo.toStdString()) )
{
projectError = "Missing observed meteo cell: " + idMeteo;
return false;
}

if (!observedMeteoGrid->gridStructure().isFixedFields())
if (! observedMeteoGrid->gridStructure().isFixedFields())
{
if (!observedMeteoGrid->loadGridDailyData(projectError, idMeteo, firstSimulationDate, lastSimulationDate))
if (! observedMeteoGrid->loadGridDailyData(projectError, idMeteo, firstSimulationDate, lastSimulationDate))
{
projectError = "Missing observed data: " + idMeteo;
return false;
}
}
else
{
if (!observedMeteoGrid->loadGridDailyDataFixedFields(projectError, idMeteo, firstSimulationDate, lastSimulationDate))
if (! observedMeteoGrid->loadGridDailyDataFixedFields(projectError, idMeteo, firstSimulationDate, lastSimulationDate))
{
if (projectError == "Missing MeteoPoint id")
{
Expand All @@ -501,11 +535,12 @@ bool Crit1DProject::setMeteoXmlGrid(QString idMeteo, QString idForecast, unsigne
}
}

if (this->isShortTermForecast)
if (isShortTermForecast)
{
if (!this->forecastMeteoGrid->gridStructure().isFixedFields())
if (! forecastMeteoGrid->gridStructure().isFixedFields())
{
if (!this->forecastMeteoGrid->loadGridDailyData(projectError, idForecast, lastSimulationDate.addDays(1), lastSimulationDate.addDays(daysOfForecast)))
if (! forecastMeteoGrid->loadGridDailyData(projectError, idForecast, lastSimulationDate.addDays(1),
lastSimulationDate.addDays(daysOfForecast)))
{
if (projectError == "Missing MeteoPoint id")
{
Expand All @@ -520,7 +555,8 @@ bool Crit1DProject::setMeteoXmlGrid(QString idMeteo, QString idForecast, unsigne
}
else
{
if (!this->forecastMeteoGrid->loadGridDailyDataFixedFields(projectError, idForecast, lastSimulationDate.addDays(1), lastSimulationDate.addDays(daysOfForecast)))
if (! forecastMeteoGrid->loadGridDailyDataFixedFields(projectError, idForecast,
lastSimulationDate.addDays(1), lastSimulationDate.addDays(daysOfForecast)))
{
if (projectError == "Missing MeteoPoint id")
{
Expand All @@ -536,16 +572,17 @@ bool Crit1DProject::setMeteoXmlGrid(QString idMeteo, QString idForecast, unsigne
nrDays += unsigned(daysOfForecast);
}

if (this->isEnsembleForecast)
if (isEnsembleForecast)
{
if (this->forecastMeteoGrid->gridStructure().isFixedFields())
if (forecastMeteoGrid->gridStructure().isFixedFields())
{
projectError = "DB grid fixed fields: not available";
return false;
}
else
{
if (!this->forecastMeteoGrid->loadGridDailyDataEnsemble(projectError, idForecast, int(memberNr), lastSimulationDate.addDays(1), lastSimulationDate.addDays(daysOfForecast)))
if (! forecastMeteoGrid->loadGridDailyDataEnsemble(projectError, idForecast, int(memberNr),
lastSimulationDate.addDays(1), lastSimulationDate.addDays(daysOfForecast)))
{
if (projectError == "Missing MeteoPoint id")
{
Expand All @@ -561,55 +598,57 @@ bool Crit1DProject::setMeteoXmlGrid(QString idMeteo, QString idForecast, unsigne
nrDays += unsigned(daysOfForecast);
}

myCase.meteoPoint.latitude = this->observedMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->latitude;
myCase.meteoPoint.longitude = this->observedMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->longitude;
myCase.meteoPoint.latitude = observedMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->latitude;
myCase.meteoPoint.longitude = observedMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->longitude;
myCase.meteoPoint.initializeObsDataD(nrDays, getCrit3DDate(firstSimulationDate));

float tmin, tmax, tavg, prec;
long lastIndex = long(firstSimulationDate.daysTo(lastSimulationDate)) + 1;
for (int i = 0; i < lastIndex; i++)
{
Crit3DDate myDate = getCrit3DDate(firstSimulationDate.addDays(i));
tmin = this->observedMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyAirTemperatureMin);
tmin = observedMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyAirTemperatureMin);
myCase.meteoPoint.setMeteoPointValueD(myDate, dailyAirTemperatureMin, tmin);

tmax = this->observedMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyAirTemperatureMax);
tmax = observedMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyAirTemperatureMax);
myCase.meteoPoint.setMeteoPointValueD(myDate, dailyAirTemperatureMax, tmax);

tavg = this->observedMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyAirTemperatureAvg);
tavg = observedMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyAirTemperatureAvg);
if (int(tavg) == int(NODATA))
{
tavg = (tmax + tmin)/2;
}
myCase.meteoPoint.setMeteoPointValueD(myDate, dailyAirTemperatureAvg, tavg);

prec = this->observedMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyPrecipitation);
prec = observedMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyPrecipitation);
myCase.meteoPoint.setMeteoPointValueD(myDate, dailyPrecipitation, prec);
}

if (isShortTermForecast || isEnsembleForecast)
{
QDate start = lastSimulationDate.addDays(1);
QDate end = lastSimulationDate.addDays(daysOfForecast);
for (int i = 0; i< start.daysTo(end)+1; i++)
{
Crit3DDate myDate = getCrit3DDate(start.addDays(i));
tmin = this->forecastMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyAirTemperatureMin);
tmin = forecastMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyAirTemperatureMin);
myCase.meteoPoint.setMeteoPointValueD(myDate, dailyAirTemperatureMin, tmin);

tmax = this->forecastMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyAirTemperatureMax);
tmax = forecastMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyAirTemperatureMax);
myCase.meteoPoint.setMeteoPointValueD(myDate, dailyAirTemperatureMax, tmax);

tavg = this->forecastMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyAirTemperatureAvg);
tavg = forecastMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyAirTemperatureAvg);
if (int(tavg) == int(NODATA))
{
tavg = (tmax + tmin)/2;
}
myCase.meteoPoint.setMeteoPointValueD(myDate, dailyAirTemperatureAvg, tavg);

prec = this->forecastMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyPrecipitation);
prec = forecastMeteoGrid->meteoGrid()->meteoPointPointer(row, col)->getMeteoPointValueD(myDate, dailyPrecipitation);
myCase.meteoPoint.setMeteoPointValueD(myDate, dailyPrecipitation, prec);
}
}

return true;
}

Expand Down Expand Up @@ -1328,19 +1367,21 @@ bool Crit1DProject::setPercentileOutputCsv()
QString outputCsvPath = getFilePath(outputCsvFileName);
if (! QDir(outputCsvPath).exists())
{
QDir().mkdir(outputCsvPath);
if (! QDir().mkdir(outputCsvPath))
{
logger.writeError("Error creating directory: " + outputCsvPath);
return false;
}
}

outputCsvFile.open(outputCsvFileName.toStdString().c_str(), std::ios::out | std::ios::trunc);
if ( outputCsvFile.fail())
{
logger.writeError("open failure: " + QString(strerror(errno)) + '\n');
logger.writeError("Open failure: " + outputCsvFileName + "\n" + QString(strerror(errno)));
return false;
}
else
{
logger.writeInfo("Statistics output file (csv): " + outputCsvFileName + "\n");
}

logger.writeInfo("Statistics output file (csv): " + outputCsvFileName + "\n");

if (isYearlyStatistics || isMonthlyStatistics || isSeasonalForecast)
{
Expand Down
13 changes: 12 additions & 1 deletion agrolib/dbMeteoGrid/dbMeteoGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3682,6 +3682,17 @@ bool Crit3DMeteoGridDbHandler::isHourly()
return true;
}

bool Crit3DMeteoGridDbHandler::isMonthly()
{
if ( ! _firstMonthlyDate.isValid() || _firstMonthlyDate.year() == 1800
|| ! _lastMonthlyDate.isValid() || _lastMonthlyDate.year() == 1800 )
{
return false;
}

return true;
}


QDate Crit3DMeteoGridDbHandler::getFirstDailyDate() const
{
Expand Down Expand Up @@ -3719,7 +3730,7 @@ QDate Crit3DMeteoGridDbHandler::getLastHourlyDate() const
return _lastHourlyDate;
}

QDate Crit3DMeteoGridDbHandler::getFirsMonthlytDate() const
QDate Crit3DMeteoGridDbHandler::getFirstMonthlytDate() const
{
if (_firstMonthlyDate.year() == 1800)
{
Expand Down
3 changes: 2 additions & 1 deletion agrolib/dbMeteoGrid/dbMeteoGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,12 @@
QDate getLastDailyDate() const;
QDate getFirstHourlyDate() const;
QDate getLastHourlyDate() const;
QDate getFirsMonthlytDate() const;
QDate getFirstMonthlytDate() const;
QDate getLastMonthlyDate() const;

bool isDaily();
bool isHourly();
bool isMonthly();

bool saveLogProcedures(QString *myError, QString nameProc, QDate date);

Expand Down
13 changes: 6 additions & 7 deletions agrolib/gdalHandler/gdalRasterFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ bool convertGdalRaster(GDALDataset* dataset, gis::Crit3DRasterGrid* myRaster, in

// projection
OGRSpatialReference* spatialReference;
QString prjString = QString::fromStdString(dataset->GetProjectionRef());
if (prjString != "")
if (dataset->GetProjectionRef() != "")
{
qDebug() << "Projection =" << dataset->GetProjectionRef();
spatialReference = new OGRSpatialReference(dataset->GetProjectionRef());
Expand All @@ -72,7 +71,7 @@ bool convertGdalRaster(GDALDataset* dataset, gis::Crit3DRasterGrid* myRaster, in

// UTM zone
utmZone = spatialReference->GetUTMZone();
qDebug() << "UTM zone =" << spatialReference->GetUTMZone();
qDebug() << "UTM zone = " << spatialReference->GetUTMZone();
}
else
{
Expand Down Expand Up @@ -133,8 +132,10 @@ bool convertGdalRaster(GDALDataset* dataset, gis::Crit3DRasterGrid* myRaster, in
qDebug() << "Nodata =" << QString::number(nodataValue);

// initialize raster
myRaster->header->nrCols = dataset->GetRasterXSize();
myRaster->header->nrRows = dataset->GetRasterYSize();
int xSize = dataset->GetRasterXSize();
int ySize = dataset->GetRasterYSize();
myRaster->header->nrCols = xSize;
myRaster->header->nrRows = ySize;
myRaster->header->cellSize = adfGeoTransform[1];
myRaster->header->llCorner.x = adfGeoTransform[0];
myRaster->header->llCorner.y = adfGeoTransform[3] - myRaster->header->cellSize * myRaster->header->nrRows;
Expand All @@ -147,8 +148,6 @@ bool convertGdalRaster(GDALDataset* dataset, gis::Crit3DRasterGrid* myRaster, in
}

// read data
int xSize = band->GetXSize();
int ySize = band->GetYSize();
float* data = (float *) CPLMalloc(sizeof(float) * xSize * ySize);
CPLErr errGdal = band->RasterIO(GF_Read, 0, 0, xSize, ySize, data, xSize, ySize, GDT_Float32, 0, 0);

Expand Down
Loading

0 comments on commit d3c8450

Please sign in to comment.