forked from bakerb845/fem25
-
Notifications
You must be signed in to change notification settings - Fork 0
/
krige.f
295 lines (294 loc) · 9.87 KB
/
krige.f
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
SUBROUTINE ORDKRG(MPTS,NPTS, NVAR, DECSCL,DECFAC,X0,Y0,
; XLOCS,YLOCS, OBS, RKRG,IERR)
!
! Performs an ordinary kriging on a data set. Here we use a
! scaling factor based on distance for calculating the covariance
! DECFAC*exp(-h/DECSCL). Algorithm 4.2 of Geostatistics for
! Engineers and Earth Scientsits - Olea.
! - Ben Baker
!
! INPUT MEANING
! ----- -------
! DECFAC distance scalaing factor in exponential > 0
! DECSCL distance scaling factor on exponential > 0
! MPTS leading dimension for observation list
! NPTS number of observation points
! NVAR number of variables to interpolate at each point
! OBS observations at xlocs, ylocs
! X0 x point to estimate
! XLOCS x locations of observations
! Y0 y point to estimate
! YLOCS y locations of observations
!
! OUTPUT MEANING
! ------ -------
! IERR error in factorization
! RKRG interpolated observations
!
!.... variable declarations
REAL*8, INTENT(IN) :: XLOCS(NPTS), YLOCS(NPTS), X0,Y0,
; DECFAC,DECSCL
REAL*4, INTENT(IN) :: OBS(MPTS,*)
INTEGER*4 MPTS,NPTS,NVAR
REAL*4, INTENT(OUT) :: RKRG(NVAR)
!.... local variables
REAL*8, ALLOCATABLE :: VMAT(:,:), V(:), OBS8(:)
INTEGER*4, ALLOCATABLE :: IPIV(:)
!
!----------------------------------------------------------------------!
!
!.... calculate the SPD covariance matrix and RHS
IERR = 0
LDV = NPTS + 1
ALLOCATE(VMAT(LDV,LDV))
N = NPTS + 1
ALLOCATE(V(N))
CALL GVMAT_KRIGE(LDV, NPTS,1,DECSCL,DECFAC,X0,Y0,
; XLOCS,YLOCS, V,VMAT)
!
!.... LU factor matrix
ALLOCATE(IPIV(N))
CALL DGETRF(N,N,VMAT,LDV,IPIV,INFO)
IF (INFO.LT.0) THEN
WRITE(*,*) 'ordkrg: DGETRF illegal variable:',INFO
IERR = 1
GOTO 730
ENDIF
IF (INFO.GT.0) THEN
WRITE(*,*) 'ordkrg: DGETRF Singular matrix:',INFO
IERR = 1
GOTO 730
ENDIF
CALL DGETRS('N',N,1,VMAT,LDV,IPIV,V,N,INFO)
IF (INFO.LT.0) THEN
WRITE(*,*) 'ordkrg: DGETRS illegal variable:',INFO
IERR = 1
GOTO 730
ENDIF
!
!.... dot the weights with the observations to estimate value
ALLOCATE(OBS8(N))
OBS8(N) = 0.D0
DO 1 IVAR=1,NVAR
DO 2 IPTS=1,NPTS
OBS8(IPTS) = DBLE(OBS(IPTS,IVAR))
2 CONTINUE
RKRG(IVAR) = SNGL(DOT_PRODUCT(OBS8,V))
1 CONTINUE
!
!.... clear memory
730 CONTINUE !break ahead for errors
DEALLOCATE(VMAT)
DEALLOCATE(V)
DEALLOCATE(IPIV)
IF (ALLOCATED(OBS8)) DEALLOCATE(OBS8)
RETURN
END
! !
!======================================================================!
! !
SUBROUTINE SIMKRG(MPTS,NPTS, NVAR, DECSCL,DECFAC,X0,Y0,
; XLOCS,YLOCS, OBS, RKRG,IERR)
!
! Performs a normal kriging on a data set. Here we use a
! scaling factor based on distance for calculating the covariance
! DECFAC*exp(-h/DECSCL). Algorithm 2.1 of Geostatistics for
! Engineers and Earth Scientsits - Olea.
! - Ben Baker
!
! INPUT MEANING
! ----- -------
! DECFAC distance scalaing factor in exponential > 0
! DECSCL distance scaling factor on exponential > 0
! MPTS leading dimension for observation list
! NPTS number of observation points
! NVAR number of variables to interpolate at each point
! OBS observations at xlocs, ylocs
! X0 x point to estimate
! XLOCS x locations of observations
! Y0 y point to estimate
! YLOCS y locations of observations
!
! OUTPUT MEANING
! ------ -------
! IERR error in factorization
! RKRG interpolated observations
!
!.... variable declarations
REAL*8, INTENT(IN) :: XLOCS(NPTS), YLOCS(NPTS), X0,Y0,
; DECFAC,DECSCL
REAL*4, INTENT(IN) :: OBS(MPTS,*)
INTEGER*4 MPTS,NPTS,NVAR
REAL*4, INTENT(OUT) :: RKRG(NVAR)
!.... local variables
REAL*8, ALLOCATABLE :: VMAT(:,:), V(:), OBS8(:)
REAL*8 DMEAN
!
!----------------------------------------------------------------------!
!
!.... calculate the SPD covariance matrix and RHS
IERR = 0
LDV = NPTS
ALLOCATE(VMAT(LDV,LDV))
N = NPTS
ALLOCATE(V(N))
CALL GVMAT_KRIGE(LDV, NPTS,0,DECSCL,DECFAC,X0,Y0,
; XLOCS,YLOCS, V,VMAT)
!
!.... Cholesky factor matrix
CALL DPOTRF('U',N,VMAT,LDV,INFO)
IF (INFO.LT.0) THEN
WRITE(*,*) 'ordkrg: DPOTRF illegal variable:',INFO
IERR = 1
GOTO 730
ENDIF
IF (INFO.GT.0) THEN
WRITE(*,*) 'ordkrg: DPOTRF Singular matrix:',INFO
IERR = 1
GOTO 730
ENDIF
!
!.... V now becomes the krige weights
CALL DPOTRS('U',N,1,VMAT,LDV,V,N,INFO)
IF (INFO.LT.0) THEN
WRITE(*,*) 'ordkrg: DPOTRS illegal variable:',INFO
IERR = 1
GOTO 730
ENDIF
!
!.... dot the weights with the observations to estimate value
ALLOCATE(OBS8(N))
DO 1 IVAR=1,NVAR
!
!....... copy observations and demean
DMEAN = 0.D0
DO 2 IPTS=1,NPTS
OBS8(IPTS) = DBLE(OBS(IPTS,IVAR))
DMEAN = DMEAN + OBS8(IPTS)
2 CONTINUE
DMEAN = DMEAN/DFLOAT(NPTS)
DO 3 IPTS=1,NPTS
OBS8(IPTS) = OBS8(IPTS) - DMEAN
3 CONTINUE
RKRG(IVAR) = SNGL(DMEAN + DOT_PRODUCT(OBS8,V))
1 CONTINUE
!
!.... clear memory
730 CONTINUE !break ahead for errors
DEALLOCATE(VMAT)
DEALLOCATE(V)
IF (ALLOCATED(OBS8)) DEALLOCATE(OBS8)
RETURN
END
! !
!======================================================================!
! !
SUBROUTINE GDMAT_KRIGE(MPTS, NPTS,X0,Y0,XLOCS,YLOCS, DIST,DMAT)
!
! Generates the distance matrix and distance from known points
! to krige point
!
! INPUT MEANING
! ----- -------
! MPTS leading dimension for DMAT
! NPTS number of points in x and z locations
! X0 x location of point to krige
! XLOCS x locations of observations
! Y0 y location of point to krige
! YLOCS y locations of observations
!
! OUTPUT MEANING
! ------ -------
! DIST distance of krige point to known points
! DMAT distance matrix between known points
!
!.... variable declarations
REAL*8, INTENT(IN) :: XLOCS(NPTS), YLOCS(NPTS), X0,Y0
INTEGER*4, INTENT(IN) :: MPTS,NPTS
REAL*8, INTENT(OUT) :: DMAT(MPTS,*), DIST(NPTS)
!.... local variables
REAL*8 DIST2
!
!----------------------------------------------------------------------!
!
!.... calculate the SPD distance matrix
DO 1 IPTS=1,NPTS
DO 2 JPTS=IPTS,NPTS
DIST2 = (XLOCS(IPTS) - XLOCS(JPTS))**2
; + (YLOCS(IPTS) - YLOCS(JPTS))**2
DMAT(IPTS,JPTS) = DSQRT(DIST2)
IF (IPTS.NE.JPTS) DMAT(JPTS,IPTS) = DMAT(IPTS,JPTS)
2 CONTINUE
DIST(IPTS) = DSQRT( (X0 - XLOCS(IPTS))**2
; + (Y0 - YLOCS(IPTS))**2 )
1 CONTINUE
RETURN
END
! !
!======================================================================!
! !
SUBROUTINE GVMAT_KRIGE(LDV, NPTS,JOB,DECSCL,DECFAC,X0,Y0,
; XLOCS,YLOCS, V,VMAT)
!
! Generates the V matrix for a normal or simple krige (default)
!
! INPUT MEANING
! ----- -------
! DECFAC expontial decay factor; > 0.0 in neighbors importance
! DECSCL scale factor on exponetial decay; > 0.0
! DMAT matrix of distances to points
! JOB = 1 ordinary krige, otherwise simple krige (default)
! LDD leading dimension of DMAT
! LDV leading dimension for VMAT
! NPTS number of observations
! X0 x location of point to krige
! XLOCS x locations of observation points
! Y0 y location of point to krige
! YLOCS y locations of observation points
!
! OUTPUT MEANING
! ------ -------
! VMAT covariance matrix for an ordinary or simple krige
! V RHS for krige
!
!.... variable declarations
REAL*8, INTENT(IN) :: XLOCS(NPTS), YLOCS(NPTS), X0,Y0,
; DECSCL,DECFAC
INTEGER*4, INTENT(IN) :: LDV,NPTS,JOB
REAL*8, INTENT(OUT) :: VMAT(LDV,*), V(*)
!.... local variables
REAL*8 ARG, DIST
!
!---------------------------------------------------------------------!
!
!.... calculate covariance off diagonals
IROW = 1
DO 1 IPTS=1,NPTS
DIST = DSQRT( (XLOCS(IPTS) - X0)**2 + (YLOCS(IPTS) - Y0)**2 )
ARG =-DIST/DECFAC
V(IPTS) = DECSCL*DEXP(ARG)
VMAT(IROW,IROW) = DECSCL
IROW = IROW + 1
JCOL = IROW
KPTS = 0
DO 2 JPTS=IPTS,NPTS
JCOL = JCOL + 1
DIST = DSQRT( (XLOCS(IPTS) - XLOCS(JPTS))**2
; + (YLOCS(IPTS) - YLOCS(JPTS))**2 )
ARG =-DIST/DECFAC
VMAT(IPTS,JPTS) = DECSCL*DEXP(ARG)
VMAT(JPTS,IPTS) = VMAT(IPTS,JPTS)
2 CONTINUE
1 CONTINUE
!
!.... constraints for ordinary krige
IF (JOB.EQ.1) THEN
V(NPTS+1) = 1.D0
VMAT(NPTS+1,NPTS+1) = 0.D0
DO 3 IPTS=1,NPTS
VMAT(NPTS+1,IPTS) = 1.D0
VMAT(IPTS,NPTS+1) = 1.D0
3 CONTINUE
ENDIF
RETURN
END