otbReduceSpectralResponse.cxx 7.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 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.
 */
20

21

22 23 24 25 26
#include "otbMacro.h"

#include "otbSatelliteRSR.h"
#include "otbReduceSpectralResponse.h"

27 28
typedef otb::SpectralResponse<double, double> ResponseType;
typedef ResponseType::Pointer ResponsePointerType;
29 30
typedef ResponseType::VectorPairType SpectrumType;
typedef ResponseType::PairType PairType;
31 32 33 34 35
typedef otb::SatelliteRSR<double, double> SatRSRType;
typedef SatRSRType::Pointer SatRSRPointerType;
typedef otb::ReduceSpectralResponse<ResponseType, SatRSRType> ReduceResponseType;
typedef ReduceResponseType::Pointer ReduceResponseTypePointerType;

36 37
int otbReduceSpectralResponse(int argc, char * argv[])
{
38
  if ((argc != 5) && (argc != 6))
39 40 41
    {
    std::cout << argv[0] << std::endl << "\t" << "<Spectral_response_filename>";
    std::cout << "\t" << "<RSR_filename>" << "\t" << "<Nb total satellite band>";
42
    std::cout << "\t" << "<reflectance mode>" << "\t" << "(<Output_filename>)" << std::endl;
43
    return EXIT_FAILURE;
44
    }
45

46 47 48
  const std::string spectreFile(argv[1]);
  const std::string RSRfile(argv[2]);
  unsigned int nbBand = atoi(argv[3]);
49
  bool reflectanceMode(atoi(argv[4]));
50
  //Instantiation
51
  ResponsePointerType mySpectralResponse = ResponseType::New();
52 53
  /** Load the spectral response file*/
  mySpectralResponse->Load(spectreFile, 100.0);
54
  SatRSRPointerType myRSR = SatRSRType::New();
55
  /** Set the satellite number of bands */
56
  myRSR->SetNbBands(nbBand);
57
  /** Load the satellite response file*/
58 59
  myRSR->Load(RSRfile);
  //Instantiation
60
  ReduceResponseTypePointerType myReduceResponse = ReduceResponseType::New();
61
  /** Load the satellite response in the simulator */
62 63 64
  myReduceResponse->SetInputSatRSR(myRSR);
  /** Load the spectral response of the object in the simulator*/
  myReduceResponse->SetInputSpectralResponse(mySpectralResponse);
65
  /** Set the reflectance or radiance mode */
66
  myReduceResponse->SetReflectanceMode(reflectanceMode);
67 68 69
  myReduceResponse->CalculateResponse();
  /** Print the Reduce SR*/
  std::cout << myReduceResponse << std::endl;
70
  if (argc == 6)
71
    {
72
    char * outputName = argv[5];
73 74 75 76
    std::ofstream outputFile(outputName, std::ios::out);
    outputFile << myReduceResponse << std::endl;
    }
  else std::cout << myReduceResponse << std::endl;
77 78
  return EXIT_SUCCESS;
}
79 80 81 82 83 84 85 86

bool check_spectral_response(ResponsePointerType spectralResponse,
                             SpectrumType::const_iterator first,
                             SpectrumType::const_iterator last,
                             ResponseType::ValuePrecisionType tolerance)
{
  while( first != last )
    {
87 88
    ResponseType::ValuePrecisionType expected = (*first).second;
    ResponseType::ValuePrecisionType actual = (*spectralResponse)((*first).first);
89 90 91 92 93 94 95 96
    if(fabs(expected - actual) > tolerance) return false;
    ++first;
    }
  return true;
}

