21 subroutine horiz_interp_bilinear_new_1d_ ( Interp, lon_in, lat_in, lon_out, lat_out, &
25 type(horiz_interp_type),
intent(inout) :: Interp
26 real(FMS_HI_KIND_),
intent(in),
dimension(:) :: lon_in , lat_in
27 real(FMS_HI_KIND_),
intent(in),
dimension(:,:) :: lon_out, lat_out
28 integer,
intent(in),
optional :: verbose
29 logical,
intent(in),
optional :: src_modulo
31 logical :: src_is_modulo
32 integer :: nlon_in, nlat_in, nlon_out, nlat_out, n, m
33 integer :: ie, is, je, js, ln_err, lt_err, warns, iunit
34 real(FMS_HI_KIND_) :: wtw, wte, wts, wtn, lon, lat, tpi, hpi
35 real(FMS_HI_KIND_) :: glt_min, glt_max, gln_min, gln_max, min_lon, max_lon
36 integer,
parameter :: kindl = fms_hi_kind_
37 logical :: decreasing_lat
38 logical :: decreasing_lon
41 if(
present(verbose)) warns = verbose
42 src_is_modulo = .true.
43 if (
present(src_modulo)) src_is_modulo = src_modulo
45 decreasing_lat = .false.
46 if (lat_in(1) > lat_in(2)) decreasing_lat = .true.
48 decreasing_lon = .false.
49 if (lon_in(1) > lon_in(2)) decreasing_lon = .true.
51 hpi = 0.5_kindl * real(pi, fms_hi_kind_)
63 allocate ( interp % HI_KIND_TYPE_ % wti (
size(lon_out,1),
size(lon_out,2),2), &
64 interp % HI_KIND_TYPE_ % wtj (
size(lon_out,1),
size(lon_out,2),2), &
65 interp % i_lon (
size(lon_out,1),
size(lon_out,2),2), &
66 interp % j_lat (
size(lon_out,1),
size(lon_out,2),2))
69 nlon_in =
size(lon_in(:)) ; nlat_in =
size(lat_in(:))
70 nlon_out =
size(lon_out, 1); nlat_out =
size(lon_out, 2)
71 interp%nlon_src = nlon_in; interp%nlat_src = nlat_in
72 interp%nlon_dst = nlon_out; interp%nlat_dst = nlat_out
74 if(src_is_modulo)
then
75 if(lon_in(nlon_in) - lon_in(1) .gt. tpi + real(epsln, fms_hi_kind_)) &
76 call mpp_error(fatal,
'horiz_interp_bilinear_mod: '// &
77 'The range of source grid longitude should be no larger than tpi')
79 if(lon_in(1) .lt. 0.0_kindl .OR. lon_in(nlon_in) > tpi )
then
81 max_lon = lon_in(nlon_in)
90 if(src_is_modulo)
then
91 if(lon .lt. min_lon)
then
93 else if(lon .gt. max_lon)
then
98 if((lon .lt. lon_in(1)) .or. (lon .gt. lon_in(nlon_in))) &
99 call mpp_error(fatal,
'horiz_interp_bilinear_mod: ' //&
100 'when input grid is not modulo, output grid should locate inside input grid')
103 glt_min = min(lat,glt_min); glt_max = max(lat,glt_max)
104 gln_min = min(lon,gln_min); gln_max = max(lon,gln_max)
106 is = nearest_index(lon, lon_in )
107 if (decreasing_lon)
then
112 if( lon_in(is) .lt. lon ) is = max(is-1,1)
118 if( lon_in(is) .gt. lon ) is = max(is-1,1)
120 if( lon_in(is) .eq. lon .and. is .eq. nlon_in) is = max(is - 1,1)
121 ie = min(is+1,nlon_in)
122 if(lon_in(is) .ne. lon_in(ie) .and. (decreasing_lon .or. lon_in(is) .le. lon))
then
123 wtw = ( lon_in(ie) - lon) / (lon_in(ie) - lon_in(is) )
130 if (lon_in(ie) .ge. lon )
then
131 wtw = (lon_in(ie) -lon)/(lon_in(ie)-lon_in(is)+tpi+real(epsln,fms_hi_kind_))
133 wtw = (lon_in(ie)-lon+tpi+real(epsln,fms_hi_kind_))/(lon_in(ie)-lon_in(is)+tpi+real(epsln,fms_hi_kind_))
136 wte = 1.0_kindl - wtw
138 js = nearest_index(lat, lat_in )
139 if (decreasing_lat)
then
141 if( lat_in(js) .lt. lat ) js = max(js - 1, 1)
144 if( lat_in(js) .gt. lat ) js = max(js - 1, 1)
146 if( lat_in(js) .eq. lat .and. js .eq. nlat_in) js = max(js - 1, 1)
147 je = min(js + 1, nlat_in)
149 if ( lat_in(js) .ne. lat_in(je) .and. (decreasing_lat .or. lat_in(js) .le. lat))
then
150 wts = ( lat_in(je) - lat )/(lat_in(je)-lat_in(js))
159 wtn = 1.0_kindl - wts
161 interp % i_lon (m,n,1) = is; interp % i_lon (m,n,2) = ie
162 interp % j_lat (m,n,1) = js; interp % j_lat (m,n,2) = je
163 interp % HI_KIND_TYPE_ % wti (m,n,1) = wtw
164 interp % HI_KIND_TYPE_ % wti (m,n,2) = wte
165 interp % HI_KIND_TYPE_ % wtj (m,n,1) = wts
166 interp % HI_KIND_TYPE_ % wtj (m,n,2) = wtn
173 if (ln_err .eq. 1 .and. warns > 0)
then
174 write (iunit,
'(/,(1x,a))') &
175 '==> Warning: the geographic data set does not extend far ', &
176 ' enough east or west - a cyclic boundary ', &
177 ' condition was applied. check if appropriate '
178 write (iunit,
'(/,(1x,a,2f8.4))') &
179 ' data required between longitudes:', gln_min, gln_max, &
180 ' data set is between longitudes:', lon_in(1), lon_in(nlon_in)
184 if (lt_err .eq. 1 .and. warns > 0)
then
185 write (iunit,
'(/,(1x,a))') &
186 '==> Warning: the geographic data set does not extend far ',&
187 ' enough north or south - extrapolation from ',&
188 ' the nearest data was applied. this may create ',&
189 ' artificial gradients near a geographic pole '
190 write (iunit,
'(/,(1x,a,2f8.4))') &
191 ' data required between latitudes:', glt_min, glt_max, &
192 ' data set is between latitudes:', lat_in(1), lat_in(nlat_in)
194 interp% HI_KIND_TYPE_ % is_allocated = .true.
195 interp% interp_method = bilinear
199 end subroutine horiz_interp_bilinear_new_1d_
208 verbose, src_modulo, new_search, no_crash_when_not_found )
211 type(horiz_interp_type),
intent(inout) :: Interp
215 real(FMS_HI_KIND_),
intent(in),
dimension(:,:) :: lon_in
216 real(FMS_HI_KIND_),
intent(in),
dimension(:,:) :: lat_in
217 real(FMS_HI_KIND_),
intent(in),
dimension(:,:) :: lon_out
218 real(FMS_HI_KIND_),
intent(in),
dimension(:,:) :: lat_out
219 integer,
intent(in),
optional :: verbose
220 logical,
intent(in),
optional :: src_modulo
222 logical,
intent(in),
optional :: new_search
223 logical,
intent(in),
optional :: no_crash_when_not_found
225 logical :: src_is_modulo
226 integer :: nlon_in, nlat_in, nlon_out, nlat_out
227 integer :: m, n, is, ie, js, je, num_solution
228 real(FMS_HI_KIND_) :: lon, lat, quadra, x, y, y1, y2
229 real(FMS_HI_KIND_) :: a1, b1, c1, d1, a2, b2, c2, d2, a, b, c
230 real(FMS_HI_KIND_) :: lon1, lat1, lon2, lat2, lon3, lat3, lon4, lat4
231 real(FMS_HI_KIND_) :: tpi, lon_min, lon_max
232 real(FMS_HI_KIND_) :: epsln2
233 logical :: use_new_search, no_crash
235 integer,
parameter :: kindl=fms_hi_kind_
237 tpi = 2.0_kindl * real(pi, fms_hi_kind_)
240 if(
present(verbose)) warns = verbose
241 src_is_modulo = .true.
242 if (
present(src_modulo)) src_is_modulo = src_modulo
243 use_new_search = .false.
244 if (
present(new_search)) use_new_search = new_search
246 if(
present(no_crash_when_not_found)) no_crash = no_crash_when_not_found
249 if(
size(lon_out,1) /=
size(lat_out,1) .or.
size(lon_out,2) /=
size(lat_out,2) ) &
250 call mpp_error(fatal,
'horiz_interp_bilinear_mod: when using bilinear ' // &
251 'interplation, the output grids should be geographical grids')
253 if(
size(lon_in,1) /=
size(lat_in,1) .or.
size(lon_in,2) /=
size(lat_in,2) ) &
254 call mpp_error(fatal,
'horiz_interp_bilinear_mod: when using bilinear '// &
255 'interplation, the input grids should be geographical grids')
258 nlon_in =
size(lon_in,1) ; nlat_in =
size(lat_in,2)
259 nlon_out =
size(lon_out,1); nlat_out =
size(lon_out,2)
260 interp%nlon_src = nlon_in; interp%nlat_src = nlat_in
261 interp%nlon_dst = nlon_out; interp%nlat_dst = nlat_out
263 allocate ( interp % HI_KIND_TYPE_ % wti (
size(lon_out,1),
size(lon_out,2),2), &
264 interp % HI_KIND_TYPE_ % wtj (
size(lon_out,1),
size(lon_out,2),2), &
265 interp % i_lon (
size(lon_out,1),
size(lon_out,2),2), &
266 interp % j_lat (
size(lon_out,1),
size(lon_out,2),2))
269 if(use_new_search)
then
270 epsln2 = real(epsln,fms_hi_kind_)* 1.0e5_kindl
271 call find_neighbor_new_(interp, lon_in, lat_in, lon_out, lat_out, src_is_modulo, no_crash)
273 epsln2 = real(epsln,fms_hi_kind_)
274 call find_neighbor_(interp, lon_in, lat_in, lon_out, lat_out, src_is_modulo)
311 lon_min = minval(lon_in);
312 lon_max = maxval(lon_in);
318 if(lon .lt. lon_min)
then
320 else if(lon .gt. lon_max)
then
323 is = interp%i_lon(m,n,1); ie = interp%i_lon(m,n,2)
324 js = interp%j_lat(m,n,1); je = interp%j_lat(m,n,2)
325 if( is == dummy) cycle
326 lon1 = lon_in(is,js); lat1 = lat_in(is,js);
327 lon2 = lon_in(ie,js); lat2 = lat_in(ie,js);
328 lon3 = lon_in(ie,je); lat3 = lat_in(ie,je);
329 lon4 = lon_in(is,je); lat4 = lat_in(is,je);
330 if(lon .lt. lon_min)
then
331 lon1 = lon1 -tpi; lon4 = lon4 - tpi
332 else if(lon .gt. lon_max)
then
333 lon2 = lon2 +tpi; lon3 = lon3 + tpi
337 c1 = lon1+lon3-lon4-lon2
341 c2 = lat1+lat3-lat4-lat2
345 b = a1*b2-a2*b1+c1*d2-c2*d1+c2*lon-c1*lat
346 c = a2*lon-a1*lat+a1*d2-a2*d1
347 quadra = b*b-4._kindl*a*c
348 if(abs(quadra) < real(epsln, fms_hi_kind_)) quadra = 0.0_kindl
349 if(quadra < 0.0_kindl)
call mpp_error(fatal, &
350 "horiz_interp_bilinear_mod: No solution existed for this quadratic equation")
351 if ( abs(a) .lt. epsln2)
then
352 if( abs(b) .lt. real(epsln,fms_hi_kind_))
call mpp_error(fatal, &
353 "horiz_interp_bilinear_mod: no unique solution existed for this linear equation")
356 y1 = 0.5_kindl*(-b+sqrt(quadra))/a
357 y2 = 0.5_kindl*(-b-sqrt(quadra))/a
358 if(abs(y1) < epsln2) y1 = 0.0_kindl
359 if(abs(y2) < epsln2) y2 = 0.0_kindl
360 if(abs(1.0_kindl-y1) < epsln2) y1 = 1.0_kindl
361 if(abs(1.0_kindl-y2) < epsln2) y2 = 1.0_kindl
363 if(y1 >= 0.0_kindl .and. y1 <= 1.0_kindl)
then
365 num_solution = num_solution +1
367 if(y2 >= 0.0_kindl .and. y2 <= 1.0_kindl)
then
369 num_solution = num_solution + 1
371 if(num_solution == 0)
then
372 call mpp_error(fatal,
"horiz_interp_bilinear_mod: No solution found")
373 else if(num_solution == 2)
then
374 call mpp_error(fatal,
"horiz_interp_bilinear_mod: Two solutions found")
377 if(abs(a1+c1*y) < real(epsln,fms_hi_kind_))
call mpp_error(fatal, &
378 "horiz_interp_bilinear_mod: the denomenator is 0")
379 if(abs(y) < epsln2) y = 0.0_kindl
380 if(abs(1.0_kindl-y) < epsln2) y = 1.0_kindl
381 x = (lon-b1*y-d1)/(a1+c1*y)
382 if(abs(x) < epsln2) x = 0.0_kindl
383 if(abs(1.0_kindl-x) < epsln2) x = 1.0_kindl
386 if(use_new_search)
then
387 if (x < 0.0_kindl) x = 0.0_kindl
388 if (y < 0.0_kindl) y = 0.0_kindl
389 if (x > 1.0_kindl) x = 1.0_kindl
390 if (y > 1.0_kindl) y = 1.0_kindl
392 if( x>1.0_kindl .or. x<0.0_kindl .or. y>1.0_kindl .or. y < 0.0_kindl) &
393 call mpp_error(fatal,
"horiz_interp_bilinear_mod: weight should be between 0 and 1")
394 interp % HI_KIND_TYPE_ % wti(m,n,1)=1.0_kindl-x
395 interp % HI_KIND_TYPE_ % wti(m,n,2)=x
396 interp % HI_KIND_TYPE_ % wtj(m,n,1)=1.0_kindl-y
397 interp % HI_KIND_TYPE_ % wtj(m,n,2)=y
401 interp% HI_KIND_TYPE_ % is_allocated = .true.
402 interp% interp_method = bilinear
408 subroutine find_neighbor_ ( Interp, lon_in, lat_in, lon_out, lat_out, src_modulo )
409 type(horiz_interp_type),
intent(inout) :: Interp
410 real(FMS_HI_KIND_),
intent(in),
dimension(:,:) :: lon_in , lat_in
411 real(FMS_HI_KIND_),
intent(in),
dimension(:,:) :: lon_out, lat_out
412 logical,
intent(in) :: src_modulo
413 integer :: nlon_in, nlat_in, nlon_out, nlat_out
414 integer :: max_step, n, m, l, i, j, ip1, jp1, step
415 integer :: is, js, jstart, jend, istart, iend, npts
416 integer,
allocatable,
dimension(:) :: ilon, jlat
417 real(FMS_HI_KIND_) :: lon_min, lon_max, lon, lat, tpi
419 real(FMS_HI_KIND_) :: lon1, lat1, lon2, lat2, lon3, lat3, lon4, lat4
421 integer,
parameter :: kindl=fms_hi_kind_
423 tpi = 2.0_kindl*real(pi, fms_hi_kind_)
424 nlon_in =
size(lon_in,1) ; nlat_in =
size(lat_in,2)
425 nlon_out =
size(lon_out,1); nlat_out =
size(lon_out,2)
427 lon_min = minval(lon_in);
428 lon_max = maxval(lon_in);
430 max_step = max(nlon_in,nlat_in)
431 allocate(ilon(5*max_step), jlat(5*max_step) )
440 if(lon .lt. lon_min)
then
442 else if(lon .gt. lon_max)
then
446 if(lon .lt. lon_min .or. lon .gt. lon_max ) &
447 call mpp_error(fatal,
'horiz_interp_bilinear_mod: ' //&
448 'when input grid is not modulo, output grid should locate inside input grid')
451 if(m==1 .and. n==1)
then
452 j_loop:
do j = 1, nlat_in-1
463 lon1 = lon_in(i, j); lat1 = lat_in(i,j)
464 lon2 = lon_in(ip1,j); lat2 = lat_in(ip1,j)
465 lon3 = lon_in(ip1,jp1); lat3 = lat_in(ip1,jp1)
466 lon4 = lon_in(i, jp1); lat4 = lat_in(i, jp1)
468 if(lon .lt. lon_min .or. lon .gt. lon_max)
then
469 if(i .ne. nlon_in)
then
472 if(lon .lt. lon_min)
then
473 lon1 = lon1 -tpi; lon4 = lon4 - tpi
474 else if(lon .gt. lon_max)
then
475 lon2 = lon2 +tpi; lon3 = lon3 + tpi
480 if(lat .ge. intersect(lon1,lat1,lon2,lat2,lon))
then
481 if(lon .le. intersect(lat2,lon2,lat3,lon3,lat))
then
482 if(lat .le. intersect(lon3,lat3,lon4,lat4,lon))
then
483 if(lon .ge. intersect(lat4,lon4,lat1,lon1,lat))
then
485 interp % i_lon (m,n,1) = i; interp % i_lon (m,n,2) = ip1
486 interp % j_lat (m,n,1) = j; interp % j_lat (m,n,2) = jp1
496 do while ( .not. found .and. step .lt. max_step )
499 is = interp % i_lon (m,n-1,1)
500 js = interp % j_lat (m,n-1,1)
502 is = interp % i_lon (m-1,n,1)
503 js = interp % j_lat (m-1,n,1)
512 jstart = max(js-step,1)
513 jend = min(js+step,nlat_in)
520 else if (i > nlon_in)
then
523 if( i < 1 .or. i > nlon_in)
call mpp_error(fatal, &
524 'horiz_interp_bilinear_mod: max_step is too big, decrease max_step' )
526 if( i < 1 .or. i > nlon_in) cycle
538 if( istart < 1) istart = istart + nlon_in
539 if( iend > nlon_in) iend = iend - nlon_in
541 istart = max(istart,1)
542 iend = min(iend, nlon_in)
546 if( j < 1 .or. j > nlat_in .or. j==jstart .or. j==jend) cycle
562 else if (i > nlon_in)
then
565 if( i < 1 .or. i > nlon_in)
call mpp_error(fatal, &
566 'horiz_interp_bilinear_mod: max_step is too big, decrease max_step' )
568 if( i < 1 .or. i > nlon_in) cycle
592 if(jp1>nlat_in) cycle
593 lon1 = lon_in(i, j); lat1 = lat_in(i,j)
594 lon2 = lon_in(ip1,j); lat2 = lat_in(ip1,j)
595 lon3 = lon_in(ip1,jp1); lat3 = lat_in(ip1,jp1)
596 lon4 = lon_in(i, jp1); lat4 = lat_in(i, jp1)
598 if(lon .lt. lon_min .or. lon .gt. lon_max)
then
599 if(i .ne. nlon_in)
then
602 if(lon .lt. lon_min)
then
603 lon1 = lon1 -tpi; lon4 = lon4 - tpi
604 else if(lon .gt. lon_max)
then
605 lon2 = lon2 +tpi; lon3 = lon3 + tpi
610 if(lat .ge. intersect(lon1,lat1,lon2,lat2,lon))
then
611 if(lon .le. intersect(lat2,lon2,lat3,lon3,lat))
then
612 if(lat .le. intersect(lon3,lat3,lon4,lat4,lon))
then
613 if(lon .ge. intersect(lat4,lon4,lat1,lon1,lat))
then
616 interp % i_lon (m,n,1) = i; interp % i_lon (m,n,2) = ip1
617 interp % j_lat (m,n,1) = j; interp % j_lat (m,n,2) = jp1
628 print *,
'lon,lat=',lon*180.0_kindl/real(pi,fms_hi_kind_),lat*180.0_kindl/real(pi,fms_hi_kind_)
630 print *,
'is,ie= ',istart,iend
631 print *,
'js,je= ',jstart,jend
632 print *,
'lon_in(is,js)=',lon_in(istart,jstart)*180.0_kindl/real(pi,fms_hi_kind_)
633 print *,
'lon_in(ie,js)=',lon_in(iend,jstart)*180.0_kindl/real(pi,fms_hi_kind_)
634 print *,
'lat_in(is,js)=',lat_in(istart,jstart)*180.0_kindl/real(pi,fms_hi_kind_)
635 print *,
'lat_in(ie,js)=',lat_in(iend,jstart)*180.0_kindl/real(pi,fms_hi_kind_)
636 print *,
'lon_in(is,je)=',lon_in(istart,jend)*180.0_kindl/real(pi,fms_hi_kind_)
637 print *,
'lon_in(ie,je)=',lon_in(iend,jend)*180.0_kindl/real(pi,fms_hi_kind_)
638 print *,
'lat_in(is,je)=',lat_in(istart,jend)*180.0_kindl/real(pi,fms_hi_kind_)
639 print *,
'lat_in(ie,je)=',lat_in(iend,jend)*180.0_kindl/real(pi,fms_hi_kind_)
641 call mpp_error(fatal, &
642 'FIND_NEIGHBOR_: the destination point is not inside the source grid' )
655 real(fms_hi_kind_),
dimension(:),
intent(in) :: polyx
656 real(fms_hi_kind_),
dimension(:),
intent(in) :: polyy
657 real(fms_hi_kind_),
intent(in) :: x
658 real(fms_hi_kind_),
intent(in) :: y
660 integer :: i, j, nedges
661 real(fms_hi_kind_) :: xx
664 nedges =
size(polyx(:))
667 if( (polyy(i) < y .AND. polyy(j) >= y) .OR. (polyy(j) < y .AND. polyy(i) >= y) )
then
668 xx = polyx(i)+(y-polyy(i))/(polyy(j)-polyy(i))*(polyx(j)-polyx(i))
672 else if( xx < x )
then
687 type(horiz_interp_type),
intent(inout) :: Interp
688 real(FMS_HI_KIND_),
intent(in),
dimension(:,:) :: lon_in , lat_in
689 real(FMS_HI_KIND_),
intent(in),
dimension(:,:) :: lon_out, lat_out
690 logical,
intent(in) :: src_modulo, no_crash
691 integer :: nlon_in, nlat_in, nlon_out, nlat_out
692 integer :: max_step, n, m, l, i, j, ip1, jp1, step
693 integer :: is, js, jstart, jend, istart, iend, npts
694 integer,
allocatable,
dimension(:) :: ilon, jlat
695 real(FMS_HI_KIND_) :: lon_min, lon_max, lon, lat, tpi
697 real(FMS_HI_KIND_) :: polyx(4), polyy(4)
698 real(FMS_HI_KIND_) :: min_lon, min_lat, max_lon, max_lat
700 integer,
parameter :: step_div=8, kindl = fms_hi_kind_
702 tpi = 2.0_kindl * real(pi, fms_hi_kind_)
703 nlon_in =
size(lon_in,1) ; nlat_in =
size(lat_in,2)
704 nlon_out =
size(lon_out,1); nlat_out =
size(lon_out,2)
706 lon_min = minval(lon_in);
707 lon_max = maxval(lon_in);
709 max_step = min(nlon_in,nlat_in)/step_div
710 allocate(ilon(step_div*max_step), jlat(step_div*max_step) )
719 if(lon .lt. lon_min)
then
721 else if(lon .gt. lon_max)
then
725 if(lon .lt. lon_min .or. lon .gt. lon_max ) &
726 call mpp_error(fatal,
'horiz_interp_bilinear_mod: ' //&
727 'when input grid is not modulo, output grid should locate inside input grid')
730 if(m==1 .and. n==1)
then
731 j_loop:
do j = 1, nlat_in-1
743 polyx(1) = lon_in(i, j); polyy(1) = lat_in(i,j)
744 polyx(2) = lon_in(ip1,j); polyy(2) = lat_in(ip1,j)
745 polyx(3) = lon_in(ip1,jp1); polyy(3) = lat_in(ip1,jp1)
746 polyx(4) = lon_in(i, jp1); polyy(4) = lat_in(i, jp1)
747 if(lon .lt. lon_min .or. lon .gt. lon_max)
then
748 if(i .ne. nlon_in)
then
751 if(lon .lt. lon_min)
then
752 polyx(1) = polyx(1) -tpi; polyx(4) = polyx(4) - tpi
753 else if(lon .gt. lon_max)
then
754 polyx(2) = polyx(2) +tpi; polyx(3) = polyx(3) + tpi
759 min_lon = minval(polyx)
760 max_lon = maxval(polyx)
761 min_lat = minval(polyy)
762 max_lat = maxval(polyy)
773 interp % i_lon (m,n,1) = i; interp % i_lon (m,n,2) = ip1
774 interp % j_lat (m,n,1) = j; interp % j_lat (m,n,2) = jp1
781 do while ( .not. found .and. step .lt. max_step )
784 is = interp % i_lon (m,n-1,1)
785 js = interp % j_lat (m,n-1,1)
787 is = interp % i_lon (m-1,n,1)
788 js = interp % j_lat (m-1,n,1)
797 jstart = max(js-step,1)
798 jend = min(js+step,nlat_in)
805 else if (i > nlon_in)
then
808 if( i < 1 .or. i > nlon_in)
call mpp_error(fatal, &
809 'horiz_interp_bilinear_mod: max_step is too big, decrease max_step' )
811 if( i < 1 .or. i > nlon_in) cycle
826 if( istart < 1) istart = istart + nlon_in
827 if( iend > nlon_in) iend = iend - nlon_in
829 istart = max(istart,1)
830 iend = min(iend, nlon_in)
834 if( j < 1 .or. j > nlat_in) cycle
857 if(jp1>nlat_in) cycle
858 polyx(1) = lon_in(i, j); polyy(1) = lat_in(i,j)
859 polyx(2) = lon_in(ip1,j); polyy(2) = lat_in(ip1,j)
860 polyx(3) = lon_in(ip1,jp1); polyy(3) = lat_in(ip1,jp1)
861 polyx(4) = lon_in(i, jp1); polyy(4) = lat_in(i, jp1)
864 interp % i_lon (m,n,1) = i; interp % i_lon (m,n,2) = ip1
865 interp % j_lat (m,n,1) = j; interp % j_lat (m,n,2) = jp1
874 interp % i_lon (m,n,1:2) = dummy
875 interp % j_lat (m,n,1:2) = dummy
876 print*,
'lon,lat=',lon,lat
878 call mpp_error(fatal, &
879 'horiz_interp_bilinear_mod: the destination point is not inside the source grid' )
888 function intersect_(x1, y1, x2, y2, x)
889 real(FMS_HI_KIND_),
intent(in) :: x1, y1, x2, y2, x
890 real(FMS_HI_KIND_) :: INTERSECT_
892 intersect_ = (y2-y1)*(x-x1)/(x2-x1) + y1
896 end function intersect_
904 missing_value, missing_permit, new_handle_missing )
906 type (horiz_interp_type),
intent(in) :: Interp
909 real(FMS_HI_KIND_),
intent(in),
dimension(:,:) :: data_in
910 real(FMS_HI_KIND_),
intent(out),
dimension(:,:) :: data_out
911 integer,
intent(in),
optional :: verbose
913 real(FMS_HI_KIND_),
intent(in),
dimension(:,:),
optional :: mask_in
917 real(FMS_HI_KIND_),
intent(out),
dimension(:,:),
optional :: mask_out
919 real(FMS_HI_KIND_),
intent(in),
optional :: missing_value
920 integer,
intent(in),
optional :: missing_permit
921 logical,
intent(in),
optional :: new_handle_missing
923 integer :: nlon_in, nlat_in, nlon_out, nlat_out, n, m, &
924 is, ie, js, je, iverbose, max_missing, num_missing, &
925 miss_in, miss_out, iunit
926 real(FMS_HI_KIND_) :: dwtsum, wtsum, min_in, max_in, avg_in, &
927 min_out, max_out, avg_out, wtw, wte, wts, wtn
928 real(FMS_HI_KIND_) :: mask(size(data_in,1), size(data_in,2) )
929 logical :: set_to_missing, is_missing(4), new_handler
930 real(FMS_HI_KIND_) :: f1, f2, f3, f4, middle, w, s
931 integer,
parameter :: kindl = fms_hi_kind_
935 nlon_in = interp%nlon_src; nlat_in = interp%nlat_src
936 nlon_out = interp%nlon_dst; nlat_out = interp%nlat_dst
938 if(
present(mask_in))
then
944 if (
present(verbose))
then
950 if(
present(missing_permit))
then
951 max_missing = missing_permit
956 if(
present(new_handle_missing))
then
957 new_handler = new_handle_missing
959 new_handler = .false.
962 if(max_missing .gt. 3 .or. max_missing .lt. 0)
call mpp_error(fatal, &
963 'horiz_interp_bilinear_mod: missing_permit should be between 0 and 3')
965 if (
size(data_in,1) /= nlon_in .or.
size(data_in,2) /= nlat_in) &
966 call mpp_error(fatal,
'horiz_interp_bilinear_mod: size of input array incorrect')
968 if (
size(data_out,1) /= nlon_out .or.
size(data_out,2) /= nlat_out) &
969 call mpp_error(fatal,
'horiz_interp_bilinear_mod: size of output array incorrect')
972 if( .not.
present(missing_value) )
call mpp_error(fatal, &
973 "horiz_interp_bilinear_mod: misisng_value must be present when new_handle_missing is .true.")
974 if(
present(mask_in) )
call mpp_error(fatal, &
975 "horiz_interp_bilinear_mod: mask_in should not be present when new_handle_missing is .true.")
978 is = interp % i_lon (m,n,1); ie = interp % i_lon (m,n,2)
979 js = interp % j_lat (m,n,1); je = interp % j_lat (m,n,2)
980 wtw = interp % HI_KIND_TYPE_ % wti (m,n,1)
981 wte = interp % HI_KIND_TYPE_ % wti (m,n,2)
982 wts = interp % HI_KIND_TYPE_ % wtj (m,n,1)
983 wtn = interp % HI_KIND_TYPE_ % wtj (m,n,2)
987 set_to_missing = .false.
988 if(data_in(is,js) == missing_value)
then
989 num_missing = num_missing+1
990 is_missing(1) = .true.
991 if(wtw .GE. 0.5_kindl .AND. wts .GE. 0.5_kindl) set_to_missing = .true.
994 if(data_in(ie,js) == missing_value)
then
995 num_missing = num_missing+1
996 is_missing(2) = .true.
997 if(wte .GE. 0.5_kindl .AND. wts .GE. 0.5_kindl ) set_to_missing = .true.
999 if(data_in(ie,je) == missing_value)
then
1000 num_missing = num_missing+1
1001 is_missing(3) = .true.
1002 if(wte .GE. 0.5_kindl .AND. wtn .GE. 0.5_kindl ) set_to_missing = .true.
1004 if(data_in(is,je) == missing_value)
then
1005 num_missing = num_missing+1
1006 is_missing(4) = .true.
1007 if(wtw .GE. 0.5_kindl .AND. wtn .GE. 0.5_kindl) set_to_missing = .true.
1010 if( num_missing == 4 .OR. set_to_missing )
then
1011 data_out(m,n) = missing_value
1012 if(
present(mask_out)) mask_out(m,n) = 0.0_kindl
1014 else if(num_missing == 0)
then
1021 else if(num_missing == 3)
then
1022 if(.not. is_missing(1) )
then
1023 data_out(m,n) = data_in(is,js)
1024 else if(.not. is_missing(2) )
then
1025 data_out(m,n) = data_in(ie,js)
1026 else if(.not. is_missing(3) )
then
1027 data_out(m,n) = data_in(ie,je)
1028 else if(.not. is_missing(4) )
then
1029 data_out(m,n) = data_in(is,je)
1031 if(
present(mask_out) ) mask_out(m,n) = 1.0_kindl
1034 if( num_missing == 1)
then
1035 if( is_missing(1) .OR. is_missing(3) )
then
1036 middle = 0.5_kindl *(data_in(ie,js)+data_in(is,je))
1038 middle = 0.5_kindl *(data_in(is,js)+data_in(ie,je))
1041 if( is_missing(1) .AND. is_missing(2) )
then
1042 middle = 0.5_kindl *(data_in(ie,je)+data_in(is,je))
1043 else if( is_missing(1) .AND. is_missing(3) )
then
1044 middle = 0.5_kindl *(data_in(ie,js)+data_in(is,je))
1045 else if( is_missing(1) .AND. is_missing(4) )
then
1046 middle = 0.5_kindl *(data_in(ie,js)+data_in(ie,je))
1047 else if( is_missing(2) .AND. is_missing(3) )
then
1048 middle = 0.5_kindl *(data_in(is,js)+data_in(is,je))
1049 else if( is_missing(2) .AND. is_missing(4) )
then
1050 middle = 0.5_kindl*(data_in(is,js)+data_in(ie,je))
1051 else if( is_missing(3) .AND. is_missing(4) )
then
1052 middle = 0.5_kindl*(data_in(is,js)+data_in(ie,js))
1056 if( wtw .GE. 0.5_kindl .AND. wts .GE. 0.5_kindl )
then
1057 w = 2.0_kindl*(wtw-0.5_kindl)
1058 s = 2.0_kindl*(wts-0.5_kindl)
1060 if(is_missing(2))
then
1063 f2 = 0.5_kindl*(data_in(is,js)+data_in(ie,js))
1066 if(is_missing(4))
then
1069 f4 = 0.5_kindl*(data_in(is,js)+data_in(is,je))
1071 else if( wte .GE. 0.5_kindl .AND. wts .GE. 0.5_kindl )
then
1072 w = 2.0_kindl*(1.0_kindl-wte)
1073 s = 2.0_kindl*(wts-0.5_kindl)
1075 if(is_missing(1))
then
1078 f1 = 0.5_kindl*(data_in(is,js)+data_in(ie,js))
1081 if(is_missing(3))
then
1084 f3 = 0.5_kindl*(data_in(ie,js)+data_in(ie,je))
1086 else if( wte .GE. 0.5_kindl .AND. wtn .GE. 0.5_kindl )
then
1087 w = 2.0_kindl*(1.0_kindl-wte)
1088 s = 2.0_kindl*(1.0_kindl-wtn)
1090 if(is_missing(2))
then
1093 f2 = 0.5_kindl*(data_in(ie,js)+data_in(ie,je))
1096 if(is_missing(4))
then
1099 f4 = 0.5_kindl*(data_in(ie,je)+data_in(is,je))
1101 else if( wtw .GE. 0.5_kindl .AND. wtn .GE. 0.5_kindl )
then
1102 w = 2.0_kindl*(wtw-0.5_kindl)
1103 s = 2.0_kindl*(1.0_kindl-wtn)
1105 if(is_missing(1))
then
1108 f1 = 0.5_kindl*(data_in(is,js)+data_in(is,je))
1111 if(is_missing(3))
then
1114 f3 = 0.5_kindl*(data_in(ie,je)+data_in(is,je))
1117 call mpp_error(fatal, &
1118 "horiz_interp_bilinear_mod: the point should be in one of the four zone")
1122 data_out(m,n) = f3 + (f4-f3)*w + (f2-f3)*s + ((f1-f2)+(f3-f4))*w*s
1123 if(
present(mask_out)) mask_out(m,n) = 1.0_kindl
1129 is = interp % i_lon (m,n,1); ie = interp % i_lon (m,n,2)
1130 js = interp % j_lat (m,n,1); je = interp % j_lat (m,n,2)
1131 wtw = interp % HI_KIND_TYPE_ % wti (m,n,1)
1132 wte = interp % HI_KIND_TYPE_ % wti (m,n,2)
1133 wts = interp % HI_KIND_TYPE_ % wtj (m,n,1)
1134 wtn = interp % HI_KIND_TYPE_ % wtj (m,n,2)
1136 if(
present(missing_value) )
then
1138 if(data_in(is,js) == missing_value)
then
1139 num_missing = num_missing+1
1140 mask(is,js) = 0.0_kindl
1142 if(data_in(ie,js) == missing_value)
then
1143 num_missing = num_missing+1
1144 mask(ie,js) = 0.0_kindl
1146 if(data_in(ie,je) == missing_value)
then
1147 num_missing = num_missing+1
1148 mask(ie,je) = 0.0_kindl
1150 if(data_in(is,je) == missing_value)
then
1151 num_missing = num_missing+1
1152 mask(is,je) = 0.0_kindl
1156 dwtsum = data_in(is,js)*mask(is,js)*wtw*wts &
1157 + data_in(ie,js)*mask(ie,js)*wte*wts &
1158 + data_in(ie,je)*mask(ie,je)*wte*wtn &
1159 + data_in(is,je)*mask(is,je)*wtw*wtn
1160 wtsum = mask(is,js)*wtw*wts + mask(ie,js)*wte*wts &
1161 + mask(ie,je)*wte*wtn + mask(is,je)*wtw*wtn
1163 if(.not.
present(mask_in) .and. .not.
present(missing_value)) wtsum = 1.0_kindl
1165 if(num_missing .gt. max_missing )
then
1166 data_out(m,n) = missing_value
1167 if(
present(mask_out)) mask_out(m,n) = 0.0_kindl
1168 else if(wtsum .lt. real(epsln, fms_hi_kind_))
then
1169 if(
present(missing_value))
then
1170 data_out(m,n) = missing_value
1172 data_out(m,n) = 0.0_kindl
1174 if(
present(mask_out)) mask_out(m,n) = 0.0_kindl
1176 data_out(m,n) = dwtsum/wtsum
1177 if(
present(mask_out)) mask_out(m,n) = wtsum
1185 if (iverbose > 0)
then
1189 call stats (data_in, min_in, max_in, avg_in, miss_in, missing_value, mask_in)
1192 call stats (data_out, min_out, max_out, avg_out, miss_out, missing_value, mask_out)
1197 write (iunit,901) min_in ,max_in, avg_in
1198 if (
present(mask_in))
write (iunit,903) miss_in
1199 write (iunit,902) min_out,max_out,avg_out
1200 if (
present(mask_out))
write (iunit,903) miss_out
1202 900
format (/,1x,10(
'-'),
' output from horiz_interp ',10(
'-'))
1203 901
format (
' input: min=',f16.9,
' max=',f16.9,
' avg=',f22.15)
1204 902
format (
' output: min=',f16.9,
' max=',f16.9,
' avg=',f22.15)
1205 903
format (
' number of missing points = ',i6)
1216 weight_file_source, interp_method, isw, iew, jsw, jew, nglon, nglat)
1217 type(horiz_interp_type),
intent(inout) :: Interp
1218 character(len=*),
intent(in) :: weight_filename
1219 real(FMS_HI_KIND_),
target,
intent(in) :: lat_out(:,:)
1220 real(FMS_HI_KIND_),
target,
intent(in) :: lon_out(:,:)
1221 real(FMS_HI_KIND_),
intent(in) :: lat_in(:)
1222 real(FMS_HI_KIND_),
intent(in) :: lon_in(:)
1223 character(len=*),
intent(in) :: weight_file_source
1224 character(len=*),
intent(in) :: interp_method
1225 integer,
intent(in) :: isw, iew, jsw, jew
1226 integer,
intent(in) :: nglon
1227 integer,
intent(in) :: nglat
1230 real(FMS_HI_KIND_),
allocatable :: var(:,:,:)
1231 type(fmsnetcdffile_t) :: weight_fileobj
1237 if (.not. open_file(weight_fileobj, weight_filename,
"read" )) &
1238 call mpp_error(fatal,
"Error opening the weight file:"//&
1239 &trim(weight_filename))
1242 select case (trim(weight_file_source))
1244 call get_dimension_size(weight_fileobj,
"nlon", nlon)
1245 if (nlon .ne. nglon) &
1246 call mpp_error(fatal,
"The nlon from the weight file is not the same as in the input grid."//&
1247 &
" From weight file:"//string(nlon)//
" from input grid:"//string(
size(lon_out,1)))
1248 call get_dimension_size(weight_fileobj,
"nlat", nlat)
1249 if (nlat .ne. nglat) &
1250 call mpp_error(fatal,
"The nlat from the weight file is not the same as in the input grid."//&
1251 &
" From weight file:"//string(nlat)//
" from input grid:"//string(
size(lon_out,2)))
1253 call mpp_error(fatal, trim(weight_file_source)//&
1254 &
" is not a supported weight file source. fregrid is the only supported weight file source." )
1257 interp%nlon_src =
size(lon_in(:)) ; interp%nlat_src =
size(lat_in(:))
1258 interp%nlon_dst =
size(lon_out,1); interp%nlat_dst =
size(lon_out,2)
1260 allocate ( interp % HI_KIND_TYPE_ % wti (interp%nlon_dst,interp%nlat_dst,2), &
1261 interp % HI_KIND_TYPE_ % wtj (interp%nlon_dst,interp%nlat_dst,2), &
1262 interp % i_lon (interp%nlon_dst,interp%nlat_dst,2), &
1263 interp % j_lat (interp%nlon_dst,interp%nlat_dst,2))
1268 allocate(var(interp%nlon_dst,interp%nlat_dst, 3))
1269 call read_data(weight_fileobj,
"index", var, corner=(/isw, jsw, 1/), edge_lengths=(/iew-isw+1, jew-jsw+1, 3/))
1273 interp % i_lon (:,:,1) = var(:,:,1)
1274 interp % i_lon (:,:,2) = interp % i_lon (:,:,1) + 1
1275 where (interp % i_lon (:,:,2) >
size(lon_in(:))) interp % i_lon (:,:,2) = 1
1277 interp % j_lat (:,:,1) = var(:,:,2)
1278 interp % j_lat (:,:,2) = interp % j_lat (:,:,1) + 1
1279 where (interp % j_lat (:,:,2) >
size(lat_in(:))) interp % j_lat (:,:,2) = 1
1283 allocate(var(interp%nlon_dst,interp%nlat_dst, 4))
1284 call read_data(weight_fileobj,
"weight", var, corner=(/isw, jsw, 1/), edge_lengths=(/iew-isw+1, jew-jsw+1, 4/))
1291 interp % HI_KIND_TYPE_ % wti = var(:,:,1:2)
1292 interp % HI_KIND_TYPE_ % wtj = var(:,:,3:4)
1295 interp% HI_KIND_TYPE_ % is_allocated = .true.
1296 interp% interp_method = bilinear
1297 interp% I_am_initialized = .true.
1298 call close_file(weight_fileobj)
subroutine find_neighbor_(Interp, lon_in, lat_in, lon_out, lat_out, src_modulo)
this routine will search the source grid to fine the grid box that encloses each destination grid.
subroutine horiz_interp_bilinear_new_2d_(Interp, lon_in, lat_in, lon_out, lat_out, verbose, src_modulo, new_search, no_crash_when_not_found)
Initialization routine.
subroutine find_neighbor_new_(Interp, lon_in, lat_in, lon_out, lat_out, src_modulo, no_crash)
this routine will search the source grid to fine the grid box that encloses each destination grid.
subroutine horiz_interp_read_weights_bilinear_(Interp, weight_filename, lon_out, lat_out, lon_in, lat_in, weight_file_source, interp_method, isw, iew, jsw, jew, nglon, nglat)
Subroutine for reading a weight file and use it to fill in the horiz interp type for the bilinear int...
subroutine horiz_interp_bilinear_(Interp, data_in, data_out, verbose, mask_in, mask_out, missing_value, missing_permit, new_handle_missing)
Subroutine for performing the horizontal interpolation between two grids.
logical function inside_polygon_(polyx, polyy, x, y)
The function will return true if the point x,y is inside a polygon, or false if it is not....
integer function stdout()
This function returns the current standard fortran unit numbers for output.