OTB  9.0.0
Orfeo Toolbox
otbReflectanceToSurfaceReflectanceImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbReflectanceToSurfaceReflectanceImageFilter_hxx
22 #define otbReflectanceToSurfaceReflectanceImageFilter_hxx
23 
25 
26 namespace otb
27 {
28 
32 template <class TInputImage, class TOutputImage>
34  : m_IsSetAtmosphericRadiativeTerms(false), m_IsSetAtmoCorrectionParameters(false), m_IsSetAcquiCorrectionParameters(false), m_UseGenerateParameters(true)
35 {
39 }
41 
42 template <class TInputImage, class TOutputImage>
44 {
45  Superclass::BeforeThreadedGenerateData();
46  if (m_UseGenerateParameters)
47  this->GenerateParameters();
48 }
49 
50 
51 template <class TInputImage, class TOutputImage>
53 {
54  Superclass::Modified();
55  m_FunctorParametersHaveBeenComputed = false;
56 }
57 
58 
59 template <class TInputImage, class TOutputImage>
61 {
62  if (!m_IsSetAtmosphericRadiativeTerms)
63  {
64  this->UpdateAtmosphericRadiativeTerms();
65  m_IsSetAtmosphericRadiativeTerms = true;
66  }
67 
68  if (!m_FunctorParametersHaveBeenComputed)
69  {
70  this->UpdateFunctors();
71  m_FunctorParametersHaveBeenComputed = true;
72  }
73 }
74 
75 
76 template <class TInputImage, class TOutputImage>
78 {
79  if (this->GetInput() == nullptr)
80  {
81  itkExceptionMacro(<< "Input must be set before updating the atmospheric radiative terms");
82  }
83 
84  // Atmospheric parameters
85  if (!m_IsSetAtmoCorrectionParameters)
86  {
87  itkExceptionMacro(<< "Atmospheric correction parameters must be provided before updating the atmospheric radiative terms");
88  }
89 
90 
91  // load filter function values
92  bool SetFilterFunctionValuesFileName = false;
93  if (m_AcquiCorrectionParameters->GetFilterFunctionValuesFileName() != "")
94  {
95  m_AcquiCorrectionParameters->LoadFilterFunctionValue();
96  SetFilterFunctionValuesFileName = true;
97  }
98 
99  const auto & metadata = this->GetInput()->GetImageMetadata();
100 
101  if (m_AtmoCorrectionParameters->GetAeronetFileName() != "")
102  {
103  m_AtmoCorrectionParameters->UpdateAeronetData(metadata[MDTime::AcquisitionDate].GetYear(),
104  metadata[MDTime::AcquisitionDate].GetHour(),
105  metadata[MDTime::AcquisitionDate].GetMinute());
106  }
107 
108  // Acquisition parameters
109  if (!m_IsSetAcquiCorrectionParameters) // Get info from image metadata
110  {
111  m_AcquiCorrectionParameters = AcquiCorrectionParametersType::New();
112 
113  m_AcquiCorrectionParameters->SetSolarZenithalAngle(90. - metadata[MDNum::SunElevation]);
114  m_AcquiCorrectionParameters->SetSolarAzimutalAngle(metadata[MDNum::SunAzimuth]);
115  m_AcquiCorrectionParameters->SetViewingZenithalAngle(90. - metadata[MDNum::SatElevation]);
116  m_AcquiCorrectionParameters->SetViewingAzimutalAngle(metadata[MDNum::SatAzimuth]);
117 
118  m_AcquiCorrectionParameters->SetDay(metadata[MDTime::AcquisitionDate].GetDay());
119  m_AcquiCorrectionParameters->SetMonth(metadata[MDTime::AcquisitionDate].GetMonth());
120 
121 
122  if (!SetFilterFunctionValuesFileName)
123  {
124  if (metadata.HasBandMetadata(MDL1D::SpectralSensitivity))
125  {
126  auto spectralSensitivity = AcquiCorrectionParametersType::InternalWavelengthSpectralBandVectorType::New();
127  for (const auto & band : metadata.Bands)
128  {
129  const auto & spectralSensitivityLUT = band[MDL1D::SpectralSensitivity];
130  const auto & axis = spectralSensitivityLUT.Axis[0];
131  auto filterFunction = FilterFunctionValues::New();
132  // LUT1D stores a double vector whereas FilterFunctionValues stores a float vector
133  std::vector<float> vec(spectralSensitivityLUT.Array.begin(), spectralSensitivityLUT.Array.end());
134  filterFunction->SetFilterFunctionValues(vec);
135  filterFunction->SetMinSpectralValue(axis.Origin);
136  filterFunction->SetMaxSpectralValue(axis.Origin + axis.Spacing * (axis.Size-1));
137  filterFunction->SetUserStep(axis.Spacing);
138  spectralSensitivity->PushBack(filterFunction);
139  }
140 
141  m_AcquiCorrectionParameters->SetWavelengthSpectralBand(spectralSensitivity);
142  }
143  else
144  {
145  otbMsgDevMacro(<< "use dummy filter");
146  WavelengthSpectralBandVectorType spectralDummy = AcquiCorrectionParametersType::InternalWavelengthSpectralBandVectorType::New();
147  spectralDummy->Clear();
148  for (unsigned int i = 0; i < this->GetInput()->GetNumberOfComponentsPerPixel(); ++i)
149  {
150  spectralDummy->PushBack(FilterFunctionValuesType::New());
151  }
152  m_AcquiCorrectionParameters->SetWavelengthSpectralBand(spectralDummy);
153  }
154  }
155  }
156 
157 
158  m_AtmosphericRadiativeTerms = CorrectionParametersToRadiativeTermsType::Compute(m_AtmoCorrectionParameters, m_AcquiCorrectionParameters);
159 }
160 
161 template <class TInputImage, class TOutputImage>
163 {
164 
165  if (this->GetInput() == nullptr)
166  {
167  itkExceptionMacro(<< "Input must be set before updating the functors");
168  }
169 
170  this->GetFunctorVector().clear();
171 
172  for (unsigned int i = 0; i < this->GetInput()->GetNumberOfComponentsPerPixel(); ++i)
173  {
174  double coef;
175  double res;
176  // Don't change the order of atmospheric parameters.
177  // FIXME Need to check in 6S that
178  // unsigned int wavelengthPosition = imageMetadataInterface->BandIndexToWavelengthPosition(i);
179  unsigned int wavelengthPosition = i;
180 
181  coef = static_cast<double>(m_AtmosphericRadiativeTerms->GetTotalGaseousTransmission(wavelengthPosition) *
182  m_AtmosphericRadiativeTerms->GetDownwardTransmittance(wavelengthPosition) *
183  m_AtmosphericRadiativeTerms->GetUpwardTransmittance(wavelengthPosition));
184  coef = 1. / coef;
185  res = -m_AtmosphericRadiativeTerms->GetIntrinsicAtmosphericReflectance(wavelengthPosition);
186  FunctorType functor;
187  functor.SetCoefficient(coef);
188  functor.SetResidu(res);
189  functor.SetSphericalAlbedo(static_cast<double>(m_AtmosphericRadiativeTerms->GetSphericalAlbedo(wavelengthPosition)));
190 
191  this->GetFunctorVector().push_back(functor);
192 
193  otbMsgDevMacro(<< "Parameters to compute surface reflectance: ");
194 
195  otbMsgDevMacro(<< "Band wavelengh position: " << wavelengthPosition);
196  otbMsgDevMacro(<< "Coef (A): " << functor.GetCoefficient());
197  otbMsgDevMacro(<< "Residu: " << functor.GetResidu());
198  otbMsgDevMacro(<< "Spherical albedo: " << functor.GetSphericalAlbedo());
199  }
200 }
201 
202 
203 /* Standard "PrintSelf" method */
204 template <class TInputImage, class TOutputImage>
206 {
207  os << indent << "Atmospheric radiative terms : " << m_AtmosphericRadiativeTerms << std::endl;
208  os << indent << "Atmospheric correction terms : " << m_AtmoCorrectionParameters << std::endl;
209  os << indent << "Acquisition correction terms : " << m_AcquiCorrectionParameters << std::endl;
210 }
211 
212 } // end namespace otb
213 
214 #endif
otb::ReflectanceToSurfaceReflectanceImageFilter::GenerateParameters
void GenerateParameters()
Definition: otbReflectanceToSurfaceReflectanceImageFilter.hxx:60
otb::MDNum::SatElevation
@ SatElevation
otb::ReflectanceToSurfaceReflectanceImageFilter::WavelengthSpectralBandVectorType
AcquiCorrectionParametersType::WavelengthSpectralBandVectorType WavelengthSpectralBandVectorType
Definition: otbReflectanceToSurfaceReflectanceImageFilter.h:193
otb::ReflectanceToSurfaceReflectanceImageFilter::Modified
void Modified() const override
Definition: otbReflectanceToSurfaceReflectanceImageFilter.hxx:52
otbReflectanceToSurfaceReflectanceImageFilter.h
otb::MDTime::AcquisitionDate
@ AcquisitionDate
otb::ReflectanceToSurfaceReflectanceImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbReflectanceToSurfaceReflectanceImageFilter.hxx:205
otb::Functor::ReflectanceToSurfaceReflectanceImageFunctor
Compute the surface reflectance pixel from a TOA reflectance.
Definition: otbReflectanceToSurfaceReflectanceImageFilter.h:50
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::ReflectanceToSurfaceReflectanceImageFunctor::SetCoefficient
void SetCoefficient(double coef)
Definition: otbReflectanceToSurfaceReflectanceImageFilter.h:80
otb::ReflectanceToSurfaceReflectanceImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: otbReflectanceToSurfaceReflectanceImageFilter.hxx:43
otb::Functor::ReflectanceToSurfaceReflectanceImageFunctor::GetResidu
double GetResidu()
Definition: otbReflectanceToSurfaceReflectanceImageFilter.h:97
otb::Functor::ReflectanceToSurfaceReflectanceImageFunctor::GetSphericalAlbedo
double GetSphericalAlbedo()
Definition: otbReflectanceToSurfaceReflectanceImageFilter.h:71
otb::Functor::ReflectanceToSurfaceReflectanceImageFunctor::SetResidu
void SetResidu(double res)
Definition: otbReflectanceToSurfaceReflectanceImageFilter.h:93
otb::Functor::ReflectanceToSurfaceReflectanceImageFunctor::SetSphericalAlbedo
void SetSphericalAlbedo(double albedo)
Definition: otbReflectanceToSurfaceReflectanceImageFilter.h:67
otb::ReflectanceToSurfaceReflectanceImageFilter::UpdateFunctors
void UpdateFunctors()
Definition: otbReflectanceToSurfaceReflectanceImageFilter.hxx:162
otb::Functor::ReflectanceToSurfaceReflectanceImageFunctor::GetCoefficient
double GetCoefficient()
Definition: otbReflectanceToSurfaceReflectanceImageFilter.h:84
otb::MDNum::SunAzimuth
@ SunAzimuth
otb::MDNum::SatAzimuth
@ SatAzimuth
otb::ReflectanceToSurfaceReflectanceImageFilter::ReflectanceToSurfaceReflectanceImageFilter
ReflectanceToSurfaceReflectanceImageFilter()
Definition: otbReflectanceToSurfaceReflectanceImageFilter.hxx:33
otb::FilterFunctionValues::New
static Pointer New()
otb::ReflectanceToSurfaceReflectanceImageFilter::UpdateAtmosphericRadiativeTerms
void UpdateAtmosphericRadiativeTerms()
Definition: otbReflectanceToSurfaceReflectanceImageFilter.hxx:77
otbMsgDevMacro
#define otbMsgDevMacro(x)
Definition: otbMacro.h:64
otb::ReflectanceToSurfaceReflectanceImageFilter::m_AcquiCorrectionParameters
AcquiCorrectionParametersPointerType m_AcquiCorrectionParameters
Definition: otbReflectanceToSurfaceReflectanceImageFilter.h:284
otb::ImageMetadataCorrectionParameters::New
static Pointer New()
otb::ReflectanceToSurfaceReflectanceImageFilter::m_AtmoCorrectionParameters
AtmoCorrectionParametersPointerType m_AtmoCorrectionParameters
Definition: otbReflectanceToSurfaceReflectanceImageFilter.h:283
otb::AtmosphericRadiativeTerms::New
static Pointer New()
otb::ReflectanceToSurfaceReflectanceImageFilter::m_AtmosphericRadiativeTerms
AtmosphericRadiativeTermsPointerType m_AtmosphericRadiativeTerms
Definition: otbReflectanceToSurfaceReflectanceImageFilter.h:282
otb::AtmosphericCorrectionParameters::New
static Pointer New()
otb::MDL1D::SpectralSensitivity
@ SpectralSensitivity
otb::MDNum::SunElevation
@ SunElevation