Isis 3 Programmer Reference
Photometry.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include "IException.h"
8 #include "IString.h"
9 #include "Pvl.h"
10 #include "Photometry.h"
11 #include "PhotoModelFactory.h"
12 #include "PhotoModel.h"
13 #include "AtmosModelFactory.h"
14 #include "AtmosModel.h"
15 #include "NormModelFactory.h"
16 #include "NormModel.h"
17 #include "Plugin.h"
18 #include "FileName.h"
19 
20 namespace Isis {
28  Photometry::Photometry(Pvl &pvl) {
29  p_phtAmodel = NULL;
30  p_phtPmodel = NULL;
31  p_phtNmodel = NULL;
32  if(pvl.hasObject("PhotometricModel")) {
33  p_phtPmodel = PhotoModelFactory::Create(pvl);
34  } else {
35  std::string msg = "A Photometric model must be specified to do any type of photometry";
36  throw IException(IException::User, msg, _FILEINFO_);
37  }
38  if(pvl.hasObject("AtmosphericModel")) {
39  p_phtAmodel = AtmosModelFactory::Create(pvl, *p_phtPmodel);
40  }
41  if (pvl.hasObject("NormalizationModel")) {
42  if (p_phtAmodel != NULL) {
43  p_phtNmodel = NormModelFactory::Create(pvl, *p_phtPmodel, *p_phtAmodel);
44  } else {
45  p_phtNmodel = NormModelFactory::Create(pvl, *p_phtPmodel);
46  }
47  }
48  }
49 
52  if(p_phtAmodel != NULL) {
53  delete p_phtAmodel;
54  p_phtAmodel = NULL;
55  }
56 
57  if(p_phtPmodel != NULL) {
58  delete p_phtPmodel;
59  p_phtPmodel = NULL;
60  }
61 
62  if(p_phtNmodel != NULL) {
63  delete p_phtNmodel;
64  p_phtNmodel = NULL;
65  }
66  }
67 
73  void Photometry::SetPhotomWl(double wl) {
74  p_phtNmodel->SetNormWavelength(wl);
75  }
76 
83  void Photometry::Compute(double pha, double inc, double ema,
84  double dn, double &albedo, double &mult,
85  double &base) {
86 
87  // Calculate the surface brightness
88  p_phtNmodel->CalcNrmAlbedo(pha, inc, ema, dn, albedo, mult, base);
89  return;
90  }
91 
98  void Photometry::Compute(double pha, double inc, double ema,
99  double deminc, double demema, double dn,
100  double &albedo, double &mult, double &base) {
101 
102  // Calculate the surface brightness
103  p_phtNmodel->CalcNrmAlbedo(pha, inc, ema, deminc, demema, dn, albedo, mult, base);
104  return;
105  }
106 
131  int Photometry::brentsolver(double x_lo, double x_hi, gsl_function *Func, double tolerance, double &root){
132  int status;
133  int iter=0, max_iter=100;
134  const gsl_root_fsolver_type *T;
135  gsl_root_fsolver *s;
136 
137  T = gsl_root_fsolver_brent;
138  s = gsl_root_fsolver_alloc (T);
139  gsl_root_fsolver_set(s, Func, x_lo, x_hi);
140 
141  do {
142  iter++;
143  status = gsl_root_fsolver_iterate(s);
144  root = gsl_root_fsolver_x_lower(s);
145  x_lo = gsl_root_fsolver_x_lower(s);
146  x_hi = gsl_root_fsolver_x_upper(s);
147  status = gsl_root_test_interval(x_lo, x_hi, 0, tolerance);
148 
149  } while (status != GSL_SUCCESS && iter < max_iter);
150 
151  gsl_root_fsolver_free(s);
152  return status;
153  }
154 
174  int Photometry::brentminimizer(double x_lower, double x_upper, gsl_function *Func,
175  double & x_minimum, double tolerance) {
176  int status;
177  int iter=0, max_iter=100;
178 
179  const gsl_min_fminimizer_type *T;
180  gsl_min_fminimizer *s;
181  //double m_expected = M_PI;
182 
183  T = gsl_min_fminimizer_brent;
184  s = gsl_min_fminimizer_alloc(T);
185 
186  // This function sets, or resets, an existing minimizer s to use the function Func and
187  // the initial search interval [x_lower, x_upper], with a guess for the location of
188  // the minimum x_minimum. If the interval given does not contain a minimum, then
189  // the function returns an error code of GSL_EINVAL.
190  gsl_min_fminimizer_set(s, Func, x_minimum, x_lower, x_upper);
191 
192  do {
193  iter++;
194  status = gsl_min_fminimizer_iterate(s);
195  x_minimum = gsl_min_fminimizer_x_minimum(s);
196  x_lower = gsl_min_fminimizer_x_lower(s);
197  x_upper = gsl_min_fminimizer_x_upper(s);
198 
199  status = gsl_min_test_interval(x_lower, x_upper, tolerance, 0.0);
200  } while(status == GSL_CONTINUE && iter < max_iter);
201 
202  // This function frees all the memory associated with the minimizer s.
203  gsl_min_fminimizer_free(s);
204 
205  return status;
206  }
207 
213  void Photometry::minbracket(double &xa, double &xb, double &xc, double &fa, double &fb,
214  double &fc, double Func(double par, void *params), void *params) {
215  double eps = 1.0e-21;
216  double Gold = 1.618034;
217  double GrowLimit = 110;
218  int maxiter = 1000;
219 
220  fa = Func(xa, params);
221  fb = Func(xb, params);
222  if (fa < fb) {
223  double tmp = xa;
224  xa = xb;
225  xb = tmp;
226  tmp = fa;
227  fa = fb;
228  fb = tmp;
229  }
230  xc = xb + Gold * (xb - xa);
231  fc = Func(xc, params);
232  int iter = 0;
233 
234  while (fc < fb) {
235  double tmp1 = (xb - xa) * (fb - fc);
236  double tmp2 = (xb - xc) * (fb - fa);
237  double val = tmp2 - tmp1;
238  double denom;
239  if (fabs(val) < eps) {
240  denom = 2.0 * eps;
241  } else {
242  denom = 2.0 * val;
243  }
244  double w = xb - ((xb -xc) * tmp2 - (xb - xa) * tmp1) / denom;
245  double wlim = xb + GrowLimit * (xc - xb);
246  if (iter > maxiter) {
247  IString msg = "Maximum iterations exceeded in minimum bracketing ";
248  msg += "algorithm (minbracket) - root cannot be bracketed";
249  throw IException(IException::User, msg, _FILEINFO_);
250  }
251  iter = iter + 1;
252  double fw;
253  if (((w-xc)*(xb-w)) > 0.0) {
254  fw = Func(w, params);
255  if (fw < fc) {
256  xa = xb;
257  xb = w;
258  fa = fb;
259  fb = fw;
260  return;
261  } else if (fw > fb) {
262  xc = w;
263  fc = fw;
264  return;
265  }
266  w = xc + Gold * (xc - xb);
267  fw = Func(w, params);
268  } else if (((w-wlim)*(wlim-xc)) >= 0.0) {
269  w = wlim;
270  fw = Func(w, params);
271  } else if (((w-wlim)*(xc-w)) > 0.0) {
272  fw = Func(w, params);
273  if (fw < fc) {
274  xb = xc;
275  xc = w;
276  w = xc + Gold * (xc - xb);
277  fb = fc;
278  fc = fw;
279  fw = Func(w, params);
280  }
281  } else {
282  w = xc + Gold * (xc - xb);
283  fw = Func(w, params);
284  }
285  xa = xb;
286  xb = xc;
287  xc = w;
288  fa = fb;
289  fb = fc;
290  fc = fw;
291  }
292  return;
293  }
294 }
Isis::AtmosModelFactory::Create
static AtmosModel * Create(Pvl &pvl, PhotoModel &pmodel)
Create an AtmosModel object using a PVL specification.
Definition: AtmosModelFactory.cpp:44
Isis::Photometry::~Photometry
virtual ~Photometry()
Destroy Photometry object.
Definition: Photometry.cpp:51
Isis::Photometry::SetPhotomWl
virtual void SetPhotomWl(double wl)
Set the wavelength.
Definition: Photometry.cpp:73
Isis::Photometry::minbracket
static void minbracket(double &xa, double &xb, double &xc, double &fa, double &fb, double &fc, double Func(double par, void *params), void *params)
Double precision version of bracketing algorithm ported from Python. Solution bracketing for 1-D mini...
Definition: Photometry.cpp:213
Isis::Photometry::Compute
void Compute(double pha, double inc, double ema, double dn, double &albedo, double &mult, double &base)
Calculate the surface brightness.
Definition: Photometry.cpp:83
Isis::Pvl
Container for cube-like labels.
Definition: Pvl.h:119
Isis::NormModelFactory::Create
static NormModel * Create(Pvl &pvl, PhotoModel &pmodel)
Create a NormModel object using a PVL specification.
Definition: NormModelFactory.cpp:36
Isis::Photometry::brentminimizer
static int brentminimizer(double x_lower, double x_upper, gsl_function *Func, double &x_minimum, double tolerance)
Brent's method 1-D minimization routine using GSL's r8Brent minimization Algorithm.
Definition: Photometry.cpp:174
Isis::PhotoModelFactory::Create
static PhotoModel * Create(Pvl &pvl)
Create a PhotoModel object using a PVL specification.
Definition: PhotoModelFactory.cpp:35
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::PvlObject::hasObject
bool hasObject(const QString &name) const
Returns a boolean value based on whether the object exists in the current PvlObject or not.
Definition: PvlObject.h:323
Isis::IString
Adds specific functionality to C++ strings.
Definition: IString.h:165
Isis::Photometry::brentsolver
static int brentsolver(double x_lo, double x_hi, gsl_function *Func, double tolerance, double &root)
GSL's the Brent-Dekker method (Brent's method) combines an interpolation strategy with the bisection ...
Definition: Photometry.cpp:131
Isis::NormModel::CalcNrmAlbedo
void CalcNrmAlbedo(double pha, double inc, double ema, double dn, double &albedo, double &mult, double &base)
Calculate the albedo normalization.
Definition: NormModel.cpp:64
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::IException::User
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition: IException.h:126
Isis::NormModel::SetNormWavelength
virtual void SetNormWavelength(double wavelength)
Set the wavelength parameter.
Definition: NormModel.cpp:51