int otbReduceSpectralResponseSimpleValues(int argc, char * argv[])
{
97
  if (argc != 3)
98 99
    {
    std::cout << argv[0] << "\t <temporary spectral response filename>";
100
    std::cout << "\t <reflectance mode>";
101 102 103 104 105 106 107 108 109 110 111 112 113 114
    std::cout << std::endl;
    return EXIT_FAILURE;
    }

  // let's create a spectral response
  const ResponseType::PrecisionType lambdaMin(0.4);
  const ResponseType::PrecisionType lambdaMax(1.4);
  const ResponseType::PrecisionType lambdaStep(0.001);
  ResponseType::PrecisionType currentLambda(lambdaMin);
  SpectrumType spectrum;
  while(currentLambda < lambdaMax)
    {
    PairType spectralValue;
    spectralValue.first = currentLambda;
115
    spectralValue.second = ResponseType::ValuePrecisionType(1.0);
116 117 118 119 120 121 122
    spectrum.push_back(spectralValue);
    currentLambda += lambdaStep;
    }
  ResponsePointerType spectralResponse = ResponseType::New();
  spectralResponse->SetResponse(spectrum);

  // Check that the spectral response is OK
123
  const ResponseType::ValuePrecisionType tolerance = 10e-6;
124 125 126 127 128 129 130 131 132 133 134
  if(!check_spectral_response(spectralResponse, spectrum.begin(),
                              spectrum.end(), tolerance))
    {
    std::cout << "Error detected in stored spectral response (without guess)"
              << std::endl;
    return EXIT_FAILURE;
    }
  spectralResponse->SetUsePosGuess(true);
  if(!check_spectral_response(spectralResponse, spectrum.begin(),
                              spectrum.end(), tolerance))
    {
135
    std::cout << "Error detected in stored spectral response (with guess)"
136 137 138 139 140 141 142
              << std::endl;
    return EXIT_FAILURE;
    }
 
  // Let's create a satellite RSR with 2 bands and constant solar irradiance per band
  // Disjoint bands and B0 is shorter wavelength than B1 for simplicity
  const std::string RSRfileName(argv[1]);
143
  std::ofstream satFile(RSRfileName.c_str());
144 145 146 147
  const ResponseType::PrecisionType lambdaMinB0(0.45);
  const ResponseType::PrecisionType lambdaMaxB0(0.65);
  const ResponseType::PrecisionType lambdaMinB1(0.75);
  const ResponseType::PrecisionType lambdaMaxB1(0.85);
148
  const ResponseType::ValuePrecisionType rsrValueB0(0.5);
149 150 151 152 153 154
  const ResponseType::ValuePrecisionType rsrValueB1(1.0);
  const ResponseType::ValuePrecisionType solarIrradianceB0(2.0);
  const ResponseType::ValuePrecisionType solarIrradianceB1(4.0);
  currentLambda = lambdaMin;
  while(currentLambda < lambdaMax)
    {
Jordi Inglada's avatar
Jordi Inglada committed
155
    ResponseType::ValuePrecisionType solarIrradiance(0);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    ResponseType::ValuePrecisionType rsrB0(0);
    ResponseType::ValuePrecisionType rsrB1(0);
    // B0
    if(currentLambda >= lambdaMinB0 && currentLambda <= lambdaMaxB0)
      {
      solarIrradiance = solarIrradianceB0;
      rsrB0 = rsrValueB0;
      }
    // B1
    if(currentLambda >= lambdaMinB1 && currentLambda <= lambdaMaxB1)
      {
      solarIrradiance = solarIrradianceB1;
      rsrB1 = rsrValueB1;
      }
    satFile.setf( std::ios::fixed, std:: ios::floatfield );
    satFile.precision(4);
    satFile << currentLambda << "\t"
           << solarIrradiance << "\t"
           << rsrB0 << "\t"
           << rsrB1 << "\t" << std::endl;
    currentLambda += lambdaStep;
    }
  satFile.close();

  SatRSRPointerType myRSR = SatRSRType::New();
  myRSR->SetNbBands(2);
  myRSR->Load(RSRfileName);

  ReduceResponseTypePointerType myReduceResponse = ReduceResponseType::New();
  myReduceResponse->SetInputSatRSR(myRSR);
  myReduceResponse->SetInputSpectralResponse(spectralResponse);
187
  myReduceResponse->SetReflectanceMode(static_cast<bool>(atoi(argv[2])));
188 189 190 191 192
  myReduceResponse->CalculateResponse();

  //check B0
  ResponseType::ValuePrecisionType b0Result = (*myReduceResponse)(0);
  ResponseType::PrecisionType centralLambda = 0.5*(lambdaMaxB0+lambdaMinB0);
193
  ResponseType::ValuePrecisionType b0Expected = ((*spectralResponse)(centralLambda));
194 195 196

  if(fabs(b0Result-b0Expected)>tolerance)
    {
197
    std::cout << "Wrong value for B0: expected eq. radiance = " << b0Expected
198 199 200 201 202 203 204 205
              << "; got " << b0Result
              << std::endl;
    return EXIT_FAILURE;
    }

  //check B1
  ResponseType::ValuePrecisionType b1Result = (*myReduceResponse)(1);
  centralLambda = 0.5*(lambdaMaxB1+lambdaMinB1);
206
  ResponseType::ValuePrecisionType b1Expected = ((*spectralResponse)(centralLambda));
207 208 209

  if(fabs(b1Result-b1Expected)>tolerance)
    {
210
    std::cout << "Wrong value for B1: expected eq. radiance = " << b1Expected
211 212 213 214 215 216
              << "; got " << b1Result
              << std::endl;
    return EXIT_FAILURE;
    }
  return EXIT_SUCCESS;
}