-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectrans_module.f90
283 lines (228 loc) · 8.39 KB
/
vectrans_module.f90
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
module vectrans_module
use shtrans_module, only: shtrans_verbose, shtrans_isinit, shtrans_init, &
shtrans_transf, shtrans_transb, shtrans_hoskinsfilter, shtrans_truncate
use kind_module, only: dp, i4b
private
logical, public :: vectrans_verbose = .false.
integer(kind=i4b), parameter, private :: ityp = 0, isym = 0
real(kind=dp), dimension(:), allocatable, private :: wvts, wvhags, wvhsgs, work
real(kind=dp), dimension(:,:,:), allocatable, private :: v, w, br, bi, cr, ci, a, b, g
integer(kind=i4b), private :: &
nlon, nlat, ntrunc, mdab, ndab, idvw, jdvw
logical, private :: lyrev
public :: vectrans_init, vectrans_clean, vectrans_dlat, vectrans_uv2vrdv, vectrans_gradient
private :: checkerror, checkval
contains
subroutine vectrans_init(nx,ny,nt,np2sp)
implicit none
integer(kind=i4b), intent(in) :: nx, ny, nt
logical, optional, intent(in) :: np2sp
real(kind=dp), dimension(:), allocatable :: dwork
integer(kind=i4b) :: l1, l2, ldwork, lvhags, lvhsgs, lwvts, lwork, ierror
nlon = nx
nlat = ny
ntrunc = ceiling(nlon/3.0)-1
lyrev = .true.
if (present(np2sp).and.np2sp) then
lyrev = .false.
end if
if (vectrans_verbose) then
print *, "nlat=", nlat, " nlon=", nlon, " ntrunc=", ntrunc, &
" nt=", nt, " lyrev=", lyrev
end if
! spherepack uses colatitudes (colat = pi/2 - lat)
! - colat = pi/2 - lat, colat = 0 at NP
! - data is stored from NP->SP
! - meridional wind is reveresed -v
! - merional derivative is reversed -d/dtheta
! init analysis
l1 = min0(nlat,nlon/2)
l2 = nlat/2
lvhags = l1*l2*(nlat+nlat-l1+1)+nlon+15
ldwork = (3*nlat*(nlat+3)+2)/2
allocate(wvhags(lvhags),dwork(ldwork))
call vhagsi(nlat,nlon,wvhags,lvhags,dwork,ldwork,ierror)
call checkerror(ierror,"vhagsi")
! init synthesis
lvhsgs = l1*l2*(nlat+nlat-l1+1)+nlon+15+2*nlat
allocate(wvhsgs(lvhsgs))
call vhsgsi(nlat,nlon,wvhsgs,lvhsgs,dwork,ldwork,ierror)
call checkerror(ierror,"vhsgsi")
deallocate(dwork)
! init colatitude derivatives
lwvts = l1*l2*(nlat+nlat-l1+1)+nlon+15
ldwork = 3*nlat+2
lwork = 3*(max0(l1-2,0)*(nlat+nlat-l1-1))/2+(5*l2+2)*nlat
allocate(wvts(lwvts),work(lwork),dwork(ldwork))
call vtsgsi(nlat,nlon,wvts,lwvts,work,lwork,dwork,ldwork,ierror)
call checkerror(ierror,"vtsgsi")
deallocate(dwork,work)
mdab = min0(nlat,nlon/2)
ndab = nlat
allocate(br(mdab,ndab,nt),bi(mdab,ndab,nt),cr(mdab,ndab,nt),ci(mdab,ndab,nt))
allocate(g(nlat,nlon,nt),a(mdab,ndab,nt),b(mdab,ndab,nt))
idvw = nlat
jdvw = nlon
allocate(v(idvw,jdvw,nt), w(idvw,jdvw,nt))
! vhags, vtsgs=vhsgs
lwork = max(3*nlat*(nlat+1)+2,(2*nt+1)*nlat*nlon)
! vrtgs
lwork = max(lwork,nlat*((nt+1)*nlon+2*nt*l1+1))
! divgs
l1 = min0(nlat,(nlon+2)/2)
lwork = max(lwork,nlat*((nt+1)*nlon+2*nt*l1+1))
allocate(work(lwork))
end subroutine vectrans_init
subroutine vectrans_clean
use shtrans_module, only: shtrans_clean
implicit none
deallocate(wvhags,wvhsgs,wvts,work,br,bi,cr,ci,v,w,g,a,b)
if (shtrans_isinit) then
call shtrans_clean()
end if
end subroutine vectrans_clean
subroutine vectrans_dlat(ui,vi,uy,vy,lfil)
implicit none
real(kind=dp), dimension(:,:,:), intent(in) :: ui, vi
real(kind=dp), dimension(:,:,:), intent(inout) :: uy, vy
logical, optional, intent(in) :: lfil
real(kind=dp) :: fil
integer(kind=i4b) :: i, j, jr, k, n, m, nt, ierror
nt = size(ui,3)
call shtrans_transf(vi,v,lyrev)
call shtrans_transf(ui,w,lyrev)
v(:,:,:) = -v(:,:,:)
call vhags(nlat,nlon,isym,nt,v,w,idvw,jdvw,br,bi,cr,ci, &
mdab,ndab,wvhags,size(wvhags),work,size(work),ierror)
call checkerror(ierror,"vhags")
call shtrans_truncate(br,ntrunc)
call shtrans_truncate(bi,ntrunc)
call shtrans_truncate(cr,ntrunc)
call shtrans_truncate(ci,ntrunc)
if (present(lfil).and.lfil) then
call shtrans_hoskinsfilter(br,ntrunc)
call shtrans_hoskinsfilter(bi,ntrunc)
call shtrans_hoskinsfilter(cr,ntrunc)
call shtrans_hoskinsfilter(ci,ntrunc)
end if
call vtsgs(nlat,nlon,isym,nt,v,w,idvw,jdvw,br,bi,cr,ci, &
mdab,ndab,wvts,size(wvts),work,size(work),ierror)
call checkerror(ierror,"vtsgs")
call shtrans_transb(w,uy,lyrev)
call shtrans_transb(v,vy,lyrev)
if (vectrans_verbose) then
call checkval(ui,"ui")
call checkval(vi,"vi")
call checkval(br,"br")
call checkval(bi,"bi")
call checkval(cr,"cr")
call checkval(ci,"ci")
call checkval(uy,"uy")
call checkval(vy,"vy")
end if
end subroutine vectrans_dlat
subroutine vectrans_uv2vrdv(ui,vi,vr,dv,lfil)
use shtrans_module, only: wshsgs=>shtrans_wshsgs
implicit none
real(kind=dp), dimension(:,:,:), intent(in) :: ui, vi
real(kind=dp), dimension(:,:,:), intent(inout) :: vr, dv
logical, optional, intent(in) :: lfil
real(kind=dp) :: fil
integer(kind=i4b) :: i, j, jr, k, n, m, nx, ny, nt, ierror
nx = size(ui,1)
ny = size(ui,2)
nt = size(ui,3)
if (.not.shtrans_isinit) then
shtrans_verbose = vectrans_verbose
call shtrans_init(nx,ny,nt,.not.lyrev)
end if
call shtrans_transf(ui,w,lyrev)
call shtrans_transf(vi,v,lyrev)
v(:,:,:) = -v(:,:,:)
call vhags(nlat,nlon,ityp,nt,v,w,idvw,jdvw,br,bi,cr,ci, &
mdab,ndab,wvhags,size(wvhags),work,size(work),ierror)
call checkerror(ierror,"vhags")
call shtrans_truncate(br,ntrunc)
call shtrans_truncate(bi,ntrunc)
call shtrans_truncate(cr,ntrunc)
call shtrans_truncate(ci,ntrunc)
if (present(lfil).and.lfil) then
call shtrans_hoskinsfilter(br,ntrunc)
call shtrans_hoskinsfilter(bi,ntrunc)
call shtrans_hoskinsfilter(cr,ntrunc)
call shtrans_hoskinsfilter(ci,ntrunc)
end if
call vrtgs(nlat,nlon,isym,nt,v,idvw,jdvw,cr,ci, &
mdab,ndab,wshsgs,size(wshsgs),work,size(work),ierror)
call checkerror(ierror,"vrtgs")
call shtrans_transb(v,vr,lyrev)
call divgs(nlat,nlon,isym,nt,w,idvw,jdvw,br,bi, &
mdab,ndab,wshsgs,size(wshsgs),work,size(work),ierror)
call checkerror(ierror,"divgs")
call shtrans_transb(w,dv,lyrev)
if (vectrans_verbose) then
call checkval(ui,"ui")
call checkval(vi,"vi")
call checkval(br,"br")
call checkval(bi,"bi")
call checkval(cr,"cr")
call checkval(ci,"ci")
call checkval(vr,"vr")
call checkval(dv,"dv")
end if
end subroutine vectrans_uv2vrdv
subroutine vectrans_gradient(gin,gx,gy,lfil)
use shtrans_module, only: wshags=>shtrans_wshags
implicit none
real(kind=dp), dimension(:,:,:), intent(in) :: gin
real(kind=dp), dimension(:,:,:), intent(inout) :: gx, gy
logical, optional, intent(in) :: lfil
integer(kind=i4b) :: nx, ny, nt, ierror
nx = size(gin,1)
ny = size(gin,2)
nt = size(gin,3)
if (.not.shtrans_isinit) then
shtrans_verbose = vectrans_verbose
call shtrans_init(nx,ny,nt,.not.lyrev)
end if
call shtrans_transf(gin,g,lyrev)
call shags(nlat,nlon,isym,nt,g,idvw,jdvw,a,b,mdab,ndab, &
wshags,size(wshags),work,size(work),ierror)
call checkerror(ierror,"shags")
call shtrans_truncate(a,ntrunc)
call shtrans_truncate(b,ntrunc)
if (present(lfil).and.lfil) then
call shtrans_hoskinsfilter(a,ntrunc)
call shtrans_hoskinsfilter(b,ntrunc)
end if
call gradgs(nlat,nlon,isym,nt,v,w,idvw,jdvw,a,b,mdab,ndab, &
wvhsgs,size(wvhsgs),work,size(work),ierror)
call checkerror(ierror,"gradgs")
v(:,:,:) = -v(:,:,:)
call shtrans_transb(v,gy,lyrev)
call shtrans_transb(w,gx,lyrev)
if (vectrans_verbose) then
call checkval(gin,"gin")
call checkval(a,"a")
call checkval(b,"b")
call checkval(gx,"gx")
call checkval(gy,"gy")
end if
end subroutine vectrans_gradient
subroutine checkerror(ierror,name)
implicit none
integer(kind=i4b), intent(in) :: ierror
character(len=*), intent(in) :: name
if (ierror/=0) then
print *, "*** error in ",name,": ", ierror
stop
end if
end subroutine checkerror
subroutine checkval(x,name)
implicit none
real(kind=dp), dimension(:,:,:) :: x
character(len=*), intent(in) :: name
print *, name, " min=", minval(x), " at ", minloc(x), &
name, " max=", maxval(x), " at ", maxloc(x)
end subroutine checkval
end module vectrans_module