Isis 3 Programmer Reference
Photometry.cpp
1 #include "IException.h"
2 #include "IString.h"
3 #include "Pvl.h"
4 #include "Photometry.h"
5 #include "PhotoModelFactory.h"
6 #include "PhotoModel.h"
7 #include "AtmosModelFactory.h"
8 #include "AtmosModel.h"
9 #include "NormModelFactory.h"
10 #include "NormModel.h"
11 #include "Plugin.h"
12 #include "FileName.h"
13 
14 namespace Isis {
22  Photometry::Photometry(Pvl &pvl) {
23  p_phtAmodel = NULL;
24  p_phtPmodel = NULL;
25  p_phtNmodel = NULL;
26  if(pvl.hasObject("PhotometricModel")) {
27  p_phtPmodel = PhotoModelFactory::Create(pvl);
28  } else {
29  std::string msg = "A Photometric model must be specified to do any type of photometry";
31  }
32  if(pvl.hasObject("AtmosphericModel")) {
33  p_phtAmodel = AtmosModelFactory::Create(pvl, *p_phtPmodel);
34  }
35  if (pvl.hasObject("NormalizationModel")) {
36  if (p_phtAmodel != NULL) {
37  p_phtNmodel = NormModelFactory::Create(pvl, *p_phtPmodel, *p_phtAmodel);
38  } else {
39  p_phtNmodel = NormModelFactory::Create(pvl, *p_phtPmodel);
40  }
41  }
42  }
43 
46  if(p_phtAmodel != NULL) {
47  delete p_phtAmodel;
48  p_phtAmodel = NULL;
49  }
50 
51  if(p_phtPmodel != NULL) {
52  delete p_phtPmodel;
53  p_phtPmodel = NULL;
54  }
55 
56  if(p_phtNmodel != NULL) {
57  delete p_phtNmodel;
58  p_phtNmodel = NULL;
59  }
60  }
61 
67  void Photometry::SetPhotomWl(double wl) {
68  p_phtNmodel->SetNormWavelength(wl);
69  }
70 
77  void Photometry::Compute(double pha, double inc, double ema,
78  double dn, double &albedo, double &mult,
79  double &base) {
80 
81  // Calculate the surface brightness
82  p_phtNmodel->CalcNrmAlbedo(pha, inc, ema, dn, albedo, mult, base);
83  return;
84  }
85 
92  void Photometry::Compute(double pha, double inc, double ema,
93  double deminc, double demema, double dn,
94  double &albedo, double &mult, double &base) {
95 
96  // Calculate the surface brightness
97  p_phtNmodel->CalcNrmAlbedo(pha, inc, ema, deminc, demema, dn, albedo, mult, base);
98  return;
99  }
100 
125  int Photometry::brentsolver(double x_lo, double x_hi, gsl_function *Func, double tolerance, double &root){
126  int status;
127  int iter=0, max_iter=100;
128  const gsl_root_fsolver_type *T;
129  gsl_root_fsolver *s;
130 
131  T = gsl_root_fsolver_brent;
132  s = gsl_root_fsolver_alloc (T);
133  gsl_root_fsolver_set(s, Func, x_lo, x_hi);
134 
135  do {
136  iter++;
137  status = gsl_root_fsolver_iterate(s);
138  root = gsl_root_fsolver_x_lower(s);
139  x_lo = gsl_root_fsolver_x_lower(s);
140  x_hi = gsl_root_fsolver_x_upper(s);
141  status = gsl_root_test_interval(x_lo, x_hi, 0, tolerance);
142 
143  } while (status != GSL_SUCCESS && iter < max_iter);
144 
145  gsl_root_fsolver_free(s);
146  return status;
147  }
148 
168  int Photometry::brentminimizer(double x_lower, double x_upper, gsl_function *Func,
169  double & x_minimum, double tolerance) {
170  int status;
171  int iter=0, max_iter=100;
172 
173  const gsl_min_fminimizer_type *T;
174  gsl_min_fminimizer *s;
175  //double m_expected = M_PI;
176 
177  T = gsl_min_fminimizer_brent;
178  s = gsl_min_fminimizer_alloc(T);
179 
180  // This function sets, or resets, an existing minimizer s to use the function Func and
181  // the initial search interval [x_lower, x_upper], with a guess for the location of
182  // the minimum x_minimum. If the interval given does not contain a minimum, then
183  // the function returns an error code of GSL_EINVAL.
184  gsl_min_fminimizer_set(s, Func, x_minimum, x_lower, x_upper);
185 
186  do {
187  iter++;
188  status = gsl_min_fminimizer_iterate(s);
189  x_minimum = gsl_min_fminimizer_x_minimum(s);
190  x_lower = gsl_min_fminimizer_x_lower(s);
191  x_upper = gsl_min_fminimizer_x_upper(s);
192 
193  status = gsl_min_test_interval(x_lower, x_upper, tolerance, 0.0);
194  } while(status == GSL_CONTINUE && iter < max_iter);
195 
196  // This function frees all the memory associated with the minimizer s.
197  gsl_min_fminimizer_free(s);
198 
199  return status;
200  }
201 
207  void Photometry::minbracket(double &xa, double &xb, double &xc, double &fa, double &fb,
208  double &fc, double Func(double par, void *params), void *params) {
209  double eps = 1.0e-21;
210  double Gold = 1.618034;
211  double GrowLimit = 110;
212  int maxiter = 1000;
213 
214  fa = Func(xa, params);
215  fb = Func(xb, params);
216  if (fa < fb) {
217  double tmp = xa;
218  xa = xb;
219  xb = tmp;
220  tmp = fa;
221  fa = fb;
222  fb = tmp;
223  }
224  xc = xb + Gold * (xb - xa);
225  fc = Func(xc, params);
226  int iter = 0;
227 
228  while (fc < fb) {
229  double tmp1 = (xb - xa) * (fb - fc);
230  double tmp2 = (xb - xc) * (fb - fa);
231  double val = tmp2 - tmp1;
232  double denom;
233  if (fabs(val) < eps) {
234  denom = 2.0 * eps;
235  } else {
236  denom = 2.0 * val;
237  }
238  double w = xb - ((xb -xc) * tmp2 - (xb - xa) * tmp1) / denom;
239  double wlim = xb + GrowLimit * (xc - xb);
240  if (iter > maxiter) {
241  IString msg = "Maximum iterations exceeded in minimum bracketing ";
242  msg += "algorithm (minbracket) - root cannot be bracketed";
244  }
245  iter = iter + 1;
246  double fw;
247  if (((w-xc)*(xb-w)) > 0.0) {
248  fw = Func(w, params);
249  if (fw < fc) {
250  xa = xb;
251  xb = w;
252  fa = fb;
253  fb = fw;
254  return;
255  } else if (fw > fb) {
256  xc = w;
257  fc = fw;
258  return;
259  }
260  w = xc + Gold * (xc - xb);
261  fw = Func(w, params);
262  } else if (((w-wlim)*(wlim-xc)) >= 0.0) {
263  w = wlim;
264  fw = Func(w, params);
265  } else if (((w-wlim)*(xc-w)) > 0.0) {
266  fw = Func(w, params);
267  if (fw < fc) {
268  xb = xc;
269  xc = w;
270  w = xc + Gold * (xc - xb);
271  fb = fc;
272  fc = fw;
273  fw = Func(w, params);
274  }
275  } else {
276  w = xc + Gold * (xc - xb);
277  fw = Func(w, params);
278  }
279  xa = xb;
280  xb = xc;
281  xc = w;
282  fa = fb;
283  fb = fc;
284  fc = fw;
285  }
286  return;
287  }
288 }
virtual ~Photometry()
Destroy Photometry object.
Definition: Photometry.cpp:45
virtual void SetNormWavelength(double wavelength)
Set the wavelength parameter.
Definition: NormModel.cpp:45
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:207
virtual void SetPhotomWl(double wl)
Set the wavelength.
Definition: Photometry.cpp:67
static AtmosModel * Create(Pvl &pvl, PhotoModel &pmodel)
Create an AtmosModel object using a PVL specification.
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:335
static int brentsolver(double x_lo, double x_hi, gsl_function *Func, double tolerance, double &root)
GSL&#39;s the Brent-Dekker method (Brent&#39;s method) combines an interpolation strategy with the bisection ...
Definition: Photometry.cpp:125
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
A type of error that could only have occurred due to a mistake on the user&#39;s part (e...
Definition: IException.h:142
static PhotoModel * Create(Pvl &pvl)
Create a PhotoModel object using a PVL specification.
Container for cube-like labels.
Definition: Pvl.h:135
void Compute(double pha, double inc, double ema, double dn, double &albedo, double &mult, double &base)
Calculate the surface brightness.
Definition: Photometry.cpp:77
static NormModel * Create(Pvl &pvl, PhotoModel &pmodel)
Create a NormModel object using a PVL specification.
Isis exception class.
Definition: IException.h:107
Adds specific functionality to C++ strings.
Definition: IString.h:181
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
void CalcNrmAlbedo(double pha, double inc, double ema, double dn, double &albedo, double &mult, double &base)
Calculate the albedo normalization.
Definition: NormModel.cpp:58
static int brentminimizer(double x_lower, double x_upper, gsl_function *Func, double &x_minimum, double tolerance)
Brent&#39;s method 1-D minimization routine using GSL&#39;s r8Brent minimization Algorithm.
Definition: Photometry.cpp:168