aboutsummaryrefslogtreecommitdiff
path: root/pkg/utilities/nttools/keyselect/tab.x
blob: 1e4dd76900222d93040444c3c537e1ba40828e84 (plain) (blame)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
include <imhdr.h>
include <tbset.h>
include "keyselect.h"

#* HISTORY *
#* B.Simon	12-Mar-92	Original

# CPY_TABLE -- Copy keywords from header to table row

procedure cpy_table (im, tp, colptr, keywords)

pointer	im		# i: image descriptor
pointer	tp		# i: table descriptor
pointer	colptr		# i: pointer to array of column descriptors
char	keywords	# i: list of header keywords
#--
char	cat, sep
int	row, dtype, ic, jc, kc
pointer	sp, cp, nlist, vlist, name, value

data	cat	/ CONCAT_CHAR /
data	sep	/ SEP_CHAR /
 
string	nocolumn  "cpy_table: not enough columns to store keywords"

int	tbpsta(), brk_list(), stridx(), gstrcpy()

begin
	call smark(sp)
	call salloc (nlist, SZ_LINE, TY_CHAR)
	call salloc (vlist, SZ_LINE, TY_CHAR)
	call salloc (name, SZ_LINE, TY_CHAR)
	call salloc (value, SZ_LINE, TY_CHAR)
	
	ic = 1
	cp = colptr
	row = tbpsta (tp, TBL_NROWS) + 1

	# Extract each keyword from the list of keywords

	while (brk_list (keywords, ic, sep, Memc[nlist], SZ_LINE) > 0) {

	    # If the keyword is not a list of concatenated keywords
	    # copy its value into a string

	    if (stridx (cat, Memc[nlist]) == 0) {
		call get_keyword (im, Memc[nlist], dtype, 
				   Memc[vlist], SZ_LINE)

	    # Otherwise break the list of concatenated keywords
	    # and concatenate their values into a string

	    } else {
		jc = 1
		kc = 0
		while (brk_list (Memc[nlist], jc, cat, 
				 Memc[name], SZ_LINE) > 0){

		    call get_keyword (im, Memc[name], dtype, 
				      Memc[value], SZ_LINE)

		    if (dtype != 0) {
			kc = kc + gstrcpy (Memc[value], Memc[vlist+kc], 
					   SZ_LINE-kc)
			Memc[vlist+kc] = SEP_CHAR
			kc = kc + 1
		    }
		}
		kc = max (kc, 1)
		Memc[vlist+kc-1] = EOS
	    }

	    # Write the value into the table

	    if (Memi[cp] == NULL)
		call error (1, nocolumn)

	    call tbeptt (tp, Memi[cp], row, Memc[vlist])
	    cp = cp + 1
	}

	call sfree(sp)
end

# FMT_TABLE -- Retrieve column format from column description file

procedure fmt_table (cd, col, units, fmt, dtype)

int	cd		# i: file descriptor of column description file
char	col[ARB]	# i: name of column to retrieve information for
char	units[ARB]	# o: column units
char	fmt[ARB]	# o: column format
int	dtype		# o: column data type
#--
char	star, comment
bool	match
int	idx, junk, length, typevals[5]
pointer	sp, line, input, name, type, ftnfmt, errmsg

string	typestr  "rdibc"
string	badtype  "Illegal datatype for column %s (%s)"
string	badname  "Warning: column not found in column description file (%s)\n"

data	star	 / '*' /
data	comment  / '#' /
data	typevals /TY_REAL, TY_DOUBLE, TY_INT, TY_BOOL, TY_CHAR /

bool	streq()
int	getline(), stridx(), ctoi

begin
	call smark (sp)
	call salloc (line, SZ_LINE, TY_CHAR)
	call salloc (input, SZ_COLNAME, TY_CHAR)
	call salloc (name, SZ_COLNAME, TY_CHAR)
	call salloc (type, SZ_COLFMT, TY_CHAR)
	call salloc (ftnfmt, SZ_COLFMT, TY_CHAR)
	call salloc (errmsg, SZ_LINE, TY_CHAR)

	call strcpy (col, Memc[input], SZ_COLNAME)
	call strlwr (Memc[input])

	match = false
	call seek (cd, BOF)
	while (! match && getline (cd, Memc[line]) != EOF) {

	    # Remove trailing comments from line

	    idx = stridx (comment, Memc[line])
	    if (idx > 0)
		Memc[line+idx-1] = EOS

	    # Column name is the first word on the line

	    call sscan (Memc[line])
	    call gargwrd (Memc[name], SZ_COLNAME)
	    call strlwr (Memc[name])

	    # If the name matches the procedure argument 
	    # read the remaining fields on the line

	    match = streq (Memc[input], Memc[name])
	    if (match) {
		call gargwrd (Memc[type], SZ_COLFMT)
		call gargwrd (Memc[ftnfmt], SZ_COLFMT)
		call gargwrd (units, SZ_COLUNITS)

		call strlwr (Memc[type])
		call tbbftp (Memc[ftnfmt], fmt)

		# Convert the type string to the corresponding integer value

		if (Memc[type] == EOS) {
		    dtype = 0

		} else {
		    idx = stridx (Memc[type], typestr)
		    if (idx == 0) {
			call sprintf (Memc[errmsg], SZ_LINE, badtype)
			call pargstr (Memc[name])
			call pargstr (Memc[type])
			call error (1, Memc[errmsg])
		    }
		    
		    dtype = typevals[idx]
		    if (dtype == TY_CHAR) {
			idx = stridx (star, Memc[type])
			if (idx > 0) {
			    idx = idx + 1
			    junk = ctoi (Memc[type], idx, length)
			    if (length > 0)
				dtype = - length
			}
		    }
		}

	    }
	}

	# Send warning message and set defaults if no match

	if (! match) {
	    dtype = 0
	    fmt[1] = EOS
	    units[1] = EOS

	    call eprintf (badname)
	    call pargstr (col)
	}

	call sfree (sp)
