Skip to content

Commit

Permalink
export meteo points daily data
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Oct 18, 2023
1 parent 7c216f5 commit 684c7dd
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 97 deletions.
27 changes: 14 additions & 13 deletions agrolib/dbMeteoGrid/dbMeteoGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3546,46 +3546,47 @@ bool Crit3DMeteoGridDbHandler::saveLogProcedures(QString *myError, QString nameP

/*!
* \brief ExportDailyDataCsv
* export daily meteo data to csv files (id.csv)
* \param isTPrec save only Tmin, Tmax, Tavg, Prec
* \param idListFile filename of cells id list (a column list) DEFAULT: if idListFile == "" save ALL cells
* \param outputPath path for output files
* export gridded daily meteo data to csv files
* \param isTPrec save only variables: Tmin, Tmax, Tavg, Prec
* \param idListFileName filename of cells id list (list by columns)
* if idListFile == "" save ALL cells
* \param outputPath path for output files
* \return true on success, false otherwise
*/
bool Crit3DMeteoGridDbHandler::exportDailyDataCsv(QString &errorStr, bool isTPrec, QDate firstDate, QDate lastDate,
QString idListFile, QString outputPath)
QString idListFileName, QString outputPath)
{
errorStr = "";

// check output dir and id list file
// check output path
QDir outDir(outputPath);
if (! outDir.exists())
{
if (! outDir.mkpath(outputPath))
{
errorStr = "Wrong outputPath, this directory could not be created: " + outputPath;
errorStr = "Wrong outputPath, unable to create this directory: " + outputPath;
return false;
}
}
outputPath = outDir.absolutePath();

bool isList = (idListFile != "");
bool isList = (idListFileName != "");
QList<QString> idList;
if (isList)
{
if (! QFile::exists(idListFile))
if (! QFile::exists(idListFileName))
{
errorStr = "The ID list file does not exist: " + idListFile;
errorStr = "The ID list file does not exist: " + idListFileName;
return false;
}

idList = readListSingleColumn(idListFile, errorStr);
idList = readListSingleColumn(idListFileName, errorStr);
if (errorStr != "")
return false;

if (idList.size() == 0)
{
errorStr = "The ID list is empty: " + idListFile;
errorStr = "The ID list file is empty: " + idListFileName;
return false;
}
}
Expand Down Expand Up @@ -3693,7 +3694,6 @@ bool Crit3DMeteoGridDbHandler::exportDailyDataCsv(QString &errorStr, bool isTPre
currentDate = currentDate.addDays(1);
}

// data
outputFile.close();
}
}
Expand All @@ -3702,6 +3702,7 @@ bool Crit3DMeteoGridDbHandler::exportDailyDataCsv(QString &errorStr, bool isTPre
return true;
}


