ARTS  2.2.66
mc_interp.cc
Go to the documentation of this file.
1 /* Copyright (C) 2005-2012 Cory Davis <cory@met.ed.ac.uk>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16  USA. */
17 
18 
19 
20 /*===========================================================================
21  === File description
22  ===========================================================================*/
23 
33 /*===========================================================================
34  === External declarations
35  ===========================================================================*/
36 #include "mc_interp.h"
37 #include "logic.h"
38 #include "montecarlo.h"
39 
40 
42 
53 {
54  GridPos gp1,gpl,gpr;
55  Vector itw1(2),itwl(2),itwr(2);
56  Numeric yl,yr;
57  //Get interpolation weights for x1 interpolation
58  gridpos(gp1,this->x1a,x1);
59  interpweights(itw1,gp1);
60  //Get y values on bounding x1 grid points for desired x2
61  gridpos(gpl,this->x2a[gp1.idx],x2);
62  interpweights(itwl,gpl);
63  gridpos(gpr,this->x2a[gp1.idx+1],x2);
64  interpweights(itwr,gpr);
65  yl=interp(itwl,this->ya[gp1.idx],gpl);
66  yr=interp(itwr,this->ya[gp1.idx+1],gpr);
67  //interpolate these two y values useing x1 interpolation weights
68  return itw1[0]*yl+itw1[1]*yr;
69 }
70 
71 //void SLIData2::check() const
72 //{
73 // Index nx1=this->x1a.nelem();
74 // assert(nx1>0);
75 //}
76 
77 
78 ostream& operator<< (ostream& os, const SLIData2& /* sli */)
79 {
80  os << "SLIData2 : Output operator not implemented";
81  return os;
82 }
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
94 
112  ConstVectorView itw,
113  const ArrayOfMatrix& a,
114  const GridPos& tc )
115 {
116  DEBUG_ONLY (const Numeric sum_check_epsilon = 1e-6);
117 
118  assert(is_size(itw,2)); // We need 2 interpolation
119  // weights.
120 
121  // Check that interpolation weights are valid. The sum of all
122  // weights (last dimension) must always be approximately one.
123  assert( is_same_within_epsilon( itw.sum(),
124  1,
125  sum_check_epsilon ) );
126 
127  Index anr = a[0].nrows();
128  Index anc = a[0].ncols();
129 
130  assert(tia.nrows() == anr);
131  assert(tia.ncols() == anc);
132 
133  for (Index inr = 0; inr < anr; inr++)
134  for (Index inc = 0; inc < anc; inc++)
135  {
136  tia(inr,inc) = a[tc.idx](inr,inc)*itw[0] + a[tc.idx+1](inr,inc)*itw[1];
137  }
138 }
139 
140 
142 
159  ConstVectorView itw,
160  const ArrayOfVector& a,
161  const GridPos& tc )
162 {
163  DEBUG_ONLY (const Numeric sum_check_epsilon = 1e-6);
164  assert(is_size(itw,2)); // We need 2 interpolation
165  // weights.
166 
167  // Check that interpolation weights are valid. The sum of all
168  // weights (last dimension) must always be approximately one.
169  assert( is_same_within_epsilon( itw.sum(),
170  1,
171  sum_check_epsilon ) );
172 
173  Index an = a[0].nelem();
174 
175  assert(tia.nelem() == an);
176 
177  for ( Index i=0; i<an; ++i )
178  {
179  tia[i] = a[tc.idx][i]*itw[0] + a[tc.idx+1][i]*itw[1];
180  }
181 }
182 
184  VectorView pha_mat_int,
185  Numeric& theta_rad,
186  //Input:
187  const SingleScatteringData& scat_data,
188  const Numeric& za_sca,
189  const Numeric& aa_sca,
190  const Numeric& za_inc,
191  const Numeric& aa_inc,
192  const Numeric& rtp_temperature
193  )
194 {
195  Numeric ANG_TOL=1e-7;
196 
197  //Calculate scattering angle from incident and scattered directions.
198  //The two special cases are implemented here to avoid NaNs that can
199  //sometimes occur in in the acos... formula in forward and backscatter
200  //cases. CPD 5/10/03.
201 
202  if(abs(aa_sca-aa_inc)<ANG_TOL)
203  {
204  theta_rad=DEG2RAD*abs(za_sca-za_inc);
205  }
206  else if (abs(abs(aa_sca-aa_inc)-180)<ANG_TOL)
207  {
208  theta_rad=DEG2RAD*(za_sca+za_inc);
209  if (theta_rad>PI){theta_rad=2*PI-theta_rad;}
210  }
211  else
212  {
213  const Numeric za_sca_rad = za_sca * DEG2RAD;
214  const Numeric za_inc_rad = za_inc * DEG2RAD;
215  const Numeric aa_sca_rad = aa_sca * DEG2RAD;
216  const Numeric aa_inc_rad = aa_inc * DEG2RAD;
217 
218  // cout << "Interpolation on scattering angle" << endl;
219  assert (scat_data.pha_mat_data.ncols() == 6);
220  // Calculation of the scattering angle:
221  theta_rad = acos(cos(za_sca_rad) * cos(za_inc_rad) +
222  sin(za_sca_rad) * sin(za_inc_rad) *
223  cos(aa_sca_rad - aa_inc_rad));
224  }
225  const Numeric theta = RAD2DEG * theta_rad;
226 
227  // Interpolation of the data on the scattering angle:
228 
229  GridPos thet_gp;
230  gridpos(thet_gp, scat_data.za_grid, theta);
231  GridPos t_gp;
232 
233  if( scat_data.T_grid.nelem() == 1)
234  {
235  Vector itw(2);
236  interpweights(itw, thet_gp);
237 
238  for (Index i = 0; i < 6; i++)
239  {
240  pha_mat_int[i] = interp(itw, scat_data.pha_mat_data(0,0,joker, 0, 0, 0, i),thet_gp);
241  }
242  }
243  else
244  {
245  gridpos(t_gp, scat_data.T_grid, rtp_temperature);
246 
247  Vector itw(4);
248  interpweights(itw, t_gp,thet_gp);
249 
250  for (Index i = 0; i < 6; i++)
251  {
252  pha_mat_int[i] = interp(itw, scat_data.pha_mat_data(0,joker,joker, 0, 0, 0, i),
253  t_gp,thet_gp);
254  }
255  }
256 }
257 
258 
259 
SLIData2::x1a
Vector x1a
Definition: mc_interp.h:61
is_same_within_epsilon
bool is_same_within_epsilon(const Numeric &a, const Numeric &b, const Numeric &epsilon)
Check, if two numbers agree within a given epsilon.
Definition: logic.cc:446
interp
void interp(MatrixView tia, ConstVectorView itw, const ArrayOfMatrix &a, const GridPos &tc)
Red 1D Interpolate.
Definition: mc_interp.cc:111
SingleScatteringData::za_grid
Vector za_grid
Definition: optproperties.h:89
MatrixView
The MatrixView class.
Definition: matpackI.h:679
operator<<
ostream & operator<<(ostream &os, const SLIData2 &)
Definition: mc_interp.cc:78
gridpos
void gridpos(ArrayOfGridPos &gp, ConstVectorView old_grid, ConstVectorView new_grid, const Numeric &extpolfac)
Set up a grid position Array.
Definition: interpolation.cc:167
mc_interp.h
Interpolation classes and functions created for use within Monte Carlo scattering simulations.
RAD2DEG
const Numeric RAD2DEG
Global constant, conversion from radians to degrees.
ConstTensor7View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackVII.cc:68
joker
const Joker joker
DEBUG_ONLY
#define DEBUG_ONLY(...)
Definition: debug.h:37
SLIData2::x2a
ArrayOfVector x2a
Definition: mc_interp.h:63
ConstMatrixView::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:832
DEG2RAD
const Numeric DEG2RAD
Global constant, conversion from degrees to radians.
montecarlo.h
is_size
bool is_size(ConstVectorView x, const Index &n)
Verifies that the size of x is l.
Definition: logic.cc:91
SingleScatteringData
Structure which describes the single scattering properties of a particle or a particle distribution.
Definition: optproperties.h:84
Array< Matrix >
ConstMatrixView::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:838
abs
#define abs(x)
Definition: continua.cc:20458
ConstVectorView::nelem
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:180
VectorView
The VectorView class.
Definition: matpackI.h:372
ConstVectorView::sum
Numeric sum() const
The sum of all elements of a Vector.
Definition: matpackI.cc:186
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:29
SLIData2::ya
ArrayOfVector ya
Definition: mc_interp.h:65
GridPos
Structure to store a grid position.
Definition: interpolation.h:74
logic.h
Header file for logic.cc.
SLIData2
A 2D sequential linear interpolation (SLI) lookup table.
Definition: mc_interp.h:58
PI
const Numeric PI
SLIData2::interpolate
Numeric interpolate(Numeric x1, Numeric x2) const
Perform sequential interpolation.
Definition: mc_interp.cc:52
GridPos::idx
Index idx
Definition: interpolation.h:75
interp_scat_angle_temperature
void interp_scat_angle_temperature(VectorView pha_mat_int, Numeric &theta_rad, const SingleScatteringData &scat_data, const Numeric &za_sca, const Numeric &aa_sca, const Numeric &za_inc, const Numeric &aa_inc, const Numeric &rtp_temperature)
Definition: mc_interp.cc:183
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
sum_check_epsilon
const Numeric sum_check_epsilon
The maximum difference from 1 that we allow for a sum check.
Definition: interpolation.cc:63
Vector
The Vector class.
Definition: matpackI.h:556
SingleScatteringData::pha_mat_data
Tensor7 pha_mat_data
Definition: optproperties.h:91
ConstVectorView
A constant view of a Vector.
Definition: matpackI.h:292
SingleScatteringData::T_grid
Vector T_grid
Definition: optproperties.h:88
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:176
interpweights
void interpweights(VectorView itw, const GridPos &tc)
Red 1D interpolation weights.
Definition: interpolation.cc:802