OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
modis_numerical_module.f90
Go to the documentation of this file.
2 
3 implicit none
4 
5 private
6 
8 
9 contains
10 
11 
12 SUBROUTINE bisectionsimple(theta,thetaArray,ntheta,iAngLow, iAngHi)
13 
14  INTEGER,INTENT(IN)::ntheta
15  REAL,INTENT(IN)::theta,thetaarray(1:ntheta)
16  INTEGER,INTENT(OUT)::ianglow, ianghi
17 
18  INTEGER:: iang
19 
20 
21  ianglow = 1
22  ianghi = ntheta
23 
24  if (ntheta == 1) then
25  ianglow = 1
26  ianghi = 1
27  return
28  endif
29 
30  DO
31  IF((ianghi - ianglow) == 1)EXIT
32  iang = (ianghi + ianglow)/2
33  IF(thetaarray(iang) < theta)THEN
34  ianglow = iang
35  ELSE
36  ianghi = iang
37  ENDIF
38  ENDDO
39 
40  if (ianglow < 1) ianglow = 1
41  if (ianghi < 1) ianghi = 1
42  if (ianghi > ntheta) ianghi = ntheta
43  if (ianglow > ntheta) ianglow = ntheta
44 
45 END SUBROUTINE bisectionsimple
46 
47 
48 
49 ! currently bisectionsearch works for ascending arrays only
50 subroutine bisectionsearch(xx,x,jlo,jhi)
51 
52 IMPLICIT NONE
53 INTEGER, INTENT(INOUT) :: jlo, jhi
54 REAL, INTENT(IN) :: x
55 REAL, DIMENSION(:), INTENT(IN) :: xx
56 INTEGER :: n,inc,jm
57 LOGICAL :: ascnd
58 n=size(xx)
59 ascnd = (xx(n) >= xx(1))
60 if (jlo <= 0 .or. jlo > n) then
61  jlo=0
62  jhi=n+1
63 else
64  inc=1
65  if (x >= xx(jlo) .eqv. ascnd) then
66  do
67  jhi=jlo+inc
68  if (jhi > n) then
69  jhi=n+1
70  exit
71  else
72  if (x < xx(jhi) .eqv. ascnd) exit
73  jlo=jhi
74  inc=inc+inc
75  end if
76  end do
77  else
78  jhi=jlo
79  do
80  jlo=jhi-inc
81  if (jlo < 1) then
82  jlo=0
83  exit
84  else
85  if (x >= xx(jlo) .eqv. ascnd) exit
86  jhi=jlo
87  inc=inc+inc
88  end if
89  end do
90  end if
91 end if
92 do
93  if (jhi-jlo <= 1) then
94 ! NOTE: the following two lines assume that the xx array MUST have been sorted in ascending order for this to work as designed
95  if (x >= xx(n)) jlo=n-1
96  if (x <= xx(1)) jlo=1
97  exit
98  else
99  jm=(jhi+jlo)/2
100  if (x >= xx(jm) .eqv. ascnd) then
101  jlo=jm
102  else
103  jhi=jm
104  end if
105  end if
106 end do
107 
108 ! G.Wind 12.16.05 inserted logic to make sure that the values can not go outside
109 ! the valid array bounds
110 if (jlo < 1) jlo = 1
111 if (jlo > n) jlo = n
112 if (jhi < 1) jhi = 1
113 if (jhi > n) jhi = n
114 
115 end subroutine bisectionsearch
116 
117 
118 logical function real_s_equal(x,y)
119  real :: x, y
120  real_s_equal = (abs(x-y) <= epsilon(x))
121 end function real_s_equal
122 
123 logical function realsingle_s_equal(x,y)
124  real :: x, y
125  realsingle_s_equal = (abs(x-y) <= epsilon(x))
126 end function realsingle_s_equal
127 
128 ! WDR this is not used in favor of exact copy in GeneralAuxType.f90
129 !subroutine realsingle_s_where_equal(x,y)
130 ! real ,intent(inout) :: x(:)
131 ! real :: y
132 !
133 ! where( abs(x - y) <= epsilon(y) )
134 ! x = 1.
135 ! elsewhere
136 ! x = 0.
137 ! endwhere
138 !
139 !end subroutine realsingle_s_where_equal
140 
141 
142 real function linearinterpolation(X,Y,XX)
143 
144 
145 ! XX R Interpolation POINT
146 ! X R(NN) INDEPENDENT VARIABLE
147 ! Y R(NN) DEPENDENT VARIABLE
148 !
149 ! Written by
150 ! Mark A Gray
151 ! SM&A SSG .
152 ! Code 913, NASA/GSFC
153 ! Greenbelt, MD 20771
154 
155  implicit none
156 
157  real, intent(in) :: x(2), xx
158  real, intent(in) :: y(2)
159 
160  if (realsingle_s_equal(x(1),x(2))) then
161  linearinterpolation = y(1)
162  return
163  elseif(realsingle_s_equal(x(1),xx)) then
164  linearinterpolation = y(1)
165  return
166  elseif (realsingle_s_equal(x(2),xx)) then
167  linearinterpolation = y(2)
168  return
169  else
170  linearinterpolation = y(1) + ( ( xx - x(1) ) / ( x(2) - x(1)) ) * ( y(2) -y(1) )
171  endif
172 
173 end function linearinterpolation
174 
175 
176 real function bilinear_interpolation( X, Y, XX, YY, source, method )
177 
178  implicit none
179 
180  real, dimension(2), intent(in) :: x, y
181  real, intent(in) :: xx, yy
182  integer, intent(in) :: method
183  real, dimension(:,:), intent(in) :: source
184 
185  real :: mid1, mid2
186 
187 
188  real :: area1, area2, area3, area4
189  real :: deltax1, deltax2, deltay1, deltay2
190 
191  real :: num1, num2
192 
193  if (method == 1) then
194 
195  deltax1 = x(1) - xx
196  deltax2 = x(2) - xx
197 
198  deltay1 = y(1) - yy
199  deltay2 = y(2) - yy
200 
201  area1 = abs(deltax1 * deltay1)
202  area2 = abs(deltax2 * deltay1)
203  area3 = abs(deltax1 * deltay2)
204  area4 = abs(deltax2 * deltay2)
205 
206  num2 = area1 + area2 + area3 + area4
207 
208  num1 = source(2, 2) * area1 + &
209  source(1, 1) * area4 + &
210  source(1, 2) * area2 + &
211  source(2, 1) * area3
212 
213  bilinear_interpolation = num1 / num2
214  else
215 
216  mid1 = linearinterpolation(x, (/source(1,1), source(1,2)/) , xx)
217  mid2 = linearinterpolation(x, (/source(2,1), source(2,2)/) , xx)
218 
219  bilinear_interpolation = linearinterpolation(y, (/mid1, mid2/), yy)
220  endif
221 
222 
223 end function bilinear_interpolation
224 
225 
226 real function linearinterpolation_3d(index_theta01,index_theta02, &
227  index_theta1, index_theta2, &
228  index_phi1, index_phi2,&
229  theta,theta0,phi,&
230  theta_grid, &
231  theta0_grid,&
232  phi_grid,&
233  debug, &
234  reflectance)
235 
236 
237 !Description:
238 ! Performa a linear interpolation within a 3d grid for a defined
239 ! point bounded by defined points.
240 !
241 !Input parameters:
242 !
243 !Output parameters:
244 !
245 !Revision history:
246 !
247 !Team-unique header:
248 !
249 !References and credits:
250 ! written by; Mark Gray
251 !
252 
253  implicit none
254 
255  integer, intent(in) :: index_theta1, index_theta2, index_phi1,index_phi2, &
256  index_theta01, index_theta02
257 
258  real, intent(in) :: reflectance(:,:,:)
259  real, intent(in) :: theta_grid(:), &
260  theta0_grid(:),phi_grid(:),theta,theta0,phi
261 
262  logical, intent(in) :: debug
263 
264  real :: corner1, corner2, corner3, corner4, &
265  corner5, corner6, corner7, corner8
266  real :: volume1, volume2, volume3, volume4, &
267  volume5, volume6, volume7, volume8
268  real :: num_1, num_2
269  real :: theta0_val_1, theta0_val_2, theta_val_1, theta_val_2,phi_val_1, phi_val_2
270 
271 
272  theta0_val_1 = theta0_grid(index_theta01)-theta0
273  theta0_val_2 = theta0_grid(index_theta02)-theta0
274 
275  theta_val_1 = theta_grid(index_theta1)-theta
276  theta_val_2 = theta_grid(index_theta2)-theta
277 
278  phi_val_1 = phi_grid(index_phi1)-phi
279  phi_val_2 = phi_grid(index_phi2)-phi
280 
281  volume2= abs(theta0_val_1 * theta_val_1 * phi_val_2)
282  volume3= abs(theta0_val_1 * theta_val_2 * phi_val_2)
283  volume4= abs(theta0_val_1 * theta_val_2 * phi_val_1)
284  volume5= abs(theta0_val_2 * theta_val_1 * phi_val_1)
285  volume6= abs(theta0_val_2 * theta_val_1 * phi_val_2)
286  volume7= abs(theta0_val_2 * theta_val_2 * phi_val_2)
287  volume8= abs(theta0_val_2 * theta_val_2 * phi_val_1)
288  volume1= abs(theta0_val_1 * theta_val_1 * phi_val_1)
289 
290  num_1 = reflectance(index_theta01,index_theta1,index_phi1) * volume7 + &
291  reflectance(index_theta01,index_theta1,index_phi2) * volume8 + &
292  reflectance(index_theta02,index_theta1,index_phi2) * volume4 + &
293  reflectance(index_theta02,index_theta1,index_phi1) * volume3 + &
294  reflectance(index_theta01,index_theta2,index_phi1) * volume6 + &
295  reflectance(index_theta01,index_theta2,index_phi2) * volume5 + &
296  reflectance(index_theta02,index_theta2,index_phi2) * volume1 + &
297  reflectance(index_theta02,index_theta2,index_phi1) * volume2
298 
299  num_2 = volume1 + volume2 + &
300  volume3 + volume4 + &
301  volume5 + volume6 + &
302  volume7 + volume8
303 
304  linearinterpolation_3d = num_1/num_2
305 
306 end function linearinterpolation_3d
307 
308 end module modis_numerical_module
subroutine, public bisectionsimple(theta, thetaArray, ntheta, iAngLow, iAngHi)
real function, public linearinterpolation_3d(index_theta01, index_theta02, index_theta1, index_theta2, index_phi1, index_phi2, theta, theta0, phi, theta_grid, theta0_grid, phi_grid, debug, reflectance)
real function, public linearinterpolation(X, Y, XX)
real function, public bilinear_interpolation(X, Y, XX, YY, source, method)
subroutine, public bisectionsearch(xx, x, jlo, jhi)
#define abs(a)
Definition: misc.h:90