Commit b005bced authored by remi cresson's avatar remi cresson
Browse files

ADD: New function to read ASCII file containing dates in the YYYYMMDD format

parent aa85649b
...@@ -19,7 +19,16 @@ ...@@ -19,7 +19,16 @@
#include "boost/date_time/gregorian/gregorian.hpp" #include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/math/distributions/students_t.hpp" #include "boost/math/distributions/students_t.hpp"
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
#include <vector> #include <vector>
using std::vector;
#include <iterator>
using std::istream_iterator;
#include <algorithm>
using std::copy;
#include "itkMacro.h" #include "itkMacro.h"
namespace otb namespace otb
...@@ -40,13 +49,13 @@ class SingleDate ...@@ -40,13 +49,13 @@ class SingleDate
{ {
public: public:
SingleDate(int yyyy, int mm, int dd){ SingleDate(int yyyy, int mm, int dd){
year = yyyy; year = yyyy;
day = dd; day = dd;
month = mm; month = mm;
// Compute julianday // Compute julianday
boost::gregorian::date d(yyyy, mm, dd); boost::gregorian::date d(yyyy, mm, dd);
julianday = d.julian_day(); julianday = d.julian_day();
} }
~SingleDate (){} ~SingleDate (){}
...@@ -55,19 +64,19 @@ public: ...@@ -55,19 +64,19 @@ public:
{ {
if (startMonth < month && month < endMonth) if (startMonth < month && month < endMonth)
{ {
return true; return true;
} }
else if (startMonth == month && startDay <= day) else if (startMonth == month && startDay <= day)
{ {
return true; return true;
} }
else if (endMonth == month && day <= endDay) else if (endMonth == month && day <= endDay)
{ {
return true; return true;
} }
else else
{ {
return false; return false;
} }
} }
...@@ -81,45 +90,71 @@ typedef std::vector<SingleDate> DatesType; ...@@ -81,45 +90,71 @@ typedef std::vector<SingleDate> DatesType;
/* /*
* Retrieve the vector of SingleDate from the input vector of strings. * Retrieve the vector of SingleDate from the input vector of strings.
* Input string must be in the form of "dd/mm/yyyy....." (separator "/" can * Input string must be in the form of "YYYYMMDD ....."
* be replaced by any character)
*/ */
DatesType GetDatesFromStringVector(std::vector<std::string> list, bool check_order = true){ DatesType GetDatesFromStringVector(std::vector<std::string> & list, bool check_order = true){
DatesType dates; DatesType dates;
for (unsigned int i = 0 ; i < list.size() ; i++) for (unsigned int i = 0 ; i < list.size() ; i++)
{ {
if (list[i].size()<=10)
{ if (list[i].size() >= 8) // YYYYMMDD
int yyyy = std::stoi( list[i].substr(0,4) ); {
int mm = std::stoi( list[i].substr(5,2) ); int yyyy, mm, dd;
int dd = std::stoi( list[i].substr(8,2) ); try
{
// Add new date yyyy = std::stoi( list[i].substr(0,4) );
// The boost::gregorian::date(yyyy, mm, dd) takes care of mm = std::stoi( list[i].substr(4,2) );
// checking the date validity dd = std::stoi( list[i].substr(6,2) );
SingleDate d(yyyy, mm, dd); }
dates.push_back(d); catch (...)
} {
itkGenericExceptionMacro("Dates format must be YYYYMMDD");
}
// Add new date
// The boost::gregorian::date(yyyy, mm, dd) takes care of
// checking the date validity
SingleDate d(yyyy, mm, dd);
dates.push_back(d);
}
} }
// Check dates ordering // Check dates ordering
if (check_order) if (check_order)
{ {
for (unsigned int i = 0 ; i < list.size()-1 ; i++) for (unsigned int i = 0 ; i < dates.size()-1 ; i++)
{
if (dates[i+1].julianday <= dates[i].julianday)
{ {
itkGenericExceptionMacro("Date " << i << " is older than date " << (i+1) << " !"); if (dates[i+1].julianday <= dates[i].julianday)
{
itkGenericExceptionMacro("Date " << i << " is older than date " << (i+1) << " !");
}
} }
}
} }
// return output // return output
return dates; return dates;
} }
/*
* Retrieve the vector of dates from an ASCII file.
* Format of dates must be YYYYMMDD
*/
DatesType GetDatesFromFile(std::string filename)
{
std::vector<std::string> list;
ifstream file(filename);
std::copy(std::istream_iterator<std::string>(file),
std::istream_iterator<std::string>(),
std::back_inserter(list));
return GetDatesFromStringVector(list);
}
} // namespace dates } // namespace dates
} // namespace otb } // namespace otb
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment