Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Aug 21, 2024
1 parent 4410284 commit c9fa07c
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 104 deletions.
21 changes: 1 addition & 20 deletions crit3dDate/crit3dDate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ Crit3DDate getDateFromJulianDay(long julianDay)
}


std::string Crit3DDate::toStdString()
std::string Crit3DDate::toISOString() const
{
char myStr[11];
sprintf (myStr, "%d-%02d-%02d", this->year, this->month, this->day);
Expand All @@ -312,29 +312,10 @@ std::string Crit3DDate::toStdString()
}


std::string Crit3DDate::toStdString() const
{
char myStr[11];
sprintf (myStr, "%d-%02d-%02d", this->year, this->month, this->day);

return std::string(myStr);
}


std::string Crit3DDate::toString()
{
char myStr[9];
sprintf (myStr, "%d%02d%02d", this->year, this->month, this->day);

return std::string(myStr);
}


std::string Crit3DDate::toString() const
{
char myStr[9];
sprintf (myStr, "%d%02d%02d", this->year, this->month, this->day);

return std::string(myStr);
}

11 changes: 6 additions & 5 deletions crit3dDate/crit3dDate.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@
void setDate(int myDay, int myMonth, int myYear);
bool isNullDate();
void setNullDate();

Crit3DDate addDays(long offset) const;
int daysTo(const Crit3DDate& myDate) const;
std::string toStdString();
std::string toStdString() const;
std::string toString();

std::string toISOString() const;

std::string toString() const;
};

Expand Down Expand Up @@ -81,8 +82,8 @@
int getMinutes() const;
int getSeconds() const;

std::string toISOString();
std::string toString();
std::string toISOString() const;
std::string toString() const;
};

bool isLeapYear(int year);
Expand Down
1 change: 0 additions & 1 deletion crit3dDate/crit3dDate.pro
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ CONFIG += staticlib
DEFINES += _CRT_SECURE_NO_WARNINGS

CONFIG += debug_and_release
CONFIG += c++11 c++14 c++17

