-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKRIGING.f90
171 lines (136 loc) · 4.82 KB
/
KRIGING.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
! This code performs Kriging at a target location on the basis of known data and variogram parameters
PROGRAM KRIGING
INTEGER::I,J,K,P
INTEGER::N_Z,N_KNOWN,DUMMY
REAL::SIGMA_SQUARE,L,SUM_M
REAL::SEARCH_RADIUS,FACTOR
REAL,ALLOCATABLE,DIMENSION(:,:)::A
REAL,ALLOCATABLE,DIMENSION(:)::B,WEIGHT
REAL,ALLOCATABLE,DIMENSION(:)::X_Z,Y_Z,T_Z
REAL,ALLOCATABLE,DIMENSION(:)::X_KNOWN,Y_KNOWN,T
INTEGER,ALLOCATABLE,DIMENSION(:)::N_SEARCH_RADIUS
REAL::X_KRIGE(1000),Y_KRIGE(1000)
!N_Z:NUMBER OF TARGETS AT WHICH ESTIMATED VALUE IS NEEDED
!SIGMA_SQUARE,L:PARAMTERS OF GAUSSIAN VARIOGRAM USED
!SEARCH_RADIUS: RADIUS IN WHICH POINTS LYING PARTICIPATE IN KRIGING
!N_SEARCH_RADIUS:NUMBER OF POINTS LYING INSIDE SEARCH RADIUS AND THUS PARTICIPATING IN KRIGED
OPEN(UNIT=100,FILE='VARIOGRAM_PARAMETERS.TXT',STATUS='OLD')
READ (100,*) SIGMA_SQUARE,L
CLOSE (100)
OPEN(UNIT=200,FILE='KNOWN_POINTS.TXT',STATUS='OLD')
READ (200,*) N_KNOWN
ALLOCATE (X_KNOWN(N_KNOWN),Y_KNOWN(N_KNOWN),T(N_KNOWN))
DO I=1,N_KNOWN
READ (200,*) X_KNOWN(I),Y_KNOWN(I),T(I)
END DO
CLOSE (200)
OPEN(UNIT=300,FILE='TARGET_POINTS.TXT',STATUS='OLD')
READ (300,*) N_Z
ALLOCATE (X_Z(N_Z),Y_Z(N_Z),T_Z(K))
ALLOCATE (N_SEARCH_RADIUS(N_Z))
DO I=1,N_Z
READ (300,*) X_Z(I),Y_Z(I)
WRITE (*,*) "TARGET X, Y",X_Z(I),Y_Z(I)
END DO
CLOSE (300)
OPEN (UNIT=400,FILE='SEARCH_RADIUS.TXT',STATUS='OLD')
READ (400,*) SEARCH_RADIUS
! WRITE (*,*) SEARCH_RADIUS
CLOSE (400)
DO I=1,N_Z
N_SEARCH_RADIUS(I)=0
END DO
DO K=1,N_Z
N_SEARCH_RADIUS(K)=0
DUMMY=0
DO I=1,N_KNOWN
IF (SQRT(((X_Z(K)-X_KNOWN(I))**2)+((Y_Z(K)-Y_KNOWN(I))**2))<=SEARCH_RADIUS) THEN
DUMMY=DUMMY+1
N_SEARCH_RADIUS(K)=N_SEARCH_RADIUS(K)+1
X_KRIGE(DUMMY)=X_KNOWN(I)
Y_KRIGE(DUMMY)=Y_KNOWN(I)
! WRITE (*,*) N_SEARCH_RADIUS(K),X_KRIGE(DUMMY),Y_KRIGE(DUMMY)
END IF
END DO
ALLOCATE (A(N_SEARCH_RADIUS(K)+1,N_SEARCH_RADIUS(K)+1))
ALLOCATE (B(N_SEARCH_RADIUS(K)+1),WEIGHT(N_SEARCH_RADIUS(K)+1))
WRITE (*,*) "K=",K,"N_SEARCH_RADIUS=",N_SEARCH_RADIUS(K)
DO I=1,N_SEARCH_RADIUS(K)
DO J=1,N_SEARCH_RADIUS(K)
! WRITE (*,*) I,X_KRIGE(I),Y_KRIGE(I),X_KRIGE(J),Y_KRIGE(J)
A(I,J)=GAMMA_H(X_KRIGE(I),Y_KRIGE(I),X_KRIGE(J),Y_KRIGE(J))
! WRITE (*,*) "I=",I,"J=",J,"A(I,J=",A(I,J)
END DO
END DO
DO I=1,N_SEARCH_RADIUS(K)
A(I,N_SEARCH_RADIUS(K)+1)=1
END DO
DO J=1,N_SEARCH_RADIUS(K)
A(N_SEARCH_RADIUS(K)+1,J)=1
END DO
A(N_SEARCH_RADIUS(K)+1,N_SEARCH_RADIUS(K)+1)=0.0
DO I=1,N_SEARCH_RADIUS(K)
B(I)=GAMMA_H(X_KRIGE(I),Y_KRIGE(I),X_Z,Y_Z)
! WRITE (*,*) X_KRIGE(I),Y_KRIGE(I),X_Z,Y_Z,"I=",I,"B(I)=",B(I)
END DO
B(N_SEARCH_RADIUS(K)+1)=1
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!THIS SECTION PRINTS THE MATRIX A & B
OPEN (UNIT=9000,FILE='MATRIX_A.TXT',STATUS='UNKNOWN')
DO J=1,N_SEARCH_RADIUS(K)+1
WRITE (9000,*) (A(I,J),I=1,(N_SEARCH_RADIUS(K)+1))
END DO
CLOSE (9000)
OPEN (UNIT=9001,FILE='MATRIX_B.TXT',STATUS='UNKNOWN')
DO I=1,N_SEARCH_RADIUS(K)+1
WRITE (9001,*) B(I)
END DO
CLOSE (9001)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! USING GAUSS ELIMINATION METHOD TO FIND WEIGHTS
! FORWARD SUBSTITUTION
DO P=1,(N_SEARCH_RADIUS(K)+1)-1
DO I=P+1,N_SEARCH_RADIUS(K)+1
FACTOR=A(I,P)/A(P,P)
DO J=P+1,N_SEARCH_RADIUS(K)+1
A(I,J)=A(I,J)-FACTOR*A(P,J)
END DO
B(I)=B(I)-FACTOR*B(P)
END DO
END DO
! BACK SUBSTITUTION
WEIGHT(N_SEARCH_RADIUS(K)+1)=B(N_SEARCH_RADIUS(K)+1)/A(N_SEARCH_RADIUS(K)+1,N_SEARCH_RADIUS(K)+1)
DO I=N_SEARCH_RADIUS(K)+1-1,1,-1
SUM_M=B(I)
DO J=I+1,N_SEARCH_RADIUS(K)+1
SUM_M=SUM_M-A(I,J)*WEIGHT(J)
END DO
WEIGHT(I)=SUM_M/(A(I,I))
END DO
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
DO I=1,N_SEARCH_RADIUS(K)
WRITE (*,*) "I=",I,"WEIGHT(I)=",WEIGHT(I)
END DO
WRITE (*,*) "LAGRANGE MULTIPLIER=",WEIGHT(N_SEARCH_RADIUS(K)+1)
T_Z(K)=0.0
DO I=1,N_SEARCH_RADIUS(K)
T_Z(K)=T_Z(K)+WEIGHT(I)*T(I)
END DO
WRITE (*,*) "K=",K,"T_Z=",T_Z(K)
DEALLOCATE(A,B,WEIGHT)
END DO !END OF K LOOP
! MATRIX FORMATION
END PROGRAM
FUNCTION GAMMA_H(X_1,Y_1,X_2,Y_2)
REAL,INTENT(IN)::X_1
REAL,INTENT(IN)::Y_1
REAL,INTENT(IN)::X_2
REAL,INTENT(IN)::Y_2
REAL::SIGMA_SQUARE,L,H
OPEN(UNIT=100,FILE='VARIOGRAM_PARAMETERS.TXT',STATUS='OLD')
READ (100,*) SIGMA_SQUARE,L
CLOSE (100)
H=SQRT(((X_1-X_2)**2)+((Y_1-Y_2)**2))
GAMMA_H=1+SIGMA_SQUARE*(1-EXP(-((H/L)**2)))
END FUNCTION