diff --git a/agrolib/dbMeteoPoints/dbArkimet.cpp b/agrolib/dbMeteoPoints/dbArkimet.cpp index 97c141deb..686fc255f 100644 --- a/agrolib/dbMeteoPoints/dbArkimet.cpp +++ b/agrolib/dbMeteoPoints/dbArkimet.cpp @@ -78,7 +78,7 @@ QList DbArkimet::getId(QString VarName) if( !qry.exec(myQuery)) { - error = "Error in execute query:\n" + myQuery + "\n" + qry.lastError().text(); + this->setErrorString("Error in execute query:\n" + myQuery + "\n" + qry.lastError().text()); } else { diff --git a/agrolib/dbMeteoPoints/dbMeteoPointsHandler.cpp b/agrolib/dbMeteoPoints/dbMeteoPointsHandler.cpp index 50cf15d28..da8753834 100644 --- a/agrolib/dbMeteoPoints/dbMeteoPointsHandler.cpp +++ b/agrolib/dbMeteoPoints/dbMeteoPointsHandler.cpp @@ -9,14 +9,14 @@ Crit3DMeteoPointsDbHandler::Crit3DMeteoPointsDbHandler() { - error = ""; + errorStr = ""; _mapIdMeteoVar.clear(); } Crit3DMeteoPointsDbHandler::Crit3DMeteoPointsDbHandler(QString provider_, QString host_, QString dbname_, int port_, QString user_, QString pass_) { - error = ""; + errorStr = ""; _mapIdMeteoVar.clear(); if(_db.isOpen()) @@ -38,14 +38,14 @@ Crit3DMeteoPointsDbHandler::Crit3DMeteoPointsDbHandler(QString provider_, QStrin if (!_db.open()) { - error = _db.lastError().text(); + errorStr = _db.lastError().text(); } } Crit3DMeteoPointsDbHandler::Crit3DMeteoPointsDbHandler(QString dbname_) { - error = ""; + errorStr = ""; _mapIdMeteoVar.clear(); if(_db.isOpen()) @@ -59,7 +59,7 @@ Crit3DMeteoPointsDbHandler::Crit3DMeteoPointsDbHandler(QString dbname_) if (!_db.open()) { - error = _db.lastError().text(); + errorStr = _db.lastError().text(); } } @@ -181,7 +181,7 @@ QDateTime Crit3DMeteoPointsDbHandler::getFirstDate(frequencyType frequency) if( !qry.exec() ) { - error = qry.lastError().text(); + errorStr = qry.lastError().text(); } else { @@ -245,7 +245,7 @@ QDateTime Crit3DMeteoPointsDbHandler::getLastDate(frequencyType frequency) if( !qry.exec() ) { - error = qry.lastError().text(); + errorStr = qry.lastError().text(); } else { @@ -464,7 +464,7 @@ bool Crit3DMeteoPointsDbHandler::deleteAllData(frequencyType myFreq) if( !qry.exec() ) { - error = qry.lastError().text(); + errorStr = qry.lastError().text(); } else { @@ -507,7 +507,7 @@ bool Crit3DMeteoPointsDbHandler::loadDailyData(Crit3DDate firstDate, Crit3DDate // check dates if (firstDate > lastDate) { - this->error = "wrong dates: first > last"; + this->errorStr = "wrong dates: first > last"; return false; } @@ -738,7 +738,7 @@ std::vector Crit3DMeteoPointsDbHandler::loadHourlyVar(QString *myError, m bool Crit3DMeteoPointsDbHandler::setAndOpenDb(QString dbname_) { - error = ""; + errorStr = ""; _mapIdMeteoVar.clear(); if(_db.isOpen()) @@ -753,7 +753,7 @@ bool Crit3DMeteoPointsDbHandler::setAndOpenDb(QString dbname_) if (!_db.open()) { - error = _db.lastError().text(); + errorStr = _db.lastError().text(); return false; } return true; @@ -954,7 +954,7 @@ QString Crit3DMeteoPointsDbHandler::getNameGivenId(QString id) if( !qry.exec() ) { - error = qry.lastError().text(); + errorStr = qry.lastError().text(); return name; } @@ -977,7 +977,7 @@ double Crit3DMeteoPointsDbHandler::getAltitudeGivenId(QString id) if( !qry.exec() ) { - error = qry.lastError().text(); + errorStr = qry.lastError().text(); return altitude; } @@ -1019,15 +1019,14 @@ bool Crit3DMeteoPointsDbHandler::writePointProperties(Crit3DMeteoPoint *myPoint) } else return true; - } -bool Crit3DMeteoPointsDbHandler::updatePointProperties(QList columnList, QList valueList) -{ +bool Crit3DMeteoPointsDbHandler::updatePointProperties(const QList &columnList, const QList &valueList) +{ if (columnList.size() != valueList.size()) { - qDebug() << "invalid input"; + errorStr = "invalid input: nr columns != nr values"; return false; } QSqlQuery qry(_db); @@ -1038,43 +1037,43 @@ bool Crit3DMeteoPointsDbHandler::updatePointProperties(QList columnList "orog_code TEXT(20), PRIMARY KEY(id_point))").arg("point_properties"); qry.prepare(queryStr); - if( !qry.exec() ) + if( ! qry.exec() ) { - qDebug() << qry.lastError(); + errorStr = qry.lastError().text(); return false; } queryStr = "INSERT OR REPLACE INTO point_properties ("; - for (int i = 0; i columnList, QList valueList) { @@ -1118,7 +1117,7 @@ bool Crit3DMeteoPointsDbHandler::loadVariableProperties() QString statement = QString( "SELECT * FROM `%1` ").arg(tableName); if( !qry.exec(statement) ) { - error = qry.lastError().text(); + errorStr = qry.lastError().text(); return false; } else @@ -1136,12 +1135,12 @@ bool Crit3DMeteoPointsDbHandler::loadVariableProperties() ret = _mapIdMeteoVar.insert(std::pair(id_variable, meteoVar)); if (ret.second==false) { - error = "element 'z' already existed"; + errorStr = "element 'z' already existed"; } } else { - error = "Wrong variable: " + variable; + errorStr = "Wrong variable: " + variable; return false; } } @@ -1149,17 +1148,15 @@ bool Crit3DMeteoPointsDbHandler::loadVariableProperties() return true; } -bool Crit3DMeteoPointsDbHandler::getNameColumn(QString tableName, QList* columnList) + +bool Crit3DMeteoPointsDbHandler::getFieldList(const QString &tableName, QList &fieldList) { QSqlQuery qry(_db); - - std::string varStdString; - std::pair::iterator,bool> ret; - QString statement = QString( "PRAGMA table_info('%1')").arg(tableName); + if( !qry.exec(statement) ) { - error = qry.lastError().text(); + errorStr = qry.lastError().text(); return false; } else @@ -1168,12 +1165,14 @@ bool Crit3DMeteoPointsDbHandler::getNameColumn(QString tableName, QList while (qry.next()) { getValue(qry.value("name"), &name); - *columnList << name; + fieldList << name; } } + return true; } + int Crit3DMeteoPointsDbHandler::getIdfromMeteoVar(meteoVariable meteoVar) { std::map::const_iterator it; @@ -1391,7 +1390,7 @@ bool Crit3DMeteoPointsDbHandler::importHourlyMeteoData(QString csvFileName, bool } -bool Crit3DMeteoPointsDbHandler::writeDailyDataList(QString pointCode, QList listEntries, QString& log) +bool Crit3DMeteoPointsDbHandler::writeDailyDataList(const QString &pointCode, const QList &listEntries, QString& log) { if (! existIdPoint(pointCode)) { @@ -1413,7 +1412,6 @@ bool Crit3DMeteoPointsDbHandler::writeDailyDataList(QString pointCode, QList listEntries, QString &log) +bool Crit3DMeteoPointsDbHandler::writeHourlyDataList(const QString &pointCode, const QList &listEntries, QString &log) { if (! existIdPoint(pointCode)) { @@ -1447,7 +1445,6 @@ bool Crit3DMeteoPointsDbHandler::writeHourlyDataList(QString pointCode, QList& pointList, bool activeState) { - error = ""; + errorStr = ""; QString idList = ""; for (int i = 0; i < pointList.size(); i++) { @@ -1573,7 +1570,7 @@ bool Crit3DMeteoPointsDbHandler::setActiveStatePointList(const QList& p QSqlQuery qry(_db); if( !qry.exec(sqlStr)) { - error = qry.lastError().text(); + errorStr = qry.lastError().text(); return false; } @@ -1585,7 +1582,7 @@ bool Crit3DMeteoPointsDbHandler::deleteAllPointsFromIdList(const QList& { QSqlQuery qry(_db); - error = ""; + errorStr = ""; for (int i = 0; i < pointList.size(); i++) { QString id_point = pointList[i]; @@ -1593,7 +1590,7 @@ bool Crit3DMeteoPointsDbHandler::deleteAllPointsFromIdList(const QList& qry.bindValue(":id_point", id_point); if( !qry.exec() ) { - error += id_point + " " + qry.lastError().text(); + errorStr += id_point + " " + qry.lastError().text(); return false; } @@ -1602,14 +1599,14 @@ bool Crit3DMeteoPointsDbHandler::deleteAllPointsFromIdList(const QList& QString queryStr = "DROP TABLE IF EXISTS '" + table +"'"; if( !qry.exec(queryStr)) { - error += "\n" + qry.lastError().text(); + errorStr += "\n" + qry.lastError().text(); } table = id_point + "_D"; queryStr = "DROP TABLE IF EXISTS '" + table +"'"; if( !qry.exec(queryStr)) { - error += "\n" + qry.lastError().text(); + errorStr += "\n" + qry.lastError().text(); } } @@ -1883,7 +1880,7 @@ bool Crit3DMeteoPointsDbHandler::setActiveStateIfCondition(bool activeState, QSt if(! qry.exec(statement)) { - error += "\nError in SET is_active: " + condition + " " + qry.lastError().text(); + errorStr += "\nError in SET is_active: " + condition + " " + qry.lastError().text(); return false; } @@ -1901,7 +1898,7 @@ bool Crit3DMeteoPointsDbHandler::setOrogCode(QString id, int orogCode) if(! qry.exec()) { - error += "\nError in SET orog_code, ID: " + id + " " + qry.lastError().text(); + errorStr += "\nError in SET orog_code, ID: " + id + " " + qry.lastError().text(); return false; } @@ -1949,7 +1946,7 @@ bool Crit3DMeteoPointsDbHandler::setJointStations(const QString& idPoint, QList< qry.prepare(queryStr); if( !qry.exec() ) { - error += idPoint + " " + qry.lastError().text(); + errorStr += idPoint + " " + qry.lastError().text(); return false; } @@ -1957,11 +1954,11 @@ bool Crit3DMeteoPointsDbHandler::setJointStations(const QString& idPoint, QList< qry.bindValue(":id_point", idPoint); if( !qry.exec() ) { - error += idPoint + " " + qry.lastError().text(); + errorStr += idPoint + " " + qry.lastError().text(); return false; } - error.clear(); + errorStr.clear(); for (int i = 0; i < stationsList.size(); i++) { qry.prepare( "INSERT INTO joint_stations (id_point, joint_station) VALUES (:id_point, :joint_station)" ); @@ -1970,10 +1967,10 @@ bool Crit3DMeteoPointsDbHandler::setJointStations(const QString& idPoint, QList< qry.bindValue(":joint_station", stationsList[i]); if( !qry.exec() ) { - error += idPoint + "," + stationsList[i] + " " + qry.lastError().text(); + errorStr += idPoint + "," + stationsList[i] + " " + qry.lastError().text(); } } - if (error.isEmpty()) + if (errorStr.isEmpty()) { return true; } diff --git a/agrolib/dbMeteoPoints/dbMeteoPointsHandler.h b/agrolib/dbMeteoPoints/dbMeteoPointsHandler.h index 82a5d0a90..08940e901 100644 --- a/agrolib/dbMeteoPoints/dbMeteoPointsHandler.h +++ b/agrolib/dbMeteoPoints/dbMeteoPointsHandler.h @@ -23,9 +23,10 @@ { Q_OBJECT - public: - QString error; + private: + QString errorStr; + public: explicit Crit3DMeteoPointsDbHandler(); explicit Crit3DMeteoPointsDbHandler(QString dbname_); explicit Crit3DMeteoPointsDbHandler(QString provider_, QString host_, QString dbname_, int port_, QString user_, QString pass_); @@ -36,6 +37,8 @@ QString getDbName() { return _db.databaseName(); } QSqlDatabase getDb() const { return _db; } void setDb(const QSqlDatabase &db) { _db = db; } + QString getErrorString() { return errorStr; } + void setErrorString(QString str) { errorStr = str; } QString getDatasetURL(QString dataset); bool setAndOpenDb(QString dbname_); @@ -55,7 +58,7 @@ bool deleteData(QString pointCode, frequencyType myFreq, QList varList, QDate first, QDate last); bool deleteAllData(frequencyType myFreq); bool writePointProperties(Crit3DMeteoPoint* pointProp); - bool updatePointProperties(QList columnList, QList valueList); + bool updatePointProperties(const QList &columnList, const QList &valueList); bool updatePointPropertiesGivenId(QString id, QList columnList, QList valueList); bool getPropertiesFromDb(QList& meteoPointsList, const gis::Crit3DGisSettings& gisSettings, QString& errorString); @@ -71,7 +74,7 @@ QDateTime* firstDateDB, Crit3DMeteoPoint *meteoPoint); bool loadVariableProperties(); - bool getNameColumn(QString tableName, QList* columnList); + bool getFieldList(const QString &tableName, QList &fieldList); int getIdfromMeteoVar(meteoVariable meteoVar); int getArkIdFromVar(const QString& variable); std::map getMapIdMeteoVar() const; @@ -83,8 +86,8 @@ int* nrMissingData, int* nrWrongData, Crit3DQuality* dataQuality); bool importHourlyMeteoData(QString fileNameComplete, bool deletePreviousData, QString *log); - bool writeDailyDataList(QString pointCode, QList listEntries, QString& log); - bool writeHourlyDataList(QString pointCode, QList listEntries, QString& log); + bool writeDailyDataList(const QString &pointCode, const QList &listEntries, QString& log); + bool writeHourlyDataList(const QString &pointCode, const QList &listEntries, QString& log); bool setAllPointsActive(); bool setAllPointsNotActive(); diff --git a/agrolib/homogeneityWidget/homogeneityWidget.cpp b/agrolib/homogeneityWidget/homogeneityWidget.cpp index 2c5de9d91..ece2311ad 100644 --- a/agrolib/homogeneityWidget/homogeneityWidget.cpp +++ b/agrolib/homogeneityWidget/homogeneityWidget.cpp @@ -547,7 +547,7 @@ void Crit3DHomogeneityWidget::saveToDbClicked() } if (!meteoPointsDbHandler->setJointStations(QString::fromStdString(meteoPointsNearDistanceList[0].id), stationsList)) { - QMessageBox::critical(nullptr, "Error", meteoPointsDbHandler->error); + QMessageBox::critical(nullptr, "Error", meteoPointsDbHandler->getErrorString()); } saveToDb.setEnabled(false); } diff --git a/agrolib/importDataXML/importDataXML.pro b/agrolib/importDataXML/importDataXML.pro index 1a8cf2d83..cd786f2df 100644 --- a/agrolib/importDataXML/importDataXML.pro +++ b/agrolib/importDataXML/importDataXML.pro @@ -28,13 +28,11 @@ INCLUDEPATH += ../crit3dDate ../mathFunctions ../meteo ../gis ../interpolation . SOURCES += importDataXML.cpp \ fieldXML.cpp \ - importPropertiesCSV.cpp \ variableXML.cpp HEADERS += importDataXML.h \ fieldXML.h \ - importPropertiesCSV.h \ variableXML.h diff --git a/agrolib/importDataXML/importPropertiesCSV.cpp b/agrolib/importDataXML/importPropertiesCSV.cpp deleted file mode 100644 index 6989c6cd4..000000000 --- a/agrolib/importDataXML/importPropertiesCSV.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include "importPropertiesCSV.h" -#include -#include - - -ImportPropertiesCSV::ImportPropertiesCSV(QString csvFileName) - :csvFileName(csvFileName) -{ - -} - - -bool ImportPropertiesCSV::parserCSV(QString *error) -{ - if (csvFileName == "") - { - *error = "Missing CSV file."; - return false; - } - - QFile myFile(csvFileName); - if (!myFile.open(QIODevice::ReadOnly)) - { - *error = "Open XML failed: " + csvFileName + "\n " + myFile.errorString(); - return (false); - } - - QTextStream myStream (&myFile); - QList line; - if (myStream.atEnd()) - { - *error += "\nFile is void."; - myFile.close(); - return false; - } - else - { - header = myStream.readLine().split(','); - } - while(!myStream.atEnd()) - { - line = myStream.readLine().split(','); - - // skip void lines - if (line.length() <= 2) continue; - data.append(line); - } - - myFile.close(); - return true; -} - - -QList ImportPropertiesCSV::getHeader() const -{ - return header; -} - - -QList > ImportPropertiesCSV::getData() const -{ - return data; -} diff --git a/agrolib/importDataXML/importPropertiesCSV.h b/agrolib/importDataXML/importPropertiesCSV.h deleted file mode 100644 index 9eb62d010..000000000 --- a/agrolib/importDataXML/importPropertiesCSV.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef IMPORTPROPERTIESCSV_H -#define IMPORTPROPERTIESCSV_H - - #include - #include - - class ImportPropertiesCSV - { - public: - ImportPropertiesCSV(QString csvFileName); - bool parserCSV(QString *error); - QList getHeader() const; - QList > getData() const; - - private: - QString csvFileName; - QList header; - QList> data; - }; - -#endif // IMPORTPROPERTIESCSV_H diff --git a/agrolib/interpolation/interpolation.cpp b/agrolib/interpolation/interpolation.cpp index d76526227..34f43cd4f 100644 --- a/agrolib/interpolation/interpolation.cpp +++ b/agrolib/interpolation/interpolation.cpp @@ -1678,7 +1678,7 @@ bool preInterpolation(std::vector &myPoints, Crit if (mySettings->getUseTD() && getUseTdVar(myVar)) topographicDistanceOptimize(myVar, myMeteoPoints, nrMeteoPoints, myPoints, mySettings, meteoSettings, myTime); - return (true); + return true; } diff --git a/agrolib/pointStatisticsWidget/pointStatisticsWidget.cpp b/agrolib/pointStatisticsWidget/pointStatisticsWidget.cpp index 17bc76129..1d1c7a4ee 100644 --- a/agrolib/pointStatisticsWidget/pointStatisticsWidget.cpp +++ b/agrolib/pointStatisticsWidget/pointStatisticsWidget.cpp @@ -1903,7 +1903,7 @@ void Crit3DPointStatisticsWidget::saveToDbClicked() } if (!meteoPointsDbHandler->setJointStations(QString::fromStdString(meteoPoints[0].id), stationsList)) { - QMessageBox::critical(nullptr, "Error", meteoPointsDbHandler->error); + QMessageBox::critical(nullptr, "Error", meteoPointsDbHandler->getErrorString()); } } diff --git a/agrolib/pragaDialogs/dialogXMLComputation.cpp b/agrolib/pragaDialogs/dialogXMLComputation.cpp index 2bad34fec..6896bc9cd 100644 --- a/agrolib/pragaDialogs/dialogXMLComputation.cpp +++ b/agrolib/pragaDialogs/dialogXMLComputation.cpp @@ -42,5 +42,7 @@ DialogXMLComputation::DialogXMLComputation(bool isAnomaly, QList listXM void DialogXMLComputation::elabClicked(QListWidgetItem* item) { + Q_UNUSED(item); + index = listXMLWidget.currentRow(); } diff --git a/agrolib/pragaProject/pragaProject.cpp b/agrolib/pragaProject/pragaProject.cpp index dc447925c..0b43c0921 100644 --- a/agrolib/pragaProject/pragaProject.cpp +++ b/agrolib/pragaProject/pragaProject.cpp @@ -44,7 +44,6 @@ void PragaProject::initializePragaProject() lastElabTargetisGrid = false; outputMeteoPointsDbHandler = nullptr; - outputMeteoPointsDbFileName = ""; outputMeteoPointsLoaded = false; } @@ -1303,10 +1302,10 @@ bool PragaProject::downloadDailyDataArkimet(QList variables, bool prec0 else { arkIdVar.append(myDownload->getDbArkimet()->getId(variables[i])); - if (myDownload->getDbArkimet()->error != "") + if (myDownload->getDbArkimet()->getErrorString() != "") { - logError(myDownload->getDbArkimet()->error); - myDownload->getDbArkimet()->error.clear(); + logError(myDownload->getDbArkimet()->getErrorString()); + myDownload->getDbArkimet()->setErrorString(""); } } } @@ -1887,7 +1886,72 @@ bool PragaProject::hourlyDerivedVariablesGrid(QDate first, QDate last, bool load return true; } -bool PragaProject::interpolationMeteoGridPeriod(QDate dateIni, QDate dateFin, QList variables, QList aggrVariables, bool saveRasters, int nrDaysLoading, int nrDaysSaving) + +bool PragaProject::interpolationOutputPointsPeriod(QDate dateIni, QDate dateFin, QList variables) +{ + // check + if (variables.size() == 0) + { + errorString = "No variables"; + return false; + } + + if (! meteoPointsLoaded || nrMeteoPoints == 0) + { + errorString = "No meteo points"; + return false; + } + + if (! outputMeteoPointsLoaded || outputPoints.empty()) + { + errorString = "No output points"; + return false; + } + + // check dates + if (dateIni.isNull() || dateFin.isNull() || dateIni > dateFin) + { + errorString = "Wrong period."; + return false; + } + + // check variables + bool isDaily = false; + bool isHourly = false; + QList varToSave; + meteoVariable myVar; + + foreach (myVar, variables) + { + frequencyType freq = getVarFrequency(myVar); + + if (freq == noFrequency) + { + errorString = "Unknown variable: " + QString::fromStdString(getMeteoVarName(myVar)); + return false; + } + else if (freq == hourly) + isHourly = true; + else if (freq == daily) + isDaily = true; + + varToSave.push_back(myVar); + + // save two variables for vector wind + if (myVar == windVectorIntensity) + varToSave.push_back(windVectorDirection); + else if (myVar == windVectorDirection) + varToSave.push_back(windVectorIntensity); + } + + errorString = "TODO"; + return false; +} + + +bool PragaProject::interpolationMeteoGridPeriod(QDate dateIni, QDate dateFin, QList variables, + QList aggrVariables, bool saveRasters, + int nrDaysLoading, int nrDaysSaving) { // check variables if (variables.size() == 0) @@ -1917,9 +1981,7 @@ bool PragaProject::interpolationMeteoGridPeriod(QDate dateIni, QDate dateFin, QL return false; } - //order variables for derived computation - - std::string id; + // order variables for derived computation std::string errString; QString myError, rasterName, varName; int myHour; @@ -3550,6 +3612,8 @@ bool PragaProject::saveLogProceduresGrid(QString nameProc, QDate date) } +// --------------------------- OUTPUT METEO POINTS ---------------------------------- + void PragaProject::closeOutputMeteoPointsDB() { if (outputMeteoPointsDbHandler != nullptr) @@ -3563,7 +3627,7 @@ void PragaProject::closeOutputMeteoPointsDB() } -bool PragaProject::loadOutputMeteoPointsDB(QString fileName) +bool PragaProject::loadOutputMeteoPointsDB(const QString &fileName) { if (fileName.isEmpty()) return false; @@ -3580,10 +3644,10 @@ bool PragaProject::loadOutputMeteoPointsDB(QString fileName) } outputMeteoPointsDbHandler = new Crit3DMeteoPointsDbHandler(outputDbName); - if (! outputMeteoPointsDbHandler->error.isEmpty()) + if (! outputMeteoPointsDbHandler->getErrorString().isEmpty()) { - errorString = "Function loadOutputMeteoPointsDB:\n" + outputDbName - + "\n" + outputMeteoPointsDbHandler->error; + errorString = "Error in opening:\n" + outputDbName + + "\n" + outputMeteoPointsDbHandler->getErrorString(); return false; } @@ -3612,9 +3676,58 @@ bool PragaProject::loadOutputMeteoPointsDB(QString fileName) } listMeteoPoints.clear(); - outputMeteoPointsDbFileName = outputDbName; outputMeteoPointsLoaded = true; - logInfo("Output meteo points DB = " + outputMeteoPointsDbFileName); + logInfo("Output meteo points DB = " + outputDbName); + + return true; +} + + +bool PragaProject::writeMeteoPointsProperties(const QList &joinedPropertiesList, const QList &csvFields, + const QList> &csvData, bool isOutputPoints) +{ + QList propertiesList; + QList posValues; + + for (int i = 0; i < joinedPropertiesList.size(); i++) + { + QList couple = joinedPropertiesList[i].split("-->"); + QString pragaProperty = couple[0]; + QString csvProperty = couple[1]; + int pos = csvFields.indexOf(csvProperty); + if (pos != -1) + { + propertiesList << pragaProperty; + posValues << pos; + } + } + + for (int row = 0; row < csvData.size(); row++) + { + QList csvDataList; + + for (int j = 0; j < posValues.size(); j++) + { + csvDataList << csvData[row][posValues[j]]; + } + + if (isOutputPoints) + { + if (! outputMeteoPointsDbHandler->updatePointProperties(propertiesList, csvDataList)) + { + errorString = outputMeteoPointsDbHandler->getErrorString(); + return false; + } + } + else + { + if (! meteoPointsDbHandler->updatePointProperties(propertiesList, csvDataList)) + { + errorString = meteoPointsDbHandler->getErrorString(); + return false; + } + } + } return true; } diff --git a/agrolib/pragaProject/pragaProject.h b/agrolib/pragaProject/pragaProject.h index dc0aeb36d..c485cb83c 100644 --- a/agrolib/pragaProject/pragaProject.h +++ b/agrolib/pragaProject/pragaProject.h @@ -79,7 +79,6 @@ ImportDataXML* importData; Crit3DMeteoPointsDbHandler* outputMeteoPointsDbHandler; - QString outputMeteoPointsDbFileName; bool outputMeteoPointsLoaded; #ifdef NETCDF @@ -99,15 +98,21 @@ bool loadPragaSettings(); void closeOutputMeteoPointsDB(); - bool loadOutputMeteoPointsDB(QString fileName); + bool loadOutputMeteoPointsDB(const QString &fileName); + bool writeMeteoPointsProperties(const QList &joinedPropertiesList, const QList &csvFields, + const QList> &csvData, bool isOutputPoints); gis::Crit3DRasterGrid* getPragaMapFromVar(meteoVariable myVar); bool downloadDailyDataArkimet(QList variables, bool prec0024, QDate startDate, QDate endDate, bool showInfo); bool downloadHourlyDataArkimet(QList variables, QDate startDate, QDate endDate, bool showInfo); + bool interpolationOutputPointsPeriod(QDate dateIni, QDate dateFin, QList variables); + bool interpolationMeteoGrid(meteoVariable myVar, frequencyType myFrequency, const Crit3DTime& myTime); - bool interpolationMeteoGridPeriod(QDate dateIni, QDate dateFin, QList variables, QList aggrVariables, bool saveRasters, int nrDaysLoading, int nrDaysSaving); + bool interpolationMeteoGridPeriod(QDate dateIni, QDate dateFin, QList variables, + QList aggrVariables, bool saveRasters, int nrDaysLoading, int nrDaysSaving); + bool saveGrid(meteoVariable myVar, frequencyType myFrequency, const Crit3DTime& myTime, bool showInfo); bool timeAggregateGridVarHourlyInDaily(meteoVariable dailyVar, Crit3DDate dateIni, Crit3DDate dateFin); bool timeAggregateGrid(QDate dateIni, QDate dateFin, QList variables, bool loadData, bool saveData); diff --git a/agrolib/project/project.cpp b/agrolib/project/project.cpp index 93d146337..81d0edce6 100644 --- a/agrolib/project/project.cpp +++ b/agrolib/project/project.cpp @@ -1044,16 +1044,16 @@ bool Project::loadMeteoPointsDB(QString fileName) } meteoPointsDbHandler = new Crit3DMeteoPointsDbHandler(dbName); - if (meteoPointsDbHandler->error != "") + if (! meteoPointsDbHandler->getErrorString().isEmpty()) { - errorString = "Function loadMeteoPointsDB:\n" + dbName + "\n" + meteoPointsDbHandler->error; + errorString = "Function loadMeteoPointsDB:\n" + dbName + "\n" + meteoPointsDbHandler->getErrorString(); closeMeteoPointsDB(); return false; } if (! meteoPointsDbHandler->loadVariableProperties()) { - errorString = meteoPointsDbHandler->error; + errorString = meteoPointsDbHandler->getErrorString(); closeMeteoPointsDB(); return false; } @@ -1140,16 +1140,16 @@ bool Project::loadAggregationDBAsMeteoPoints(QString fileName) } meteoPointsDbHandler = new Crit3DMeteoPointsDbHandler(dbName); - if (meteoPointsDbHandler->error != "") + if (meteoPointsDbHandler->getErrorString() != "") { - errorString = "Function loadAggregationPointsDB:\n" + dbName + "\n" + meteoPointsDbHandler->error; + errorString = "Function loadAggregationPointsDB:\n" + dbName + "\n" + meteoPointsDbHandler->getErrorString(); closeMeteoPointsDB(); return false; } if (! meteoPointsDbHandler->loadVariableProperties()) { - errorString = meteoPointsDbHandler->error; + errorString = meteoPointsDbHandler->getErrorString(); closeMeteoPointsDB(); return false; } @@ -2061,6 +2061,12 @@ bool Project::interpolationOutputPoints(std::vector proxyValues; proxyValues.resize(unsigned(interpolationSettings.getProxyNr())); @@ -2189,7 +2195,7 @@ bool Project::interpolationDem(meteoVariable myVar, const Crit3DTime& myTime, gi &qualityInterpolationSettings, &interpolationSettings, meteoSettings, &climateParameters, interpolationPoints, checkSpatialQuality)) { - logError("No data available: " + QString::fromStdString(getVariableString(myVar))); + errorString = "No data available: " + QString::fromStdString(getVariableString(myVar)); return false; } @@ -2197,29 +2203,27 @@ bool Project::interpolationDem(meteoVariable myVar, const Crit3DTime& myTime, gi if (! preInterpolation(interpolationPoints, &interpolationSettings, meteoSettings, &climateParameters, meteoPoints, nrMeteoPoints, myVar, myTime)) { - logError("Interpolation: error in function preInterpolation"); + errorString = "Error in function preInterpolation."; return false; } // interpolate - bool result; if (getComputeOnlyPoints()) { - result = interpolationOutputPoints(interpolationPoints, myRaster, myVar); + myRaster->initializeGrid(DEM); + if (! interpolationOutputPoints(interpolationPoints, myRaster, myVar)) + return false; } else { - result = interpolationRaster(interpolationPoints, &interpolationSettings, meteoSettings, myRaster, DEM, myVar); - } - - if (!result) - { - logError("Interpolation: error in function interpolationRaster"); - return false; + if (! interpolationRaster(interpolationPoints, &interpolationSettings, meteoSettings, myRaster, DEM, myVar)) + { + errorString = "Error in function interpolationRaster."; + return false; + } } myRaster->setMapTime(myTime); - return true; } @@ -2343,12 +2347,13 @@ bool Project::interpolateDemRadiation(const Crit3DTime& myTime, gis::Crit3DRaste // interpolate transmissivity if (getComputeOnlyPoints()) { - result = interpolationOutputPoints(interpolationPoints, this->radiationMaps->transmissivityMap, atmTransmissivity); + radiationMaps->transmissivityMap->initializeGrid(DEM); + result = interpolationOutputPoints(interpolationPoints, radiationMaps->transmissivityMap, atmTransmissivity); } else { result = interpolationRaster(interpolationPoints, &interpolationSettings, meteoSettings, - this->radiationMaps->transmissivityMap, DEM, atmTransmissivity); + radiationMaps->transmissivityMap, DEM, atmTransmissivity); } if (! result) { @@ -2384,7 +2389,7 @@ bool Project::checkInterpolation(meteoVariable myVar) { if (! DEM.isLoaded) { - logError("Digital Elevation Model not loaded"); + logError("Load a Digital Elevation Model before."); return false; } @@ -3362,68 +3367,12 @@ void Project::deleteMeteoWidgetGrid(int id) } } + void Project::deleteProxyWidget() { proxyWidget = nullptr; } -bool Project::parseMeteoPointsPropertiesCSV(QString csvFileName, QList* csvFields) -{ - if (! QFile(csvFileName).exists() || ! QFileInfo(csvFileName).isFile()) - { - logError("Missing file: " + csvFileName); - return false; - } - importProperties = new ImportPropertiesCSV(csvFileName); - - errorString = ""; - if (!importProperties->parserCSV(&errorString)) - { - logError(errorString); - delete importProperties; - return false; - } - *csvFields = importProperties->getHeader(); - return true; -} - - -bool Project::writeMeteoPointsProperties(QList propertiesList) -{ - QList header = importProperties->getHeader(); - QList> dataFields = importProperties->getData(); - - QList column; - QList posValues; - - for (int i = 0; i < propertiesList.size(); i++) - { - QList couple = propertiesList[i].split("-->"); - QString pragaProperties = couple[0]; - QString fileProperties = couple[1]; - int pos = header.indexOf(fileProperties); - if (pos != -1) - { - column << pragaProperties; - posValues << pos; - } - } - - QList values; - - for (int row = 0; rowupdatePointProperties(column, values); - } - - return true; -} - void Project::showProxyGraph() { @@ -3503,7 +3452,7 @@ bool Project::setActiveStateSelectedPoints(bool isActive) if (!meteoPointsDbHandler->setActiveStatePointList(selectedPointList, isActive)) { - logError("Failed to activate/deactivate selected points:\n" + meteoPointsDbHandler->error); + logError("Failed to activate/deactivate selected points:\n" + meteoPointsDbHandler->getErrorString()); return false; } @@ -3523,7 +3472,7 @@ bool Project::setActiveStatePointList(QString fileName, bool isActive) if (!meteoPointsDbHandler->setActiveStatePointList(pointList, isActive)) { - logError("Failed to activate/deactivate point list:\n" + meteoPointsDbHandler->error); + logError("Failed to activate/deactivate point list:\n" + meteoPointsDbHandler->getErrorString()); return false; } @@ -3624,7 +3573,7 @@ bool Project::setActiveStateWithCriteria(bool isActive) } if (!meteoPointsDbHandler->setActiveStatePointList(points, isActive)) { - logError("Failed to activate/deactivate points selected:\n" + meteoPointsDbHandler->error); + logError("Failed to activate/deactivate points selected:\n" + meteoPointsDbHandler->getErrorString()); return false; } } @@ -3668,15 +3617,15 @@ bool Project::deleteMeteoPoints(const QList& pointList) bool isOk = meteoPointsDbHandler->deleteAllPointsFromIdList(pointList); closeLogInfo(); - if (!isOk) + if (! isOk) { - logError("Failed to delete points:" + meteoPointsDbHandler->error); + logError("Failed to delete points:" + meteoPointsDbHandler->getErrorString()); return false; } - if (meteoPointsDbHandler->error != "") + if (! meteoPointsDbHandler->getErrorString().isEmpty()) { - logError("WARNING: " + meteoPointsDbHandler->error); + logError("WARNING: " + meteoPointsDbHandler->getErrorString()); } // reload meteoPoint, point properties table is changed @@ -3815,9 +3764,9 @@ bool Project::writeOutputPointList(QString fileName) return true; } -void Project::setComputeOnlyPoints(bool isOnlyPoints) +void Project::setComputeOnlyPoints(bool value) { - computeOnlyPoints = isOnlyPoints; + computeOnlyPoints = value; } bool Project::getComputeOnlyPoints() diff --git a/agrolib/project/project.h b/agrolib/project/project.h index 01b210f08..9c4e94701 100644 --- a/agrolib/project/project.h +++ b/agrolib/project/project.h @@ -31,9 +31,6 @@ #ifndef METEOMAPS_H #include "meteoMaps.h" #endif - #ifndef IMPORTPROPERTIESCSV_H - #include "importPropertiesCSV.h" - #endif #ifndef PROXYWIDGET_H #include "proxyWidget.h" #endif @@ -66,7 +63,6 @@ QString projectPath; bool computeOnlyPoints; FormInfo* formLog; - ImportPropertiesCSV* importProperties; void clearMeteoPoints(); bool createDefaultProject(QString fileName); @@ -260,9 +256,6 @@ bool checkMeteoGridForExport(); void importHourlyMeteoData(const QString& fileName, bool importAllFiles, bool deletePreviousData); - bool parseMeteoPointsPropertiesCSV(QString csvFileName, QList *csvFields); - bool writeMeteoPointsProperties(QList propertiesList); - gis::Crit3DRasterGrid* getHourlyMeteoRaster(meteoVariable myVar); void showMeteoWidgetPoint(std::string idMeteoPoint, std::string namePoint, bool isAppend); void showMeteoWidgetGrid(std::string idCell, bool isAppend); @@ -286,7 +279,7 @@ bool loadAndExportMeteoGridToRasterFlt(QString fileName, double cellSize, meteoVariable myVar, QDate dateIni, QDate dateFin); int computeDefaultCellSizeFromMeteoGrid(float resolutionRatio); - void setComputeOnlyPoints(bool isOnlyPoints); + void setComputeOnlyPoints(bool value); bool getComputeOnlyPoints(); private slots: diff --git a/agrolib/utilities/utilities.cpp b/agrolib/utilities/utilities.cpp index 78b82876b..e1789cc22 100644 --- a/agrolib/utilities/utilities.cpp +++ b/agrolib/utilities/utilities.cpp @@ -580,14 +580,17 @@ QList readListSingleColumn(QString fileName, QString& error) } -QList removeList(QList list, QList toDelete) +QList removeList(const QList &list, QList &toDelete) { + QList newList = list; + QList::iterator i; for (i = toDelete.begin(); i != toDelete.end(); ++i) { - list.removeAll(*i); + newList.removeAll(*i); } - return list; + + return newList; } @@ -615,3 +618,45 @@ void removeOldFiles(const QString &targetPath, const QString &targetStr, int nrD } } } + + +bool parseCSV(const QString &csvFileName, QList &csvFields, QList> &csvData, QString &errorString) +{ + if (csvFileName.isEmpty() || ! QFile(csvFileName).exists() || ! QFileInfo(csvFileName).isFile()) + { + errorString = "Missing file: " + csvFileName; + return false; + } + + QFile myFile(csvFileName); + if (! myFile.open(QIODevice::ReadOnly)) + { + errorString = "Open failed: " + csvFileName + "\n " + myFile.errorString(); + return false; + } + + QTextStream myStream (&myFile); + if (myStream.atEnd()) + { + errorString = "File is void"; + myFile.close(); + return false; + } + else + { + csvFields = myStream.readLine().split(','); + } + + csvData.clear(); + while(! myStream.atEnd()) + { + QList line = myStream.readLine().split(','); + + // skip void lines + if (line.size() <= 1) continue; + csvData.append(line); + } + + myFile.close(); + return true; +} diff --git a/agrolib/utilities/utilities.h b/agrolib/utilities/utilities.h index 47a5c88f0..060b0f826 100644 --- a/agrolib/utilities/utilities.h +++ b/agrolib/utilities/utilities.h @@ -55,8 +55,10 @@ void removeOldFiles(const QString &targetPath, const QString &targetStr, int nrDays); - void clearDir( const QString path ); - QList removeList(QList list, QList toDelete); + void clearDir( const QString path); + QList removeList(const QList &list, QList &toDelete); + + bool parseCSV(const QString &csvFileName, QList &csvFields, QList> &csvData, QString &errorString); #endif // UTILITIES_H