Skip to content

Commit

Permalink
Merge commit 'de375270b164ef6bfcac7e29452b7890a8c1a63a'
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Oct 31, 2023
2 parents 2ce4f21 + de37527 commit 94fc701
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 81 deletions.
52 changes: 26 additions & 26 deletions criteriaOutput/criteriaOutputElaboration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ int computeAllDtxUnit(QSqlDatabase db, QString idCase, QString &errorStr)
if( !qry.exec(statement) )
{
errorStr = qry.lastError().text();
return ERROR_DBHISTORICAL;
return ERROR_DBCLIMATE;
}
qry.first();
if (!qry.isValid())
{
errorStr = qry.lastError().text();
return ERROR_DBHISTORICAL ;
return ERROR_DBCLIMATE ;
}
do
{
Expand All @@ -62,7 +62,7 @@ int computeAllDtxUnit(QSqlDatabase db, QString idCase, QString &errorStr)
if( !qry.exec(statement) )
{
errorStr = qry.lastError().text();
return ERROR_DBHISTORICAL;
return ERROR_DBCLIMATE;
}
}
if (insertTD90Col)
Expand All @@ -71,7 +71,7 @@ int computeAllDtxUnit(QSqlDatabase db, QString idCase, QString &errorStr)
if( !qry.exec(statement) )
{
errorStr = qry.lastError().text();
return ERROR_DBHISTORICAL;
return ERROR_DBCLIMATE;
}
}
if (insertTD180Col)
Expand All @@ -80,7 +80,7 @@ int computeAllDtxUnit(QSqlDatabase db, QString idCase, QString &errorStr)
if( !qry.exec(statement) )
{
errorStr = qry.lastError().text();
return ERROR_DBHISTORICAL;
return ERROR_DBCLIMATE;
}
}

Expand Down Expand Up @@ -288,7 +288,7 @@ bool writeDtxToDB(QSqlDatabase db, QString idCase, std::vector<double>& dt30,
}


