-
Notifications
You must be signed in to change notification settings - Fork 4
/
Area_weighted_interpolation_function.f90
243 lines (185 loc) · 8.54 KB
/
Area_weighted_interpolation_function.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
!Area_weighted_interpolation_function
function area_weighted_interpolation_function(xgrid,ygrid,zgrid,xdim,ydim,delta,xval,yval)
!Returns the area weight value for a a point at position xval, yval from the grid values xgrid,ygrid,zgrid
implicit none
integer, intent(in) :: xdim,ydim
real, intent(in) :: delta(2)
real, intent(in) :: xgrid(xdim,ydim),ygrid(xdim,ydim),zgrid(xdim,ydim)
real, intent(in) :: xval,yval
real zval
real area_weighted_interpolation_function
real sum_weight
real weighting
integer i,j,ii,jj
real xpos_area_max,xpos_area_min,ypos_area_max,ypos_area_min
real xpos_max,xpos_min,ypos_max,ypos_min
!If only on grid available then return the value of that grid
if (xdim.eq.1.and.ydim.eq.1) then
area_weighted_interpolation_function=zgrid(xdim,ydim)
return
endif
!Find grid index for position val
i=1+floor((xval-xgrid(1,1))/delta(1)+0.5)
j=1+floor((yval-ygrid(1,1))/delta(2)+0.5)
i=max(1,i);i=min(xdim,i)
j=max(1,j);j=min(ydim,j)
if (i.lt.1.or.j.lt.1.or.i.gt.xdim.or.j.gt.ydim) then
write(*,'(A,4i6)') 'Interpolation out of range in area_weighted_interpolation_function. Stopping. (i,j,xdim,ydim)',i,j,xdim,ydim
write(*,'(4f12.2)') xval,yval,xgrid(1,1),ygrid(1,1)
stop
else
xpos_area_max=xval+delta(1)/2.
xpos_area_min=xval-delta(1)/2.
ypos_area_max=yval+delta(2)/2.
ypos_area_min=yval-delta(2)/2.
zval=0.
sum_weight=0.
do jj=j-1,j+1
do ii=i-1,i+1
xpos_min=max(xpos_area_min,xgrid(ii,jj)-delta(1)/2.)
xpos_max=min(xpos_area_max,xgrid(ii,jj)+delta(1)/2.)
ypos_min=max(ypos_area_min,ygrid(ii,jj)-delta(2)/2.)
ypos_max=min(ypos_area_max,ygrid(ii,jj)+delta(2)/2.)
if (xpos_max.gt.xpos_min.and.ypos_max.gt.ypos_min) then
weighting=(ypos_max-ypos_min)*(xpos_max-xpos_min)/delta(1)/delta(2)
else
weighting=0.
endif
zval=zval+zgrid(ii,jj)*weighting
!write(*,'(4i6,f12.4,f12.4)') i,j,ii,jj,weighting,zgrid(ii,jj)
sum_weight=sum_weight+weighting
enddo
enddo
!write(*,'(f12.4,f12.4)') zval,sum_weight
endif
area_weighted_interpolation_function=zval
end function area_weighted_interpolation_function
function area_weighted_extended_interpolation_function(xgrid,ygrid,zgrid,xdim,ydim,delta,xval,yval,delta_val)
!Returns the area weighted value for rectangle of size delta_val at position xval, yval from the grid values xgrid,ygrid,zgrid
!Delta_val can be any size
implicit none
integer, intent(in) :: xdim,ydim
real, intent(in) :: delta(2)
real, intent(in) :: delta_val(2)
real, intent(in) :: xgrid(xdim,ydim),ygrid(xdim,ydim),zgrid(xdim,ydim)
real, intent(in) :: xval,yval
real zval
real area_weighted_extended_interpolation_function
real sum_weight
real weighting
integer i,j,ii,jj,iii,jjj
real xpos_area_max,xpos_area_min,ypos_area_max,ypos_area_min
real xpos_max,xpos_min,ypos_max,ypos_min
integer ii_delta,jj_delta
!If only on grid available then return the value of that grid
if (xdim.eq.1.and.ydim.eq.1) then
area_weighted_extended_interpolation_function=zgrid(xdim,ydim)
return
endif
!Find grid index for position val
i=1+floor((xval-xgrid(1,1))/delta(1)+0.5)
j=1+floor((yval-ygrid(1,1))/delta(2)+0.5)
i=max(1,i);i=min(xdim,i)
j=max(1,j);j=min(ydim,j)
if (i.lt.1.or.j.lt.1.or.i.gt.xdim.or.j.gt.ydim) then
write(*,'(A,4i6)') 'Interpolation out of range in area_weighted_extended_interpolation_function. Stopping. (i,j,xdim,ydim)',i,j,xdim,ydim
write(*,'(4f12.2)') xval,yval,xgrid(1,1),ygrid(1,1)
stop
else
xpos_area_max=xval+delta_val(1)/2.
xpos_area_min=xval-delta_val(1)/2.
ypos_area_max=yval+delta_val(2)/2.
ypos_area_min=yval-delta_val(2)/2.
jj_delta=1+floor(0.5*(delta_val(2)/delta(2)-1.))
ii_delta=1+floor(0.5*(delta_val(1)/delta(1)-1.))
zval=0.
sum_weight=0.
!write(*,*) i,j,ii_delta,jj_delta
do jjj=j-jj_delta,j+jj_delta
do iii=i-ii_delta,i+ii_delta
jj=max(jjj,1);jj=min(jj,ydim)
ii=max(iii,1);ii=min(ii,xdim)
xpos_min=max(xpos_area_min,xgrid(ii,jj)-delta(1)/2.)
xpos_max=min(xpos_area_max,xgrid(ii,jj)+delta(1)/2.)
ypos_min=max(ypos_area_min,ygrid(ii,jj)-delta(2)/2.)
ypos_max=min(ypos_area_max,ygrid(ii,jj)+delta(2)/2.)
if (xpos_max.gt.xpos_min.and.ypos_max.gt.ypos_min) then
weighting=(ypos_max-ypos_min)*(xpos_max-xpos_min)/delta_val(1)/delta_val(2)
else
weighting=0.
endif
zval=zval+zgrid(ii,jj)*weighting
!write(*,'(4i6,f12.4,f12.4)') i,j,ii,jj,weighting,zgrid(ii,jj)
sum_weight=sum_weight+weighting
!write(*,*) ii,jj,sum_weight
enddo
enddo
!write(*,'(f12.4,f12.4)') zval,sum_weight
endif
area_weighted_extended_interpolation_function=zval
end function area_weighted_extended_interpolation_function
function area_weighted_extended_vectorgrid_interpolation_function(xgrid,ygrid,zgrid,xdim,ydim,delta,xval,yval,delta_val)
!Returns the area weighted value for rectangle of size delta_val at position xval, yval from the grid values xgrid,ygrid,zgrid
!Delta_val can be any size
!vectorgrid means the grid positions only have one dimension
implicit none
integer, intent(in) :: xdim,ydim
real, intent(in) :: delta(2)
real, intent(in) :: delta_val(2)
real, intent(in) :: xgrid(xdim),ygrid(ydim),zgrid(xdim,ydim)
real, intent(in) :: xval,yval
real zval
real area_weighted_extended_vectorgrid_interpolation_function
real sum_weight
real weighting
integer i,j,ii,jj,iii,jjj
real xpos_area_max,xpos_area_min,ypos_area_max,ypos_area_min
real xpos_max,xpos_min,ypos_max,ypos_min
integer ii_delta,jj_delta
!If only on grid available then return the value of that grid
if (xdim.eq.1.and.ydim.eq.1) then
area_weighted_extended_vectorgrid_interpolation_function=zgrid(xdim,ydim)
return
endif
!Find grid index for position val
i=1+floor((xval-xgrid(1))/delta(1)+0.5)
j=1+floor((yval-ygrid(1))/delta(2)+0.5)
i=max(1,i);i=min(xdim,i)
j=max(1,j);j=min(ydim,j)
if (i.lt.1.or.j.lt.1.or.i.gt.xdim.or.j.gt.ydim) then
write(*,'(A,4i6)') 'Interpolation out of range in area_weighted_extended_interpolation_function. Stopping. (i,j,xdim,ydim)',i,j,xdim,ydim
write(*,'(4f12.2)') xval,yval,xgrid(1),ygrid(1)
stop
else
xpos_area_max=xval+delta_val(1)/2.
xpos_area_min=xval-delta_val(1)/2.
ypos_area_max=yval+delta_val(2)/2.
ypos_area_min=yval-delta_val(2)/2.
jj_delta=1+floor(0.5*(delta_val(2)/delta(2)-1.))
ii_delta=1+floor(0.5*(delta_val(1)/delta(1)-1.))
zval=0.
sum_weight=0.
!write(*,*) i,j,ii_delta,jj_delta
do jjj=j-jj_delta,j+jj_delta
do iii=i-ii_delta,i+ii_delta
jj=max(jjj,1);jj=min(jj,ydim)
ii=max(iii,1);ii=min(ii,xdim)
xpos_min=max(xpos_area_min,xgrid(ii)-delta(1)/2.)
xpos_max=min(xpos_area_max,xgrid(ii)+delta(1)/2.)
ypos_min=max(ypos_area_min,ygrid(jj)-delta(2)/2.)
ypos_max=min(ypos_area_max,ygrid(jj)+delta(2)/2.)
if (xpos_max.gt.xpos_min.and.ypos_max.gt.ypos_min) then
weighting=(ypos_max-ypos_min)*(xpos_max-xpos_min)/delta_val(1)/delta_val(2)
else
weighting=0.
endif
zval=zval+zgrid(ii,jj)*weighting
!write(*,'(4i6,f12.4,f12.4)') i,j,ii,jj,weighting,zgrid(ii,jj)
sum_weight=sum_weight+weighting
!write(*,*) ii,jj,sum_weight
enddo
enddo
!write(*,'(f12.4,f12.4)') zval,sum_weight
endif
area_weighted_extended_vectorgrid_interpolation_function=zval
end function area_weighted_extended_vectorgrid_interpolation_function