forked from louiswicker/pyOPAWS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcressman.f90
More file actions
executable file
·294 lines (238 loc) · 9.35 KB
/
Copy pathcressman.f90
File metadata and controls
executable file
·294 lines (238 loc) · 9.35 KB
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
subroutine cressman(x, y, ob, xg, yg, method, min_count, min_weight, min_range, roi, missing_value, anal, nobs, nx, ny)
implicit none
real(8), intent(in), dimension(nobs) :: x
real(8), intent(in), dimension(nobs) :: y
real(4), intent(in), dimension(nobs) :: ob
real(8), intent(in), dimension(nx) :: xg
real(8), intent(in), dimension(ny) :: yg
real(4), intent(out), dimension(nx,ny) :: anal
real, intent(in) :: roi, missing_value, min_weight, min_range
integer, intent(in) :: nobs, nx, ny, min_count, method
integer n, i, j, count
real(kind=8) dis, R2, w_sum, sum, wk, rk2
real, parameter :: hsp0 = 1.33
logical, parameter :: debug = .true.
IF ( method .eq. 1 ) THEN
R2 = roi**2.0
ELSE
R2 = (hsp0*roi/1000.)**2 ! Pauley and Wu (1990)
ENDIF
IF( debug ) THEN
print *, 'Method: ', method
print *, 'Min gates for analysis: ', min_count
print *, 'Min weight for analysis:', min_weight
print *, 'Min range for analysis: ', min_range
print *, 'Nobs: ', nobs
print *, 'Nx/Ny: ', nx, ny
print *, 'Maxval of anal before: ', maxval(anal)
print *, 'Minval of anal before: ', minval(anal)
print *, ''
print *, 'Maxval of observations: ', maxval(ob)
print *, 'Minval of observations: ', minval(ob)
print *, 'Min/Max xob: ', minval(x), maxval(x)
print *, 'Min/Max yob: ', minval(y), maxval(y)
print *, 'Min/Max xgrid: ', minval(xg), maxval(xg)
print *, 'Min/Max ygrid: ', minval(yg), maxval(yg)
print *, 'Radius of influence: ', roi
print *, ''
ENDIF
DO j = 1,ny
DO i = 1,nx
IF( method .eq. 1 ) THEN ! Cressman
count = 0
w_sum = 0.0
sum = 0.0
anal(i,j) = missing_value
DO n = 1,nobs
dis = sqrt( (xg(i) - x(n))**2 + (yg(j)-y(n))**2 )
IF ((dis .le. roi) .and. (dis .ge. min_range)) THEN
rk2 = dis**2.0
wk = (R2-rk2) / (R2+rk2)
sum = sum + wk*ob(n)
w_sum = w_sum + wk
count = count + 1
ENDIF
ENDDO
ELSE ! Barnes 1-pass
count = 0
w_sum = 0.0
sum = 0.0
anal(i,j) = missing_value
DO n = 1,nobs
dis = sqrt( (xg(i) - x(n))**2 + (yg(j)-y(n))**2 )
IF ((dis .le. 6.0*roi) .and. (dis .ge. min_range)) THEN
rk2 = dis**2.0
wk = exp( -rk2 / R2 )
sum = sum + wk*ob(n)
w_sum = w_sum + wk
count = count + 1
ENDIF
ENDDO
ENDIF
IF ((w_sum .ge. min_weight) .and. (count .ge. min_count)) THEN
anal(i,j) = anal(i,j) + DBLE(sum/w_sum)
ENDIF
ENDDO
ENDDO
IF( debug ) THEN
print *, 'Maxval of anal after: ', maxval(anal)
print *, 'Minval of anal after: ', minval(anal)
ENDIF
end subroutine cressman
!======================================================================================================'
!
! Routine to cressman a set of obs to a 2D grid (could be a random grid). Implemention
! uses the cKDTree algorithm in python to find the indices for each ob that is on the grid.
!
! Example code in python to create fields that are needed.
!=======================================================================================================
!# Begin python code
!
!import numpy, scipy.spatial
!import time
!
!# set up test grid
!
!x1d = numpy.arange(500) / 500.
!y1d = numpy.arange(600) / 600.
!z1d = numpy.arange(60) / 60.
!
!y_array, z_array, x_array = numpy.meshgrid(y1d, z1d, x1d)
!combined_xyz_arrays = numpy.dstack([z_array.ravel(),y_array.ravel(),x_array.ravel()])[0]
!print 'XYZ ARRAY SHAPE', combined_xyz_arrays.shape
!
!obs = numpy.random.random(30).reshape(3,10)
!print 'Obs Shape:', obs.shape
!obs_list = list(obs.transpose())
!
!# Okay create the KDTree data structure
!
!mytree = scipy.spatial.cKDTree(combined_xyz_arrays)
!
!distances, indices1D = mytree.query(obs_list)
!
!indices3D = numpy.unravel_index(numpy.ravel(indices1D, y_array.size), y_array.shape)
!
!# these are the integer indices that you now pass into the fortran routine. They
!# are the un-raveled 3D index locations nearest the observation point in the 3D array
!
!k = indices3D[0]
!j = indices3D[1]
!i = indices3D[2]
!
!for n in numpy.arange(obs.shape[1]) :
! print n, obs[0,n], obs[1,n], obs[2,n], z_array[k[n],j[n],i[n]], y_array[k[n],j[n],i[n]], x_array[k[n],j[n],i[n]]
!
!# END python code
!======================================================================================================'
SUBROUTINE OBS_2_GRID2D(obs, xob, yob, xc, yc, ii, jj, method, min_count, min_weight, min_range, roi, missing, field, nobs, nx, ny)
implicit none
! Passed in variables
real(kind=8), INTENT(OUT) :: field(ny,nx) ! 2D analysis passed back to calling routine
integer, INTENT(IN) :: nx, ny, nobs ! grid dimensions
real(kind=4), INTENT(IN) :: xob(nobs) ! x coords for each ob
real(kind=4), INTENT(IN) :: yob(nobs) ! y coords for each ob
real(kind=4), INTENT(IN) :: obs(nobs) ! obs
integer(kind=8), INTENT(IN) :: ii(nobs) ! nearest index to the x-point on grid for ob
integer(kind=8), INTENT(IN) :: jj(nobs) ! nearest index to the x-point on grid for o
real(kind=4), INTENT(IN) :: xc(nx) ! coordinates corresponding to WRF model grid locations
real(kind=4), INTENT(IN) :: yc(ny) ! coordinates corresponding to WRF model grid locations
real(kind=4), INTENT(IN) :: roi
real(kind=4), INTENT(IN) :: missing
real(kind=4), INTENT(IN) :: min_weight, min_range
INTEGER(kind=8), INTENT(IN) :: min_count, method
! Local variables
integer(kind=8) i, j, i0, j0, n, i0m, i0p, j0m, j0p, idx, jdx ! loop variables
real(kind=8) dis, wgt, R2, dx, dy, rk2, dxy
real(kind=8), allocatable, dimension(:,:) :: sum, wgt_sum
integer(kind=8), allocatable, dimension(:,:) :: count
real, parameter :: hsp0 = 1.33
logical, parameter :: debug = .false.
! Allocate local memory
allocate(wgt_sum(ny, nx))
allocate(sum(ny, nx))
allocate(count(ny, nx))
! Initialize values
field(:,:) = missing
wgt_sum(:,:) = 0.0
sum(:,:) = 0.0
count(:,:) = 0
dx = xc(2) - xc(1)
dy = yc(2) - yc(1)
dxy = sqrt(dx*dy)
IF ( method .eq. 1 ) THEN
R2 = roi**2.0
idx = 1 + nint(2.0*roi/dx)
jdx = 1 + nint(2.0*roi/dy)
ELSE
R2 = (hsp0*roi/1000.)**2 ! Pauley and Wu (1990)
idx = 1 + nint(7.*roi/dx)
jdx = 1 + nint(7.*roi/dy)
ENDIF
IF( debug) THEN
print *, "----------------------------------------------------------------"
print *
print *, "FORTRAN OBS_2_GRID2D: Method ", method
print *, 'FORTRAN OBS_2_GRID2D: Min gates for analysis: ', min_count
print *, 'FORTRAN OBS_2_GRID2D: Min weight for analysis:', min_weight
print *, 'FORTRAN OBS_2_GRID2D: Min range for analysis: ', min_range
print *, "FORTRAN OBS_2_GRID2D: dims ", nx, ny, nobs
print *, "FORTRAN OBS_2_GRID2D: dx, ROI, R2", dx, ROI, R2
print *, "FORTRAN OBS_2_GRID2D: dy, ROI, R2", dy, ROI, R2
print *, "FORTRAN OBS_2_GRID2D: obs ", minval(obs), maxval(obs)
print *, "FORTRAN OBS_2_GRID2D: x-obs ", minval(xob), maxval(xob)
print *, "FORTRAN OBS_2_GRID2D: y-obs ", minval(yob), maxval(yob)
print *, "FORTRAN OBS_2_GRID2D: x-grid", minval(xc), maxval(xc)
print *, "FORTRAN OBS_2_GRID2D: y-grid", minval(yc), maxval(yc)
print *, "FORTRAN OBS_2_GRID2D: i-index", minval(ii), maxval(ii)
print *, "FORTRAN OBS_2_GRID2D: j-index", minval(jj), maxval(jj)
print *, "FORTRAN OBS_2_GRID2D: i-width", idx
print *, "FORTRAN OBS_2_GRID2D: j-width", jdx
ENDIF
DO n = 1,nobs
i0 = ii(n)
j0 = jj(n)
i0m = max(i0-idx,1)
j0m = max(j0-jdx,1)
i0p = min(i0+idx,nx)
j0p = min(j0+jdx,ny)
IF( method .eq. 1 ) THEN ! Cressman
DO i = i0m, i0p
DO j = j0m, j0p
dis = (xc(i) - xob(n))**2 + (yc(j)-yob(n))**2
wgt = (R2 - dis) / (R2 + dis)
IF (wgt > 0.0) THEN
sum(j,i) = sum(j,i) + wgt*obs(n)
wgt_sum(j,i) = wgt_sum(j,i) + wgt
count(j,i) = count(j,i) + 1
ENDIF
ENDDO ! END J
ENDDO ! END I
ELSE
DO i = i0m, i0p
DO j = j0m, j0p
dis = sqrt( (xc(i) - xob(n))**2 + (yc(j)-yob(n))**2 )
IF ((dis .le. 5.0*roi) .and. (dis .ge. min_range)) THEN
rk2 = (dis/dxy)**2.0
wgt = exp( -rk2 / R2 )
sum(j,i) = sum(j,i) + wgt*obs(n)
wgt_sum(j,i) = wgt_sum(j,i) + wgt
count(j,i) = count(j,i) + 1
ENDIF
ENDDO ! END J
ENDDO ! END I
ENDIF
ENDDO ! END N
WHERE( wgt_sum > min_weight ) field = sum / wgt_sum
WHERE( count < min_count ) field = missing
IF( debug ) THEN
print *, "FORTRAN OBS_2_GRID2D: counts ", minval(count), maxval(count)
print *, "FORTRAN OBS_2_GRID2D: wgts " , minval(wgt_sum), maxval(wgt_sum)
print *, "FORTRAN OBS_2_GRID2D: sums ", minval(sum), maxval(sum)
print *, "FORTRAN OBS_2_GRID2D: field ", minval(field), maxval(field)
ENDIF
deallocate(wgt_sum)
deallocate(sum)
deallocate(count)
RETURN
END SUBROUTINE OBS_2_GRID2D