bool Crit3DMeteoGridDbHandler::MeteoGridToRasterFlt(double cellSize, const gis::Crit3DGisSettings& gisSettings, gis::Crit3DRasterGrid& myGrid)
{
if (! gridStructure().isUTM())
Expand Down
2 changes: 1 addition & 1 deletion agrolib/dbMeteoGrid/dbMeteoGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
bool activeAllCells(QString *myError);
bool setActiveStateCellsInList(QString *myError, QList<QString> idList, bool activeState);

bool exportDailyDataCsv(QString &errorStr, bool isTPrec, QDate firstDate, QDate lastDate, QString idListFile, QString outputPath);
bool exportDailyDataCsv(QString &errorStr, bool isTPrec, QDate firstDate, QDate lastDate, QString idListFileName, QString outputPath);
bool MeteoGridToRasterFlt(double cellSize, const gis::Crit3DGisSettings &gisSettings, gis::Crit3DRasterGrid& myGrid);

QDate getFirstDailyDate() const;
Expand Down
113 changes: 51 additions & 62 deletions agrolib/dbMeteoPoints/dbMeteoPointsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

Crit3DMeteoPointsDbHandler::Crit3DMeteoPointsDbHandler()
{

error = "";
_mapIdMeteoVar.clear();
}

Crit3DMeteoPointsDbHandler::Crit3DMeteoPointsDbHandler(QString provider_, QString host_, QString dbname_, int port_,
Expand All @@ -36,10 +37,12 @@ Crit3DMeteoPointsDbHandler::Crit3DMeteoPointsDbHandler(QString provider_, QStrin
}

if (!_db.open())
error = _db.lastError().text();

{
error = _db.lastError().text();
}
}


Crit3DMeteoPointsDbHandler::Crit3DMeteoPointsDbHandler(QString dbname_)
{
error = "";
Expand All @@ -55,10 +58,12 @@ Crit3DMeteoPointsDbHandler::Crit3DMeteoPointsDbHandler(QString dbname_)
_db.setDatabaseName(dbname_);

if (!_db.open())
{
error = _db.lastError().text();

}
}


Crit3DMeteoPointsDbHandler::~Crit3DMeteoPointsDbHandler()
{
if ((_db.isValid()) && (_db.isOpen()))
Expand All @@ -71,11 +76,6 @@ Crit3DMeteoPointsDbHandler::~Crit3DMeteoPointsDbHandler()
}


QString Crit3DMeteoPointsDbHandler::getDbName()
{
return _db.databaseName();
}

QString Crit3DMeteoPointsDbHandler::getDatasetURL(QString dataset)
{
QSqlQuery qry(_db);
Expand Down Expand Up @@ -227,6 +227,7 @@ QDateTime Crit3DMeteoPointsDbHandler::getFirstDate(frequencyType frequency)
return firstDate;
}


QDateTime Crit3DMeteoPointsDbHandler::getLastDate(frequencyType frequency)
{
QSqlQuery qry(_db);
Expand Down Expand Up @@ -386,6 +387,7 @@ bool Crit3DMeteoPointsDbHandler::existData(Crit3DMeteoPoint *meteoPoint, frequen
return false;
}


bool Crit3DMeteoPointsDbHandler::deleteData(QString pointCode, frequencyType myFreq, QDate first, QDate last)
{
QString tableName = pointCode + ((myFreq == daily) ? "_D" : "_H");
Expand All @@ -396,14 +398,14 @@ bool Crit3DMeteoPointsDbHandler::deleteData(QString pointCode, frequencyType myF
QString firstStr = first.toString("yyyy-MM-dd");
QString lastStr = last.toString("yyyy-MM-dd");
statement = QString( "DELETE FROM `%1` WHERE date_time BETWEEN DATE('%2') AND DATE('%3')")
.arg(tableName).arg(firstStr).arg(lastStr);
.arg(tableName, firstStr, lastStr);
}
else
{
QString firstStr = first.toString("yyyy-MM-dd");
QString lastStr = last.toString("yyyy-MM-dd");
statement = QString( "DELETE FROM `%1` WHERE date_time BETWEEN DATETIME('%2 00:00:00') AND DATETIME('%3 23:30:00')")
.arg(tableName).arg(firstStr).arg(lastStr);
.arg(tableName, firstStr, lastStr);
}

return qry.exec(statement);
Expand Down Expand Up @@ -499,48 +501,47 @@ bool Crit3DMeteoPointsDbHandler::deleteAllPointsFromDataset(QList<QString> datas
}
}

bool Crit3DMeteoPointsDbHandler::loadDailyData(Crit3DDate dateStart, Crit3DDate dateEnd, Crit3DMeteoPoint *meteoPoint)
{
QString dateStr;
meteoVariable variable;
QDate d;
int idVar;
float value;

int numberOfDays = difference(dateStart, dateEnd) +1;
QString startDate = QString::fromStdString(dateStart.toStdString());
QString endDate = QString::fromStdString(dateEnd.toStdString());

QSqlQuery myQuery(_db);
bool Crit3DMeteoPointsDbHandler::loadDailyData(Crit3DDate firstDate, Crit3DDate lastDate, Crit3DMeteoPoint *meteoPoint)
{
// check dates
if (firstDate > lastDate)
{
this->error = "wrong dates: first > last";
return false;
}

meteoPoint->initializeObsDataD(numberOfDays, dateStart);
int numberOfDays = difference(firstDate, lastDate) + 1;
meteoPoint->initializeObsDataD(numberOfDays, firstDate);

QString firstDateStr = QString::fromStdString(firstDate.toStdString());
QString lastDateStr = QString::fromStdString(lastDate.toStdString());
QString tableName = QString::fromStdString(meteoPoint->id) + "_D";

QString statement = QString( "SELECT * FROM `%1` WHERE date_time >= DATE('%2') AND date_time < DATE('%3', '+1 day')")
.arg(tableName).arg(startDate).arg(endDate);
.arg(tableName, firstDateStr, lastDateStr);

if( !myQuery.exec(statement) )
{
return false;
}
else
QSqlQuery myQuery(_db);
if( myQuery.exec(statement) )
{
while (myQuery.next())
{
dateStr = myQuery.value(0).toString();
d = QDate::fromString(dateStr, "yyyy-MM-dd");
QString dateStr = myQuery.value(0).toString();
QDate d = QDate::fromString(dateStr, "yyyy-MM-dd");

idVar = myQuery.value(1).toInt();
variable = _mapIdMeteoVar.at(idVar);
int idVar = myQuery.value(1).toInt();
meteoVariable variable = _mapIdMeteoVar.at(idVar);

value = myQuery.value(2).toFloat();
float value = myQuery.value(2).toFloat();

meteoPoint->setMeteoPointValueD(Crit3DDate(d.day(), d.month(), d.year()), variable, value);
}
return true;
}
else
{
return false;
}

return true;
}


Expand Down Expand Up @@ -734,16 +735,6 @@ std::vector<float> Crit3DMeteoPointsDbHandler::loadHourlyVar(QString *myError, m
return hourlyVarList;
}

QSqlDatabase Crit3DMeteoPointsDbHandler::getDb() const
{
return _db;
}

void Crit3DMeteoPointsDbHandler::setDb(const QSqlDatabase &db)
{
_db = db;
}


bool Crit3DMeteoPointsDbHandler::setAndOpenDb(QString dbname_)
{
Expand Down Expand Up @@ -1343,7 +1334,7 @@ bool Crit3DMeteoPointsDbHandler::importHourlyMeteoData(QString csvFileName, bool
// check hour
bool isNumber = false;
hour = line.at(1).toInt(&isNumber);
if (! isNumber || hour < 0 || hour > 23)
if (!isNumber || (hour < 0) || (hour > 23))
{
*log += "\nWrong dateTime: " + line.at(0) + " h" + line.at(1);
nrWrongDateTime++;
Expand Down Expand Up @@ -1875,6 +1866,7 @@ int Crit3DMeteoPointsDbHandler::getArkIdFromVar(const QString& variable)
return arkId;
}


bool Crit3DMeteoPointsDbHandler::setActiveStateIfCondition(bool activeState, QString condition)
{
QSqlQuery qry(_db);
Expand All @@ -1889,18 +1881,16 @@ bool Crit3DMeteoPointsDbHandler::setActiveStateIfCondition(bool activeState, QSt
statement = QString("UPDATE point_properties SET is_active = 0 WHERE %1 ").arg(condition);
}

if( !qry.exec(statement) )
if(! qry.exec(statement))
{
qDebug() << qry.lastError();
error += "\nError in SET is_active: " + condition + " " + qry.lastError().text();
return false;
}
else
{
return true;
}

return true;
}


bool Crit3DMeteoPointsDbHandler::setOrogCode(QString id, int orogCode)
{
QSqlQuery qry(_db);
Expand All @@ -1909,21 +1899,18 @@ bool Crit3DMeteoPointsDbHandler::setOrogCode(QString id, int orogCode)
qry.bindValue(":orogCode", orogCode);
qry.bindValue(":id", id);

if( !qry.exec() )
if(! qry.exec())
{
error += id + " " + qry.lastError().text();
error += "\nError in SET orog_code, ID: " + id + " " + qry.lastError().text();
return false;
}
else
{
return true;
}

return true;
}


QList<QString> Crit3DMeteoPointsDbHandler::getJointStations(const QString& idPoint)
{

QSqlQuery qry(_db);
QList<QString> stationsList;
QString station;
Expand All @@ -1947,12 +1934,13 @@ QList<QString> Crit3DMeteoPointsDbHandler::getJointStations(const QString& idPoi
}
}
}

return stationsList;
}


bool Crit3DMeteoPointsDbHandler::setJointStations(const QString& idPoint, QList<QString> stationsList)
{

QSqlQuery qry(_db);

QString queryStr;
Expand Down Expand Up @@ -1994,3 +1982,4 @@ bool Crit3DMeteoPointsDbHandler::setJointStations(const QString& idPoint, QList<
return false;
}
}

11 changes: 6 additions & 5 deletions agrolib/dbMeteoPoints/dbMeteoPointsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@

~Crit3DMeteoPointsDbHandler();
void dbManager();
QString getDatasetURL(QString dataset);
QString getDbName();

QSqlDatabase getDb() const;
void setDb(const QSqlDatabase &db);
QString getDbName() { return _db.databaseName(); }
QSqlDatabase getDb() const { return _db; }
void setDb(const QSqlDatabase &db) { _db = db; }

QString getDatasetURL(QString dataset);
bool setAndOpenDb(QString dbname_);

QList<QString> getAllDatasetsList();
Expand All @@ -60,7 +61,7 @@
const gis::Crit3DGisSettings& gisSettings, QString& errorString);
bool getPropertiesGivenId(QString id, Crit3DMeteoPoint* meteoPoint,
const gis::Crit3DGisSettings& gisSettings, QString& errorString);
bool loadDailyData(Crit3DDate dateStart, Crit3DDate dateEnd, Crit3DMeteoPoint *meteoPoint);
bool loadDailyData(Crit3DDate firstDate, Crit3DDate lastDate, Crit3DMeteoPoint *meteoPoint);
std::vector<float> loadDailyVar(QString *myError, meteoVariable variable,
Crit3DDate dateStart, Crit3DDate dateEnd,
QDate* firstDateDB, Crit3DMeteoPoint *meteoPoint);
Expand Down
Loading

0 comments on commit 684c7dd

Please sign in to comment.