aboutsummaryrefslogtreecommitdiff
path: root/noao/astcat/src/attools/atcathdr.x
blob: d261e3cdc3c03d051dfc4b453a2e463432f82e5d (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
include <pkg/cq.h>
include "../../lib/acatalog.h"

# AT_GCATHDR -- Read a standard ASTROMZ catalog header.

int procedure at_gcathdr (fd, hdrtext, maxch)

int	fd			#I the input file descriptor
char	hdrtext[ARB]		#O the output catalog description
int	maxch			#I the maximum size of the catalog description

pointer	sp, line
int	nlines, strfd, nchars
bool	first_line
int	stropen(), getline(), strncmp()

begin
	call smark (sp)
	call salloc (line, SZ_LINE, TY_CHAR)

	# Go to the beginning of the file.
	call seek (fd, BOF)

	# Initialize the number of lines  in the catalog description.
	nlines = 0
	first_line = true

	# Open the output text as a string.
	hdrtext[1] = EOS
	strfd = stropen (hdrtext, maxch, NEW_FILE)

	# Read in the catalog header as delimited by BEGIN CATALOG HEADER
	# and END CATALOG HEADER strings. Skip leading blank lines.
	repeat {
	    nchars = getline (fd, Memc[line])
	    if (nchars == EOF)
		break
	    if (first_line) {
	        if (Memc[line] == '\n')
		    next
	        if (strncmp (Memc[line], "#\n", 2) == 0)
		    next
	        if (strncmp (Memc[line], "# \n", 3) == 0)
		    next
	        if (strncmp (Memc[line], "# BEGIN CATALOG HEADER", 22) != 0)
		    break
		first_line = false
		next
	    }
	    if (strncmp (Memc[line], "# END CATALOG HEADER", 20) == 0)
		break
	    call fprintf (strfd, "%s")
		call pargstr (Memc[line+2])
	    nlines = nlines + 1
	}
	call close (strfd)

	# Return to the beginning of the file if no header was found.
	if (nlines == 0)
	    call seek (fd, BOF)

	call sfree (sp)

	return (nlines)
end


# AT_PCATHDR -- Read in the catalog format from a parameter set and create
# a standard ASTROMZ catalog header suitable for input to the catalog
# query routines.

int procedure at_pcathdr (pset, hdrtxt, maxch)

char	pset[ARB]		#I the name of the catalog description pset
char	hdrtxt[ARB]		#O the standard output catalog description
int	maxch			#I the maximum size of the header text

pointer	sp, fname, fval, funits, fmts, findex, ranges, pp
int	i, j, nfields, ncols, nvals, nlines, number, type, fsize, fd
char	cdtype
pointer	clopset()
int	at_wrdstr(), decode_ranges(), stropen(), strdic(), nscan()
int	get_next_number

begin
	# Get working space.
	call smark (sp)
	call salloc (fname, SZ_FNAME, TY_CHAR)
	call salloc (fval, SZ_FNAME, TY_CHAR)
	call salloc (funits, SZ_FNAME, TY_CHAR)
	call salloc (fmts, SZ_FNAME, TY_CHAR)
	call salloc (findex, AT_NSTDCOLS, TY_INT)
	call salloc (ranges, 3 * AT_MAX_NRANGES + 1, TY_INT)

	# Open the pset
	pp = clopset (pset)

	# Get the file type.
	call clgpset (pp, "ftype", Memc[fval], SZ_FNAME) 
	type = strdic (Memc[fval], Memc[fval], SZ_FNAME, CQ_RTYPESTR)
	if (type <= 0)
	    type = CQ_STEXT

	# Count the fields.
	nfields = 0; ncols = 0
	do i = 1, AT_NSTDCOLS {
	    if (at_wrdstr (i, Memc[fname], SZ_FNAME, AT_CATNAMES) <= 0)
		next
	    if (Memc[fname] == EOS)
		next
	    call clgpset (pp, Memc[fname], Memc[fval], SZ_FNAME) 
	    if (Memc[fval] == EOS)
		next
	    call sscan (Memc[fval])
		call gargwrd (Memc[fval], SZ_FNAME)
	    if (nscan() < 1)
		next
	    if (decode_ranges (Memc[fval], Memi[ranges], AT_MAX_NRANGES,
	        nvals) == ERR)
		next
	    if (nvals <= 0)
		next
	    if (type == CQ_BTEXT) {
		call gargi (j)
		if (nscan() < 2)
		    next
	    }
	    Memi[findex+nfields] = i
	    nfields = nfields + 1
	    ncols = ncols + nvals
	}

	# Write the header description.
	nlines = 0
	if (nfields > 0) {

	    # Open the string as a file.
	    hdrtxt[1] = EOS
	    fd = stropen (hdrtxt, maxch, NEW_FILE)

	    # Write the catalog type.
	    call clgpset (pp, "ftype", Memc[fval], SZ_FNAME) 
	    type = strdic (Memc[fval], Memc[fval], SZ_FNAME, CQ_RTYPESTR)
	    call fprintf (fd, "%s %s\n")
		call pargstr ("type")
		call pargstr (Memc[fval])
	    nlines = nlines + 1

	    # Write out the header parameters. At present there is only one
	    # the catalog coordinate system.
	    call fprintf (fd, "nheader 1\n")
	    nlines = nlines + 1
	    call clgpset (pp, "csystem", Memc[fval], SZ_FNAME) 
	    call fprintf (fd, "    %s %s\n")
		call pargstr ("csystem")
		call pargstr (Memc[fval])
	    nlines = nlines + 1

	    # Write out the legal fields.
	    call fprintf (fd, "nfields %d\n")
		call pargi (ncols)
	    nlines = nlines + 1
	    do i = 1, nfields {
	        if (at_wrdstr (Memi[findex+i-1], Memc[fname], SZ_FNAME,
		    AT_CATNAMES) <= 0)
		    next
	        if (Memc[fname] == EOS)
		    next
	        call clgpset (pp, Memc[fname], Memc[fval], SZ_FNAME) 
	        if (Memc[fval] == EOS)
		    next
	    	call sscan (Memc[fval])
		switch (type) {
		case CQ_BTEXT:
		    call gargwrd (Memc[fval], SZ_FNAME)
		    call gargi (fsize)
		    call gargwrd (Memc[funits], SZ_FNAME)
		    call gargwrd (Memc[fmts], SZ_FNAME)
		default:
		    call gargwrd (Memc[fval], SZ_FNAME)
		    call gargwrd (Memc[funits], SZ_FNAME)
		    call gargwrd (Memc[fmts], SZ_FNAME)
		}
	        if (decode_ranges (Memc[fval], Memi[ranges], AT_MAX_NRANGES,
	            nvals) == ERR)
		    next
	        if (nvals <= 0)
		    next
		if (at_wrdstr (Memi[findex+i-1], cdtype, 1, AT_CATTYPES) <= 0)
		    cdtype = 'c'
		if (Memc[funits] == EOS) {
		    if (at_wrdstr (Memi[findex+i-1], Memc[funits], SZ_FNAME,
		        AT_CATUNITS) <= 0)
			call strcpy ("INDEF", Memc[fmts], SZ_FNAME)
		}
		if (Memc[fmts] == EOS) {
		    if (at_wrdstr (Memi[findex+i-1], Memc[fmts], SZ_FNAME,
		        AT_CATFORMATS) <= 0)
			call strcpy ("%s", Memc[fmts], SZ_FNAME)
		}
		switch (type) {
		case CQ_BTEXT:
		    if (nvals == 1) {
			call fprintf (fd, "    %s %s %d %c %s %s\n")
			    call pargstr (Memc[fname])
			    call pargstr (Memc[fval])
			    call pargi (fsize)
			    call pargc (cdtype)
			    call pargstr (Memc[funits])
			    call pargstr (Memc[fmts])
			nlines = nlines + 1
		    } else {
			number = 0
			do j = 1, nvals {
			    call fprintf (fd, "    %s%d %d %d %c %s %s\n")
			        call pargstr (Memc[fname])
				call pargi (j)
			        call pargi (get_next_number (Memi[ranges],
				    number))
				call pargi (fsize)
			        call pargc (cdtype)
			        call pargstr (Memc[funits])
			        call pargstr (Memc[fmts])
			    nlines = nlines + 1
			}
		    }
		default:
		    if (nvals == 1) {
			call fprintf (fd, "    %s %s 0 %c %s %s\n")
			    call pargstr (Memc[fname])
			    call pargstr (Memc[fval])
			    call pargc (cdtype)
			    call pargstr (Memc[funits])
			    call pargstr (Memc[fmts])
			nlines = nlines + 1
		    } else {
			number = 0
			do j = 1, nvals {
			    call fprintf (fd, "    %s%d %d 0 %c %s %s\n")
			        call pargstr (Memc[fname])
				call pargi (j)
			        call pargi (get_next_number (Memi[ranges],
				    number))
			        call pargc (cdtype)
			        call pargstr (Memc[funits])
			        call pargstr (Memc[fmts])
			    nlines = nlines + 1
			}
		    }
		}
	    }

	    call close (fd)
	}

	# Close the pset
	call clcpset (pp)

	call sfree (sp)

	return (nlines)
end