This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
krige.c
executable file
·178 lines (158 loc) · 4.47 KB
/
krige.c
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
/*
* krige.c
*
* David Garen 8/91, 3/94
*
* Calculate kriging weights
*
* 26 May 2000:
* Change solution method to LU decomposition
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc/malloc.h>
#include "dk_x.h"
double *krige(l, nsta, ad, dgrid, elevations, w)
int l; /* grid index */
int nsta; /* number of stations used */
float **ad; /* matrix of distances between prec/temp
stations for computing kriging weights */
float **dgrid; /* matrix of distances between grid cells
and prec/temp stations */
int *elevations; /* vector of station elevations */
double *w; /* kriging weights */
{
float elevsave; /* stored value of station elevation */
int m, mm, n, nn, i, j; /* loop indexes */
int msave; /* stored value of m index */
int nsp1; /* ns plus 1 */
double *wcalc; /* calculation vector for weights */
int ns; /* number of stations */
int luret; /* return value from lusolv() */
int *staflg; /* station use flags*/
float temp; /* temporary variable */
int itemp; /* temporary variable */
float *dist; /* sorted distance */
int *idx; /* index sorted distance */
double **a; /* data matrix for solving for kriging
weights (input to m_inv()) */
// double *w; /* kriging weights */
// double w[nsta+1];
// nsta = ns;
// find the N closest stations
dist = vector(nsta);
idx = ivector(nsta);
for (i = 0; i < nsta; ++i){
dist[i] = dgrid[l][i];
idx[i] = i;
}
for (i = 0; i < nsta; ++i){
for (j = i + 1; j < nsta; ++j){
if (dist[i] > dist[j]) {
// sort the distance
temp = dist[i];
dist[i] = dist[j];
dist[j] = temp;
// re-sort the index
itemp = idx[i];
idx[i] = idx[j];
idx[j] = itemp;
}
}
}
// set station use flags
ns = 0;
staflg = ivector(nsta);
for (m = 0; m < nsta; m++) {
staflg[idx[m]] = 1;
ns++;
}
// for (i = 0; i < nsta; ++i){
// printf("%f - %i - %i\n",dist[i],idx[i],staflg[i]);
// }
// exit(0);
a = dmatrix(nsta+1, nsta+2);
// w = dvector(nsta+1);
wcalc = dvector(nsta+1);
while (1) {
nsp1 = ns + 1;
/* Load matrix for calculating kriging weights using only
the desired stations (staflg = 1) */
mm = -1;
for (m = 0; m < nsta; m++) {
if (staflg[m] == 1) {
mm++;
nn = -1;
for (n = 0; n < nsta; n++) {
if (staflg[n] == 1) {
nn++;
a[mm][nn] = ad[m][n];
}
}
a[mm][ns] = a[ns][mm] = 1;
a[mm][nsp1] = dgrid[l][m];
}
}
a[ns][ns] = 0;
a[ns][nsp1] = 1;
n = nsp1;
/* Solve linear system for kriging weights */
if ((luret = lusolv(n, a, wcalc)) != 0) {
if (icoord == 1)
fprintf(fpout, "\n\n%s\n%s%d%s%5.2f%s%6.2f%s%6.0f\n\n%s\n",
"Indeterminate linear system ... ",
" Grid cell ", l+1, ": lat ", grid[l].north,
" long ", grid[l].east, " elev ", grid[l].elev*1000,
"Program terminating ...");
else
fprintf(fpout, "\n\n%s\n%s%d%s%10.2f%s%10.2f%s%6.0f\n\n%s\n",
"Indeterminate linear system ... ",
" Grid cell ", l+1, ": northing ", grid[l].north,
" easting ", grid[l].east, " elev ", grid[l].elev*1000,
"Program terminating ...");
exit(0);
}
/* Check for negative weights, throw out the most distant station by elevation with
a negative weight, and recalculate weights until all are positive */
elevsave = 0.0;
mm = msave = -1;
for (m = 0; m < nsta; m++) {
if (staflg[m] == 1) {
mm++;
if (wcalc[mm] < 0.0) {
if (elevations[m] > elevsave) {
msave = m;
elevsave = elevations[m];
}
}
}
}
if (msave >= 0) {
staflg[msave] = 0; // set station use flag to zero for furthest station
ns--;
}
else {
mm = -1;
for (m = 0; m < nsta; m++) {
if (staflg[m] == 1) {
mm++;
w[m] = wcalc[mm];
}
else
w[m] = 0.0;
}
break;
}
}
/* clean up 1D arrays*/
free(dist);
free(idx);
free(staflg);
free(wcalc);
/* clean up 2D arrays*/
for (m = 0; m < nsta+1; m++) {
free(a[m]);
}
free(a);
// return w;
}