ARTS  2.0.49
mc_interp.cc
Go to the documentation of this file.
1 /* Copyright (C) 2005-2008 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& rte_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, rte_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 
261 
289 //interpolates TArray and PPath to give T and rte_pos(los) at a given pathlength
291  Vector& K_abs,
292  Numeric& temperature,
293  MatrixView& K,
294  Vector& rte_pos,
295  Vector& rte_los,
296  VectorView& pnd_vec,
297  const ArrayOfMatrix& TArray,
298  const ArrayOfMatrix& ext_matArray,
299  const ArrayOfVector& abs_vecArray,
300  const Vector& t_ppath,
301  const Matrix& pnd_ppath,
302  const Vector& cum_l_step,
303  const Numeric& pathlength,
304  const Index& stokes_dim,
305  const Ppath& ppath
306  )
307 {
308  //Internal Declarations
309  Matrix incT(stokes_dim,stokes_dim,0.0);
310  Matrix opt_depth_mat(stokes_dim,stokes_dim);
311  Vector itw(2);
312  Numeric delta_s;
313  Index N_pt=pnd_vec.nelem();
314  ArrayOfGridPos gp(1);
315 
316  //interpolate transmittance matrix
317  gridpos(gp, cum_l_step, pathlength);
318  interpweights(itw,gp[0]);
319  interp(K, itw,ext_matArray,gp[0]);
320  delta_s = pathlength - cum_l_step[gp[0].idx];
321  opt_depth_mat = K;
322  opt_depth_mat+=ext_matArray[gp[0].idx];
323  opt_depth_mat*=-delta_s/2;
324  if ( stokes_dim == 1 )
325  {
326  incT(0,0)=exp(opt_depth_mat(0,0));
327  }
328  else if ( is_diagonal( opt_depth_mat))
329  {
330  for ( Index i=0;i<stokes_dim;i++)
331  {
332  incT(i,i)=exp(opt_depth_mat(i,i));
333  }
334  }
335  else
336  {
337  //matrix_exp(incT,opt_depth_mat,10);
338  matrix_exp_p30(incT,opt_depth_mat);
339  }
340  mult(T,TArray[gp[0].idx],incT);
341 
342  interp(K_abs, itw, abs_vecArray,gp[0]);
343 
344  temperature=interp(itw,t_ppath,gp[0]);
345 
346  for (Index i=0;i<N_pt;i++)
347  {
348  pnd_vec[i]=interp(itw,pnd_ppath(i,Range(joker)),gp[0]);
349  }
350 
351  for (Index i=0; i<2; i++)
352  {
353  rte_pos[i] = interp(itw,ppath.pos(Range(joker),i),gp[0]);
354  rte_los[i] = interp(itw,ppath.los(Range(joker),i),gp[0]);
355  }
356  rte_pos[2] = interp(itw,ppath.pos(Range(joker),2),gp[0]);
357 }
Matrix
The Matrix class.
Definition: matpackI.h:767
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:421
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:79
MatrixView
The MatrixView class.
Definition: matpackI.h:668
operator<<
ostream & operator<<(ostream &os, const SLIData2 &)
Definition: mc_interp.cc:78
matrix_exp_p30
void matrix_exp_p30(MatrixView M, ConstMatrixView A)
matrix_exp_p30
Definition: montecarlo.cc:517
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:67
is_diagonal
bool is_diagonal(ConstMatrixView A)
Checks if a square matrix is diagonal.
Definition: logic.cc:387
joker
const Joker joker
SLIData2::x2a
ArrayOfVector x2a
Definition: mc_interp.h:63
ConstMatrixView::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:796
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:90
Ppath
The structure to describe a propagation path and releated quantities.
Definition: ppath.h:59
SingleScatteringData
Structure which describes the single scattering properties of a.
Definition: optproperties.h:74
DEBUG_ONLY
#define DEBUG_ONLY(...)
Definition: arts.h:146
Array
This can be used to make arrays out of anything.
Definition: array.h:103
mult
void mult(VectorView y, const ConstMatrixView &M, const ConstVectorView &x)
Matrix Vector multiplication.
Definition: matpackI.cc:1607
ConstMatrixView::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:802
abs
#define abs(x)
Definition: continua.cc:13094
Ppath::los
Matrix los
Definition: ppath.h:69
ConstVectorView::nelem
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:175
VectorView
The VectorView class.
Definition: matpackI.h:373
ConstVectorView::sum
Numeric sum() const
The sum of all elements of a Vector.
Definition: matpackI.cc:181
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
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 &rte_temperature)
Definition: mc_interp.cc:183
SLIData2::ya
ArrayOfVector ya
Definition: mc_interp.h:65
interpTArray
void interpTArray(Matrix &T, Vector &K_abs, Numeric &temperature, MatrixView &K, Vector &rte_pos, Vector &rte_los, VectorView &pnd_vec, const ArrayOfMatrix &TArray, const ArrayOfMatrix &ext_matArray, const ArrayOfVector &abs_vecArray, const Vector &t_ppath, const Matrix &pnd_ppath, const Vector &cum_l_step, const Numeric &pathlength, const Index &stokes_dim, const Ppath &ppath)
interpTarray
Definition: mc_interp.cc:290
Range
The range class.
Definition: matpackI.h:148
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
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
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:555
SingleScatteringData::pha_mat_data
Tensor7 pha_mat_data
Definition: optproperties.h:81
ConstVectorView
A constant view of a Vector.
Definition: matpackI.h:300
Ppath::pos
Matrix pos
Definition: ppath.h:63
SingleScatteringData::T_grid
Vector T_grid
Definition: optproperties.h:78
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:172
interpweights
void interpweights(VectorView itw, const GridPos &tc)
Red 1D interpolation weights.
Definition: interpolation.cc:756