int writeCsvOutputUnit(QString idCase, QString idCropClass, QSqlDatabase& dbData, QSqlDatabase& dbCrop, QSqlDatabase& dbHistoricalData,
int writeCsvOutputUnit(QString idCase, QString idCropClass, QSqlDatabase& dbData, QSqlDatabase& dbCrop, QSqlDatabase& dbClimateData,
QDate dateComputation, CriteriaOutputVariable outputVariable, QString csvFileName, QString &errorStr)
{
// IRRI RATIO (parameter for elaboration on IRRIGATION variable)
Expand Down Expand Up @@ -444,47 +444,47 @@ int writeCsvOutputUnit(QString idCase, QString idCropClass, QSqlDatabase& dbData
}
else
{
// first parameter for historical analysis (threshold)
// first parameter for climate analysis (threshold)
if (outputVariable.param1[i] != NODATA && result < outputVariable.param1[i])
{
// skip historical analysis
// skip climate analysis
resultList.append(QString::number(NODATA));
}
else
{
// find historical period available
QDate historicalFirstDate;
QDate historicalLastDate;
QSqlQuery qry(dbHistoricalData);
// find climate period available
QDate climateFirstDate;
QDate climateLastDate;
QSqlQuery qry(dbClimateData);
statement = QString("SELECT MIN(DATE),MAX(DATE) FROM `%1`").arg(idCase);
if( !qry.exec(statement) )
{
errorStr = "Error in query historical data";
return ERROR_DBHISTORICAL;
errorStr = "Error in query climate data";
return ERROR_DBCLIMATE;
}

qry.first();
if (!qry.isValid())
{
errorStr = "Historical data: " + qry.lastError().text();
return ERROR_DBHISTORICAL ;
errorStr = "climate data: " + qry.lastError().text();
return ERROR_DBCLIMATE ;
}

getValue(qry.value("MIN(DATE)"), &historicalFirstDate);
getValue(qry.value("MAX(DATE)"), &historicalLastDate);
getValue(qry.value("MIN(DATE)"), &climateFirstDate);
getValue(qry.value("MAX(DATE)"), &climateLastDate);

if (!historicalFirstDate.isValid() || !historicalLastDate.isValid())
if (!climateFirstDate.isValid() || !climateLastDate.isValid())
{
// incomplete data, there is not historical period to analyze
// incomplete data, there is not climate period to analyze
resultList.append(QString::number(NODATA));
}
else
{
std::vector<float> allYearsVector;

int year = historicalFirstDate.year();
int year = climateFirstDate.year();
bool skip = false;
while(year <= historicalLastDate.year())
while(year <= climateLastDate.year())
{
// set date
QDate previousFirstDate, previousLastDate;
Expand All @@ -493,7 +493,7 @@ int writeCsvOutputUnit(QString idCase, QString idCropClass, QSqlDatabase& dbData
if (lastDate.year() == (firstDate.year()+1))
previousLastDate.setDate(year+1, lastDate.month(), lastDate.day());

// second parameter for historical analysis (timewindow)
// second parameter for climate analysis (timewindow)
// if outputVariable.param2 is empty, current value should be compare with previous value in the same day
if (outputVariable.param2[i] != NODATA)
{
Expand All @@ -502,12 +502,12 @@ int writeCsvOutputUnit(QString idCase, QString idCropClass, QSqlDatabase& dbData
}

resultVector.clear();
int queryResult = selectSimpleVar(dbHistoricalData, idCase, varName, computation,
int queryResult = selectSimpleVar(dbClimateData, idCase, varName, computation,
previousFirstDate, previousLastDate, irriRatio, resultVector, errorStr);
if (queryResult == ERROR_DB_INCOMPLETE_DATA)
{
// only first year can be incomplete, otherwise the comparison is not valid and can be terminated
if (year != historicalFirstDate.year())
if (year != climateFirstDate.year())
{
result = NODATA;
skip = true;
Expand All @@ -534,7 +534,7 @@ int writeCsvOutputUnit(QString idCase, QString idCropClass, QSqlDatabase& dbData
}
else
{
// comparison between current value (result) and historical values during timewindow (allYearsVector)
// comparison between current value (result) and climate values during timewindow (allYearsVector)
if (outputVariable.climateComputation[i] == "PERCENTILE")
{
// compute percentile
Expand Down
2 changes: 1 addition & 1 deletion criteriaOutput/criteriaOutputElaboration.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
std::vector<double> &dt90, std::vector<double> &dt180, QString &errorStr);

int writeCsvOutputUnit(QString idCase, QString idCropClass, QSqlDatabase &dbData, QSqlDatabase &dbCrop,
QSqlDatabase &dbHistoricalData, QDate dateComputation,
QSqlDatabase &dbClimateData, QDate dateComputation,
CriteriaOutputVariable outputVariable, QString csvFileName, QString &errorStr);

int selectSimpleVar(QSqlDatabase &db, QString idCase, QString varName, QString computation, QDate firstDate,
Expand Down
83 changes: 46 additions & 37 deletions criteriaOutput/criteriaOutputProject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void CriteriaOutputProject::initialize()
operation = "";
dbComputationUnitsName = "";
dbDataName = "";
dbHistoricalDataName = "";
dbClimateDataName = "";
dbCropName = "";
variableListFileName = "";
ucmFileName = "";
Expand All @@ -62,7 +62,7 @@ void CriteriaOutputProject::initialize()
dbComputationUnitsName = "";
dbDataName = "";
dbCropName = "";
dbHistoricalDataName = "";
dbClimateDataName = "";

projectError = "";
nrUnits = 0;
Expand All @@ -85,7 +85,7 @@ void CriteriaOutputProject::closeProject()
logFile.close();
dbData.close();
dbCrop.close();
dbHistoricalData.close();
dbClimateData.close();

isProjectLoaded = false;
}
Expand All @@ -94,26 +94,26 @@ void CriteriaOutputProject::closeProject()

int CriteriaOutputProject::initializeProjectDtx()
{
// open DB Data Historical
if(dbHistoricalDataName.isEmpty())
// open DB climate
if(dbClimateDataName.isEmpty())
{
projectError = "Missing historical data parameter in the ini file ('db_historical_data')";
projectError = "Missing climate data filename in the ini file ('db_data_climate')";
return ERROR_DBPARAMETERS;
}

logger.writeInfo("DB historical data: " + dbHistoricalDataName);
logger.writeInfo("DB climate data: " + dbClimateDataName);

if (! QFile(dbHistoricalDataName).exists())
if (! QFile(dbClimateDataName).exists())
{
projectError = "DB historical data doesn't exist";
projectError = "DB climate data doesn't exist";
return ERROR_DBPARAMETERS;
}

dbHistoricalData = QSqlDatabase::addDatabase("QSQLITE", "historicalData");
dbHistoricalData.setDatabaseName(dbHistoricalDataName);
if (! dbHistoricalData.open())
dbClimateData = QSqlDatabase::addDatabase("QSQLITE", "climateData");
dbClimateData.setDatabaseName(dbClimateDataName);
if (! dbClimateData.open())
{
projectError = "Open DB historical data failed: " + dbHistoricalData.lastError().text();
projectError = "Open DB climate data failed: " + dbClimateData.lastError().text();
return ERROR_DBPARAMETERS;
}

Expand Down Expand Up @@ -155,21 +155,21 @@ int CriteriaOutputProject::initializeProjectCsv()
return ERROR_DBPARAMETERS;
}

// open DB Data Historical
if(!dbHistoricalDataName.isEmpty())
// open DB Data climate
if(!dbClimateDataName.isEmpty())
{
logger.writeInfo("DB data historical: " + dbHistoricalDataName);
if (!QFile(dbHistoricalDataName).exists())
logger.writeInfo("DB data climate: " + dbClimateDataName);
if (!QFile(dbClimateDataName).exists())
{
projectError = "DB data historical doesn't exist";
projectError = "DB data climate doesn't exist";
return ERROR_DBPARAMETERS;
}

dbHistoricalData = QSqlDatabase::addDatabase("QSQLITE", "dataHistorical");
dbHistoricalData.setDatabaseName(dbHistoricalDataName);
if (! dbHistoricalData.open())
dbClimateData = QSqlDatabase::addDatabase("QSQLITE", "climateData");
dbClimateData.setDatabaseName(dbClimateDataName);
if (! dbClimateData.open())
{
projectError = "Open DB data historical failed: " + dbHistoricalData.lastError().text();
projectError = "Open DB data climate failed: " + dbClimateData.lastError().text();
return ERROR_DBPARAMETERS;
}
}
Expand Down Expand Up @@ -234,21 +234,21 @@ bool CriteriaOutputProject::readSettings()

QString dateStr = dateComputation.toString("yyyy-MM-dd");

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

// computational units
dbComputationUnitsName = projectSettings->value("db_comp_units","").toString();
dbComputationUnitsName = projectSettings->value("db_comp_units", "").toString();
if (dbComputationUnitsName.isEmpty())
{
// check old name
dbComputationUnitsName = projectSettings->value("db_units","").toString();
dbComputationUnitsName = projectSettings->value("db_units", "").toString();
}
if (dbComputationUnitsName.isEmpty())
{
projectError = "Missing db_comp_units";
return false;
}
if (dbComputationUnitsName.at(0) == '.')
else if (dbComputationUnitsName.at(0) == '.')
{
dbComputationUnitsName = QDir().cleanPath(path + dbComputationUnitsName);
}
Expand All @@ -261,24 +261,33 @@ bool CriteriaOutputProject::readSettings()
if (! dbDataName.isEmpty())
{
if (dbDataName.at(0) == '.')
{
dbDataName = QDir::cleanPath(path + dbDataName);
}
}

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

dbHistoricalDataName = projectSettings->value("db_data_climate","").toString();
if (dbHistoricalDataName == "")
dbClimateDataName = projectSettings->value("db_data_climate", "").toString();
if (dbClimateDataName.isEmpty())
{
dbHistoricalDataName = projectSettings->value("db_data_historical","").toString();
// check old name
dbClimateDataName = projectSettings->value("db_data_historical","").toString();
}

if (dbHistoricalDataName.left(1) == ".")
if (! dbClimateDataName.isEmpty())
{
dbHistoricalDataName = QDir::cleanPath(path + dbHistoricalDataName);
if (dbClimateDataName.at(0) == '.')
{
dbClimateDataName = QDir::cleanPath(path + dbClimateDataName);
}
}

addDateTimeLogFile = projectSettings->value("add_date_to_log","").toBool();
Expand Down Expand Up @@ -437,7 +446,7 @@ int CriteriaOutputProject::precomputeDtx()
{
idCase = compUnitList[i].idCase;

int myResult = computeAllDtxUnit(dbHistoricalData, idCase, projectError);
int myResult = computeAllDtxUnit(dbClimateData, idCase, projectError);
if (myResult != CRIT1D_OK)
{
projectError = "ID CASE: " + idCase + "\n" + projectError;
Expand Down Expand Up @@ -495,7 +504,7 @@ int CriteriaOutputProject::createCsvFile()
idCase = compUnitList[i].idCase;
idCropClass = compUnitList[i].idCropClass;

myResult = writeCsvOutputUnit(idCase, idCropClass, dbData, dbCrop, dbHistoricalData,
myResult = writeCsvOutputUnit(idCase, idCropClass, dbData, dbCrop, dbClimateData,
dateComputation, outputVariable, outputCsvFileName, projectError);
if (myResult != CRIT1D_OK)
{
Expand Down Expand Up @@ -1217,7 +1226,7 @@ int CriteriaOutputProject::createCsvFileFromGUI(QDate dateComputation, QString c
idCase = compUnitList[i].idCase;
idCropClass = compUnitList[i].idCropClass;

myResult = writeCsvOutputUnit(idCase, idCropClass, dbData, dbCrop, dbHistoricalData, dateComputation, outputVariable, csvFileName, projectError);
myResult = writeCsvOutputUnit(idCase, idCropClass, dbData, dbCrop, dbClimateData, dateComputation, outputVariable, csvFileName, projectError);
if (myResult != CRIT1D_OK)
{
if (QFile(csvFileName).exists())
Expand Down
6 changes: 3 additions & 3 deletions criteriaOutput/criteriaOutputProject.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define ERROR_SETTINGS_WRONGFILENAME -2
#define ERROR_SETTINGS_MISSINGDATA -3
#define ERROR_DBPARAMETERS -5
#define ERROR_DBHISTORICAL -6
#define ERROR_DBCLIMATE -6
#define ERROR_TDXWRITE -7
#define ERROR_DBOUTPUT -10
#define ERROR_WRONGDATE -11
Expand Down Expand Up @@ -79,11 +79,11 @@ class CriteriaOutputProject
QString dbComputationUnitsName;
QString dbDataName;
QString dbCropName;
QString dbHistoricalDataName;
QString dbClimateDataName;

QSqlDatabase dbCrop;
QSqlDatabase dbData;
QSqlDatabase dbHistoricalData;
QSqlDatabase dbClimateData;

int nrUnits;
std::vector<Crit1DCompUnit> compUnitList;
Expand Down
Loading

0 comments on commit 94fc701

Please sign in to comment.