- Timestamp:
- Jul 24, 2024, 2:54:37 PM (2 months ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
LMDZ6/branches/Amaury_dev/libf/misc/lmdz_interpolation.f90
r5115 r5116 1 1 ! $Id$ 2 module interpolation2 module lmdz_interpolation 3 3 4 4 ! From Press et al., 1996, version 2.10a 5 5 ! B3 Interpolation and Extrapolation 6 6 7 IMPLICIT NONE 7 IMPLICIT NONE; PRIVATE 8 PUBLIC locate, hunt 8 9 9 10 contains 10 11 11 pure FUNCTION locate(xx, x)12 pure FUNCTION locate(xx, x) 12 13 13 14 REAL, DIMENSION(:), INTENT(IN) :: xx … … 22 23 ! See notes. 23 24 24 INTEGER n, jl,jm,ju25 INTEGER n, jl, jm, ju 25 26 LOGICAL ascnd 26 27 27 28 !---------------------------- 28 29 29 n =size(xx)30 n = size(xx) 30 31 ascnd = (xx(n) >= xx(1)) 31 32 ! (True if ascending order of table, false otherwise.) 32 33 ! Initialize lower and upper limits: 33 jl =034 ju =n+135 do while (ju -jl > 1)36 jm=(ju+jl)/2 ! Compute a midpoint,37 if (ascnd .eqv. (x >= xx(jm))) then38 jl=jm ! and replace either the lower limit39 40 ju=jm ! or the upper limit, as appropriate.41 34 jl = 0 35 ju = n + 1 36 do while (ju - jl > 1) 37 jm = (ju + jl) / 2 ! Compute a midpoint, 38 if (ascnd .eqv. (x >= xx(jm))) THEN 39 jl = jm ! and replace either the lower limit 40 else 41 ju = jm ! or the upper limit, as appropriate. 42 end if 42 43 END DO 43 44 ! {ju == jl + 1} … … 48 49 49 50 ! Then set the output, being careful with the endpoints: 50 if (x == xx(1)) then51 locate=152 else if (x == xx(n)) then53 locate=n-151 if (x == xx(1)) THEN 52 locate = 1 53 else if (x == xx(n)) THEN 54 locate = n - 1 54 55 else 55 locate=jl56 locate = jl 56 57 end if 57 58 … … 60 61 !*************************** 61 62 62 pure SUBROUTINE hunt(xx, x,jlo)63 pure SUBROUTINE hunt(xx, x, jlo) 63 64 64 65 ! Given an array xx(1:N ), and given a value x, returns a value … … 72 73 REAL, INTENT(IN) :: x 73 74 REAL, DIMENSION(:), INTENT(IN) :: xx 74 INTEGER n, inc,jhi,jm75 INTEGER n, inc, jhi, jm 75 76 LOGICAL ascnd, hunt_up 76 77 77 78 !----------------------------------------------------- 78 79 79 n =size(xx)80 n = size(xx) 80 81 ascnd = (xx(n) >= xx(1)) 81 82 ! (True if ascending order of table, false otherwise.) 82 if (jlo < 0 .or. jlo > n) then83 84 jlo=085 jhi=n+183 if (jlo < 0 .or. jlo > n) THEN 84 ! Input guess not useful. Go immediately to bisection. 85 jlo = 0 86 jhi = n + 1 86 87 else 87 inc=1 ! Set the hunting increment.88 if (jlo == 0) then89 90 91 92 93 94 95 jhi=jlo+inc96 97 jhi=n+198 99 100 101 jlo=jhi ! Not done hunting,102 inc=inc+inc ! so double the increment103 104 105 106 jhi=jlo107 108 jlo=jhi-inc109 110 jlo=0111 112 113 114 jhi=jlo ! Not done hunting,115 inc=inc+inc ! so double the increment116 117 118 88 inc = 1 ! Set the hunting increment. 89 if (jlo == 0) THEN 90 hunt_up = .TRUE. 91 else 92 hunt_up = x >= xx(jlo) .eqv. ascnd 93 end if 94 if (hunt_up) then ! Hunt up: 95 do 96 jhi = jlo + inc 97 if (jhi > n) then ! Done hunting, since off end of table. 98 jhi = n + 1 99 exit 100 else 101 if (x < xx(jhi) .eqv. ascnd) exit 102 jlo = jhi ! Not done hunting, 103 inc = inc + inc ! so double the increment 104 end if 105 END DO ! and try again. 106 else ! Hunt down: 107 jhi = jlo 108 do 109 jlo = jhi - inc 110 if (jlo < 1) then ! Done hunting, since off end of table. 111 jlo = 0 112 exit 113 else 114 if (x >= xx(jlo) .eqv. ascnd) exit 115 jhi = jlo ! Not done hunting, 116 inc = inc + inc ! so double the increment 117 end if 118 END DO ! and try again. 119 end if 119 120 end if ! Done hunting, value bracketed. 120 121 121 122 do ! Hunt is done, so begin the final bisection phase: 122 if (jhi-jlo <= 1) then123 if (x == xx(n)) jlo=n-1124 if (x == xx(1)) jlo=1125 126 127 jm=(jhi+jlo)/2128 if (x >= xx(jm) .eqv. ascnd) then129 jlo=jm130 131 jhi=jm132 133 123 if (jhi - jlo <= 1) THEN 124 if (x == xx(n)) jlo = n - 1 125 if (x == xx(1)) jlo = 1 126 exit 127 else 128 jm = (jhi + jlo) / 2 129 if (x >= xx(jm) .eqv. ascnd) THEN 130 jlo = jm 131 else 132 jhi = jm 133 end if 134 end if 134 135 END DO 135 136 136 137 END SUBROUTINE hunt 137 138 138 end module interpolation139 end module lmdz_interpolation
Note: See TracChangeset
for help on using the changeset viewer.