otbDates.h 3.61 KB
Newer Older
1
2
/*=========================================================================

3
  Program:   TimeSeriesUtils
4
5
6
7
8
9
10
11
12
13
14
  Language:  C++

  Copyright (c) Remi Cresson (IRSTEA). All rights reserved.

  See LICENSE for details.

  This software is distributed WITHOUT ANY WARRANTY; without even
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
15
16
17
18
19
20
21

#ifndef MODULES_REMOTE_TIMESERIESUTILS_INCLUDE_OTBDATES_H_
#define MODULES_REMOTE_TIMESERIESUTILS_INCLUDE_OTBDATES_H_


#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/math/distributions/students_t.hpp"
22
23
24
25
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
26
#include <vector>
27
28
29
30
31
using std::vector;
#include <iterator>
using std::istream_iterator;
#include <algorithm>
using std::copy;
32
#include "itkMacro.h"
33
34
35
36
37
38
39
40
41
42

namespace otb
{

namespace dates
{

/*
 * \class SingleDate
 *
43
44
 * \brief Class to handle dates. Julian day is computed at instantiation. Uses boost.
 * \see boost::gregorian::date
45
 *
46
 * \ingroup TimeSeriesUtils
47
48
49
50
51
 */
class SingleDate
{
public:
  SingleDate(int yyyy, int mm, int dd){
52
53
54
    year = yyyy;
    day = dd;
    month = mm;
55

56
57
58
    // Compute julianday
    boost::gregorian::date d(yyyy, mm, dd);
    julianday = d.julian_day();
59
60
61
62
63
64
65
66
  }

  ~SingleDate (){}

  bool IsInRange(const int& startMonth,const int& startDay,const int& endMonth, const int& endDay) const
  {
    if (startMonth < month && month < endMonth)
      {
67
        return true;
68
69
70
      }
    else if (startMonth == month && startDay <= day)
      {
71
        return true;
72
73
74
      }
    else if (endMonth == month && day <= endDay)
      {
75
        return true;
76
77
78
      }
    else
      {
79
        return false;
80
81
82
83
84
85
86
87
88
      }
  }

  int day;
  int month;
  int year;
  int julianday;
};

89
90
typedef std::vector<SingleDate> DatesType;

91
/*
92
 * Retrieve the vector of SingleDate from the input vector of strings.
93
 * Input string must be in the form of "YYYYMMDD ....."
94
 */
95
DatesType GetDatesFromStringVector(std::vector<std::string> & list, bool check_order = true){
96

97
  DatesType dates;
98
99
100

  for (unsigned int i = 0 ; i < list.size() ; i++)
    {
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

      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 (...)
          {
              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);
        }
122
123
124
125
126
    }

  // Check dates ordering
  if (check_order)
    {
127
      for (unsigned int i = 0 ; i < dates.size()-1 ; i++)
128
        {
129
130
131
132
          if (dates[i+1].julianday <= dates[i].julianday)
            {
              itkGenericExceptionMacro("Date " << i << " is older than date " << (i+1) << " !");
            }
133
        }
134
135
136
137
138
139
    }

  // return output
  return dates;
}

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * 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);

}


158
159
160
161
162
} // namespace dates

} // namespace otb

#endif /* MODULES_REMOTE_TIMESERIESUTILS_INCLUDE_OTBDATES_H_ */