Skip to content

Commit

Permalink
Grid Sample accept historic date
Browse files Browse the repository at this point in the history
  • Loading branch information
juan0101 committed Aug 25, 2020
1 parent 9f4a4ae commit 809409c
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 7 deletions.
166 changes: 166 additions & 0 deletions src/terrama2/core/utility/FilterUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

//Terralib
#include <terralib/datatype/TimeInstant.h>

// Qt
#include <string>
#include <QDate>
Expand All @@ -46,6 +49,8 @@
#include <QRegExp>
#include <QStringList>

#include <time.h>

// TODO: This method is being call for every check of the mask, this should only be called once to avoid a costy operation.
std::string terrama2::core::terramaMask2Regex(const std::string& mask)
{
Expand Down Expand Up @@ -182,6 +187,167 @@ std::shared_ptr<te::dt::TimeInstantTZ> terrama2::core::getFileTimestamp(const st
return timeStamp;
}

int terrama2::core::getMaxDay(int month, int year) {
if(month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11)
return 31;
else if(month == 3 || month == 5 || month == 8 || month == 10)
return 30;
else {
if(year % 4 == 0) {
if(year % 100 == 0) {
if(year % 400 == 0)
return 29;
return 28;
}
return 29;
}
return 28;
}
}

std::shared_ptr<te::dt::TimeInstantTZ> terrama2::core::gridSampleVerifyMask(std::string mask, std::shared_ptr<te::dt::TimeInstantTZ> actualDate)
{
std::shared_ptr<te::dt::TimeInstantTZ> dateReturn;
auto temp = QString::fromStdString(mask);
auto list = temp.split("%", QString::SkipEmptyParts);

time_t theTime = time(NULL);
struct tm *aTime = localtime(&theTime);

int day = aTime->tm_mday;
int month = aTime->tm_mon + 1; // Month is 0 - 11, add 1 to get a jan-dec 1-12 concept
int year = aTime->tm_year + 1900; // Year is # years since 1900

std::vector<std::string> options;
options.push_back("YYYY");
options.push_back("MM");
options.push_back("DD");
options.push_back("hh");
options.push_back("mm");
options.push_back("ss");

int optIndice = 0;

std::map<std::string, std::string> mapDate;

for(int i = 0; i < list.size(); ++i)
{
auto& parts = list[i];
auto listParts = parts.split("$", QString::SkipEmptyParts);

auto& part = listParts[0];
std::string partStr = part.toUtf8().constData();

auto& partContent = listParts[1];
std::string partContentStr = partContent.toUtf8().constData();

for(int y=optIndice; y < options.size(); ++y)
{
if(partStr.compare(options[y]) == 0)
{
switch (y) {
case 0:
mapDate.insert({"year", partContentStr});
break;
case 1:
mapDate.insert({"month", partContentStr});
break;
case 2:
mapDate.insert({"day", partContentStr});
break;
case 3:
mapDate.insert({"hour", partContentStr});
break;
case 4:
mapDate.insert({"minute", partContentStr});
break;
case 5:
mapDate.insert({"second", partContentStr});
break;
default:
std::cout << "DEFAULT" << std::endl;
}

optIndice++;
if((i+1) == list.size())
{
continue;
}
break;
}
else
{
int yearAux = 0;
int monthAux = 0;
switch (y) {
case 0:
mapDate.insert({"year", std::to_string(year)});
break;
case 1:
if(month < 10)
{
mapDate.insert({"month", "0"+std::to_string(month)});
}
else
{
mapDate.insert({"month", std::to_string(month)});
}
break;
case 2:
yearAux = std::stoi(mapDate.find("year")->second);
monthAux = std::stoi(mapDate.find("month")->second) - 1;
day = getMaxDay(monthAux,yearAux);
mapDate.insert({"day", std::to_string(day)});
break;
case 3:
mapDate.insert({"hour", "00"});
break;
case 4:
mapDate.insert({"minute", "00"});
break;
case 5:
mapDate.insert({"second", "00"});
break;
default:
std::cout << "DEFAULT" << std::endl;
}
optIndice++;
}
}
}

if(!mapDate.empty())
{

auto mapyear = mapDate.find("year");
auto mapmonth = mapDate.find("month");
auto mapday = mapDate.find("day");
auto maphour = mapDate.find("hour");
auto mapminute = mapDate.find("minute");
auto mapsecond = mapDate.find("second");

std::string finalDate = mapyear->second+"-";
finalDate = finalDate + mapmonth->second+"-";
finalDate = finalDate + mapday->second;
finalDate = finalDate + "T";
finalDate = finalDate + maphour->second+":";
finalDate = finalDate + mapminute->second+":";
finalDate = finalDate + mapsecond->second;
finalDate = finalDate + "-00";

dateReturn = terrama2::core::TimeUtils::stringToTimestamp(finalDate, terrama2::core::TimeUtils::webgui_timefacet);

return dateReturn;
}
else
{
QString errMsg = QObject::tr("The mask don't have the minimal needed parameters of a date!");
TERRAMA2_LOG_ERROR() << errMsg;
throw terrama2::core::UtilityException() << ErrorDescription(errMsg);
}
return nullptr;
}

bool terrama2::core::isValidDataSetName(const std::string& mask,
const Filter& filter,
const std::string& timezone,
Expand Down
4 changes: 4 additions & 0 deletions src/terrama2/core/utility/FilterUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ namespace terrama2
* @return Regex style mask
*/
TMCOREEXPORT std::string terramaMask2Regex(const std::string& mask);

TMCOREEXPORT std::shared_ptr<te::dt::TimeInstantTZ> gridSampleVerifyMask(std::string mask, std::shared_ptr<te::dt::TimeInstantTZ> actualDate);

TMCOREEXPORT int getMaxDay(int month, int year);
} // end namespace core
} // end namespace terrama2

