Blame view

app/otbProfileReprocessing.cxx 5.68 KB
5bc36567   Jordi Inglada   ADD: missing file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*=========================================================================
  Program:   otb-bv
  Language:  C++

  Copyright (c) CESBIO. All rights reserved.

  See otb-bv-copyright.txt 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.

=========================================================================*/

#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"

#include <vector>

a38d739b   Jordi Inglada   ENH: added CSDM f...
20
#include "phenoFunctions.h"
9293e052   Jordi Inglada   ENH: move reproce...
21
#include "otbProfileReprocessing.h"
5bc36567   Jordi Inglada   ADD: missing file
22
23
24
25
26

namespace otb
{
int date_to_doy(std::string& date_str)
{
78c493ab   Jordi Inglada   COMP: make it com...
27
  return GapFilling::doy(GapFilling::make_date(date_str));
5bc36567   Jordi Inglada   ADD: missing file
28
29
}

5bc36567   Jordi Inglada   ADD: missing file
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

namespace Wrapper
{

class ProfileReprocessing : public Application
{
public:
  /** Standard class typedefs. */
  typedef ProfileReprocessing               Self;
  typedef Application                   Superclass;
  typedef itk::SmartPointer<Self>       Pointer;
  typedef itk::SmartPointer<const Self> ConstPointer;

  /** Standard macro */
  itkNewMacro(Self);

