ReFRACtor
ils_table.cc
Go to the documentation of this file.
1 #include "ils_table.h"
2 #include <boost/lexical_cast.hpp>
3 using namespace FullPhysics;
4 using namespace blitz;
5 
6 #ifdef HAVE_LUA
7 #include "register_lua.h"
9 .def(luabind::constructor<const HdfFile&,
10  const int,
11  const std::string&, const std::string&,
12  const std::string&>())
13 .def(luabind::constructor<const HdfFile&,
14  const int,
15  const std::string&, const std::string&>())
16 .def(luabind::constructor<const blitz::Array<double, 1>&,
17  const blitz::Array<double, 2>&,
18  const blitz::Array<double, 2>&,
19  const std::string&, const std::string&,
20  bool>())
21 .def("band_name", &IlsTableLinear::band_name)
23 
25 .def(luabind::constructor<const HdfFile&,
26  const int,
27  const std::string&, const std::string&,
28  const std::string&>())
29 .def(luabind::constructor<const HdfFile&,
30  const int,
31  const std::string&, const std::string&>())
32 .def(luabind::constructor<const blitz::Array<double, 1>&,
33  const blitz::Array<double, 2>&,
34  const blitz::Array<double, 2>&,
35  const std::string&, const std::string&,
36  bool>())
37 .def("band_name", &IlsTableLog::band_name)
39 
40 #endif
41 
42 //-----------------------------------------------------------------------
48 //-----------------------------------------------------------------------
49 
50 template<class Lint>
51 IlsTable<Lint>::IlsTable(const HdfFile& Hdf_static_input, int Spec_index,
52  const std::string& Band_name, const std::string& Hdf_band_name,
53  const std::string& Hdf_group)
54 : band_name_(Band_name), hdf_band_name_(Hdf_band_name),
55  from_hdf_file(true),
56  hdf_file_name(Hdf_static_input.file_name())
57 {
58  hdf_group = Hdf_group + "/ILS_" +
59  boost::lexical_cast<std::string>(Spec_index + 1);
60  Array<double, 1> wavenumber
61  (Hdf_static_input.read_field<double, 1>(hdf_group + "/wavenumber"));
62  Array<double, 2> delta_lambda
63  (Hdf_static_input.read_field<double, 2>(hdf_group + "/delta_lambda"));
64  Array<double, 2> response
65  (Hdf_static_input.read_field<double, 2>(hdf_group + "/response"));
66  std::string ftype =
67  Hdf_static_input.read_field<std::string>(hdf_group + "/function_type");
68  if(ftype == "table")
69  interpolate_wavenumber = false;
70  else if(ftype == "interpol")
71  interpolate_wavenumber = true;
72  else {
73  Exception e;
74  e << "Unrecognized function_type given in the file "
75  << Hdf_static_input.file_name() << " HDF group " << hdf_group
76  << ". The value was '" << ftype << "'. This should be one of "
77  << "'table' or 'interpol'";
78  throw e;
79  }
80  create_delta_lambda_to_response(wavenumber, delta_lambda, response);
81 }
82 
83 template<class Lint>
85 (const blitz::Array<double, 1>& Wavenumber,
86  const blitz::Array<double, 2>& Delta_lambda,
87  const blitz::Array<double, 2>& Response)
88 {
89  wavenumber_.reference(Wavenumber);
90  delta_lambda_.reference(Delta_lambda);
91  response_.reference(Response);
92 
93  delta_lambda_to_response.clear();
94  for(int i = 0; i < Wavenumber.rows(); ++i) {
95  // Create AutoDerivative version of these arrays, for use by the
96  // interpolator.
97  Array<ad, 1>
98  delta_lambda(arrad(Delta_lambda(i, Range::all())).to_array()),
99  response(arrad(Response(i, Range::all())).to_array());
100  delta_lambda_to_response[Wavenumber(i)] =
101  Lint(delta_lambda.begin(), delta_lambda.end(), response.begin(),
103  }
104 }
105 
106 // See base class for description
107 template<class Lint>
109 (const AutoDerivative<double>& wn_center,
110  const blitz::Array<double, 1>& wn,
111  ArrayAd<double, 1>& res) const
112 {
113  // Note that this function is a bottleneck, so we have written this
114  // for speed at the expense of some clarity.
115  res.resize(wn.rows(), wn_center.number_variable());
117  f.gradient().resize(wn_center.number_variable());
119  t.gradient().resize(wn_center.number_variable());
121  // w will be wn(i) - wn_center. We do this calculation in 2 steps
122  // to speed up the calculation.
124  w.gradient().resize(wn_center.number_variable());
125  w.gradient() = -wn_center.gradient();
126  // This returns the value for the first wn not less than wn_center.
127  it inter = delta_lambda_to_response.lower_bound(wn_center.value());
128  // By the convention, we want the value for the last wn less than
129  // wn_center.
130  if(inter != delta_lambda_to_response.begin())
131  --inter;
132  for(int i = 0; i < res.rows(); ++i) {
133  w.value() = wn(i) - wn_center.value();
134  inter->second.interpolate(w, res(i));
135  }
136 
137  // Do an interpolation in the wavenumber direction, if requested.
138  if(interpolate_wavenumber) {
139  double wn1 = inter->first;
140  ++inter;
141  if(inter != delta_lambda_to_response.end()) {
142  double wn2 = inter->first;
143  f = (wn_center - wn1) / (wn2 - wn1);
144  for(int i = 0; i < res.rows(); ++i) {
145  w.value() = wn(i) - wn_center.value();
146  inter->second.interpolate(w, tref);
147  double v = res(i).value();
148  res(i).value_ref() = (1 - f.value()) * v + f.value() * t.value();
149  res(i).gradient_ref() *= (1 - f.value());
150  res(i).gradient_ref() += f.gradient() * (t.value() - v) +
151  f.value() * t.gradient();
152  }
153  }
154  }
155 }
156 
157 template<class Lint>
158 void IlsTable<Lint>::print(std::ostream& Os) const
159 {
160  Os << "IlsTable for band " << band_name() << "\n"
161  << " Interpolate wavenumber: " << (interpolate_wavenumber ? "true" : "false");
162  if(from_hdf_file)
163  Os << "\n"
164  << " Hdf file name: " << hdf_file_name << "\n"
165  << " Hdf group: " << hdf_group;
166 }
167 
168 
170 template class FullPhysics::IlsTable<FullPhysics::LinearLogInterpolate<FullPhysics::AutoDerivative<double>, FullPhysics::AutoDerivative<double> > >;
171 
blitz::Array< T, D > read_field(const std::string &Dataname) const
Read a given field.
Definition: hdf_file.h:564
This class takes a set of points and values, and linearly interpolates between those values...
virtual std::string band_name() const
Descriptive name of the band.
Definition: ils_table.h:74
This is the base of the exception hierarchy for Full Physics code.
Definition: fp_exception.h:16
STL namespace.
#define REGISTER_LUA_DERIVED_CLASS(X, Y)
Definition: register_lua.h:136
virtual void print(std::ostream &Os) const
Definition: ils_table.cc:158
const blitz::Array< T, 1 > & gradient() const
This class models an Instrument Line Shape (ILS) function.
Definition: ils_function.h:17
Helper class that gives us a reference that we can assign a AutoDerivative to and write into the corr...
This class reads and writes a HDF5 file.
Definition: hdf_file.h:39
Apply value function to a blitz array.
void create_delta_lambda_to_response(const blitz::Array< double, 1 > &Wavenumber, const blitz::Array< double, 2 > &Delta_lambda, const blitz::Array< double, 2 > &Response)
Creates the datastructures needed for setting up the ILS table, only necessary to rerun if modifying ...
Definition: ils_table.cc:85
const blitz::Array< T, D > & value() const
Definition: array_ad.h:306
void resize(const blitz::TinyVector< int, D > &Shape, int nvar)
Definition: array_ad.h:177
IlsTable(const blitz::Array< double, 1 > &Wavenumber, const blitz::Array< double, 2 > &Delta_lambda, const blitz::Array< double, 2 > &Response, const std::string &Band_name, const std::string &Hdf_band_name, bool Interpolate_wavenumber=false)
Constructor where we just supply the wavenumber, delta_lambda and response values.
Definition: ils_table.h:51
virtual blitz::Array< double, 1 > wavenumber() const
Accessors for retrieving arrays used to create table.
Definition: ils_table.h:78
virtual void ils(const AutoDerivative< double > &wn_center, const blitz::Array< double, 1 > &wn, ArrayAd< double, 1 > &res) const
Return response function.
Definition: ils_table.cc:109
int number_variable() const
Number of variables in gradient.
const blitz::Array< T, 1 > & gradient() const
Gradient.
virtual blitz::Array< double, 2 > response() const
Definition: ils_table.h:80
const std::string & file_name() const
File name.
Definition: hdf_file.h:131
virtual blitz::Array< double, 2 > delta_lambda() const
Definition: ils_table.h:79
This class in a IlsFunction where we get the ILS response by using a table of measured values...
Definition: ils_table.h:34
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
const T & value() const
Convert to type T.
#define REGISTER_LUA_END()
Definition: register_lua.h:134
def(luabind::constructor< int >()) .def("rows"
int rows() const
Definition: array_ad.h:368

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:08