File: heal.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 (185 lines) | stat: -rw-r--r-- 4,548 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
#include	<math.h>
#include	<stdio.h>
#include	<string.h>
#include	"extern.hh"
#include	"gr.hh"

static bool     heal_columnsCmd(void);
static bool     heal_gridCmd(void);
void            heal_col(double *c, int n);


// `heal columns|{grid along x|y}'
bool
healCmd()
{
    if (word_is(1, "columns")) {
	return heal_columnsCmd();
    } else if (word_is(1, "grid")) {
	if (word_is(2, "along")) {
	    return heal_gridCmd();
	} else {
	    err("third word must be `along'");
	    return false;
	}
    } else {
	err("second word must be `columns' or `grid'");
	return false;
    }
}

// `heal columns'
bool
heal_columnsCmd()
{
    if (_nword != 2) {
	NUMBER_WORDS_ERROR;
	demonstrate_command_usage();
	return false;
    }
    if (_colX.size() > 0)
	heal_col(_colX.begin(), _colX.size());
    if (_colY.size() > 0)
	heal_col(_colY.begin(), _colY.size());
    if (_colU.size() > 0)
	heal_col(_colU.begin(), _colU.size());
    if (_colV.size() > 0)
	heal_col(_colV.begin(), _colV.size());
    if (_colZ.size() > 0)
	heal_col(_colZ.begin(), _colZ.size());
    if (_colR.size() > 0)
	heal_col(_colR.begin(), _colR.size());
    if (_colTHETA.size() > 0)
	heal_col(_colTHETA.begin(), _colTHETA.size());
    return true;
}

void
heal_col(double *c, int n)
{
    int             i, first_good;
    int             last_good_index = 0;
    bool            last_was_missing = false;
    // Find first good data point
    for (first_good = 0; first_good < n; first_good++)
	if (!gr_missing((double) *(c + first_good)))
	    break;
    // Heal gaps, using last_good_value
    for (i = first_good; i < n; i++) {
	if (!gr_missing((double) *(c + i))) {
	    // This point is good.  If last was bad, have a hole to fill
	    if (last_was_missing) {
		int             ii;
		double          range = *(c + i) - *(c + last_good_index);
		for (ii = last_good_index + 1; ii < i; ii++) {
		    *(c + ii) = *(c + last_good_index)
			+ (ii - last_good_index)
			* range
			/ (i - last_good_index);
		}
	    }
	    last_was_missing = false;
	    last_good_index = i;
	} else {
	    last_was_missing = true;
	}
    }
}

// `heal columns|{grid along x|y}'
bool
heal_gridCmd()
{
    bool            doing_x;
    // Check for obvious errors.
    if (_nword != 4) {
	NUMBER_WORDS_ERROR;
	demonstrate_command_usage();
	return false;
    }
    if (!strcmp(_word[3], "x")) {
	if (!_xgrid_exists) {
	    err("First `read grid x' or `set x grid'");
	    return false;
	}
	if (!grid_exists()) {
	    err("No grid exists yet\n");
	    return false;
	}
	doing_x = true;
    } else if (!strcmp(_word[3], "y")) {
	if (!_ygrid_exists) {
	    err("First `read grid y' or `set y grid'");
	    return false;
	}
	if (!grid_exists()) {
	    err("No grid exists yet\n");
	    return false;
	}
	doing_x = false;
    } else {
	err("Expecting `x' or `y', not `\\", _word[3], "'.", "\\");
	demonstrate_command_usage();
	return false;
    }
    if (doing_x) {
	// Heal across x.
	unsigned int i, j;
	for (j = 0; j < _num_ymatrix_data; j++) {
	    for (i = 0; i < _num_xmatrix_data; i++) {
		if (_legit_xy(i, j) == false) {
		    int             last_good = i - 1;
		    for (; i < _num_xmatrix_data; i++) {
			if (_legit_xy(i, j) == true) {
			    // Got to other side of hole. Fill in if it is
			    // interior.
			    if (i < _num_xmatrix_data - 1 && last_good > -1) {
				unsigned int ii;
				double          a, b;	// y = a + bx
				a = _f_xy(last_good, j);
				b = (_f_xy(i, j) - _f_xy(last_good, j)) /
				    (_xmatrix[i] - _xmatrix[last_good]);
				for (ii = last_good + 1; ii < i; ii++) {
				    _f_xy(ii, j) = a + b
					* (_xmatrix[ii] - _xmatrix[last_good]);
				    _legit_xy(ii, j) = true;
				}
			    }
			    break;
			}
		    }
		}
	    }
	}
    } else {
	// Heal across y.
	unsigned int i, j;
	for (i = 0; i < _num_xmatrix_data; i++) {
	    for (j = 0; j < _num_ymatrix_data; j++) {
		if (_legit_xy(i, j) == false) {
		    int             last_good = j - 1;
		    for (; j < _num_ymatrix_data; j++) {
			if (_legit_xy(i, j) == true) {
			    // Got to other side of hole. Fill in if it is
			    // interior.
			    if (j < _num_ymatrix_data - 1 && last_good > -1) {
				unsigned int jj;
				double          a, b;	// y = a + bx
				a = _f_xy(i, last_good);
				b = (_f_xy(i, j) - _f_xy(i, last_good)) /
				    (_ymatrix[j] - _ymatrix[last_good]);
				for (jj = last_good + 1; jj < j; jj++) {
				    _f_xy(i, jj) = a + b
					* (_ymatrix[jj] - _ymatrix[last_good]);
				    _legit_xy(i, jj) = true;
				}
			    }
			    break;
			}
		    }
		}
	    }
	}
    }
    return true;
}