/*
 * Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "otbEnvelopeSavitzkyGolayInterpolationFunctor.h"
#include "otbTimeSeries.h"

int otbEnvelopeSavitzkyGolayInterpolationFunctorTest(int itkNotUsed(argc), char* itkNotUsed(argv)[])
{

  typedef float        PixelType;
  typedef unsigned int DoYType;
  const unsigned int   nbDates = 100;
  typedef itk::FixedArray<PixelType, nbDates> SeriesType;
  typedef itk::FixedArray<DoYType, nbDates>   DatesType;
  const unsigned int Radius = 2;


  // Create a time series
  SeriesType inSeries;
  SeriesType cleanSeries;
  SeriesType weightSeries;
  DatesType  doySeries;

  for (unsigned int i = 0; i < nbDates; ++i)
  {
    inSeries[i]     = 10 * std::cos(i / 10.0);
    cleanSeries[i]  = inSeries[i];
    doySeries[i]    = i;
    weightSeries[i] = 1;
  }

  // Set artifacts
  inSeries[nbDates / 4] = 0.0;
  inSeries[nbDates / 2] = 0.0;

  weightSeries[nbDates / 4] = 100000.0;
  weightSeries[nbDates / 2] = 100000.0;

  typedef otb::Functor::EnvelopeSavitzkyGolayInterpolationFunctor<Radius, SeriesType, DatesType, SeriesType> FunctorType;

  FunctorType f;
  f.SetWeights(weightSeries);
  f.SetDates(doySeries);
  f.SetUpperEnvelope();
  f.SetDecreaseFactor(0.9);
  f.SetIterations(4);

  SeriesType outSeries = f(inSeries);

  double interpolError = 0.0;

  for (unsigned int i = 0; i < nbDates; ++i)
  {

    interpolError += std::fabs(outSeries[i] - cleanSeries[i]);
  }

  interpolError /= nbDates;


  if (interpolError > 0.1)
  {
    std::cout << "Interpolation error = " << interpolError << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}