  itkTypeMacro(ProfileReprocessing, otb::Application);

private:
7a253812   Jordi Inglada   ENH: override spe...
49
  void DoInit() override
5bc36567   Jordi Inglada   ADD: missing file
50
51
52
53
  {

    SetName("ProfileReprocessing");
    SetDescription("Reprocess a BV time profile.");
2187f38b   Jordi Inglada   ENH: use new app ...
54
    SetDocLink("http://tully.ups-tlse.fr/jordi/otb-bv#tab-readme");
5bc36567   Jordi Inglada   ADD: missing file
55
56
57
58
59
60
61
62
63
   
    AddParameter(ParameterType_InputFilename, "ipf", "Input profile file.");
    SetParameterDescription( "ipf", "Input file containing the profile to process. This is an ASCII file where each line contains the date (YYYMMDD) the BV estimation and the error." );
    MandatoryOn("ipf");

    AddParameter(ParameterType_OutputFilename, "opf", "Output profile file.");
    SetParameterDescription( "opf", "Filename where the reprocessed profile saved. This is an ASCII file where each line contains the date (YYYMMDD) the new BV estimation and a boolean information which is 0 if the value has not been reprocessed." );
    MandatoryOn("opf");

c4031bc5   Jordi Inglada   ENH: added local ...
64
65
    AddParameter(ParameterType_Choice, "algo", 
                 "Reprocessing algorithm: local, fit.");
5bc36567   Jordi Inglada   ADD: missing file
66
    SetParameterDescription("algo", 
c4031bc5   Jordi Inglada   ENH: added local ...
67
68
                            "Reprocessing algorithm: local uses a window around the current date, fit is a double logisting fitting of the complete profile.");

64554f8e   Jordi Inglada   BUG: forgot to se...
69
70
71
    AddChoice("algo.fit", "Double logistic fitting of the complete profile.");
    SetParameterDescription("algo.fit", "This group of parameters allows to set fit window parameters. ");

c4031bc5   Jordi Inglada   ENH: added local ...
72
73
74
75
76
77
78
79
80
81
82
    AddChoice("algo.local", "Uses a window around the current date.");
    SetParameterDescription("algo.local", "This group of parameters allows to set local window parameters. ");

    AddParameter(ParameterType_Int, "algo.local.bwr", "Local window backward radius");
    SetParameterInt("algo.local.bwr", 1);
    SetParameterDescription("algo.local.bwr", "Backward radius of the local window. ");

    AddParameter(ParameterType_Int, "algo.local.fwr", "Local window forward radius");
    SetParameterInt("algo.local.fwr", 1);
    SetParameterDescription("algo.local.fwr", "Forward radius of the local window. ");

5bc36567   Jordi Inglada   ADD: missing file
83
    MandatoryOff("algo");
5bc36567   Jordi Inglada   ADD: missing file
84
85
  }

7a253812   Jordi Inglada   ENH: override spe...
86
  void DoUpdateParameters() override
5bc36567   Jordi Inglada   ADD: missing file
87
88
89
90
  {
    //std::cout << "ProfileReprocessing::DoUpdateParameters" << std::endl;
  }

7a253812   Jordi Inglada   ENH: override spe...
91
  void DoExecute() override
5bc36567   Jordi Inglada   ADD: missing file
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  {
    auto ipfn = GetParameterString("ipf");
    std::ifstream in_profile_file;
    try
      {
      in_profile_file.open(ipfn.c_str());
      }
    catch(...)
      {
      itkGenericExceptionMacro(<< "Could not open file " 
                               << ipfn);
      }

    auto nb_dates = 0;
    std::vector<std::string> date_str_vec{};
    VectorType date_vec{};
    VectorType bv_vec{};
    VectorType err_vec{};
    for(std::string line; std::getline(in_profile_file, line); )
      {
      if(line.size() > 1)
        {
        std::istringstream ss(line);
        std::string date_str;
        PrecisionType in_bv;
        PrecisionType bv_err;

        ss >> date_str;
        ss >> in_bv;
        ss >> bv_err;

        date_str_vec.push_back(date_str);
        date_vec.push_back(date_to_doy(date_str));
        bv_vec.push_back(in_bv);
        err_vec.push_back(bv_err);

        nb_dates++;
        }
      }
    otbAppLogINFO("Input profile contains " << nb_dates << " dates.\n");

    in_profile_file.close();

    VectorType out_bv_vec{};
    VectorType out_flag_vec{};

c4031bc5   Jordi Inglada   ENH: added local ...
138
    std::string algo{"local"};
a38d739b   Jordi Inglada   ENH: added CSDM f...
139
140
    if (IsParameterEnabled("algo"))
      algo = GetParameterString("algo");    
c4031bc5   Jordi Inglada   ENH: added local ...
141
142
143
144
145
146
147
    if (algo == "local")
      {
      size_t bwr{1};
      size_t fwr{1};
      if (IsParameterEnabled("algo.local.bwr"))
        bwr = GetParameterInt("algo.local.bwr");
      if (IsParameterEnabled("algo.local.fwr"))
746019c4   Jordi Inglada   BUG: assigning th...
148
        fwr = GetParameterInt("algo.local.fwr");
a38d739b   Jordi Inglada   ENH: added CSDM f...
149
      std::tie(out_bv_vec, out_flag_vec) = 
c4031bc5   Jordi Inglada   ENH: added local ...
150
151
152
        smooth_time_series_local_window_with_error(date_vec, bv_vec, err_vec, 
                                                   bwr, fwr);
      }
a38d739b   Jordi Inglada   ENH: added CSDM f...
153
154
    else if (algo == "fit")
      std::tie(out_bv_vec, out_flag_vec) = 
42cf5628   Jordi Inglada   BUG: unused param...
155
        fit_csdm(date_vec, bv_vec);
a38d739b   Jordi Inglada   ENH: added CSDM f...
156
157
    else
      itkGenericExceptionMacro(<< "Unknown algorithm " << algo 
c4031bc5   Jordi Inglada   ENH: added local ...
158
                               << ". Available algorithms are: local, fit.\n");
5bc36567   Jordi Inglada   ADD: missing file
159
160
161
162
163
164
165
166
167
168
169

    auto opfn = GetParameterString("opf");
    std::ofstream out_profile_file;
    try
      {
      out_profile_file.open(opfn.c_str());
      }
    catch(...)
      {
      itkGenericExceptionMacro(<< "Could not open file " << opfn);
      }
952673f6   Jordi Inglada   WAR: signed compa...
170
    for(size_t i=0; i<date_vec.size(); ++i)
5bc36567   Jordi Inglada   ADD: missing file
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
      {
      std::stringstream ss;
      ss << date_str_vec[i] << "\t" << out_bv_vec[i] << "\t" 
         << out_flag_vec[i] << std::endl;
      out_profile_file << ss.str();
      }

    out_profile_file.close();


  }
};
}
}

OTB_APPLICATION_EXPORT(otb::Wrapper::ProfileReprocessing)