end

# OP_TABLE -- Open the output table

pointer procedure op_table(im, output, keywords, columns, cdfile)

pointer	im		# i: image descriptor
char	output[ARB]	# i: table name
char	keywords[ARB]	# i: list of header keywords
char	columns[ARB]	# i: list of column names
char	cdfile[ARB]	# i: optional column description file
#--
bool	append
char	sep, cat
int	ic, jc, dtype
pointer	sp, tp, cp, cd, col, key, units, fmt, errmsg

data	sep	/ SEP_CHAR /
data	cat	/ CONCAT_CHAR /

string	nocolumn  "Column not found in existing output table (%s)"
string	nokeyword "op_table: no matching keyword for column"
string	notfound  "Warning: keyword not found when creating table (%s)\n"

bool	isblank()
int	open(), stridx(), imgftype(), brk_list(), type_keyword()
pointer	tbtopn()

begin
	call smark (sp)
	call salloc (col, SZ_COLNAME, TY_CHAR)
	call salloc (key, SZ_LINE, TY_CHAR)
	call salloc (units, SZ_COLUNITS, TY_CHAR)
	call salloc (fmt, SZ_COLFMT, TY_CHAR)
	call salloc (errmsg, SZ_LINE, TY_CHAR)

	# Open column description file

	if (isblank (cdfile)) {
	    cd = NULL
	} else {
	    cd = open (cdfile, READ_ONLY, TEXT_FILE)
	}

	# Append rows to the table if the table already exists
	# otherwise create a new table

	append = true
	iferr (tp = tbtopn (output, READ_WRITE, NULL)) {
	    append = false
	    tp = tbtopn (output, NEW_FILE, NULL)
	}

	ic = 1
	jc = 1
	while (brk_list (columns, ic, sep, Memc[col], SZ_COLNAME) > 0) {

	    if (brk_list (keywords, jc, sep, Memc[key], SZ_LINE) == 0)
		call error (1, nokeyword)

	    # Verify that the columns exist if we are in append mode
	    # Define the new columns if we are not

	    if (append) {
		call tbcfnd (tp, Memc[col], cp, 1)
		if (cp == NULL) {
		    call sprintf (Memc[errmsg], SZ_LINE, nocolumn)
		    call pargstr (Memc[col])
		    call error (1, Memc[errmsg])
		}

	    } else {
		# Get column characteristics from the column description file
		# or use defaults and image header keyword type

		if (cd != NULL)
		    call fmt_table (cd, Memc[col], 
				    Memc[units], Memc[fmt], dtype)

		if (cd == NULL || dtype == 0) {
		    Memc[units] = EOS
		    Memc[fmt] = EOS

		    if (stridx (cat, Memc[key]) != 0) {
			dtype = - SZ_BIGCOL

		    } else if (Memc[key] == '$'){
			dtype = type_keyword (Memc[key])

		    } else {
			iferr {
			    dtype = imgftype (im, Memc[key])
			} then {
			    dtype = 0
			    call eprintf (notfound)
			    call pargstr (Memc[key])
			}
		    }
		}

		if (dtype == 0 || dtype == TY_CHAR)
		    dtype = - SZ_STRCOL
		if (dtype == TY_SHORT || dtype == TY_LONG)
		    dtype = TY_INT
		if (dtype == TY_REAL)
		    dtype = TY_DOUBLE

		call tbcdef (tp, cp, Memc[col], Memc[units], Memc[fmt], 
			     dtype, 1, 1)
	    }
	}

	# Create the new table if not in append mode

	if (! append)
	    call tbtcre (tp)

	call sfree (sp)
	return (tp)
end

# RD_TABLE -- Create an array of column pointers from the list of column names

procedure rd_table (columns, tp, colptr)

char	columns[ARB]	# i: list of column names
pointer	tp		# i: table descriptor
pointer	colptr		# o: pointer to array of column names
#--
char	sep
int	nptr, ic
pointer	sp, cp, col, errmsg

data	sep	/ SEP_CHAR /
string	nocolumn "rd_table: column not found (%s)"

int	cnt_list(), brk_list()

begin
	call smark (sp)
	call salloc (col, SZ_LINE, TY_CHAR)
	call salloc (errmsg, SZ_LINE, TY_CHAR)

	nptr = cnt_list (columns) + 1
	call malloc (colptr, nptr, TY_POINTER)

	ic = 1
	cp = colptr
	while (brk_list (columns, ic, sep, Memc[col], SZ_LINE) > 0) {
	    call tbcfnd (tp, Memc[col], Memi[cp], 1)
	    if (cp == NULL) {
		call sprintf (Memc[errmsg], SZ_LINE, nocolumn)
		call pargstr (Memc[col])

		call error (1, Memc[errmsg])
	    }
	    cp = cp + 1
	}

	Memi[cp] = NULL
	call sfree(sp)
end