Skip to content

Commit

Permalink
export daily data grid
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Jan 24, 2024
1 parent b274c82 commit 0777dc1
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 115 deletions.
114 changes: 36 additions & 78 deletions agrolib/dbMeteoGrid/dbMeteoGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "meteoGrid.h"
#include "basicMath.h"
#include "utilities.h"
#include "meteoPoint.h"
#include "commonConstants.h"

#include <iostream>
Expand Down Expand Up @@ -3632,25 +3633,30 @@ bool Crit3DMeteoGridDbHandler::saveLogProcedures(QString *myError, QString nameP
/*!
* \brief ExportDailyDataCsv
* export gridded daily meteo data to csv files
* \param isTPrec export variables: Tmin, Tmax, Tavg, Prec
* \param variable export specific variable
* \param idListFileName filename of cells id list (list by columns)
* if idListFile == "" save ALL cells
* \param variableList list of meteo variables
* \param idListFileName text file of cells id list by columns - if idListFileName is empty save ALL cells
* \param outputPath path for output files
* \return true on success, false otherwise
*/
bool Crit3DMeteoGridDbHandler::exportDailyDataCsv(QString &errorStr, bool isTPrec, meteoVariable variable,
bool Crit3DMeteoGridDbHandler::exportDailyDataCsv(QString &errorStr, QList<meteoVariable> variableList,
QDate firstDate, QDate lastDate, QString idListFileName, QString outputPath)
{
errorStr = "";

// check output path
if (outputPath == "")
{
errorStr = "Missing output path.";
return false;
}

QDir outDir(outputPath);
// make directory
if (! outDir.exists())
{
if (! outDir.mkpath(outputPath))
{
errorStr = "Wrong outputPath, unable to create this directory: " + outputPath;
errorStr = "Wrong output path, unable to create directory: " + outputPath;
return false;
}
}
Expand All @@ -3662,7 +3668,7 @@ bool Crit3DMeteoGridDbHandler::exportDailyDataCsv(QString &errorStr, bool isTPre
{
if (! QFile::exists(idListFileName))
{
errorStr = "The ID list file does not exist: " + idListFileName;
errorStr = "The ID list does not exist: " + idListFileName;
return false;
}

Expand All @@ -3672,7 +3678,7 @@ bool Crit3DMeteoGridDbHandler::exportDailyDataCsv(QString &errorStr, bool isTPre

if (idList.size() == 0)
{
errorStr = "The ID list file is empty: " + idListFileName;
errorStr = "The ID list is empty: " + idListFileName;
return false;
}
}
Expand Down Expand Up @@ -3710,89 +3716,41 @@ bool Crit3DMeteoGridDbHandler::exportDailyDataCsv(QString &errorStr, bool isTPre
continue;
}

// header
// write header
QTextStream out(&outputFile);
if (variable != noMeteoVar)
{
std::string varName = getMeteoVarName(variable);
std::string unit = getUnitFromVariable(variable);
QString VarField = QString::fromStdString(varName + " (" + unit + ")");
out << "Date, " + VarField + "\n";
}
else
out << "Date";
for (int i = 0; i < variableList.size(); i++)
{
out << "Date, Tmin (C), Tmax (C), Tavg (C), Prec (mm)";
if (isTPrec)
out << "\n";
else
out << "RHmin (%), RHmax (%), RHavg (%), Rad (MJ)\n";
if (variableList[i] != noMeteoVar)
{
std::string varName = getMeteoVarName(variableList[i]);
std::string unit = getUnitFromVariable(variableList[i]);
QString VarString = QString::fromStdString(varName + " (" + unit + ")");
out << ", " + VarString;
}
}
out << "\n";

// save data
// write data
QDate currentDate = firstDate;
while (currentDate <= lastDate)
{
Crit3DDate myDate = getCrit3DDate(currentDate);
out << currentDate.toString("yyyy-MM-dd");

if (variable != noMeteoVar)
for (int i = 0; i < variableList.size(); i++)
{
float value = _meteoGrid->meteoPointPointer(row,col)->getMeteoPointValueD(myDate, variable);
QString varStr = "";
if (value != NODATA)
varStr = QString::number(value);

out << currentDate.toString("yyyy-MM-dd") << "," << varStr <<"\n";
}
else
{
float tmin = _meteoGrid->meteoPointPointer(row,col)->getMeteoPointValueD(myDate, dailyAirTemperatureMin);
QString tminStr = "";
if (tmin != NODATA)
tminStr = QString::number(tmin);

float tmax = _meteoGrid->meteoPointPointer(row,col)->getMeteoPointValueD(myDate, dailyAirTemperatureMax);
QString tmaxStr = "";
if (tmax != NODATA)
tmaxStr = QString::number(tmax);

float tavg = _meteoGrid->meteoPointPointer(row,col)->getMeteoPointValueD(myDate, dailyAirTemperatureAvg);
QString tavgStr = "";
if (tavg != NODATA)
tavgStr = QString::number(tavg);

float prec = _meteoGrid->meteoPointPointer(row,col)->getMeteoPointValueD(myDate, dailyPrecipitation);
QString precStr = "";
if (prec != NODATA)
precStr = QString::number(prec);

out << currentDate.toString("yyyy-MM-dd") << "," << tminStr << "," << tmaxStr << "," << tavgStr << "," << precStr;
if (isTPrec)
out << "\n";
else
if (variableList[i] != noMeteoVar)
{
float rhmin = _meteoGrid->meteoPointPointer(row,col)->getMeteoPointValueD(myDate, dailyAirRelHumidityMin);
QString rhminStr = "";
if (rhmin != NODATA)
rhminStr = QString::number(rhmin);

float rhmax = _meteoGrid->meteoPointPointer(row,col)->getMeteoPointValueD(myDate, dailyAirRelHumidityMax);
QString rhmaxStr = "";
if (rhmax != NODATA)
rhmaxStr = QString::number(rhmax);

float rhavg = _meteoGrid->meteoPointPointer(row,col)->getMeteoPointValueD(myDate, dailyAirRelHumidityAvg);
QString rhavgStr = "";
if (rhavg != NODATA)
rhavgStr = QString::number(rhavg);

float rad = _meteoGrid->meteoPointPointer(row,col)->getMeteoPointValueD(myDate, dailyGlobalRadiation);
QString radStr = "";
if (rad != NODATA)
radStr = QString::number(rad);

out << "," << rhminStr << "," << rhmaxStr << "," << rhavgStr << "," << "," << radStr << "\n";
float value = _meteoGrid->meteoPointPointer(row,col)->getMeteoPointValueD(myDate, variableList[i]);
QString valueString = "";
if (value != NODATA)
valueString = QString::number(value);

out << "," << valueString;
}
}
out << "\n";

currentDate = currentDate.addDays(1);
}
Expand Down
2 changes: 1 addition & 1 deletion agrolib/dbMeteoGrid/dbMeteoGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
bool activeAllCells(QString *myError);
bool setActiveStateCellsInList(QString *myError, QList<QString> idList, bool activeState);

bool exportDailyDataCsv(QString &errorStr, bool isTPrec, meteoVariable variable,
bool exportDailyDataCsv(QString &errorStr, QList<meteoVariable> variableList,
QDate firstDate, QDate lastDate, QString idListFileName, QString outputPath);
bool MeteoGridToRasterFlt(double cellSize, const gis::Crit3DGisSettings &gisSettings, gis::Crit3DRasterGrid& myGrid);

Expand Down
2 changes: 1 addition & 1 deletion agrolib/meteo/meteo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ frequencyType getVarFrequency(meteoVariable myVar)
return noFrequency;
}


meteoVariable getMeteoVar(std::string varString)
{
auto search = MapDailyMeteoVar.find(varString);
Expand All @@ -1031,7 +1032,6 @@ meteoVariable getMeteoVar(std::string varString)
return search->second;
}
}

}

return noMeteoVar;
Expand Down
12 changes: 6 additions & 6 deletions agrolib/meteo/meteo.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@
};

const std::map<std::vector<meteoVariable>, std::string> MapVarUnit = {
{ {dailyAirTemperatureMin,airTemperature,monthlyAirTemperatureMin}, "°C"} ,
{ {dailyAirTemperatureMax,monthlyAirTemperatureMax}, "°C"} ,
{ {dailyAirTemperatureAvg,dailyAirTemperatureRange,monthlyAirTemperatureAvg}, "°C"} ,
{ {dailyAirTemperatureMin,airTemperature,monthlyAirTemperatureMin}, "C"} ,
{ {dailyAirTemperatureMax,monthlyAirTemperatureMax}, "C"} ,
{ {dailyAirTemperatureAvg,dailyAirTemperatureRange,monthlyAirTemperatureAvg}, "C"} ,
{ {dailyPrecipitation,precipitation,monthlyPrecipitation}, "mm"} ,
{ {dailyReferenceEvapotranspirationHS,dailyReferenceEvapotranspirationPM,referenceEvapotranspiration,monthlyReferenceEvapotranspirationHS}, "mm"} ,
{ {dailyAirRelHumidityMin,dailyAirRelHumidityMax,dailyAirRelHumidityAvg,airRelHumidity}, "%"} ,
Expand All @@ -263,12 +263,12 @@
{ {dailyWindVectorDirectionPrevailing, dailyWindVectorIntensityMax, windVectorDirection}, "deg"} ,
{ {windVectorIntensity, windVectorX, windVectorY}, "m s-1"} ,
{ {dailyLeafWetness,leafWetness}, "h"} ,
{ {dailyHeatingDegreeDays,dailyCoolingDegreeDays}, "°D"} ,
{ {dailyHeatingDegreeDays,dailyCoolingDegreeDays}, "D"} ,
{ {airRelHumidity,dailyAirRelHumidityMin,dailyAirRelHumidityMax,dailyAirRelHumidityAvg}, "%"} ,
{ {airDewTemperature}, "°C"} ,
{ {airDewTemperature}, "C"} ,
{ {dailyThomAvg,dailyThomDaytime,dailyThomNighttime,thom}, "-"} ,
{ {dailyWaterTableDepth,snowWaterEquivalent,snowFall,snowMelt,snowLiquidWaterContent}, "mm"} ,
{ {snowSurfaceTemperature}, "°C"} ,
{ {snowSurfaceTemperature}, "C"} ,
{ {snowInternalEnergy,snowSurfaceEnergy,sensibleHeat,latentHeat}, "kJ m-2"} ,
};

Expand Down
57 changes: 32 additions & 25 deletions agrolib/project/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,37 +283,49 @@ int cmdExportDailyDataCsv(Project* myProject, QList<QString> argumentList)
if (argumentList.size() < 2)
{
QString usage = "Usage:\n"
"ExportDailyDataCsv [-TPREC] [-v:varname] [-t:type] -d1:firstDate [-d2:lastDate] [-l:idList] [-p:outputPath]\n"
"-TPREC save Tmin, Tmax, Tavg, Prec (default: ALL variables) \n"
"-v save single Variable (varname: TMIN, TMAX, TAVG, PREC, RHMIN, RHMAX, RHAVG, RAD, ET0_HS, ET0_PM, LEAFW) \n"
"-t Type: GRID|POINTS (default: GRID) \n"
"-d1, -d2 Date format: YYYY-MM-DD (default: lastDate = yesterday) \n"
"-l List of output points or cells filename (default: ALL active cells/points) \n"
"ExportDailyDataCsv -v:variableList [-TPREC] [-t:type] -d1:firstDate [-d2:lastDate] [-l:idList] [-p:outputPath]\n"
"-v list of comma separated variables (varname: TMIN, TMAX, TAVG, PREC, RHMIN, RHMAX, RHAVG, RAD, ET0_HS, ET0_PM, LEAFW) \n"
"-TPREC export Tmin, Tmax, Tavg, Prec \n"
"-t type: GRID|POINTS (default: GRID) \n"
"-d1, -d2 date format: YYYY-MM-DD (default: lastDate = yesterday) \n"
"-l list of output points or cells filename (default: ALL active cells/points) \n"
"-p output Path (default: " + outputPath + ") \n";
myProject->logInfo(usage);
return PRAGA_OK;
}

QString typeStr = "GRID";
QString idListFileName = "";
QDate firstDate, lastDate;
QList<meteoVariable> variableList;
bool isTPrec = false;
QString varName = "";
meteoVariable variable = noMeteoVar;
QString idListFileName = "";

for (int i = 1; i < argumentList.size(); i++)
{
if (argumentList.at(i).left(6).toUpper() == "-TPREC")
{
isTPrec = true;
variableList = {dailyAirTemperatureMin, dailyAirTemperatureMax, dailyAirTemperatureAvg, dailyPrecipitation};
}

// variables list
if (argumentList.at(i).left(3) == "-v:")
{
varName = "DAILY_" + argumentList[i].right(argumentList[i].length()-3).toUpper();
variable = getMeteoVar(varName.toStdString());
if (variable == noMeteoVar)
QString variables = argumentList[i].right(argumentList[i].length()-3).toUpper();
QList<QString> varNameList = variables.split(",");
for (int i = 0; i < varNameList.size(); i++)
{
myProject->logError("Wrong variable: " + varName);
return PRAGA_OK;
std::string varString = "DAILY_" + varNameList[i].toStdString();
meteoVariable var = getMeteoVar(varString);
if (var != noMeteoVar)
{
variableList.append(var);
}
else
{
myProject->logError("Wrong variable: " + varNameList[i]);
return PRAGA_OK;
}
}
}

Expand Down Expand Up @@ -388,17 +400,11 @@ int cmdExportDailyDataCsv(Project* myProject, QList<QString> argumentList)
myProject->logInfo("... first date is: " + firstDate.toString());
myProject->logInfo("... last date is: " + lastDate.toString());

if (isTPrec)
{
myProject->logInfo("... output format is: Date, Tmin (C), Tmax (C), Tavg (C), Prec (mm)");
}
else if(variable != noMeteoVar)
{
myProject->logInfo("... output format is: Date, " + varName);
}
else

if (variableList.isEmpty())
{
myProject->logInfo("... export ALL variables");
myProject->logError("Missing variables.");
return PRAGA_OK;
}

if (idListFileName != "")
Expand All @@ -412,6 +418,7 @@ int cmdExportDailyDataCsv(Project* myProject, QList<QString> argumentList)
else
myProject->logInfo("... export ALL meteo points");
}

myProject->logInfo("... output path is: " + outputPath);

if (typeStr == "GRID")
Expand All @@ -422,7 +429,7 @@ int cmdExportDailyDataCsv(Project* myProject, QList<QString> argumentList)
return PRAGA_ERROR;
}

if (! myProject->meteoGridDbHandler->exportDailyDataCsv(myProject->errorString, isTPrec, variable,
if (! myProject->meteoGridDbHandler->exportDailyDataCsv(myProject->errorString, variableList,
firstDate, lastDate, idListFileName, outputPath))
{
myProject->logError();
Expand Down
27 changes: 23 additions & 4 deletions src/mainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5906,12 +5906,31 @@ void MainWindow::on_actionFileMeteogrid_ExportDailyData_triggered()
return;
}

QList<QString> varList = exportDialog.getDailyVariableList();
// variables list
QList<QString> varNameList = exportDialog.getDailyVariableList();
QList<meteoVariable> variableList;
for (int i = 0; i < varNameList.size(); i++)
{
meteoVariable var = getMeteoVar(varNameList[i].toStdString());
if (var != noMeteoVar)
{
variableList.append(var);
}
}

QDate firstDate = exportDialog.getFirstDate();
QDate lastDate = exportDialog.getLastDate();
// cell list
// output path

// TO DO
// TODO
QString cellListFileName = myProject.getProjectPath() + PATH_OUTPUT + "/elenco.txt";
QString outputPath = myProject.getProjectPath() + PATH_OUTPUT;

if (! myProject.meteoGridDbHandler->exportDailyDataCsv(myProject.errorString, variableList,
firstDate, lastDate, cellListFileName, outputPath) )
{
myProject.logError();
}

myProject.logInfoGUI("Files exported to the directory: " + outputPath);
}

0 comments on commit 0777dc1

Please sign in to comment.