/
SHReturnTapersMap.f95
358 lines (306 loc) · 11.7 KB
/
SHReturnTapersMap.f95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
subroutine SHReturnTapersMap(tapers, eigenvalues, dh_mask, n_dh, lmax, &
sampling, ntapers, degrees, exitstatus)
!------------------------------------------------------------------------------
!
! This subroutine will calculate the eigenvalues and eigenfunctions of the
! generalized concentration problem where the region of interest R is given
! by the grid DH_MASK. This matrix is sampled according to the Driscoll and
! Healy sampling theorem, and possesses a value of 1 inside of R, and 0
! elsewhere. Returned tapers and eigenvalues are ordered from the largest to
! smallest eigenvalue, and the spectral coefficients are packed into a 1D
! column vector according to the scheme described in YilmIndexVector.
!
! The elements Dij are calculated approximately using spherical harmonic
! transforms. The effective bandwidth of the grid DH_MASK should in general
! be larger than LMAX by about a factor of 3 or so for accurate results. In
! addition, as a result of the approximate manner in which the space-
! concentration kernel is calculated, it is preferable to use SAMPLING=2.
!
! Calling Parameters
!
! IN
! dh_mask Integer grid sampled according to the Driscoll and
! Healy sampling theorem. A value of 1 indicates that the
! grid node is in the concentration domain, and a value
! of 0 indicates that it is outside. Dimensioned as
! (n_dh, n_dh) for SAMPLING = 1 or (n_dh, 2*n_dh) for
! SAMPLING = 2.
! n_dh The number of latitude samples in the Driscoll and
! Healy sampled grid.
! lmax Maximum spherical harmonic degree of the outpt
! spherical harmonic coefficients.
! sampling 1 corresponds to equal sampling (n_dh, n_dh), whereas
! 2 corresponds to equal spaced grids (n_dh, 2*n_dh).
!
! IN, OPTIONAL
! ntapers Number of tapers and eigenvalues to output.
! degrees Specify those degrees of the coupling matrix to
! compute. If degrees(l+1) is zero, degree l will not
! be used.
!
! OUT
! tapers Column vectors contain the spherical harmonic
! coefficients, packed according to the scheme described
! in YilmIndexVector. The dimension of this array is
! (lmax+1)**2 by (lmax+1)**2, or (lmax+1)**2 by ntapers
! if ntapers is present.
! eigenvalues A 1-dimensional vector containing the eigenvalues
! corresponding to the columns of tapers, dimensioned as
! (lmax+1)**2.
!
! OPTIONAL (OUT)
! exitstatus If present, instead of executing a STOP when an error
! is encountered, the variable exitstatus will be
! returned describing the error.
! 0 = No errors;
! 1 = Improper dimensions of input array;
! 2 = Improper bounds for input variable;
! 3 = Error allocating memory;
! 4 = File IO error.
!
! Copyright (c) 2005-2019, SHTOOLS
! All rights reserved.
!
!------------------------------------------------------------------------------
use SHTOOLS, only : EigValVecSym, ComputeDMap
use ftypes
implicit none
real(dp), intent(out) :: tapers(:,:), eigenvalues(:)
integer(int32), intent(in) :: dh_mask(:,:), n_dh, lmax, sampling
integer(int32), intent(in), optional :: ntapers, degrees(:)
integer(int32), intent(out), optional :: exitstatus
real(dp), allocatable :: dij(:,:), dijex(:, :), evec(:, :)
integer(int32) :: nlat, nlong, lmax_dh, astat(2), i, j, l, m, exclude, n, &
ind((lmax+1)**2), numk
if (present(exitstatus)) exitstatus = 0
if (present(ntapers)) then
if (ntapers > (lmax+1)**2) then
print*, "Error --- SHRetrunTapersMap"
print*, "The number of output tapers must be less than or " // &
"equal to (LMAX+1)**2."
print*, "LMAX = ", lmax
print*, "NTAPERS = ", ntapers
if (present(exitstatus)) then
exitstatus = 2
return
else
stop
end if
else if (size(tapers(:,1)) < (lmax+1)**2 .or. &
size(tapers(1,:)) < ntapers) then
print*, "Error --- SHReturnTapersMap"
print*, "TAPERS must be dimensioned as ((LMAX+1)**2, NTAPERS)."
print*, "Dimension of TAPERS = ", size(tapers(:,1)), &
size(tapers(1,:))
print*, "LMAX = ", lmax
print*, "NTAPERS = ", ntapers
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
else if (size(eigenvalues(:)) < ntapers) then
print*, "Error --- SHReturnTapersMap"
print*, "EIGENVALUES must be dimensioned as NTAPERS."
print*, "Dimension of EIGENVALUES = ", size(eigenvalues)
print*, "NTAPERS = ", ntapers
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
end if
else
if (size(tapers(:,1)) < (lmax+1)**2 .or. &
size(tapers(1,:)) < (lmax+1)**2) then
print*, "Error --- SHReturnTapersMap"
print*, "TAPERS must be dimensioned as ((LMAX+1)**2, (LMAX+1)**2)."
print*, "Dimension of TAPERS = ", size(tapers(:,1)), &
size(tapers(1,:))
print*, "LMAX = ", lmax
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
else if (size(eigenvalues(:)) < (lmax+1)**2) then
print*, "Error --- SHReturnTapersMap"
print*, "EIGENVALUES must be dimensioned as (LMAX+1)**2"
print*, "Dimension of EIGENVALUES = ", size(eigenvalues)
print*, "LMAX = ", lmax
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
end if
end if
if (mod(n_dh,2) /= 0) then
print*, "Error --- SHReturnTapersMap"
print*, "Number of samples in latitude must be even for the " // &
"Driscoll and Healy sampling theorem."
print*, "N_DH = ", n_dh
if (present(exitstatus)) then
exitstatus = 2
return
else
stop
end if
end if
if (present(degrees)) then
if (size(degrees) < lmax+1) then
print*, "Error --- SHReturnTapersMap"
print*, "DEGREES must have dimension LMAX+1, where LMAX is ", lmax
print*, "Input array is dimensioned as ", size(degrees)
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
end if
end if
nlat = n_dh
if (sampling == 1) then
nlong = nlat
else if (sampling == 2) then
nlong = 2 * nlat
else
print*, "Error --- SHReturnTapersMap"
print*, "SAMPLING must be either 1 (equally sampled) " // &
"or 2 (equally spaced)."
print*, "SAMPLING = ", sampling
if (present(exitstatus)) then
exitstatus = 2
return
else
stop
end if
end if
if (size(dh_mask(:,1)) < nlat .or. size(dh_mask(1,:)) < nlong) then
print*, "Error --- SHReturnTapersMap"
print*, "DH_MASK must be dimensioned as ", nlat, nlong
print*, "Dimensions of DH_MASK are ", size(dh_mask(:,1)), &
size(dh_mask(1,:))
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
end if
lmax_dh = n_dh/2 - 1
if (lmax_dh < lmax) then
print*, "Error --- SHReturnTapersMap"
print*, "The effective bandwith of the input grid DH_MASK must " // &
"be greater or equal than LMAX."
print*, "In practice, this should be about 3 * LMAX."
print*, "LMAX = ", lmax
print*, "Effective bandwidth of DH_MASK = (N/2 -1) = ", lmax_dh
if (present(exitstatus)) then
exitstatus = 2
return
else
stop
end if
end if
eigenvalues = 0.0_dp
tapers = 0.0_dp
allocate (dij((lmax+1)**2, (lmax+1)**2), stat = astat(1))
allocate (evec((lmax+1)**2, (lmax+1)**2), stat = astat(2))
if (astat(1) /= 0 .or. astat(2) /= 0) then
print*, "Error --- SHReturnTapersMap"
print*, "Problem allocating arrays DIJ and EVEC."
if (present(exitstatus)) then
exitstatus = 3
return
else
stop
end if
end if
if (present(exitstatus)) then
call ComputeDMap(Dij, dh_mask, n_dh, lmax, sampling = sampling, &
exitstatus = exitstatus)
if (exitstatus /= 0) return
else
call ComputeDMap(Dij, dh_mask, n_dh, lmax, sampling = sampling)
end if
exclude = 0
n = (lmax+1)**2
if (present(degrees)) then
if (sum(degrees(1:lmax+1)) /= lmax + 1 .and. &
sum(degrees(1:lmax+1)) /= 0) then
exclude = 1
ind = 0
i = 0
do l = 0, lmax
if (degrees(l+1) /= 0) then
do m = 0, l
i = i + 1
ind(i) = l**2 + m + 1
end do
do m = 1, l, 1
i = i + 1
ind(i) = l**2 + l + m + 1
end do
end if
end do
n = i
allocate (dijex(n, n), stat = astat(1))
if (astat(1) /= 0) then
print*, "Error --- SHReturnTapersMap"
print*, "Problem allocating array DIJEX.", astat(1)
if (present(exitstatus)) then
exitstatus = 3
return
else
stop
end if
end if
dijex = 0.0_dp
do i = 1, n
do j = 1, n
dijex(i, j) = dij(ind(i), ind(j))
end do
end do
dij(1:n, 1:n) = dijex(1:n, 1:n)
end if
end if
if (present(exitstatus)) then
if (present(ntapers)) then
call EigValVecSym(Dij(1:n, 1:n), n, eigenvalues, evec, &
k = min(ntapers, n), exitstatus = exitstatus)
if (exitstatus /= 0) return
else
call EigValVecSym(Dij(1:n, 1:n), n, eigenvalues, evec, &
exitstatus = exitstatus)
if (exitstatus /= 0) return
end if
else
if (present(ntapers)) then
call EigValVecSym(Dij(1:n, 1:n), n, eigenvalues, evec, &
k = min(ntapers, n))
else
call EigValVecSym(Dij(1:n, 1:n), n, eigenvalues, evec)
end if
end if
if (present(ntapers)) then
numk = min(ntapers, n)
else
numk = n
end if
if (exclude == 0) then
tapers(1:n, 1:numk) = evec(1:n, 1:numk)
else
do i = 1, n
tapers(ind(i), 1:numk) = evec(i, 1:numk)
end do
end if
deallocate (dij)
deallocate (evec)
if (exclude == 1) deallocate (dijex)
end subroutine SHReturnTapersMap