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 @@
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/math/distributions/students_t.hpp"
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <iterator>
using std::istream_iterator;
#include <algorithm>
using std::copy;
#include "itkMacro.h"
namespace otb
......@@ -81,20 +90,28 @@ typedef std::vector<SingleDate> DatesType;
/*
* Retrieve the vector of SingleDate from the input vector of strings.
* Input string must be in the form of "dd/mm/yyyy....." (separator "/" can
* be replaced by any character)
* Input string must be in the form of "YYYYMMDD ....."
*/
DatesType GetDatesFromStringVector(std::vector<std::string> list, bool check_order = true){
DatesType GetDatesFromStringVector(std::vector<std::string> & list, bool check_order = true){
DatesType dates;
for (unsigned int i = 0 ; i < list.size() ; i++)
{
if (list[i].size()<=10)
if (list[i].size() >= 8) // YYYYMMDD
{
int yyyy, mm, dd;
try
{
yyyy = std::stoi( list[i].substr(0,4) );
mm = std::stoi( list[i].substr(4,2) );
dd = std::stoi( list[i].substr(6,2) );
}
catch (...)
{
int yyyy = std::stoi( list[i].substr(0,4) );
int mm = std::stoi( list[i].substr(5,2) );
int dd = std::stoi( list[i].substr(8,2) );
itkGenericExceptionMacro("Dates format must be YYYYMMDD");
}
// Add new date
// The boost::gregorian::date(yyyy, mm, dd) takes care of
......@@ -107,7 +124,7 @@ DatesType GetDatesFromStringVector(std::vector<std::string> list, bool check_ord
// Check dates ordering
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)
{
......@@ -120,6 +137,24 @@ DatesType GetDatesFromStringVector(std::vector<std::string> list, bool check_ord
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 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