Skip to content

Commit

Permalink
Merge commit 'b6237d7de1f5f310f81558844014120ce6598bc8'
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Sep 5, 2024
2 parents 3350c6e + b6237d7 commit 400d3ea
Show file tree
Hide file tree
Showing 39 changed files with 1,295 additions and 1,510 deletions.
16 changes: 16 additions & 0 deletions agrolib/crit3dDate/crit3dDate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,22 @@ int getDoyFromDate(const Crit3DDate& myDate)
return doy;
}

int getMonthFromDoy(int doy,int year)
{
if (doy <1 || doy > 366) return NODATA;
int month = 0;
int doyMonthSpecific[12];
for (int i=0;i<12;i++)
{
doyMonthSpecific[i] = doyMonth[i+1];
if (isLeapYear(year) && i>0)
doyMonthSpecific[i]++;
}
while (doy > doyMonthSpecific[month])
month++;

return month;
}

static inline long floordiv(long a, long b)
{
Expand Down
1 change: 1 addition & 0 deletions agrolib/crit3dDate/crit3dDate.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
int getDaysInMonth(int month, int year);

int getDoyFromDate(const Crit3DDate& myDate);
int getMonthFromDoy(int doy,int year);
Crit3DDate getDateFromDoy(int year, int doy);

Crit3DDate max(const Crit3DDate& myDate1, const Crit3DDate& myDate2);
Expand Down
38 changes: 19 additions & 19 deletions agrolib/dbMeteoPoints/dbMeteoPointsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ QString Crit3DMeteoPointsDbHandler::getNewDataEntry(int pos, const QList<QString
return "";
}

QString newEntry = "('" + dateTimeStr + "','" + idVarStr + "'," + QString::number(double(value)) + "),";
QString newEntry = "('" + dateTimeStr + "'," + idVarStr + "," + QString::number(double(value)) + "),";
return newEntry;
}

Expand All @@ -1365,31 +1365,31 @@ QString Crit3DMeteoPointsDbHandler::getNewDataEntry(int pos, const QList<QString
\details fixed format:
DATE(yyyy-mm-dd), HOUR, TAVG, PREC, RHAVG, RAD, W_SCAL_INT
*/
bool Crit3DMeteoPointsDbHandler::importHourlyMeteoData(QString csvFileName, bool deletePreviousData, QString* log)
bool Crit3DMeteoPointsDbHandler::importHourlyMeteoData(const QString &csvFileName, bool deletePreviousData, QString &log)
{
QString fileName = getFileName(csvFileName);
*log = "\nInput file: " + fileName;
log = "\nInput file: " + fileName;

// check point code
QString pointCode = fileName.left(fileName.length()-4);
if (! existIdPoint(pointCode))
{
*log += "\nID " + pointCode + " is not present in the point properties table.";
log += "\nID " + pointCode + " is not present in the point properties table.";
return false;
}

// check input file
QFile myFile(csvFileName);
if(! myFile.open (QIODevice::ReadOnly))
{
*log += myFile.errorString();
log += myFile.errorString();
return false;
}

QTextStream myStream (&myFile);
if (myStream.atEnd())
{
*log += "\nFile is void.";
log += "\nFile is void.";
myFile.close();
return false;
}
Expand All @@ -1403,7 +1403,7 @@ bool Crit3DMeteoPointsDbHandler::importHourlyMeteoData(QString csvFileName, bool
QString tableName = pointCode + "_H";
if (! createTable(tableName, deletePreviousData))
{
*log += "\nError in create table: " + tableName + _db.lastError().text();
log += "\nError in create table: " + tableName + _db.lastError().text();
myFile.close();
return false;
}
Expand All @@ -1422,9 +1422,9 @@ bool Crit3DMeteoPointsDbHandler::importHourlyMeteoData(QString csvFileName, bool
int nrWrongDateTime = 0;
int nrWrongData = 0;
int nrMissingData = 0;
QString queryStr = "INSERT INTO " + tableName + " VALUES";
QString queryStr = "INSERT INTO '" + tableName + "' VALUES";

while(!myStream.atEnd())
while(! myStream.atEnd())
{
line = myStream.readLine().split(',');

Expand All @@ -1435,17 +1435,17 @@ bool Crit3DMeteoPointsDbHandler::importHourlyMeteoData(QString csvFileName, bool
currentDate = QDate::fromString(line.at(0),"yyyy-MM-dd");
if (! currentDate.isValid())
{
*log += "\nWrong dateTime: " + line.at(0) + " h" + line.at(1);
log += "\nWrong dateTime: " + line.at(0) + " h" + line.at(1);
nrWrongDateTime++;
continue;
}

// 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);
log += "\nWrong dateTime: " + line.at(0) + " h" + line.at(1);
nrWrongDateTime++;
continue;
}
Expand All @@ -1460,7 +1460,7 @@ bool Crit3DMeteoPointsDbHandler::importHourlyMeteoData(QString csvFileName, bool
if ((currentDate < previousDate) ||
(currentDate == previousDate && hour <= previousHour))
{
*log += "\nDuplicate dateTime: " + dateTimeStr;
log += "\nDuplicate dateTime: " + dateTimeStr;
nrWrongDateTime++;
continue;
}
Expand All @@ -1485,16 +1485,16 @@ bool Crit3DMeteoPointsDbHandler::importHourlyMeteoData(QString csvFileName, bool
qry.prepare(queryStr);
if (! qry.exec())
{
*log += "\nError in execute query: " + qry.lastError().text() +"\n";
*log += "Maybe there are missing or wrong data values.";
log += "\nError in execute query: " + qry.lastError().text() +"\n";
log += "Maybe there are missing or wrong data values.";
return false;
}
}

*log += "\nData imported successfully.";
*log += "\nWrong date/time: " + QString::number(nrWrongDateTime);
*log += "\nMissing data: " + QString::number(nrMissingData);
*log += "\nWrong values: " + QString::number(nrWrongData);
log += "\nData imported successfully.";
log += "\nWrong date/time: " + QString::number(nrWrongDateTime);
log += "\nMissing data: " + QString::number(nrMissingData);
log += "\nWrong values: " + QString::number(nrWrongData);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion agrolib/dbMeteoPoints/dbMeteoPointsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
QString getNewDataEntry(int pos, const QList<QString>& dataStr, const QString& dateTimeStr,
const QString& idVarStr, meteoVariable myVar,
int* nrMissingData, int* nrWrongData, Crit3DQuality* dataQuality);
bool importHourlyMeteoData(QString fileNameComplete, bool deletePreviousData, QString *log);
bool importHourlyMeteoData(const QString &fileNameComplete, bool deletePreviousData, QString &log);

bool writeDailyDataList(const QString &pointCode, const QList<QString> &listEntries, QString& log);
bool writeHourlyDataList(const QString &pointCode, const QList<QString> &listEntries, QString& log);
Expand Down
123 changes: 0 additions & 123 deletions agrolib/gis/gis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,33 +285,6 @@ namespace gis
return initializeGrid(initValue);
}

bool Crit3DRasterGrid::initializeParameters(const Crit3DRasterHeader& initHeader)
{
singleCell.clear();
singleCell.resize(initHeader.nrRows*initHeader.nrCols);
for (int i = 0; i < int(singleCell.size()); i++)
{
singleCell[i].row = i / initHeader.nrCols;
singleCell[i].col = i % initHeader.nrCols;
singleCell[i].fittingParameters.clear();
}

return true;
}

bool Crit3DRasterGrid::initializeParametersLatLonHeader(const Crit3DLatLonHeader& latLonHeader)
{
singleCell.resize(latLonHeader.nrRows*latLonHeader.nrCols);
for (int i = 0; i < int(singleCell.size()); i++)
{
singleCell[i].row = i / latLonHeader.nrCols;
singleCell[i].col = i % latLonHeader.nrCols;
singleCell[i].fittingParameters.clear();
}

return true;
}


bool Crit3DRasterGrid::copyGrid(const Crit3DRasterGrid& initGrid)
{
Expand Down Expand Up @@ -485,103 +458,7 @@ namespace gis
return getValueFromRowCol(row, col);
}

std::vector<std::vector<double>> Crit3DRasterGrid::getParametersFromRowCol(int row, int col)
{
std::vector<std::vector<double>> parameters;
parameters.clear();

if(isOutOfGrid(row, col))
{
return parameters;
}

int index = row * header->nrCols + col;

if (index < int(singleCell.size()))
parameters = singleCell[index].fittingParameters;

return parameters;


}

bool Crit3DRasterGrid::setParametersForRowCol(int row, int col, std::vector<std::vector<double>> parameters)
{
if (isOutOfGrid(row, col) || parameters.empty())
return false;

int index = row * header->nrCols + col;
singleCell[index].fittingParameters = parameters;

return true;
}

std::vector<std::vector<double>> Crit3DRasterGrid::prepareParameters(int row, int col, std::vector<bool> activeList)
{
std::vector<std::vector<double>> tempProxyVector;
tempProxyVector.clear();
tempProxyVector.resize(activeList.size());
int l, m, k, p;
l = 0;
m = 0;
std::vector<double> avg;
int counter, index;
bool findFirst = 0;

if (isOutOfGrid(row, col))
return tempProxyVector;

for (unsigned int i = 0; i < activeList.size(); i++)
{
findFirst = 0;
if (activeList[i])
{
//look for the first cell that has data for that proxy. if there isn't any, return empty vector
for (l = row-1; l < row+2; l++)
{
for (m = col-1; m < col+2; m++)
{
index = l * header->nrCols + m;
if (index >= 0 && index < int(singleCell.size()) && (singleCell[index].fittingParameters.size() > i && !singleCell[index].fittingParameters[i].empty()) && (l != row || m !=col)) {
findFirst = 1;
}
if (findFirst==1) break;
}
if (findFirst==1) break;
}

if (findFirst == 0)
continue;

//you're on a specific proxy rn. cycle through the cells, calculate the avg
avg.clear();
avg.resize(singleCell[index].fittingParameters[i].size());
counter = 0;

for (k = l; k < row+2; k++)
{
for (p = m; p < col+2; p++)
{
index = k * header->nrCols + p;
if (index >= 0 && index < int(singleCell.size()) && singleCell[index].fittingParameters.size() > i && !singleCell[index].fittingParameters[i].empty()) {
for (unsigned int o = 0; o < avg.size(); o++)
{
avg[o] += singleCell[index].fittingParameters[i][o];

}
counter++;
}
}
}
for (unsigned int o = 0; o < avg.size(); o++)
avg[o] /= counter;

tempProxyVector[i] = avg;
}
}

return tempProxyVector;
}

void convertFlagToNodata(Crit3DRasterGrid& myGrid)
{
Expand Down
14 changes: 0 additions & 14 deletions agrolib/gis/gis.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,6 @@
Crit3DRasterCell();
};

struct RasterGridCell {
public:
int row;
int col;
std::vector<std::vector<double>> fittingParameters;
};

class Crit3DRasterGrid
{
public:
Expand All @@ -155,7 +148,6 @@
float minimum, maximum;
bool isLoaded;
Crit3DTime mapTime;
std::vector<RasterGridCell> singleCell;

Crit3DUtmPoint* utmPoint(int myRow, int myCol);
void getXY(int myRow, int myCol, double &x, double &y) const;
Expand All @@ -178,9 +170,6 @@
bool initializeGrid(const Crit3DLatLonHeader& latLonHeader);
bool initializeGrid(const Crit3DRasterGrid& initGrid, float initValue);

bool initializeParameters(const Crit3DRasterHeader &initHeader);
bool initializeParametersLatLonHeader(const Crit3DLatLonHeader& latLonHeader);

bool copyGrid(const Crit3DRasterGrid& initGrid);

bool setConstantValueWithBase(float initValue, const Crit3DRasterGrid& initGrid);
Expand All @@ -189,9 +178,6 @@
bool isFlag(int myRow, int myCol) const;
float getValueFromRowCol(int myRow, int myCol) const;
float getValueFromXY(double x, double y) const;
std::vector<std::vector<double>> getParametersFromRowCol(int row, int col);
bool setParametersForRowCol(int row, int col, std::vector<std::vector<double>> parameters);
std::vector<std::vector<double>> prepareParameters(int row, int col, std::vector<bool> activeList);

Crit3DTime getMapTime() const;
void setMapTime(const Crit3DTime &value);
Expand Down
Loading

0 comments on commit 400d3ea

Please sign in to comment.