-
Notifications
You must be signed in to change notification settings - Fork 0
/
latlon_module.F90
311 lines (232 loc) · 8.08 KB
/
latlon_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
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
module latlon_module
use netcdf
use tile_module
implicit none
!-----------------------------------------------------------------------
! Define interfaces and attributes for module routines
private
public :: latlongrid
public :: initialize_latlongrid
public :: finalize_latlongrid
public :: generate_weight
!-----------------------------------------------------------------------
type latlongrid
character(len=1024) :: filename
integer :: ncid
integer :: dimidx, dimidy, dimidz, &
dimidt
integer :: nlon, nlat, nlev, npnt, ntim
real(kind=8), dimension(:), allocatable :: lon, lat, lev, pnt, time
integer, dimension(:, :), allocatable :: counter
integer, dimension(:, :, :), allocatable :: tile
integer, dimension(:, :, :), allocatable :: ilon, jlat
real(kind=8), dimension(:, :, :), allocatable :: dist, wgt
real(kind=8), dimension(:, :), allocatable :: pos
end type latlongrid
!-----------------------------------------------------------------------
contains
!-----------------------------------------------------------------------
subroutine initialize_latlongrid(nlon, nlat, npnt, latlon)
implicit none
integer, intent(in) :: nlon, nlat, npnt
type(latlongrid), intent(out) :: latlon
integer :: i, j, k
real(kind=8) :: dlon, dlat
!print *, 'enter initialize_latlongrid'
dlon = 360.0/nlon
dlat = 180.0/nlat
latlon%nlon = nlon
latlon%nlat = nlat
latlon%nlev = 20
latlon%npnt = npnt
latlon%ntim = 2
allocate(latlon%counter(nlon, nlat))
allocate(latlon%tile(nlon, nlat, npnt))
allocate(latlon%ilon(nlon, nlat, npnt))
allocate(latlon%jlat(nlon, nlat, npnt))
allocate(latlon%dist(nlon, nlat, npnt))
allocate(latlon%wgt(nlon, nlat, npnt))
allocate(latlon%pos(nlon, nlat))
allocate(latlon%lon(nlon))
allocate(latlon%lat(nlat))
allocate(latlon%pnt(npnt))
do i = 1, nlon
latlon%lon(i) = dlon*dble(i-1) + 0.5*dlat
end do
do j = 1, nlat
latlon%lat(j) = dlat*dble(j-1) - 90.0 + 0.5*dlat
do i = 1, nlon
latlon%pos(i, j) = -1.0
latlon%counter(i, j) = 0
do k = 1, npnt
latlon%tile(i, j, k) = 0
latlon%ilon(i, j, k) = 0
latlon%jlat(i, j, k) = 0
latlon%dist(i, j, k) = 1.0e36
latlon%wgt(i, j, k) = 0.0
end do
end do
end do
do i = 1, npnt
latlon%pnt(i) = dble(i)
end do
!print *, 'latlon%lon = ', latlon%lon
!print *, 'latlon%lat = ', latlon%lat
!print *, 'leave initialize_latlongrid'
end subroutine initialize_latlongrid
!----------------------------------------------------------------------
subroutine finalize_latlongrid(latlon)
implicit none
type(latlongrid), intent(inout) :: latlon
deallocate(latlon%lon)
deallocate(latlon%lat)
if(allocated(latlon%lev)) deallocate(latlon%lev)
if(allocated(latlon%time)) deallocate(latlon%time)
if(allocated(latlon%pnt)) deallocate(latlon%pnt)
deallocate(latlon%counter)
deallocate(latlon%tile)
deallocate(latlon%ilon)
deallocate(latlon%jlat)
deallocate(latlon%dist)
deallocate(latlon%wgt)
deallocate(latlon%pos)
end subroutine finalize_latlongrid
!----------------------------------------------------------------------
subroutine generate_weight(tile, latlon)
implicit none
type(tilegrid), dimension(6), intent(in) :: tile
type(latlongrid), intent(inout) :: latlon
integer :: ik, jk
do jk = 1, latlon%nlat
do ik = 1, latlon%nlon
call process_point(ik, jk, tile, latlon)
latlon%pos(ik, jk) = dble(latlon%tile(ik, jk, 1))
if((mod(ik-1,10) == 0) .and. (mod(jk-1, 10) == 0)) then
print *, 'ik,jk,latlon%pos(ik, jk),latlon%dist(ik, jk, :) = ', &
ik,jk,latlon%pos(ik, jk),latlon%dist(ik, jk, :)
end if
end do
end do
!Check pos info
do jk = 1, latlon%nlat
do ik = 1, latlon%nlon
call weighting(latlon%npnt, latlon%npnt, &
latlon%dist(ik, jk, :), latlon%wgt(ik, jk, :))
end do
end do
end subroutine generate_weight
!----------------------------------------------------------------------
subroutine process_point(ik, jk, tile, latlon)
implicit none
integer, intent(in) :: ik, jk
type(tilegrid), dimension(6), intent(in) :: tile
type(latlongrid), intent(inout) :: latlon
integer :: i, j, n
real(kind=8) :: plat, plon
plon = latlon%lon(ik)
plat = latlon%lat(jk)
do n = 1, 6
do j = 1, tile(n)%ny
do i = 1, tile(n)%nx
call check_point(ik, jk, i, j, n, plat, plon, &
tile(n)%lat(i,j), tile(n)%lon(i,j), latlon)
end do
end do
end do
end subroutine process_point
!----------------------------------------------------------------------
subroutine check_point(ik, jk, i, j, n, xlat1, xlon1, xlat2, xlon2, latlon)
implicit none
integer, intent(in) :: ik, jk, i, j, n
real(kind=8), intent(in) :: xlat1, xlon1, xlat2, xlon2
type(latlongrid), intent(inout) :: latlon
real(kind=8) :: dist
integer :: k
call distance(xlat1, xlon1, xlat2, xlon2, dist)
k = latlon%counter(ik, jk)
if((k > latlon%npnt) .and. (dist >= latlon%dist(ik, jk, latlon%npnt))) then
latlon%counter(ik, jk) = k + 1
return
end if
call insert(ik, jk, i, j, n, dist, latlon)
end subroutine check_point
!----------------------------------------------------------------------
subroutine insert(ik, jk, i, j, n, dist, latlon)
implicit none
integer, intent(in) :: ik, jk, i, j, n
real(kind=8), intent(in) :: dist
type(latlongrid), intent(inout) :: latlon
integer :: k, kk, m
k = latlon%counter(ik, jk)
latlon%counter(ik, jk) = k + 1
if(k >= latlon%npnt) then
k = latlon%npnt
else
k = k + 1
end if
do kk = 1, latlon%npnt
m = k - 1
if(m < 1) then
exit
end if
if(dist < latlon%dist(ik, jk, m)) then
latlon%tile(ik, jk, k) = latlon%tile(ik, jk, m)
latlon%ilon(ik, jk, k) = latlon%ilon(ik, jk, m)
latlon%jlat(ik, jk, k) = latlon%jlat(ik, jk, m)
latlon%dist(ik, jk, k) = latlon%dist(ik, jk, m)
else
exit
end if
k = k - 1
end do
latlon%tile(ik, jk, k) = n
latlon%ilon(ik, jk, k) = i
latlon%jlat(ik, jk, k) = j
latlon%dist(ik, jk, k) = dist
!print *, 'ik,jk,latlon%dist(ik, jk, :) = ', ik,jk,latlon%dist(ik, jk, :)
end subroutine insert
!----------------------------------------------------------------------
subroutine distance(xlat1, xlon1, xlat2, xlon2, dist)
implicit none
real(kind=8), intent(in) :: xlat1, xlon1, xlat2, xlon2
real(kind=8), intent(out) :: dist
real(kind=8) :: lat1, lon1, lat2, lon2, dlon, dlat
real(kind=8) :: deg2arc, ang, sindlat, sindlon
deg2arc = 3.1415926536/180.0
lat1 = xlat1 * deg2arc
lat2 = xlat2 * deg2arc
lon1 = xlon1 * deg2arc
lon2 = xlon2 * deg2arc
dlon = lon2 - lon1
dlat = lat2 - lat1
sindlat = sin(0.5*dlat)
sindlon = sin(0.5*dlon)
ang = sindlat * sindlat + cos(lat1) * cos(lat2) * sindlon * sindlon
!dist = 2.0 * atan2(sqrt(ang), sqrt(1.0-ang))
dist = 2.0 * asin(min(1.0,sqrt(ang)))
end subroutine distance
!----------------------------------------------------------------------
subroutine weighting(n, m, dist, wgt)
implicit none
integer, intent(in) :: n, m
real(kind=8), dimension(n), intent(in) :: dist
real(kind=8), dimension(n), intent(out) :: wgt
real(kind=8) :: total, factor
integer :: k
total = 0.0
do k = 1, m
total = total + dist(k)
end do
if(m > 1) then
factor = float(m - 1) * total
else
factor = total
end if
do k = 1, m
wgt(k) = (total - dist(k)) / factor
end do
do k = m+1, n
wgt(k) = 0.0
end do
end subroutine weighting
end module latlon_module