Expand Down
19 changes: 16 additions & 3 deletions src/terrama2/services/analysis/core/grid/Operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "../ContextManager.hpp"
#include "../../../../core/data-model/DataSetGrid.hpp"
#include "../../../../core/utility/Logger.hpp"
#include "../../../../core/utility/FilterUtils.hpp"
#include "../utility/Utils.hpp"
#include "../utility/Verify.hpp"
#include "../GridContext.hpp"
Expand Down Expand Up @@ -59,12 +60,12 @@ double terrama2::services::analysis::core::grid::getValue(std::shared_ptr<te::rs
return value;
}

double terrama2::services::analysis::core::grid::sample(const std::string& dataSeriesName, size_t bandIdx)
double terrama2::services::analysis::core::grid::sample(const std::string& dataSeriesName, size_t bandIdx, const std::string& date)
{
OperatorCache cache;
terrama2::services::analysis::core::python::readInfoFromDict(cache);
auto& contextManager = ContextManager::getInstance();
auto analysis = cache.analysisPtr;
auto analysis = cache.analysisPtr;

try
{
Expand Down Expand Up @@ -113,12 +114,24 @@ double terrama2::services::analysis::core::grid::sample(const std::string& dataS
{
auto startTime = context->getStartTime();

filter.discardAfter = startTime;
if(date.compare("") != 0)
{
filter.discardAfter = terrama2::core::gridSampleVerifyMask(date, context->getStartTime());
}
else
{
filter.discardAfter = startTime;
}

filter.lastValues = std::make_shared<std::size_t>(1);
filter.isReprocessingHistoricalData = true;
}
else
{
if(date.compare("") != 0)
{
filter.discardAfter = terrama2::core::gridSampleVerifyMask(date, context->getStartTime());
}
filter.lastValues = std::make_shared<size_t>(1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/terrama2/services/analysis/core/grid/Operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace terrama2
\param dataSeriesName DataSeries name.
\return The current pixel value for the selected data series.
*/
TMANALYSISEXPORT double sample(const std::string& dataSeriesName, size_t bandIdx = 0);
TMANALYSISEXPORT double sample(const std::string& dataSeriesName, size_t bandIdx = 0, const std::string& date="");

TMANALYSISEXPORT double getValue(std::shared_ptr<te::rst::Raster> raster, std::shared_ptr<terrama2::core::SynchronizedInterpolator> interpolator, double column, double row, size_t bandIdx);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void terrama2::services::analysis::core::python::Grid::registerFunctions()
#pragma GCC diagnostic ignored "-Wunused-local-typedef"

// // Declaration needed for default parameter restriction
BOOST_PYTHON_FUNCTION_OVERLOADS(gridSample_overloads, terrama2::services::analysis::core::grid::sample, 1, 2)
BOOST_PYTHON_FUNCTION_OVERLOADS(gridSample_overloads, terrama2::services::analysis::core::grid::sample, 1, 3)

// closing "-Wunused-local-typedef" pragma
#pragma GCC diagnostic pop
Expand All @@ -81,7 +81,7 @@ void terrama2::services::analysis::core::python::Grid::registerGridFunctions()
scope gridScope = gridModule;

// export functions inside grid namespace
def("sample", terrama2::services::analysis::core::grid::sample, gridSample_overloads(args("dataSeriesName", "band"), "Grid sample operator."));
def("sample", terrama2::services::analysis::core::grid::sample, gridSample_overloads(args("dataSeriesName", "band", "date"), "Grid sample operator."));
}

// pragma to silence python macros warnings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
"name": "Sample",
"description": "Retrieves a cell value based in interpolation method to grid analysis",
"code": "grid.sample(\"<dynamic_data_grid>\", [<band>])"
"code": "grid.sample(\"<dynamic_data_grid>\", [<band>], [\"<historical_date>\"])"
}
]

0 comments on commit 809409c

Please sign in to comment.