1 subroutine da_get_gausslats( nj, glats, gwgts, sinlat, coslat)
3 !-----------------------------------------------------------------------
5 !-----------------------------------------------------------------------
9 ! Calculates nj Gaussian latitudes i.e. latitudes at which the Legendre
10 ! polynomial Pn(sin(lat)) = 0.0, n=nj, m=0.
11 ! The integral from -1 to +1 of f(x)*Pn(x) where f is a polynomial
12 ! of degree <= 2n-1 can be calculated using
13 ! 0.5 * sum(GaussWgts(:)*Pn(:)*f(:)) with the values at Gaussian latitudes.
14 ! See eqns 77-79 of 'The Spectral Technique' M.Jarraud and A.J.Simmons
15 ! (1983 ECMWF Seminar or 1990 ECMWF Lecture Notes).
16 ! The orthogonality and normalisation of the Legendre polynomials
17 ! checked in this way are very accurate on the Cray, but somewhat
18 ! less accurate on the HPs(32-bit arithmetic).
19 ! Starting with a regular latitude grid, use Newton-Raphson interpolation
20 ! (with bisection steps to add robustness)
21 ! to find the zeros of the Legendre polynomial Pn(x), 0 <= x < 1,
22 ! the negative roots(-1 < x < 0) are set by symmetry.
23 ! ASin(x) gives the Gaussian latitudes.
24 ! This gives slightly better results than finding the roots of Pn(sin(lat))
25 ! (Algorithm from Numerical Recipies(Fortran version), 1989, p 258)
27 integer, intent(in) :: nj ! Gridpoints in N-S direction.
28 real, intent(out) :: glats(1:nj) ! Gaussian latitudes(S->N, radians).
29 real, intent(out) :: gwgts(1:nj) ! Gaussian weights.
30 real, intent(out), optional :: sinlat(1:nj) ! sin(Latitude).
31 real, intent(out), optional :: coslat(1:nj) ! cos(Latitude).
33 integer, parameter :: maxiter = 100 ! Maximum number of iterations.
34 integer :: i, j, k ! Loop counters.
36 real :: fj, fjold ! Pn(x) on search grid
37 real :: xj, xjold ! search grid
38 real :: x1, x2 ! bounds on root
39 real :: x ! iterated values of x
40 real :: z ! = sqrt(1-x*x)
43 real :: dfr ! 1/Pn'(x)
44 real :: dx, dxold ! step size, previous step
46 if (trace_use) call da_trace_entry("da_get_gausslats")
52 call da_asslegpol(nj, 0, xj, z, fj)
54 if (mod(nj,2) == 1) then
55 call da_asslegpol(nj-1,0,xj,z,fn1)
57 gwgts(k) = 2.0 *(1.0 - xj * xj) /(real(nj) * fn1)**2
61 ! Search interval 0 < x <= 1 for zeros of Legendre polynomials:
66 ! Roots are approximately equally spaced in asin(x)
67 xj = Sin(real(j)*Pi/real(nj*4))
69 call da_asslegpol(nj, 0, xj, z, fj)
71 if (fj >= 0.0 .AND. fjold < 0.0 .OR. fj < 0.0 .AND. fjold >= 0.0) then
73 ! Perform simple interpolation to improve roots(find Gaussian latitudes)
74 if (fjold < 0.0) then ! Orient the search so that fn(x1) < 0
82 x = 0.5*(x1 + x2) ! Initialise the guess for the root
83 dxold = ABS(x1 - x2) ! the step size before last
84 dx = dxold ! and the last step
86 call da_asslegpol(nj, 0, x, z, fn)
87 call da_asslegpol(nj-1,0,x,z,fn1)
88 dfr =(1.0 - x * x) /(real(nj)*(fn1 - x * fn))
92 ! Bisect if Newton out of range or not decreasing fast enough
93 if (((x-x1)-fn*dfr)*((x-x2)-fn*dfr) > 0.0 &
94 .OR. ABS(2.0*fn) > ABS(dxold/dfr)) then
98 else ! Newton-Raphson step
104 if (ABS(dx) < 2.0*SPACinG(x)) exit
106 call da_asslegpol(nj,0,x,z,fn)
107 call da_asslegpol(nj-1,0,x,z,fn1)
108 dfr =(1.0 - x * x) /(real(nj)*(fn1 - x * fn))
110 if (fn < 0.0) then ! Maintain the bracket on the root
117 if (i >= MaxIter) then
118 call da_error(__FILE__,__LINE__, &
119 (/"No convergence finding Gaussian latitudes"/))
124 call da_asslegpol(nj-1,0,x,z,fn1)
125 gwgts(k) = 2.0*(1.0 - x * x) /(real(nj) * fn1)**2
126 glats(nj+1-k) = -glats(k)
127 gwgts(nj+1-k) = gwgts(k)
133 call da_error(__FILE__,__LINE__,(/"Not all roots found"/))
136 ! Calculate sin, cosine:
139 sinlat(j) = sin(glats(j))
140 coslat(j) = cos(glats(j))
142 ! use symmetry for northern hemisphere:
143 sinlat(nj+1-j) = -sinlat(j)
144 coslat(nj+1-j) = coslat(j)
147 if ((nj+1) / 2 == nj/2 + 1) then ! Odd, then equator point:
153 if (trace_use) call da_trace_exit("da_get_gausslats")
155 end subroutine da_get_gausslats