unix:{
CONFIG(debug, debug|release) {
Expand Down
4 changes: 2 additions & 2 deletions crit3dDate/crit3dTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Crit3DTime Crit3DTime::addSeconds(long mySeconds) const
}


std::string Crit3DTime::toISOString()
std::string Crit3DTime::toISOString() const
{
char myStr[17];
sprintf (myStr, "%d-%02d-%02d %02d:%02d", this->date.year, this->date.month, this->date.day, this->getHour(), this->getMinutes());
Expand All @@ -144,7 +144,7 @@ std::string Crit3DTime::toISOString()
}


std::string Crit3DTime::toString()
std::string Crit3DTime::toString() const
{
char myStr[13];
sprintf (myStr, "%d%02d%02dT%02d%02d", this->date.year, this->date.month, this->date.day, this->getHour(), this->getMinutes());
Expand Down
2 changes: 1 addition & 1 deletion crop/crop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ bool Crit3DCrop::dailyUpdate(const Crit3DDate &myDate, double latitude, const st
double dailyDD = getDailyDegreeIncrease(tmin, tmax, currentDoy);
if (isEqual(dailyDD, NODATA))
{
myError = "Error in computing degree days for " + myDate.toStdString();
myError = "Error in computing degree days for " + myDate.toISOString();
return false;
}
degreeDays += dailyDD;
Expand Down
119 changes: 61 additions & 58 deletions dbMeteoPoints/dbMeteoPointsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,12 @@ QDateTime Crit3DMeteoPointsDbHandler::getLastDate(frequencyType frequency, std::

bool Crit3DMeteoPointsDbHandler::existData(Crit3DMeteoPoint *meteoPoint, frequencyType myFreq)
{
QSqlQuery myQuery(_db);
QSqlQuery query(_db);
QString tableName = QString::fromStdString(meteoPoint->id) + ((myFreq == daily) ? "_D" : "_H");
QString statement = QString( "SELECT 1 FROM `%1`").arg(tableName);

if (myQuery.exec(statement))
if (myQuery.next())
if (query.exec(statement))
if (query.next())
return true;

return false;
Expand Down Expand Up @@ -517,7 +517,7 @@ bool Crit3DMeteoPointsDbHandler::deleteAllPointsFromDataset(QList<QString> datas
}


bool Crit3DMeteoPointsDbHandler::loadDailyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint *meteoPoint)
bool Crit3DMeteoPointsDbHandler::loadDailyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint &meteoPoint)
{
// check dates
if (firstDate > lastDate)
Expand All @@ -527,11 +527,11 @@ bool Crit3DMeteoPointsDbHandler::loadDailyData(const Crit3DDate &firstDate, cons
}

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

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

QString statement;
if (numberOfDays == 1)
Expand All @@ -544,31 +544,34 @@ bool Crit3DMeteoPointsDbHandler::loadDailyData(const Crit3DDate &firstDate, cons
.arg(tableName, firstDateStr, lastDateStr);
}

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

int idVar = myQuery.value(1).toInt();
meteoVariable variable = _mapIdMeteoVar.at(idVar);
while (query.next())
{
// date
QString dateStr = query.value(0).toString();
QDate d = QDate::fromString(dateStr, "yyyy-MM-dd");

float value = myQuery.value(2).toFloat();
// variable
int idVar = query.value(1).toInt();
meteoVariable variable = _mapIdMeteoVar.at(idVar);

meteoPoint->setMeteoPointValueD(Crit3DDate(d.day(), d.month(), d.year()), variable, value);
}
return true;
}
else
{
return false;
// value
float value = query.value(2).toFloat();

meteoPoint.setMeteoPointValueD(Crit3DDate(d.day(), d.month(), d.year()), variable, value);
}

return true;
}


bool Crit3DMeteoPointsDbHandler::loadHourlyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint *meteoPoint)
bool Crit3DMeteoPointsDbHandler::loadHourlyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint &meteoPoint)
{
// check dates
if (firstDate > lastDate)
Expand All @@ -580,11 +583,11 @@ bool Crit3DMeteoPointsDbHandler::loadHourlyData(const Crit3DDate &firstDate, con
// initialize obs data
int numberOfDays = difference(firstDate, lastDate) + 1;
int myHourlyFraction = 1;
meteoPoint->initializeObsDataH(myHourlyFraction, numberOfDays, firstDate);
meteoPoint.initializeObsDataH(myHourlyFraction, numberOfDays, firstDate);

QString startDateStr = QString::fromStdString(firstDate.toStdString());
QString endDateStr = QString::fromStdString(lastDate.toStdString());
QString tableName = QString::fromStdString(meteoPoint->id) + "_H";
QString startDateStr = QString::fromStdString(firstDate.toISOString());
QString endDateStr = QString::fromStdString(lastDate.toISOString());
QString tableName = QString::fromStdString(meteoPoint.id) + "_H";

QString statement = QString( "SELECT * FROM `%1` WHERE date_time >= DATETIME('%2 01:00:00') AND date_time <= DATETIME('%3 00:00:00', '+1 day')")
.arg(tableName, startDateStr, endDateStr);
Expand Down Expand Up @@ -615,13 +618,13 @@ bool Crit3DMeteoPointsDbHandler::loadHourlyData(const Crit3DDate &firstDate, con
if (variable != noMeteoVar)
{
float value = qry.value(2).toFloat();
meteoPoint->setMeteoPointValueH(myDate, d.time().hour(), d.time().minute(), variable, value);
meteoPoint.setMeteoPointValueH(myDate, d.time().hour(), d.time().minute(), variable, value);

// copy scalar intensity to vector intensity (instantaneous values are equivalent, following WMO)
// should be removed when hourly averages are available
if (variable == windScalarIntensity)
{
meteoPoint->setMeteoPointValueH(myDate, d.time().hour(), d.time().minute(), windVectorIntensity, value);
meteoPoint.setMeteoPointValueH(myDate, d.time().hour(), d.time().minute(), windVectorIntensity, value);
}
}
}
Expand All @@ -631,7 +634,8 @@ bool Crit3DMeteoPointsDbHandler::loadHourlyData(const Crit3DDate &firstDate, con
}


std::vector<float> Crit3DMeteoPointsDbHandler::loadDailyVar(QString *myError, meteoVariable variable, Crit3DDate dateStart, Crit3DDate dateEnd, QDate* firstDateDB, Crit3DMeteoPoint *meteoPoint)
std::vector<float> Crit3DMeteoPointsDbHandler::loadDailyVar(meteoVariable variable, const Crit3DDate &dateStart, const Crit3DDate &dateEnd,
const Crit3DMeteoPoint &meteoPoint, QDate &firstDateDB)
{
QString dateStr;
QDate d, previousDate;
Expand All @@ -640,61 +644,60 @@ std::vector<float> Crit3DMeteoPointsDbHandler::loadDailyVar(QString *myError, me
bool firstRow = true;

int idVar = getIdfromMeteoVar(variable);
QString startDate = QString::fromStdString(dateStart.toStdString());
QString endDate = QString::fromStdString(dateEnd.toStdString());
QString startDate = QString::fromStdString(dateStart.toISOString());
QString endDate = QString::fromStdString(dateEnd.toISOString());

QSqlQuery myQuery(_db);
QSqlQuery query(_db);

QString tableName = QString::fromStdString(meteoPoint->id) + "_D";
QString tableName = QString::fromStdString(meteoPoint.id) + "_D";

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

if( !myQuery.exec(statement) )
if(! query.exec(statement))
{
*myError = myQuery.lastError().text();
errorStr = query.lastError().text();
return dailyVarList;
}
else
{
while (myQuery.next())
while (query.next())
{
if (firstRow)
{
dateStr = myQuery.value(0).toString();
*firstDateDB = QDate::fromString(dateStr, "yyyy-MM-dd");
previousDate = *firstDateDB;
dateStr = query.value(0).toString();
firstDateDB = QDate::fromString(dateStr, "yyyy-MM-dd");
previousDate = firstDateDB;

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

dailyVarList.push_back(value);

firstRow = false;
}
else
{
dateStr = myQuery.value(0).toString();
dateStr = query.value(0).toString();
d = QDate::fromString(dateStr, "yyyy-MM-dd");

int missingDate = previousDate.daysTo(d);
for (int i =1; i<missingDate; i++)
for (int i=1; i < missingDate; i++)
{
dailyVarList.push_back(NODATA);
}

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

dailyVarList.push_back(value);
previousDate = d;

}

}
}

return dailyVarList;
}


std::vector<float> Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError, frequencyType freq, meteoVariable variable, QString id, QDateTime myFirstTime, QDateTime myLastTime, std::vector<QString> &dateStr)
{
QString myDateStr;
Expand All @@ -706,7 +709,7 @@ std::vector<float> Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError
int idVar = getIdfromMeteoVar(variable);


QSqlQuery myQuery(_db);
QSqlQuery query(_db);
QString tableName;
QString startDate;
QString endDate;
Expand Down Expand Up @@ -735,18 +738,18 @@ std::vector<float> Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError
return allDataVarList;
}

if( !myQuery.exec(statement) )
if( !query.exec(statement) )
{
*myError = myQuery.lastError().text();
*myError = query.lastError().text();
return allDataVarList;
}
else
{
while (myQuery.next())
while (query.next())
{
if (freq == daily)
{
if (! getValue(myQuery.value(0), &date))
if (! getValue(query.value(0), &date))
{
*myError = "Missing fieldTime";
return allDataVarList;
Expand All @@ -755,7 +758,7 @@ std::vector<float> Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError
}
else if (freq == hourly)
{
if (! getValue(myQuery.value(0), &dateTime))
if (! getValue(query.value(0), &dateTime))
{
*myError = "Missing fieldTime";
return allDataVarList;
Expand All @@ -764,7 +767,7 @@ std::vector<float> Crit3DMeteoPointsDbHandler::exportAllDataVar(QString *myError
myDateStr = dateTime.date().toString("yyyy-MM-dd") + " " + dateTime.time().toString("hh:mm");
}
dateStr.push_back(myDateStr);
value = myQuery.value(2).toFloat();
value = query.value(2).toFloat();
allDataVarList.push_back(value);
}
}
Expand All @@ -783,8 +786,8 @@ std::vector<float> Crit3DMeteoPointsDbHandler::loadHourlyVar(QString *myError, m
bool firstRow = true;

int idVar = getIdfromMeteoVar(variable);
QString startDate = QString::fromStdString(dateStart.toStdString());
QString endDate = QString::fromStdString(dateEnd.toStdString());
QString startDate = QString::fromStdString(dateStart.toISOString());
QString endDate = QString::fromStdString(dateEnd.toISOString());

QSqlQuery qry(_db);

Expand Down
12 changes: 7 additions & 5 deletions dbMeteoPoints/dbMeteoPointsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@
const gis::Crit3DGisSettings& gisSettings, QString& errorString);
bool getPropertiesGivenId(QString id, Crit3DMeteoPoint* meteoPoint,
const gis::Crit3DGisSettings& gisSettings, QString& errorString);
bool loadDailyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint *meteoPoint);
std::vector<float> loadDailyVar(QString *myError, meteoVariable variable,
Crit3DDate dateStart, Crit3DDate dateEnd,
QDate* firstDateDB, Crit3DMeteoPoint *meteoPoint);

bool loadDailyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint &meteoPoint);

std::vector<float> loadDailyVar(meteoVariable variable, const Crit3DDate &dateStart, const Crit3DDate &dateEnd,
const Crit3DMeteoPoint &meteoPoint, QDate &firstDateDB);

std::vector<float> exportAllDataVar(QString *myError, frequencyType freq, meteoVariable variable, QString id, QDateTime myFirstTime, QDateTime myLastTime, std::vector<QString> &dateStr);

bool loadHourlyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint *meteoPoint);
bool loadHourlyData(const Crit3DDate &firstDate, const Crit3DDate &lastDate, Crit3DMeteoPoint &meteoPoint);

std::vector<float> loadHourlyVar(QString *myError, meteoVariable variable,
Crit3DDate dateStart, Crit3DDate dateEnd,
Expand Down
Loading

0 comments on commit c9fa07c

Please sign in to comment.