FMS  2026.03
Flexible Modeling System
horiz_interp_bilinear.inc
1 !***********************************************************************
2 !* Apache License 2.0
3 !*
4 !* This file is part of the GFDL Flexible Modeling System (FMS).
5 !*
6 !* Licensed under the Apache License, Version 2.0 (the "License");
7 !* you may not use this file except in compliance with the License.
8 !* You may obtain a copy of the License at
9 !*
10 !* http://www.apache.org/licenses/LICENSE-2.0
11 !*
12 !* FMS is distributed in the hope that it will be useful, but WITHOUT
13 !* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied;
14 !* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 !* PARTICULAR PURPOSE. See the License for the specific language
16 !* governing permissions and limitations under the License.
17 !***********************************************************************
18  !> @parblock
19  !! Subroutine to generate weights and mapping indices for bilinear interpolation from a
20  !! rectilinear input grid specified as 1D arrays to any output grid specified as 2D arrays.
21  !! @endparblock
22  subroutine horiz_interp_bilinear_new_1d_ ( Interp, lon_in, lat_in, lon_out, lat_out, &
23  verbose, src_modulo )
24 
25  !-----------------------------------------------------------------------
26  type(horiz_interp_type), intent(inout) :: Interp
27  !< will be populated with interpolation weights and mapping indices.
28  real(FMS_HI_KIND_), intent(in), dimension(:) :: lon_in
29  !< are the longitude coordinates for the rectilinear input grid in radians.
30  real(FMS_HI_KIND_), intent(in), dimension(:) :: lat_in
31  !< are the latitude coordinates for the rectilinear input grid in radians.
32  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lon_out
33  !< are the longitude coordinates for the output grid in radians.
34  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lat_out
35  !< are the latitude coordinates for the output grid in radians.
36  integer, intent(in), optional :: verbose
37  !< sets the verbosity level for output.
38  logical, intent(in), optional :: src_modulo
39  !< is true if the boundary condition along zonal boundary is cyclic.
40 
41  logical :: src_is_modulo
42  integer :: nlon_in, nlat_in, nlon_out, nlat_out, n, m
43  integer :: ie, is, je, js, ln_err, lt_err, warns, iunit
44  real(FMS_HI_KIND_) :: wtw, wte, wts, wtn, lon, lat, tpi, hpi
45  real(FMS_HI_KIND_) :: glt_min, glt_max, gln_min, gln_max, min_lon, max_lon
46  integer,parameter :: kindl = fms_hi_kind_
47  logical :: decreasing_lat !< .True. if latitude is monotically decreasing
48  logical :: decreasing_lon !< .True. if longitude is monotically decreasing
49 
50  warns = 0
51  if(present(verbose)) warns = verbose
52  src_is_modulo = .true.
53  if (present(src_modulo)) src_is_modulo = src_modulo
54 
55  decreasing_lat = .false.
56  if (lat_in(1) > lat_in(2)) decreasing_lat = .true.
57 
58  decreasing_lon = .false.
59  if (lon_in(1) > lon_in(2)) decreasing_lon = .true.
60 
61  hpi = 0.5_kindl * real(pi, fms_hi_kind_)
62  tpi = 4.0_kindl * hpi
63  glt_min = hpi
64  glt_max = -hpi
65  gln_min = tpi
66  gln_max = -tpi
67  min_lon = 0.0_kindl
68  max_lon = tpi
69  ln_err = 0
70  lt_err = 0
71  !-----------------------------------------------------------------------
72 
73  allocate ( interp % HI_KIND_TYPE_ % wti (size(lon_out,1),size(lon_out,2),2), &
74  interp % HI_KIND_TYPE_ % wtj (size(lon_out,1),size(lon_out,2),2), &
75  interp % i_lon (size(lon_out,1),size(lon_out,2),2), &
76  interp % j_lat (size(lon_out,1),size(lon_out,2),2))
77  !-----------------------------------------------------------------------
78 
79  nlon_in = size(lon_in(:)) ; nlat_in = size(lat_in(:))
80  nlon_out = size(lon_out, 1); nlat_out = size(lon_out, 2)
81  interp%nlon_src = nlon_in; interp%nlat_src = nlat_in
82  interp%nlon_dst = nlon_out; interp%nlat_dst = nlat_out
83 
84  if(src_is_modulo) then
85  if(lon_in(nlon_in) - lon_in(1) .gt. tpi + real(epsln, fms_hi_kind_)) &
86  call mpp_error(fatal,'horiz_interp_bilinear_mod: '// &
87  'The range of source grid longitude should be no larger than tpi')
88 
89  if(lon_in(1) .lt. 0.0_kindl .OR. lon_in(nlon_in) > tpi ) then
90  min_lon = lon_in(1)
91  max_lon = lon_in(nlon_in)
92  endif
93  endif
94 
95  do n = 1, nlat_out
96  do m = 1, nlon_out
97  lon = lon_out(m,n)
98  lat = lat_out(m,n)
99 
100  if(src_is_modulo) then
101  if(lon .lt. min_lon) then
102  lon = lon + tpi
103  else if(lon .gt. max_lon) then
104  lon = lon - tpi
105  endif
106  else ! when the input grid is in not cyclic, the output grid should located inside
107  ! the input grid
108  if((lon .lt. lon_in(1)) .or. (lon .gt. lon_in(nlon_in))) &
109  call mpp_error(fatal,'horiz_interp_bilinear_mod: ' //&
110  'when input grid is not modulo, output grid should locate inside input grid')
111  endif
112 
113  glt_min = min(lat,glt_min); glt_max = max(lat,glt_max)
114  gln_min = min(lon,gln_min); gln_max = max(lon,gln_max)
115 
116  is = nearest_index(lon, lon_in )
117  if (decreasing_lon) then
118  ! Lon_in is increasing
119  ! This is so that is is the lower bound.
120  ! For example, if the array is [50 40 30 20 10] and lon is 11, `is` is going to be 5 from `nearest_index`
121  ! but it needs to be 4 so that it can use the data at lon_in(4) and lon_in(5)
122  if( lon_in(is) .lt. lon ) is = max(is-1,1)
123  else
124  ! Lon_in is increasing
125  ! This is so that is is the lower bound.
126  ! For example, if the array is [10 20 30 40 50] and lon is 49, `is` is going to be 5 from `nearest_index`
127  ! but it needs to be 4 so that it can use the data at lon_in(4) and lon_in(5)
128  if( lon_in(is) .gt. lon ) is = max(is-1,1)
129  endif
130  if( lon_in(is) .eq. lon .and. is .eq. nlon_in) is = max(is - 1,1)
131  ie = min(is+1,nlon_in)
132  if(lon_in(is) .ne. lon_in(ie) .and. (decreasing_lon .or. lon_in(is) .le. lon)) then
133  wtw = ( lon_in(ie) - lon) / (lon_in(ie) - lon_in(is) )
134  else
135  ! east or west of the last data value. this could be because a
136  ! cyclic condition is needed or the dataset is too small.
137  ln_err = 1
138  ie = 1
139  is = nlon_in
140  if (lon_in(ie) .ge. lon ) then
141  wtw = (lon_in(ie) -lon)/(lon_in(ie)-lon_in(is)+tpi+real(epsln,fms_hi_kind_))
142  else
143  wtw = (lon_in(ie)-lon+tpi+real(epsln,fms_hi_kind_))/(lon_in(ie)-lon_in(is)+tpi+real(epsln,fms_hi_kind_))
144  endif
145  endif
146  wte = 1.0_kindl - wtw
147 
148  js = nearest_index(lat, lat_in )
149  if (decreasing_lat) then
150  ! Lat_in is decreasing
151  if( lat_in(js) .lt. lat ) js = max(js - 1, 1)
152  else
153  ! Lat_in is increasing
154  if( lat_in(js) .gt. lat ) js = max(js - 1, 1)
155  endif
156  if( lat_in(js) .eq. lat .and. js .eq. nlat_in) js = max(js - 1, 1)
157  je = min(js + 1, nlat_in)
158 
159  if ( lat_in(js) .ne. lat_in(je) .and. (decreasing_lat .or. lat_in(js) .le. lat)) then
160  wts = ( lat_in(je) - lat )/(lat_in(je)-lat_in(js))
161  else
162  ! north or south of the last data value. this could be because a
163  ! pole is not included in the data set or the dataset is too small.
164  ! in either case extrapolate north or south
165  lt_err = 1
166  wts = 1.0_kindl
167  endif
168 
169  wtn = 1.0_kindl - wts
170 
171  interp % i_lon (m,n,1) = is; interp % i_lon (m,n,2) = ie
172  interp % j_lat (m,n,1) = js; interp % j_lat (m,n,2) = je
173  interp % HI_KIND_TYPE_ % wti (m,n,1) = wtw
174  interp % HI_KIND_TYPE_ % wti (m,n,2) = wte
175  interp % HI_KIND_TYPE_ % wtj (m,n,1) = wts
176  interp % HI_KIND_TYPE_ % wtj (m,n,2) = wtn
177 
178  enddo
179  enddo
180 
181  iunit = stdout()
182 
183  if (ln_err .eq. 1 .and. warns > 0) then
184  write (iunit,'(/,(1x,a))') &
185  '==> Warning: the geographic data set does not extend far ', &
186  ' enough east or west - a cyclic boundary ', &
187  ' condition was applied. check if appropriate '
188  write (iunit,'(/,(1x,a,2f8.4))') &
189  ' data required between longitudes:', gln_min, gln_max, &
190  ' data set is between longitudes:', lon_in(1), lon_in(nlon_in)
191  warns = warns - 1
192  endif
193 
194  if (lt_err .eq. 1 .and. warns > 0) then
195  write (iunit,'(/,(1x,a))') &
196  '==> Warning: the geographic data set does not extend far ',&
197  ' enough north or south - extrapolation from ',&
198  ' the nearest data was applied. this may create ',&
199  ' artificial gradients near a geographic pole '
200  write (iunit,'(/,(1x,a,2f8.4))') &
201  ' data required between latitudes:', glt_min, glt_max, &
202  ' data set is between latitudes:', lat_in(1), lat_in(nlat_in)
203  endif
204  interp% HI_KIND_TYPE_ % is_allocated = .true.
205  interp% interp_method = bilinear
206 
207  return
208 
209  end subroutine horiz_interp_bilinear_new_1d_
210 
211  !#######################################################################
212 
213  !> @parblock
214  !! Subroutine to generate weights and mapping indices for bilinear interpolation.
215  !! @endparblock
216  subroutine horiz_interp_bilinear_new_2d_ ( Interp, lon_in, lat_in, lon_out, lat_out, &
217  verbose, src_modulo, new_search, no_crash_when_not_found )
218 
219  !-----------------------------------------------------------------------
220  type(horiz_interp_type), intent(inout) :: Interp
221  !< will be populated with interpolation weights and mapping indices.
222  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lon_in
223  !< are the longitude coordinates for the input grid in radians.
224  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lat_in
225  !< are the latitude coordinates for the input grid in radians.
226  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lon_out
227  !< are the longitude coordinates for the output grid in radians.
228  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lat_out
229  !< are the latitude coordinates for the output grid in radians.
230  integer, intent(in), optional :: verbose
231  !< sets the verbosity level for output.
232  logical, intent(in), optional :: src_modulo
233  !< is true if the boundary condition along zonal boundary is cyclic.
234  logical, intent(in), optional :: new_search
235  !< sets the neighbor search routine to find_neighbor_new if true.
236  logical, intent(in), optional :: no_crash_when_not_found
237  !< sets find_neighbor_new to error when nearest neighbors are not found.
238  integer :: warns
239  logical :: src_is_modulo
240  integer :: nlon_in, nlat_in, nlon_out, nlat_out
241  integer :: m, n, is, ie, js, je, num_solution
242  real(FMS_HI_KIND_) :: lon, lat, quadra, x, y, y1, y2
243  real(FMS_HI_KIND_) :: a1, b1, c1, d1, a2, b2, c2, d2, a, b, c
244  real(FMS_HI_KIND_) :: lon1, lat1, lon2, lat2, lon3, lat3, lon4, lat4
245  real(FMS_HI_KIND_) :: tpi, lon_min, lon_max
246  real(FMS_HI_KIND_) :: epsln2
247  logical :: use_new_search, no_crash
248 
249  integer, parameter :: kindl=fms_hi_kind_
250 
251  tpi = 2.0_kindl * real(pi, fms_hi_kind_)
252 
253  warns = 0
254  if(present(verbose)) warns = verbose
255  src_is_modulo = .true.
256  if (present(src_modulo)) src_is_modulo = src_modulo
257  use_new_search = .false.
258  if (present(new_search)) use_new_search = new_search
259  no_crash = .false.
260  if(present(no_crash_when_not_found)) no_crash = no_crash_when_not_found
261 
262  ! make sure lon and lat has the same dimension
263  if(size(lon_out,1) /= size(lat_out,1) .or. size(lon_out,2) /= size(lat_out,2) ) &
264  call mpp_error(fatal,'horiz_interp_bilinear_mod: when using bilinear ' // &
265  'interplation, the output grids should be geographical grids')
266 
267  if(size(lon_in,1) /= size(lat_in,1) .or. size(lon_in,2) /= size(lat_in,2) ) &
268  call mpp_error(fatal,'horiz_interp_bilinear_mod: when using bilinear '// &
269  'interplation, the input grids should be geographical grids')
270 
271  !--- get the grid size
272  nlon_in = size(lon_in,1) ; nlat_in = size(lat_in,2)
273  nlon_out = size(lon_out,1); nlat_out = size(lon_out,2)
274  interp%nlon_src = nlon_in; interp%nlat_src = nlat_in
275  interp%nlon_dst = nlon_out; interp%nlat_dst = nlat_out
276 
277  allocate ( interp % HI_KIND_TYPE_ % wti (size(lon_out,1),size(lon_out,2),2), &
278  interp % HI_KIND_TYPE_ % wtj (size(lon_out,1),size(lon_out,2),2), &
279  interp % i_lon (size(lon_out,1),size(lon_out,2),2), &
280  interp % j_lat (size(lon_out,1),size(lon_out,2),2))
281 
282  !--- first fine the neighbor points for the destination points.
283  if(use_new_search) then
284  epsln2 = real(epsln,fms_hi_kind_)* 1.0e5_kindl
285  call find_neighbor_new_(interp, lon_in, lat_in, lon_out, lat_out, src_is_modulo, no_crash)
286  else
287  if(kindl == r8_kind) then
288  epsln2 = epsln
289  else
290  epsln2 = epsln_r4
291  endif
292  call find_neighbor_(interp, lon_in, lat_in, lon_out, lat_out, src_is_modulo)
293  endif
294 
295  !***************************************************************************
296  ! Algorithm explanation (from disscussion with Steve Garner ) *
297  ! *
298  ! lon(x,y) = a1*x + b1*y + c1*x*y + d1 (1) *
299  ! lat(x,y) = a2*x + b2*y + c2*x*y + d2 (2) *
300  ! f (x,y) = a3*x + b3*y + c3*x*y + d3 (3) *
301  ! with x and y is between 0 and 1. *
302  ! lon1 = lon(0,0) = d1, lat1 = lat(0,0) = d2 *
303  ! lon2 = lon(1,0) = a1+d1, lat2 = lat(1,0) = a2+d2 *
304  ! lon3 = lon(1,1) = a1+b1+c1+d1, lat3 = lat(1,1) = a2+b2+c2+d2 *
305  ! lon4 = lon(0,1) = b1+d1, lat4 = lat(0,1) = b2+d2 *
306  ! where (lon1,lat1),(lon2,lat2),(lon3,lat3),(lon4,lat4) represents *
307  ! the four corners starting from the left lower corner of grid box *
308  ! that encloses a destination grid ( the rotation direction is *
309  ! counterclockwise ). With these conditions, we get *
310  ! a1 = lon2-lon1, a2 = lat2-lat1 *
311  ! b1 = lon4-lon1, b2 = lat4-lat1 *
312  ! c1 = lon3-lon2-lon4+lon1, c2 = lat3-lat2-lat4+lat1 *
313  ! d1 = lon1 d2 = lat1 *
314  ! So given any point (lon,lat), from equation (1) and (2) we can *
315  ! solve (x,y). *
316  ! From equation (3) *
317  ! f1 = f(0,0) = d3, f2 = f(1,0) = a3+d3 *
318  ! f3 = f(1,1) = a3+b3+c3+d3, f4 = f(0,1) = b3+d3 *
319  ! we obtain *
320  ! a3 = f2-f1, b3 = f4-f1 *
321  ! c3 = f3-f2-f4+f1, d3 = f1 *
322  ! at point (lon,lat) ---> (x,y) *
323  ! f(x,y) = (f2-f1)x + (f4-f1)y + (f3-f2-f4+f1)xy + f1 *
324  ! = f1*(1-x)*(1-y) + f2*x*(1-y) + f3*x*y + f4*y*(1-x) *
325  ! wtw=1-x; wte=x; wts=1-y; xtn=y *
326  ! *
327  !***************************************************************************
328 
329  lon_min = minval(lon_in);
330  lon_max = maxval(lon_in);
331  !--- calculate the weight
332  do n = 1, nlat_out
333  do m = 1, nlon_out
334  lon = lon_out(m,n)
335  lat = lat_out(m,n)
336  if(lon .lt. lon_min) then
337  lon = lon + tpi
338  else if(lon .gt. lon_max) then
339  lon = lon - tpi
340  endif
341  is = interp%i_lon(m,n,1); ie = interp%i_lon(m,n,2)
342  js = interp%j_lat(m,n,1); je = interp%j_lat(m,n,2)
343  if( is == dummy) cycle
344  lon1 = lon_in(is,js); lat1 = lat_in(is,js);
345  lon2 = lon_in(ie,js); lat2 = lat_in(ie,js);
346  lon3 = lon_in(ie,je); lat3 = lat_in(ie,je);
347  lon4 = lon_in(is,je); lat4 = lat_in(is,je);
348  if(lon .lt. lon_min) then
349  lon1 = lon1 -tpi; lon4 = lon4 - tpi
350  else if(lon .gt. lon_max) then
351  lon2 = lon2 +tpi; lon3 = lon3 + tpi
352  endif
353  a1 = lon2-lon1
354  b1 = lon4-lon1
355  c1 = lon1+lon3-lon4-lon2
356  d1 = lon1
357  a2 = lat2-lat1
358  b2 = lat4-lat1
359  c2 = lat1+lat3-lat4-lat2
360  d2 = lat1
361  !--- the coefficient of the quadratic equation
362  a = b2*c1-b1*c2
363  b = a1*b2-a2*b1+c1*d2-c2*d1+c2*lon-c1*lat
364  c = a2*lon-a1*lat+a1*d2-a2*d1
365  quadra = b*b-4._kindl*a*c
366  if(abs(quadra) < real(epsln, fms_hi_kind_)) quadra = 0.0_kindl
367  if(quadra < 0.0_kindl) call mpp_error(fatal, &
368  "horiz_interp_bilinear_mod: No solution existed for this quadratic equation")
369  if ( abs(a) .lt. epsln2) then ! a = 0 is a linear equation
370  if( abs(b) .lt. real(epsln,fms_hi_kind_)) call mpp_error(fatal, &
371  "horiz_interp_bilinear_mod: no unique solution existed for this linear equation")
372  y = -c/b
373  else
374  y1 = 0.5_kindl*(-b+sqrt(quadra))/a
375  y2 = 0.5_kindl*(-b-sqrt(quadra))/a
376  if(abs(y1) < epsln2) y1 = 0.0_kindl
377  if(abs(y2) < epsln2) y2 = 0.0_kindl
378  if(abs(1.0_kindl-y1) < epsln2) y1 = 1.0_kindl
379  if(abs(1.0_kindl-y2) < epsln2) y2 = 1.0_kindl
380  num_solution = 0
381  if(y1 >= 0.0_kindl .and. y1 <= 1.0_kindl) then
382  y = y1
383  num_solution = num_solution +1
384  endif
385  if(y2 >= 0.0_kindl .and. y2 <= 1.0_kindl) then
386  y = y2
387  num_solution = num_solution + 1
388  endif
389  if(num_solution == 0) then
390  call mpp_error(fatal, "horiz_interp_bilinear_mod: No solution found")
391  else if(num_solution == 2) then
392  call mpp_error(fatal, "horiz_interp_bilinear_mod: Two solutions found")
393  endif
394  endif
395  if(abs(a1+c1*y) < real(epsln,fms_hi_kind_)) call mpp_error(fatal, &
396  "horiz_interp_bilinear_mod: the denomenator is 0")
397  if(abs(y) < epsln2) y = 0.0_kindl
398  if(abs(1.0_kindl-y) < epsln2) y = 1.0_kindl
399  x = (lon-b1*y-d1)/(a1+c1*y)
400  if(abs(x) < epsln2) x = 0.0_kindl
401  if(abs(1.0_kindl-x) < epsln2) x = 1.0_kindl
402  ! x and y should be between 0 and 1.
403  !! Added for ECDA
404  if(use_new_search) then
405  if (x < 0.0_kindl) x = 0.0_kindl ! snz
406  if (y < 0.0_kindl) y = 0.0_kindl ! snz
407  if (x > 1.0_kindl) x = 1.0_kindl
408  if (y > 1.0_kindl) y = 1.0_kindl
409  endif
410  !! if using 4 byte reals, allow a bit more tolerance (1e-4) on weight values
411  if( kindl == r8_kind ) then
412  if( x>1.0_kindl .or. x<0.0_kindl .or. y>1.0_kindl .or. y < 0.0_kindl) &
413  call mpp_error(fatal, "horiz_interp_bilinear_mod: weight should be between 0 and 1, x=" &
414  //string(x)//" y="//string(y))
415  else
416  if( x>1.0_kindl + epsln2 .or. x<0.0_kindl - epsln2 .or. &
417  y>1.0_kindl + epsln2 .or. y < 0.0_kindl - epsln2) &
418  call mpp_error(fatal, "horiz_interp_bilinear_mod: weight should be between 0 and 1, x=" &
419  //string(x)//" y="//string(y))
420  endif
421  interp % HI_KIND_TYPE_ % wti(m,n,1)=1.0_kindl-x
422  interp % HI_KIND_TYPE_ % wti(m,n,2)=x
423  interp % HI_KIND_TYPE_ % wtj(m,n,1)=1.0_kindl-y
424  interp % HI_KIND_TYPE_ % wtj(m,n,2)=y
425  enddo
426  enddo
427 
428  interp% HI_KIND_TYPE_ % is_allocated = .true.
429  interp% interp_method = bilinear
430  end subroutine horiz_interp_bilinear_new_2d_
431 
432  !#######################################################################
433  !> @parblock
434  !! Subroutine to search the source grid to find the grid box that encloses
435  !! each destination grid. Uses the original searching algorithm.
436  !! @endparblock
437  subroutine find_neighbor_ ( Interp, lon_in, lat_in, lon_out, lat_out, src_modulo )
438  type(horiz_interp_type), intent(inout) :: Interp
439  !< will be populated with mapping indices.
440  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lon_in
441  !< are the longitude coordinates for the input grid in radians.
442  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lat_in
443  !< are the latitude coordinates for the input grid in radians.
444  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lon_out
445  !< are the longitude coordinates for the output grid in radians.
446  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lat_out
447  !< are the latitude coordinates for the output grid in radians.
448  logical, intent(in) :: src_modulo
449  !< is true if the boundary condition along zonal boundary is cyclic.
450 
451  integer :: nlon_in, nlat_in, nlon_out, nlat_out
452  integer :: max_step, n, m, l, i, j, ip1, jp1, step
453  integer :: is, js, jstart, jend, istart, iend, npts
454  integer, allocatable, dimension(:) :: ilon, jlat
455  real(FMS_HI_KIND_) :: lon_min, lon_max, lon, lat, tpi
456  logical :: found
457  real(FMS_HI_KIND_) :: lon1, lat1, lon2, lat2, lon3, lat3, lon4, lat4
458  integer, parameter :: kindl=fms_hi_kind_
459 
460  tpi = 2.0_kindl*real(pi, fms_hi_kind_)
461  nlon_in = size(lon_in,1) ; nlat_in = size(lat_in,2)
462  nlon_out = size(lon_out,1); nlat_out = size(lon_out,2)
463 
464  lon_min = minval(lon_in);
465  lon_max = maxval(lon_in);
466 
467  max_step = max(nlon_in,nlat_in) ! can be adjusted if needed
468  allocate(ilon(5*max_step), jlat(5*max_step) )
469 
470  do n = 1, nlat_out
471  do m = 1, nlon_out
472  found = .false.
473  lon = lon_out(m,n)
474  lat = lat_out(m,n)
475 
476  if(src_modulo) then
477  if(lon .lt. lon_min) then
478  lon = lon + tpi
479  else if(lon .gt. lon_max) then
480  lon = lon - tpi
481  endif
482  else
483  if(lon .lt. lon_min .or. lon .gt. lon_max ) &
484  call mpp_error(fatal,'horiz_interp_bilinear_mod: ' //&
485  'when input grid is not modulo, output grid should locate inside input grid')
486  endif
487  !--- search for the surrounding four points locatioon.
488  if(m==1 .and. n==1) then
489  j_loop: do j = 1, nlat_in-1
490  do i = 1, nlon_in
491  ip1 = i+1
492  jp1 = j+1
493  if(i==nlon_in) then
494  if(src_modulo)then
495  ip1 = 1
496  else
497  cycle
498  endif
499  endif
500  lon1 = lon_in(i, j); lat1 = lat_in(i,j)
501  lon2 = lon_in(ip1,j); lat2 = lat_in(ip1,j)
502  lon3 = lon_in(ip1,jp1); lat3 = lat_in(ip1,jp1)
503  lon4 = lon_in(i, jp1); lat4 = lat_in(i, jp1)
504 
505  if(lon .lt. lon_min .or. lon .gt. lon_max) then
506  if(i .ne. nlon_in) then
507  cycle
508  else
509  if(lon .lt. lon_min) then
510  lon1 = lon1 -tpi; lon4 = lon4 - tpi
511  else if(lon .gt. lon_max) then
512  lon2 = lon2 +tpi; lon3 = lon3 + tpi
513  endif
514  endif
515  endif
516 
517  if(lat .ge. intersect(lon1,lat1,lon2,lat2,lon))then ! south
518  if(lon .le. intersect(lat2,lon2,lat3,lon3,lat))then ! east
519  if(lat .le. intersect(lon3,lat3,lon4,lat4,lon))then ! north
520  if(lon .ge. intersect(lat4,lon4,lat1,lon1,lat))then ! west
521  found = .true.
522  interp % i_lon (m,n,1) = i; interp % i_lon (m,n,2) = ip1
523  interp % j_lat (m,n,1) = j; interp % j_lat (m,n,2) = jp1
524  exit j_loop
525  endif
526  endif
527  endif
528  endif
529  enddo
530  enddo j_loop
531  else
532  step = 0
533  do while ( .not. found .and. step .lt. max_step )
534  !--- take the adajcent point as the starting point
535  if(m == 1) then
536  is = interp % i_lon (m,n-1,1)
537  js = interp % j_lat (m,n-1,1)
538  else
539  is = interp % i_lon (m-1,n,1)
540  js = interp % j_lat (m-1,n,1)
541  endif
542  if(step==0) then
543  npts = 1
544  ilon(1) = is
545  jlat(1) = js
546  else
547  npts = 0
548  !--- bottom boundary
549  jstart = max(js-step,1)
550  jend = min(js+step,nlat_in)
551 
552  do l = -step, step
553  i = is+l
554  if(src_modulo)then
555  if( i < 1) then
556  i = i + nlon_in
557  else if (i > nlon_in) then
558  i = i - nlon_in
559  endif
560  if( i < 1 .or. i > nlon_in) call mpp_error(fatal, &
561  'horiz_interp_bilinear_mod: max_step is too big, decrease max_step' )
562  else
563  if( i < 1 .or. i > nlon_in) cycle
564  endif
565 
566  npts = npts + 1
567  ilon(npts) = i
568  jlat(npts) = jstart
569  enddo
570 
571  !--- right and left boundary -----------------------------------------------
572  istart = is - step
573  iend = is + step
574  if(src_modulo) then
575  if( istart < 1) istart = istart + nlon_in
576  if( iend > nlon_in) iend = iend - nlon_in
577  else
578  istart = max(istart,1)
579  iend = min(iend, nlon_in)
580  endif
581  do l = -step, step
582  j = js+l
583  if( j < 1 .or. j > nlat_in .or. j==jstart .or. j==jend) cycle
584  npts = npts+1
585  ilon(npts) = istart
586  jlat(npts) = j
587  npts = npts+1
588  ilon(npts) = iend
589  jlat(npts) = j
590  end do
591 
592  !--- top boundary
593 
594  do l = -step, step
595  i = is+l
596  if(src_modulo)then
597  if( i < 1) then
598  i = i + nlon_in
599  else if (i > nlon_in) then
600  i = i - nlon_in
601  endif
602  if( i < 1 .or. i > nlon_in) call mpp_error(fatal, &
603  'horiz_interp_bilinear_mod: max_step is too big, decrease max_step' )
604  else
605  if( i < 1 .or. i > nlon_in) cycle
606  endif
607 
608  npts = npts + 1
609  ilon(npts) = i
610  jlat(npts) = jend
611  enddo
612 
613 
614  end if
615 
616  !--- find the surrouding points
617  do l = 1, npts
618  i = ilon(l)
619  j = jlat(l)
620  ip1 = i+1
621  if(ip1>nlon_in) then
622  if(src_modulo) then
623  ip1 = 1
624  else
625  cycle
626  endif
627  endif
628  jp1 = j+1
629  if(jp1>nlat_in) cycle
630  lon1 = lon_in(i, j); lat1 = lat_in(i,j)
631  lon2 = lon_in(ip1,j); lat2 = lat_in(ip1,j)
632  lon3 = lon_in(ip1,jp1); lat3 = lat_in(ip1,jp1)
633  lon4 = lon_in(i, jp1); lat4 = lat_in(i, jp1)
634 
635  if(lon .lt. lon_min .or. lon .gt. lon_max) then
636  if(i .ne. nlon_in) then
637  cycle
638  else
639  if(lon .lt. lon_min) then
640  lon1 = lon1 -tpi; lon4 = lon4 - tpi
641  else if(lon .gt. lon_max) then
642  lon2 = lon2 +tpi; lon3 = lon3 + tpi
643  endif
644  endif
645  endif
646 
647  if(lat .ge. intersect(lon1,lat1,lon2,lat2,lon))then ! south
648  if(lon .le. intersect(lat2,lon2,lat3,lon3,lat))then ! east
649  if(lat .le. intersect(lon3,lat3,lon4,lat4,lon))then !north
650  if(lon .ge. intersect(lat4,lon4,lat1,lon1,lat))then ! west
651  found = .true.
652  is=i; js=j
653  interp % i_lon (m,n,1) = i; interp % i_lon (m,n,2) = ip1
654  interp % j_lat (m,n,1) = j; interp % j_lat (m,n,2) = jp1
655  exit
656  endif
657  endif
658  endif
659  endif
660  enddo
661  step = step + 1
662  enddo
663  endif
664  if(.not.found) then
665  print *,'lon,lat=',lon*180.0_kindl/real(pi,fms_hi_kind_),lat*180.0_kindl/real(pi,fms_hi_kind_)
666  print *,'npts=',npts
667  print *,'is,ie= ',istart,iend
668  print *,'js,je= ',jstart,jend
669  print *,'lon_in(is,js)=',lon_in(istart,jstart)*180.0_kindl/real(pi,fms_hi_kind_)
670  print *,'lon_in(ie,js)=',lon_in(iend,jstart)*180.0_kindl/real(pi,fms_hi_kind_)
671  print *,'lat_in(is,js)=',lat_in(istart,jstart)*180.0_kindl/real(pi,fms_hi_kind_)
672  print *,'lat_in(ie,js)=',lat_in(iend,jstart)*180.0_kindl/real(pi,fms_hi_kind_)
673  print *,'lon_in(is,je)=',lon_in(istart,jend)*180.0_kindl/real(pi,fms_hi_kind_)
674  print *,'lon_in(ie,je)=',lon_in(iend,jend)*180.0_kindl/real(pi,fms_hi_kind_)
675  print *,'lat_in(is,je)=',lat_in(istart,jend)*180.0_kindl/real(pi,fms_hi_kind_)
676  print *,'lat_in(ie,je)=',lat_in(iend,jend)*180.0_kindl/real(pi,fms_hi_kind_)
677 
678  call mpp_error(fatal, &
679  'FIND_NEIGHBOR_: the destination point is not inside the source grid' )
680  endif
681  enddo
682  enddo
683 
684  end subroutine find_neighbor_
685 
686  !#######################################################################
687 
688  !> @parblock
689  !! Internally used function that returns True if gridpoint(x, y) is inside a polygon
690  !! specified by polyx and polyy. Used to find nearest neighbors in find_neighbor_new.
691  !! @endparblock
692  function inside_polygon_(polyx, polyy, x, y)
693  real(FMS_HI_KIND_), dimension(:), intent(in) :: polyx
694  !< are the longitude coordinates of corners
695  real(FMS_HI_KIND_), dimension(:), intent(in) :: polyy
696  !< are the latitude coordinates of corners
697  real(FMS_HI_KIND_), intent(in) :: x
698  !< is the x coordinate of point to be tested
699  real(FMS_HI_KIND_), intent(in) :: y
700  !< is the y coordinate of point to be tested
701  logical :: INSIDE_POLYGON_
702  integer :: i, j, nedges
703  real(FMS_HI_KIND_) :: xx
704 
705  inside_polygon_ = .false.
706  nedges = size(polyx(:))
707  j = nedges
708  do i = 1, nedges
709  if( (polyy(i) < y .AND. polyy(j) >= y) .OR. (polyy(j) < y .AND. polyy(i) >= y) ) then
710  xx = polyx(i)+(y-polyy(i))/(polyy(j)-polyy(i))*(polyx(j)-polyx(i))
711  if( xx == x ) then
712  inside_polygon_ = .true.
713  return
714  else if( xx < x ) then
715  inside_polygon_ = .not. inside_polygon_
716  endif
717  endif
718  j = i
719  enddo
720 
721  return
722 
723  end function
724 
725  !#######################################################################
726  !> @parblock
727  !! The newer internally used subroutine to search the source grid for grid cells that neighbor
728  !! each destination grid cell.
729  !! @endparblock
730  subroutine find_neighbor_new_( Interp, lon_in, lat_in, lon_out, lat_out, src_modulo, no_crash )
731  type(horiz_interp_type), intent(inout) :: Interp
732  !< will be populated with mapping indices.
733  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lon_in
734  !< are the longitude coordinates for the input grid in radians.
735  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lat_in
736  !< are the latitude coordinates for the input grid in radians.
737  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lon_out
738  !< are the longitude coordinates for the output grid in radians.
739  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: lat_out
740  !< are the latitude coordinates for the output grid in radians.
741  logical, intent(in) :: src_modulo
742  !< is true if the boundary condition along zonal boundary is cyclic.
743  logical, intent(in) :: no_crash
744  !< sets Interp%i_lon and Interp%j_lat to DUMMY values for cells with no nearest neighbors when true.
745 
746  integer :: nlon_in, nlat_in, nlon_out, nlat_out
747  integer :: max_step, n, m, l, i, j, ip1, jp1, step
748  integer :: is, js, jstart, jend, istart, iend, npts
749  integer, allocatable, dimension(:) :: ilon, jlat
750  real(FMS_HI_KIND_) :: lon_min, lon_max, lon, lat, tpi
751  logical :: found
752  real(FMS_HI_KIND_) :: polyx(4), polyy(4)
753  real(FMS_HI_KIND_) :: min_lon, min_lat, max_lon, max_lat
754  integer, parameter :: step_div=8, kindl = fms_hi_kind_
755 
756  tpi = 2.0_kindl * real(pi, fms_hi_kind_)
757  nlon_in = size(lon_in,1) ; nlat_in = size(lat_in,2)
758  nlon_out = size(lon_out,1); nlat_out = size(lon_out,2)
759 
760  lon_min = minval(lon_in);
761  lon_max = maxval(lon_in);
762 
763  max_step = min(nlon_in,nlat_in)/step_div ! can be adjusted if needed
764  allocate(ilon(step_div*max_step), jlat(step_div*max_step) )
765 
766  do n = 1, nlat_out
767  do m = 1, nlon_out
768  found = .false.
769  lon = lon_out(m,n)
770  lat = lat_out(m,n)
771 
772  if(src_modulo) then
773  if(lon .lt. lon_min) then
774  lon = lon + tpi
775  else if(lon .gt. lon_max) then
776  lon = lon - tpi
777  endif
778  else
779  if(lon .lt. lon_min .or. lon .gt. lon_max ) &
780  call mpp_error(fatal,'horiz_interp_bilinear_mod: ' //&
781  'when input grid is not modulo, output grid should locate inside input grid')
782  endif
783  !--- search for the surrounding four points locatioon.
784  if(m==1 .and. n==1) then
785  j_loop: do j = 1, nlat_in-1
786  do i = 1, nlon_in
787  ip1 = i+1
788  jp1 = j+1
789  if(i==nlon_in) then
790  if(src_modulo)then
791  ip1 = 1
792  else
793  cycle
794  endif
795  endif
796 
797  polyx(1) = lon_in(i, j); polyy(1) = lat_in(i,j)
798  polyx(2) = lon_in(ip1,j); polyy(2) = lat_in(ip1,j)
799  polyx(3) = lon_in(ip1,jp1); polyy(3) = lat_in(ip1,jp1)
800  polyx(4) = lon_in(i, jp1); polyy(4) = lat_in(i, jp1)
801  if(lon .lt. lon_min .or. lon .gt. lon_max) then
802  if(i .ne. nlon_in) then
803  cycle
804  else
805  if(lon .lt. lon_min) then
806  polyx(1) = polyx(1) -tpi; polyx(4) = polyx(4) - tpi
807  else if(lon .gt. lon_max) then
808  polyx(2) = polyx(2) +tpi; polyx(3) = polyx(3) + tpi
809  endif
810  endif
811  endif
812 
813  min_lon = minval(polyx)
814  max_lon = maxval(polyx)
815  min_lat = minval(polyy)
816  max_lat = maxval(polyy)
817 ! if( lon .GE. min_lon .AND. lon .LE. max_lon .AND. &
818 ! lat .GE. min_lat .AND. lat .LE. max_lat ) then
819 ! print*, 'i =', i, 'j = ', j
820 ! print '(5f15.11)', lon, polyx
821 ! print '(5f15.11)', lat, polyy
822 ! endif
823 
824  if(inside_polygon_(polyx, polyy, lon, lat)) then
825  found = .true.
826 ! print*, " found ", i, j
827  interp % i_lon (m,n,1) = i; interp % i_lon (m,n,2) = ip1
828  interp % j_lat (m,n,1) = j; interp % j_lat (m,n,2) = jp1
829  exit j_loop
830  endif
831  enddo
832  enddo j_loop
833  else
834  step = 0
835  do while ( .not. found .and. step .lt. max_step )
836  !--- take the adajcent point as the starting point
837  if(m == 1) then
838  is = interp % i_lon (m,n-1,1)
839  js = interp % j_lat (m,n-1,1)
840  else
841  is = interp % i_lon (m-1,n,1)
842  js = interp % j_lat (m-1,n,1)
843  endif
844  if(step==0) then
845  npts = 1
846  ilon(1) = is
847  jlat(1) = js
848  else
849  npts = 0
850  !--- bottom and top boundary
851  jstart = max(js-step,1)
852  jend = min(js+step,nlat_in)
853 
854  do l = -step, step
855  i = is+l
856  if(src_modulo)then
857  if( i < 1) then
858  i = i + nlon_in
859  else if (i > nlon_in) then
860  i = i - nlon_in
861  endif
862  if( i < 1 .or. i > nlon_in) call mpp_error(fatal, &
863  'horiz_interp_bilinear_mod: max_step is too big, decrease max_step' )
864  else
865  if( i < 1 .or. i > nlon_in) cycle
866  endif
867 
868  npts = npts + 1
869  ilon(npts) = i
870  jlat(npts) = jstart
871  npts = npts + 1
872  ilon(npts) = i
873  jlat(npts) = jend
874  enddo
875 
876  !--- right and left boundary -----------------------------------------------
877  istart = is - step
878  iend = is + step
879  if(src_modulo) then
880  if( istart < 1) istart = istart + nlon_in
881  if( iend > nlon_in) iend = iend - nlon_in
882  else
883  istart = max(istart,1)
884  iend = min(iend, nlon_in)
885  endif
886  do l = -step, step
887  j = js+l
888  if( j < 1 .or. j > nlat_in) cycle
889  npts = npts+1
890  ilon(npts) = istart
891  jlat(npts) = j
892  npts = npts+1
893  ilon(npts) = iend
894  jlat(npts) = j
895  end do
896  end if
897 
898  !--- find the surrouding points
899  do l = 1, npts
900  i = ilon(l)
901  j = jlat(l)
902  ip1 = i+1
903  if(ip1>nlon_in) then
904  if(src_modulo) then
905  ip1 = 1
906  else
907  cycle
908  endif
909  endif
910  jp1 = j+1
911  if(jp1>nlat_in) cycle
912  polyx(1) = lon_in(i, j); polyy(1) = lat_in(i,j)
913  polyx(2) = lon_in(ip1,j); polyy(2) = lat_in(ip1,j)
914  polyx(3) = lon_in(ip1,jp1); polyy(3) = lat_in(ip1,jp1)
915  polyx(4) = lon_in(i, jp1); polyy(4) = lat_in(i, jp1)
916  if(inside_polygon_(polyx, polyy, lon, lat)) then
917  found = .true.
918  interp % i_lon (m,n,1) = i; interp % i_lon (m,n,2) = ip1
919  interp % j_lat (m,n,1) = j; interp % j_lat (m,n,2) = jp1
920  exit
921  endif
922  enddo
923  step = step + 1
924  enddo
925  endif
926  if(.not.found) then
927  if(no_crash) then
928  interp % i_lon (m,n,1:2) = dummy
929  interp % j_lat (m,n,1:2) = dummy
930  print*,'lon,lat=',lon,lat ! snz
931  else
932  call mpp_error(fatal, &
933  'horiz_interp_bilinear_mod: the destination point is not inside the source grid' )
934  endif
935  endif
936  enddo
937  enddo
938 
939  end subroutine find_neighbor_new_
940 
941  !#######################################################################
942  !> @parblock
943  !! Internally used function in the older find_neighbor subroutine.
944  !! @endparblock
945  function intersect_(x1, y1, x2, y2, x)
946  real(FMS_HI_KIND_), intent(in) :: x1
947  real(FMS_HI_KIND_), intent(in) :: y1
948  real(FMS_HI_KIND_), intent(in) :: x2
949  real(FMS_HI_KIND_), intent(in) :: y2
950  real(FMS_HI_KIND_), intent(in) :: x
951  real(FMS_HI_KIND_) :: INTERSECT_
952 
953  intersect_ = (y2-y1)*(x-x1)/(x2-x1) + y1
954 
955  return
956 
957  end function intersect_
958 
959  !#######################################################################
960 
961  !> @parblock
962  !! Subroutine to bilinearly interpolate 2D data using the weights stored in Interp.
963  !! @endparblock
964  subroutine horiz_interp_bilinear_ ( Interp, data_in, data_out, verbose, mask_in,mask_out, &
965  missing_value, missing_permit, new_handle_missing )
966  !-----------------------------------------------------------------------
967  type (horiz_interp_type), intent(in) :: Interp
968  !< contains the interpolation weights and mapping indices
969  real(FMS_HI_KIND_), intent(in), dimension(:,:) :: data_in
970  !< is the data to interpolate
971  real(FMS_HI_KIND_), intent(out), dimension(:,:) :: data_out
972  !< will contain the interpolated data
973  integer, intent(in), optional :: verbose
974  !< sets the verbosity level: 0 = no output; 1 for min, max, means; 2 for all output
975  real(FMS_HI_KIND_), intent(in), dimension(:,:), optional :: mask_in
976  !< is used to mask the input data when new_handle_missing = .false..
977  !! Mask values for cells with missing_value will be set to 0.0
978  real(FMS_HI_KIND_), intent(out), dimension(:,:), optional :: mask_out
979  !< will be zero for output cells set to missing_value.
980  real(FMS_HI_KIND_), intent(in), optional :: missing_value
981  !< is the missing_value.
982  integer, intent(in), optional :: missing_permit
983  !< is the maximum number of missing values allowed in data_in; not used when new_handle_missing = .true..
984  logical, intent(in), optional :: new_handle_missing
985  !< turns on the new missing value handling algorithm if .true..
986  !-----------------------------------------------------------------------
987  integer :: nlon_in, nlat_in, nlon_out, nlat_out, n, m, &
988  is, ie, js, je, iverbose, max_missing, num_missing, &
989  miss_in, miss_out, iunit
990  real(FMS_HI_KIND_) :: dwtsum, wtsum, min_in, max_in, avg_in, &
991  min_out, max_out, avg_out, wtw, wte, wts, wtn
992  real(FMS_HI_KIND_) :: mask(size(data_in,1), size(data_in,2) )
993  logical :: set_to_missing, is_missing(4), new_handler
994  real(FMS_HI_KIND_) :: f1, f2, f3, f4, middle, w, s
995  integer, parameter :: kindl = fms_hi_kind_
996 
997  num_missing = 0
998 
999  nlon_in = interp%nlon_src; nlat_in = interp%nlat_src
1000  nlon_out = interp%nlon_dst; nlat_out = interp%nlat_dst
1001 
1002  if(present(mask_in)) then
1003  mask = mask_in
1004  else
1005  mask = 1.0_kindl
1006  endif
1007 
1008  if (present(verbose)) then
1009  iverbose = verbose
1010  else
1011  iverbose = 0
1012  endif
1013 
1014  if(present(missing_permit)) then
1015  max_missing = missing_permit
1016  else
1017  max_missing = 0
1018  endif
1019 
1020  if(present(new_handle_missing)) then
1021  new_handler = new_handle_missing
1022  else
1023  new_handler = .false.
1024  endif
1025 
1026  if(max_missing .gt. 3 .or. max_missing .lt. 0) call mpp_error(fatal, &
1027  'horiz_interp_bilinear_mod: missing_permit should be between 0 and 3')
1028 
1029  if (size(data_in,1) /= nlon_in .or. size(data_in,2) /= nlat_in) &
1030  call mpp_error(fatal,'horiz_interp_bilinear_mod: size of input array incorrect')
1031 
1032  if (size(data_out,1) /= nlon_out .or. size(data_out,2) /= nlat_out) &
1033  call mpp_error(fatal,'horiz_interp_bilinear_mod: size of output array incorrect')
1034 
1035  if(new_handler) then
1036  if( .not. present(missing_value) ) call mpp_error(fatal, &
1037  "horiz_interp_bilinear_mod: misisng_value must be present when new_handle_missing is .true.")
1038  if( present(mask_in) ) call mpp_error(fatal, &
1039  "horiz_interp_bilinear_mod: mask_in should not be present when new_handle_missing is .true.")
1040  do n = 1, nlat_out
1041  do m = 1, nlon_out
1042  is = interp % i_lon (m,n,1); ie = interp % i_lon (m,n,2)
1043  js = interp % j_lat (m,n,1); je = interp % j_lat (m,n,2)
1044  wtw = interp % HI_KIND_TYPE_ % wti (m,n,1)
1045  wte = interp % HI_KIND_TYPE_ % wti (m,n,2)
1046  wts = interp % HI_KIND_TYPE_ % wtj (m,n,1)
1047  wtn = interp % HI_KIND_TYPE_ % wtj (m,n,2)
1048 
1049  is_missing = .false.
1050  num_missing = 0
1051  set_to_missing = .false.
1052  if(data_in(is,js) == missing_value) then
1053  num_missing = num_missing+1
1054  is_missing(1) = .true.
1055  if(wtw .GE. 0.5_kindl .AND. wts .GE. 0.5_kindl) set_to_missing = .true.
1056  endif
1057 
1058  if(data_in(ie,js) == missing_value) then
1059  num_missing = num_missing+1
1060  is_missing(2) = .true.
1061  if(wte .GE. 0.5_kindl .AND. wts .GE. 0.5_kindl ) set_to_missing = .true.
1062  endif
1063  if(data_in(ie,je) == missing_value) then
1064  num_missing = num_missing+1
1065  is_missing(3) = .true.
1066  if(wte .GE. 0.5_kindl .AND. wtn .GE. 0.5_kindl ) set_to_missing = .true.
1067  endif
1068  if(data_in(is,je) == missing_value) then
1069  num_missing = num_missing+1
1070  is_missing(4) = .true.
1071  if(wtw .GE. 0.5_kindl .AND. wtn .GE. 0.5_kindl) set_to_missing = .true.
1072  endif
1073 
1074  if( num_missing == 4 .OR. set_to_missing ) then
1075  data_out(m,n) = missing_value
1076  if(present(mask_out)) mask_out(m,n) = 0.0_kindl
1077  cycle
1078  else if(num_missing == 0) then
1079  f1 = data_in(is,js)
1080  f2 = data_in(ie,js)
1081  f3 = data_in(ie,je)
1082  f4 = data_in(is,je)
1083  w = wtw
1084  s = wts
1085  else if(num_missing == 3) then !--- three missing value
1086  if(.not. is_missing(1) ) then
1087  data_out(m,n) = data_in(is,js)
1088  else if(.not. is_missing(2) ) then
1089  data_out(m,n) = data_in(ie,js)
1090  else if(.not. is_missing(3) ) then
1091  data_out(m,n) = data_in(ie,je)
1092  else if(.not. is_missing(4) ) then
1093  data_out(m,n) = data_in(is,je)
1094  endif
1095  if(present(mask_out) ) mask_out(m,n) = 1.0_kindl
1096  cycle
1097  else !--- one or two missing value
1098  if( num_missing == 1) then
1099  if( is_missing(1) .OR. is_missing(3) ) then
1100  middle = 0.5_kindl *(data_in(ie,js)+data_in(is,je))
1101  else
1102  middle = 0.5_kindl *(data_in(is,js)+data_in(ie,je))
1103  endif
1104  else ! num_missing = 2
1105  if( is_missing(1) .AND. is_missing(2) ) then
1106  middle = 0.5_kindl *(data_in(ie,je)+data_in(is,je))
1107  else if( is_missing(1) .AND. is_missing(3) ) then
1108  middle = 0.5_kindl *(data_in(ie,js)+data_in(is,je))
1109  else if( is_missing(1) .AND. is_missing(4) ) then
1110  middle = 0.5_kindl *(data_in(ie,js)+data_in(ie,je))
1111  else if( is_missing(2) .AND. is_missing(3) ) then
1112  middle = 0.5_kindl *(data_in(is,js)+data_in(is,je))
1113  else if( is_missing(2) .AND. is_missing(4) ) then
1114  middle = 0.5_kindl*(data_in(is,js)+data_in(ie,je))
1115  else if( is_missing(3) .AND. is_missing(4) ) then
1116  middle = 0.5_kindl*(data_in(is,js)+data_in(ie,js))
1117  endif
1118  endif
1119 
1120  if( wtw .GE. 0.5_kindl .AND. wts .GE. 0.5_kindl ) then ! zone 1
1121  w = 2.0_kindl*(wtw-0.5_kindl)
1122  s = 2.0_kindl*(wts-0.5_kindl)
1123  f1 = data_in(is,js)
1124  if(is_missing(2)) then
1125  f2 = f1
1126  else
1127  f2 = 0.5_kindl*(data_in(is,js)+data_in(ie,js))
1128  endif
1129  f3 = middle
1130  if(is_missing(4)) then
1131  f4 = f1
1132  else
1133  f4 = 0.5_kindl*(data_in(is,js)+data_in(is,je))
1134  endif
1135  else if( wte .GE. 0.5_kindl .AND. wts .GE. 0.5_kindl ) then ! zone 2
1136  w = 2.0_kindl*(1.0_kindl-wte)
1137  s = 2.0_kindl*(wts-0.5_kindl)
1138  f2 = data_in(ie,js)
1139  if(is_missing(1)) then
1140  f1 = f2
1141  else
1142  f1 = 0.5_kindl*(data_in(is,js)+data_in(ie,js))
1143  endif
1144  f4 = middle
1145  if(is_missing(3)) then
1146  f3 = f2
1147  else
1148  f3 = 0.5_kindl*(data_in(ie,js)+data_in(ie,je))
1149  endif
1150  else if( wte .GE. 0.5_kindl .AND. wtn .GE. 0.5_kindl ) then ! zone 3
1151  w = 2.0_kindl*(1.0_kindl-wte)
1152  s = 2.0_kindl*(1.0_kindl-wtn)
1153  f3 = data_in(ie,je)
1154  if(is_missing(2)) then
1155  f2 = f3
1156  else
1157  f2 = 0.5_kindl*(data_in(ie,js)+data_in(ie,je))
1158  endif
1159  f1 = middle
1160  if(is_missing(4)) then
1161  f4 = f3
1162  else
1163  f4 = 0.5_kindl*(data_in(ie,je)+data_in(is,je))
1164  endif
1165  else if( wtw .GE. 0.5_kindl .AND. wtn .GE. 0.5_kindl ) then ! zone 4
1166  w = 2.0_kindl*(wtw-0.5_kindl)
1167  s = 2.0_kindl*(1.0_kindl-wtn)
1168  f4 = data_in(is,je)
1169  if(is_missing(1)) then
1170  f1 = f4
1171  else
1172  f1 = 0.5_kindl*(data_in(is,js)+data_in(is,je))
1173  endif
1174  f2 = middle
1175  if(is_missing(3)) then
1176  f3 = f4
1177  else
1178  f3 = 0.5_kindl*(data_in(ie,je)+data_in(is,je))
1179  endif
1180  else
1181  call mpp_error(fatal, &
1182  "horiz_interp_bilinear_mod: the point should be in one of the four zone")
1183  endif
1184  endif
1185 
1186  data_out(m,n) = f3 + (f4-f3)*w + (f2-f3)*s + ((f1-f2)+(f3-f4))*w*s
1187  if(present(mask_out)) mask_out(m,n) = 1.0_kindl
1188  enddo
1189  enddo
1190  else
1191  do n = 1, nlat_out
1192  do m = 1, nlon_out
1193  is = interp % i_lon (m,n,1); ie = interp % i_lon (m,n,2)
1194  js = interp % j_lat (m,n,1); je = interp % j_lat (m,n,2)
1195  wtw = interp % HI_KIND_TYPE_ % wti (m,n,1)
1196  wte = interp % HI_KIND_TYPE_ % wti (m,n,2)
1197  wts = interp % HI_KIND_TYPE_ % wtj (m,n,1)
1198  wtn = interp % HI_KIND_TYPE_ % wtj (m,n,2)
1199 
1200  if(present(missing_value) ) then
1201  num_missing = 0
1202  if(data_in(is,js) == missing_value) then
1203  num_missing = num_missing+1
1204  mask(is,js) = 0.0_kindl
1205  endif
1206  if(data_in(ie,js) == missing_value) then
1207  num_missing = num_missing+1
1208  mask(ie,js) = 0.0_kindl
1209  endif
1210  if(data_in(ie,je) == missing_value) then
1211  num_missing = num_missing+1
1212  mask(ie,je) = 0.0_kindl
1213  endif
1214  if(data_in(is,je) == missing_value) then
1215  num_missing = num_missing+1
1216  mask(is,je) = 0.0_kindl
1217  endif
1218  endif
1219 
1220  dwtsum = data_in(is,js)*mask(is,js)*wtw*wts &
1221  + data_in(ie,js)*mask(ie,js)*wte*wts &
1222  + data_in(ie,je)*mask(ie,je)*wte*wtn &
1223  + data_in(is,je)*mask(is,je)*wtw*wtn
1224  wtsum = mask(is,js)*wtw*wts + mask(ie,js)*wte*wts &
1225  + mask(ie,je)*wte*wtn + mask(is,je)*wtw*wtn
1226 
1227  if(.not. present(mask_in) .and. .not. present(missing_value)) wtsum = 1.0_kindl
1228 
1229  if(num_missing .gt. max_missing ) then
1230  data_out(m,n) = missing_value
1231  if(present(mask_out)) mask_out(m,n) = 0.0_kindl
1232  else if(wtsum .lt. real(epsln, fms_hi_kind_)) then
1233  if(present(missing_value)) then
1234  data_out(m,n) = missing_value
1235  else
1236  data_out(m,n) = 0.0_kindl
1237  endif
1238  if(present(mask_out)) mask_out(m,n) = 0.0_kindl
1239  else
1240  data_out(m,n) = dwtsum/wtsum
1241  if(present(mask_out)) mask_out(m,n) = wtsum
1242  endif
1243  enddo
1244  enddo
1245  endif
1246  !***********************************************************************
1247  ! compute statistics: minimum, maximum, and mean
1248  !-----------------------------------------------------------------------
1249  if (iverbose > 0) then
1250 
1251  ! compute statistics of input data
1252 
1253  call stats (data_in, min_in, max_in, avg_in, miss_in, missing_value, mask_in)
1254 
1255  ! compute statistics of output data
1256  call stats (data_out, min_out, max_out, avg_out, miss_out, missing_value, mask_out)
1257 
1258  !---- output statistics ----
1259  iunit = stdout()
1260  write (iunit,900)
1261  write (iunit,901) min_in ,max_in, avg_in
1262  if (present(mask_in)) write (iunit,903) miss_in
1263  write (iunit,902) min_out,max_out,avg_out
1264  if (present(mask_out)) write (iunit,903) miss_out
1265 
1266 900 format (/,1x,10('-'),' output from horiz_interp ',10('-'))
1267 901 format (' input: min=',f16.9,' max=',f16.9,' avg=',f22.15)
1268 902 format (' output: min=',f16.9,' max=',f16.9,' avg=',f22.15)
1269 903 format (' number of missing points = ',i6)
1270 
1271  endif
1272 
1273  return
1274 
1275  end subroutine horiz_interp_bilinear_
1276 
1277  !> @parblock
1278  !! Subroutine to read a weight file and populate Interp for the bilinear interpolation.
1279  !! @endparblock
1280  subroutine horiz_interp_read_weights_bilinear_(Interp, weight_filename, lon_out, lat_out, lon_in, lat_in, &
1281  weight_file_source, interp_method, isw, iew, jsw, jew, nglon, nglat)
1282  type(horiz_interp_type), intent(inout) :: Interp
1283  !< will be populated with interpolation weights and mapping indices.
1284  character(len=*), intent(in) :: weight_filename
1285  !< is the name of the weight file.
1286  real(FMS_HI_KIND_), target, intent(in) :: lat_out(:,:)
1287  !< are the latitude coordinates of output grid.
1288  real(FMS_HI_KIND_), target, intent(in) :: lon_out(:,:)
1289  !< are the longitude coordinates of output grid.
1290  real(FMS_HI_KIND_), intent(in) :: lat_in(:)
1291  !< are the latitude coordinates for the rectilinear input grid in radians.
1292  real(FMS_HI_KIND_), intent(in) :: lon_in(:)
1293  !< are the longitude coordinates for the rectilinear input grid in radians.
1294  character(len=*), intent(in) :: weight_file_source
1295  !< source of the weight file, only accepts "fregrid" at this time.
1296  character(len=*), intent(in) :: interp_method
1297  !< is not used.
1298  integer, intent(in) :: isw
1299  !< is the starting x index in compute domain, used to read subsection of weight file.
1300  integer, intent(in) :: iew
1301  !< is the ending x index in compute domain, used to read subsection of weight file.
1302  integer, intent(in) :: jsw
1303  !< is the starting y index in compute domain, used to read subsection of weight file.
1304  integer, intent(in) :: jew
1305  !< is the ending y index in compute domain, used to read subsection of weight file.
1306  integer, intent(in) :: nglon
1307  !< is the number of longitude gridpoints in the global domain, used for consistency checking.
1308  integer, intent(in) :: nglat
1309  !< is the number of latitude gridpoints in the global domain, used for consistency checking.
1310 
1311  real(FMS_HI_KIND_), allocatable :: var(:,:,:) ! Dummy variable to read the indices and weight into
1312  type(FmsNetcdfFile_t) :: weight_fileobj ! FMS2io fileob for the weight file
1313  integer :: nlon ! Number of longitudes in the model grid as read from the weight file
1314  integer :: nlat ! Number of latitude in the model grid as read from the weight file
1315 
1316  if (.not. open_file(weight_fileobj, weight_filename, "read" )) &
1317  call mpp_error(fatal, "Error opening the weight file:"//&
1318  &trim(weight_filename))
1319 
1320  !< Check that weight file has the correct dimensions
1321  select case (trim(weight_file_source))
1322  case ("fregrid")
1323  call get_dimension_size(weight_fileobj, "nlon", nlon)
1324  if (nlon .ne. nglon) &
1325  call mpp_error(fatal, "The nlon from the weight file is not the same as in the input grid."//&
1326  &" From weight file:"//string(nlon)//" from input grid:"//string(size(lon_out,1)))
1327  call get_dimension_size(weight_fileobj, "nlat", nlat)
1328  if (nlat .ne. nglat) &
1329  call mpp_error(fatal, "The nlat from the weight file is not the same as in the input grid."//&
1330  &" From weight file:"//string(nlat)//" from input grid:"//string(size(lon_out,2)))
1331  case default
1332  call mpp_error(fatal, trim(weight_file_source)//&
1333  &" is not a supported weight file source. fregrid is the only supported weight file source." )
1334  end select
1335 
1336  interp%nlon_src = size(lon_in(:)) ; interp%nlat_src = size(lat_in(:))
1337  interp%nlon_dst = size(lon_out,1); interp%nlat_dst = size(lon_out,2)
1338 
1339  allocate ( interp % HI_KIND_TYPE_ % wti (interp%nlon_dst,interp%nlat_dst,2), &
1340  interp % HI_KIND_TYPE_ % wtj (interp%nlon_dst,interp%nlat_dst,2), &
1341  interp % i_lon (interp%nlon_dst,interp%nlat_dst,2), &
1342  interp % j_lat (interp%nlon_dst,interp%nlat_dst,2))
1343 
1344 
1345  !! Three is for lon, lat, tile
1346  !! Currently, interpolation is only supported from lat,lon input data
1347  allocate(var(interp%nlon_dst,interp%nlat_dst, 3))
1348  call read_data(weight_fileobj, "index", var, corner=(/isw, jsw, 1/), edge_lengths=(/iew-isw+1, jew-jsw+1, 3/))
1349 
1350  !! Each point has a lon (i), and lat(j) index
1351  !! From there the four corners are (i,j), (i,j+1) (i+1) (i+1,j+1)
1352  interp % i_lon (:,:,1) = var(:,:,1)
1353  interp % i_lon (:,:,2) = interp % i_lon (:,:,1) + 1
1354  where (interp % i_lon (:,:,2) > size(lon_in(:))) interp % i_lon (:,:,2) = 1
1355 
1356  interp % j_lat (:,:,1) = var(:,:,2)
1357  interp % j_lat (:,:,2) = interp % j_lat (:,:,1) + 1
1358  where (interp % j_lat (:,:,2) > size(lat_in(:))) interp % j_lat (:,:,2) = 1
1359 
1360  deallocate(var)
1361 
1362  allocate(var(interp%nlon_dst,interp%nlat_dst, 4))
1363  call read_data(weight_fileobj, "weight", var, corner=(/isw, jsw, 1/), edge_lengths=(/iew-isw+1, jew-jsw+1, 4/))
1364 
1365  !! The weights for the four corners
1366  !! var(:,:,1) -> (i,j)
1367  !! var(:,:,2) -> (i,j+1)
1368  !! var(:,:,3) -> (i+1,j)
1369  !! var(:,:,4) -> (i+1,j+1)
1370  interp % HI_KIND_TYPE_ % wti = var(:,:,1:2)
1371  interp % HI_KIND_TYPE_ % wtj = var(:,:,3:4)
1372  deallocate(var)
1373 
1374  interp% HI_KIND_TYPE_ % is_allocated = .true.
1375  interp% interp_method = bilinear
1376  interp% I_am_initialized = .true.
1377  call close_file(weight_fileobj)
1378  end subroutine horiz_interp_read_weights_bilinear_
integer function stdout()
This function returns the current standard fortran unit numbers for output.
Definition: mpp_util.inc:42