File: smooth.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,881 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        <string.h>
#include	"gr.hh"
#include	"extern.hh"
#include	"GMatrix.hh"

bool            smooth_grid_dataCmd(void);
bool            smoothCmd(void);
bool            smooth_xCmd(void);
bool            smooth_yCmd(void);

bool
smoothCmd()
{
    if (_nword > 1) {
	if (!strcmp(_word[1], "grid"))
	    smooth_grid_dataCmd();
	else if (!strcmp(_word[1], "x"))
	    smooth_xCmd();
	else if (!strcmp(_word[1], "y"))
	    smooth_yCmd();
	else
	    err("`smooth' what?? (valid options x|y|grid)");
    } else {
	err("`smooth' needs more info (valid options x|y|grid)");
	return false;
    }
    return true;
}

bool
smooth_xCmd()
{
    if (!_columns_exist) {
	warning("No x data exist yet.  Try `read columns x y'");
	return false;
    }
    if (_nword < 2 || _nword > 3) {
	demonstrate_command_usage();
	NUMBER_WORDS_ERROR;
	return false;
    }
    int i, n = _colX.size() - 1;
    vector<double> copy(_colX.size(), 0.0);
    // Copy all, to get endparts
    for (i = 0; i < n; i++)
	copy[i] = _colX[i];
    if (_nword == 2) {
	copy[0] = _colX[0];
	copy[_colX.size() - 1] = _colX[_colX.size() - 1];
	for (i = 1; i < n; i++) {
	    double x0 = _colX[i];
	    double xleft = _colX[i - 1];
	    double xright = _colX[i + 1];
	    if (!gr_missingx(x0) && !gr_missingx(xleft) && !gr_missingx(xright)) {
		copy[i] = 0.25 * (xleft + xright) + 0.5 * x0;
	    } else {
		copy[i] = gr_currentmissingvalue();
	    }
	}
    } else if (_nword == 3) {
	double          tmp;
	int             halfwidth;
	if (!getdnum(_word[2], &tmp)) {
	    err("Can't read .n. in `\\", _word[2], "'.", "\\");
	    return false;
	}
	halfwidth = int(0.5 + floor(tmp) / 2);
	if (halfwidth < 1) {
	    err("Improper .n.; require >= 3");
	    return false;
	}
	if (halfwidth >= n) {
	    err("Improper .n.; require < n / 2");
	    return false;
	}
	for (i = halfwidth; i <= n - halfwidth; i++) {
	    int             j, nsum = 0;
	    double          sum = 0.0;
	    for (j = -halfwidth; j <= halfwidth; j++) {
		double tmp = _colX[i + j];
		if (-1 < (i + j) && (i + j) <= n && !gr_missingx(tmp)) {
		    sum += tmp;
		    nsum++;
		}
	    }
	    if (nsum > 0) {
		copy[i] = sum / nsum;
	    } else {
		copy[i] = gr_currentmissingvalue();
	    }
	}
    } else {
	fatal_err("Programming error in `smooth x'");
    }
    for (i = 0; i < n; i++)
	_colX[i] = copy[i];
    return true;
}

bool
smooth_yCmd()
{
    if (!_columns_exist) {
	warning("No y data exist yet.  Try `read columns x y'");
	return false;
    }
    if (_nword < 2 || _nword > 3) {
	demonstrate_command_usage();
	NUMBER_WORDS_ERROR;
	return false;
    }
    int i, n = _colY.size() - 1;
    // Copy all, to get endparts
    vector<double> copy(_colY.size(), 0.0);
    for (i = 0; i < n; i++)
	copy[i] = _colY[i];
    if (_nword == 2) {
	copy[0] = _colY[0];
	copy[_colY.size() - 1] = _colY[_colY.size() - 1];
	for (i = 1; i < n; i++) {
	    double y0 = _colY[i];
	    double yleft = _colY[i - 1];
	    double yright = _colY[i + 1];
	    if (!gr_missingy(y0) && !gr_missingy(yleft) && !gr_missingy(yright)) {
		copy[i] = 0.25 * (yleft + yright) + 0.5 * y0;
	    } else {
		copy[i] = gr_currentmissingvalue();
	    }
	}
    } else if (_nword == 3) {
	double          tmp;
	int             halfwidth;
	if (!getdnum(_word[2], &tmp)) {
	    err("Can't read .n. in `\\", _word[2], "'.", "\\");
	    return false;
	}
	halfwidth = int(0.5 + floor(tmp) / 2);
	if (halfwidth < 1) {
	    err("Improper .n.; require >= 3");
	    return false;
	}
	if (halfwidth >= n) {
	    err("Improper .n.; require < n / 2");
	    return false;
	}
	for (i = halfwidth; i <= n - halfwidth; i++) {
	    int             j, nsum = 0;
	    double          sum = 0.0;
	    for (j = -halfwidth; j <= halfwidth; j++) {
		double tmp = _colY[i + j];
		if (-1 < (i + j) && (i + j) <= n && !gr_missingy(tmp)) {
		    sum += tmp;
		    nsum++;
		}
	    }
	    if (nsum > 0) {
		copy[i] = sum / nsum;
	    } else {
		copy[i] = gr_currentmissingvalue();
	    }
	}
    } else {
	fatal_err("Programming error in `smooth y'");
    }
    for (i = 0; i < n; i++)
	_colY[i] = copy[i];
    return true;
}

// `smooth grid data [.f.|{along x|y}]'
bool
smooth_grid_dataCmd()
{
    if (!word_is(1, "grid")) {	// should not occur
	err("correct syntax 'smooth grid data [.f]'");
	return false;
    }
    // check that data exist
    if (!grid_exists()) {
	err("grid does not exist yet");
	return false;
    }
    int method;
    double f = 1.0;
    switch (_nword) {
    case 3:
	// `smooth grid data'
	f = 1.0;
	method = 0;
	break;
    case 4:
	// `smooth grid data .f.'
	getdnum(_word[3], &f);
	if (f < 0.0) {
	    warning("`smooth grid data .f.' clipping .f. to 0");
	    f = 0.0;
	} else if (f > 1.0) {
	    warning("`smooth grid data .f.' clipping .f. to 1");
	    f = 1.0;
	}
	method = 0;
	break;
    case 5:
	// `smooth grid data along x|y'
	if (!word_is(3, "along")) {
	    err("Fourth word must be `along'");
	    return false;
	}
	if (word_is(4, "x")) {
	    method = 1;
	} else if (word_is(4, "y")) {
	    method = 2;
	} else {
	    err("Last word must be `x' or `y'");
	    return false;
	}
	f = 1.0;
	break;
    default:
	NUMBER_WORDS_ERROR;
	return false;
    }
    GriMatrix<bool>    legitS;
    GriMatrix<double> zS;
    zS.set_size(_num_xmatrix_data, _num_ymatrix_data);
    legitS.set_size(_num_xmatrix_data, _num_ymatrix_data);
    // 
    // NOTE: I noticed in 2.068 that I was filling in the holes
    // which is stupid, as Peggy Sullivan from UW pointed out.  So
    // from 2.069 on I don't fill in the holes.
    gr_smootharray(1.0, 1.0, 1.0, _f_xy, zS,
		   _legit_xy, legitS, _num_xmatrix_data, _num_ymatrix_data,
		   method);
    for (unsigned int i = 0; i < _num_xmatrix_data; i++) {
	for (unsigned int j = 0; j < _num_ymatrix_data; j++) {
	    _f_xy(i, j) = (1.0 - f) * _f_xy(i, j) + f * zS(i, j);
	    _legit_xy(i, j) = legitS(i, j);
	}
    }
    return true;
}