File: grsmooth.cc

package info (click to toggle)
gri 2.4.2-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,540 kB
  • ctags: 1,966
  • sloc: cpp: 32,542; lisp: 3,243; perl: 806; makefile: 548; sh: 253
file content (241 lines) | stat: -rw-r--r-- 5,633 bytes parent folder | download
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
#include        <math.h>
#include        <stdio.h>
#include	"gr.hh"
#include	"GMatrix.hh"


// gr_smootharray() -- smooth z(x,y) data
// 
// DESCRIPTION Smooths data z[i][j] defined on a rectangular grid x[i] and y[j]
// (where 0<=i<nx and 0<=j<ny). The resultant smoothed field is put into
// zS[][]. The array legit[i][j] is 1 if z[i][j] is a good data point; 0
// otherwise. When computing the smoothed value at a point, neighboring
// points having legit[][]=0 are ignored. So are points outside the region
// 0<=i<nx and 0<=j<ny. The distance between grid points in the x direction
// is dx; in the y direction it's dy. dt indicates the strength of smoothing,
// as described below.  Holes in the grid (i.e., missing values) will be
// smoothed over if fill_in is false.
// 
// Returns true if all was OK; false if unknown method supplied.
// 
// METHOD: 'method' specifies the smoothing algorithm as follows:
// 
// method = 0
// 
// Smoothing is by equal weights over points in a + shaped window about the
// datum: zS[i][j] =   z[i][j]/2 + (z[i-1][j]+z[i+1][j])/8 +
// (z[i][j-1]+z[i][j+1])/8 The parameters dx, dy, and dt are ignored.
// 
// method = 1
// 
// Smoothing is across x: zS[i][j] = z[i][j]/2 + (z[i-1][j] + z[i+1][j]) / 4;
// 
// method = 2
// 
// Smoothing is across y: zS[i][j] = z[i][j]/2 + (z[i][j-1] + z[i][j+1]) / 4;
// 
// BUGS: None known.
bool
gr_smootharray(double dx, 
               double dy,
               double dt,
	       GriMatrix<double> &z,
	       GriMatrix<double> &zS,
	       GriMatrix<bool> &legit, 
	       GriMatrix<bool> &legitS,
	       int nx,
	       int ny,
	       int method)
{
    register int    i, j;
    double          sum;
    int             good;	// number data used per gridpoint
    int             nx_1, ny_1;
    // Test for errors
    if (nx <= 0 || ny <= 0)
	return false;
    nx_1 = nx - 1;
    ny_1 = ny - 1;
    dx = dy = dt = 0.0;		// Kill warning on non-use
    switch (method) {
    case 0:
	for (i = 0; i < nx; i++) {
	    for (j = 0; j < ny; j++) {
		good = 0;
		sum = 0.0;
		// Datum at centre
		if (legit(i, j) == true) {
		    sum += 0.5 * z(i, j);
		    good++;
		}
		// Datum to left
		if (i == 0) {
		    // Left edge: interpolate
		    if (legit(i + 1, j) == true
			&& legit(i, j) == true) {
			sum += 0.125 * (2.0 * z(i, j) - z(i + 1, j));
			good++;
		    }
		} else {
		    // Interior
		    if (legit(i - 1, j) == true) {
			sum += 0.125 * z(i - 1, j);
			good++;
		    }
		}
		// Datum to right
		if (i == nx_1) {
		    // Right edge: interpolate
		    if (legit(i - 1, j) == true
			&& legit(i, j) == true) {
			sum += 0.125 * (2.0 * z(i, j) - z(i - 1, j));
			good++;
		    }
		} else {
		    // Interior
		    if (legit(i + 1, j) == true) {
			sum += 0.125 * z(i + 1, j);
			good++;
		    }
		}
		// Datum below
		if (j == 0) {
		    // Bottom: interpolate
		    if (legit(i, j + 1) == true
			&& legit(i, j) == true) {
			sum += 0.125 * (2.0 * z(i, j) - z(i, j + 1));
			good++;
		    }
		} else {
		    if (legit(i, j - 1) == true) {
			sum += 0.125 * z(i, j - 1);
			good++;
		    }
		}
		// Datum above
		if (j == ny_1) {
		    // Top: interpolate
		    if (legit(i, j - 1) == true
			&& legit(i, j) == true) {
			sum += 0.125 * (2.0 * z(i, j) - z(i, j - 1));
			good++;
		    }
		} else {
		    if (legit(i, j + 1) == true) {
			sum += 0.125 * z(i, j + 1);
			good++;
		    }
		}
		if (good == 5) {
		    zS(i, j) = sum;
		    legitS(i, j) = true;
		} else {
		    zS(i, j) = z(i, j);	// won't be used anyway
		    legitS(i, j) = false;
		}
	    }
	}
	break;
    case 1:			// smooth across x
	for (i = 0; i < nx; i++) {
	    for (j = 0; j < ny; j++) {
		sum = 0.0;
		good = 0;
		// Datum at centre
		if (legit(i, j) == true) {
		    sum += 0.5 * z(i, j);
		    good++;
		}
		// Datum to left
		if (i == 0) {
		    // Left edge: interpolate
		    if (legit(i + 1, j) == true
			&& legit(i, j) == true) {
			sum += 0.25 * (2.0 * z(i, j) - z(i + 1, j));
			good++;
		    }
		} else {
		    // Interior
		    if (legit(i - 1, j) == true) {
			sum += 0.25 * z(i - 1, j);
			good++;
		    }
		}
		// Datum to right
		if (i == nx_1) {
		    // Right edge: interpolate
		    if (legit(i - 1, j) == true
			&& legit(i, j) == true) {
			sum += 0.25 * (2.0 * z(i, j) - z(i - 1, j));
			good++;
		    }
		} else {
		    // Interior
		    if (legit(i + 1, j) == true) {
			sum += 0.25 * z(i + 1, j);
			good++;
		    }
		}
		if (good == 5) {
		    zS(i, j) = sum;
		    legitS(i, j) = true;
		} else {
		    zS(i, j) = z(i, j);	// won't be used anyway
		    legitS(i, j) = false;
		}
	    }
	}
	break;
    case 2:			// smooth across y
	for (i = 0; i < nx; i++) {
	    for (j = 0; j < ny; j++) {
		sum = 0.0;
		good = 0;
		// Datum at centre
		if (legit(i, j) == true) {
		    sum += 0.5 * z(i, j);
		    good++;
		}
		// Datum below
		if (j == 0) {
		    // Bottom: interpolate
		    if (legit(i, j + 1) == true
			&& legit(i, j) == true) {
			sum += 0.25 * (2.0 * z(i, j) - z(i, j + 1));
			good++;
		    }
		} else {
		    if (legit(i, j - 1) == true) {
			sum += 0.25 * z(i, j - 1);
			good++;
		    }
		}
		// Datum above
		if (j == ny_1) {
		    // Top: interpolate
		    if (legit(i, j - 1) == true
			&& legit(i, j) == true) {
			sum += 0.25 * (2.0 * z(i, j) - z(i, j - 1));
			good++;
		    }
		} else {
		    if (legit(i, j + 1) == true) {
			sum += 0.25 * z(i, j + 1);
			good++;
		    }
		}
		if (good == 5) {
		    zS(i, j) = sum;
		    legitS(i, j) = true;
		} else {
		    zS(i, j) = z(i, j);	// won't be used anyway
		    legitS(i, j) = false;
		}
	    }
	}
	break;
    default:
	return false;		// unknown
    }
    return true;
}