aboutsummaryrefslogtreecommitdiff
path: root/sys/etc/votable.x
blob: ec931030fcc2e1b949e72b9c97b4d938d2acdade (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<fio.h>
include <votParse_spp.h>


# VOTABLE.X -- Utility procedures for dealing with VOTables.


# VOTINIT -- Initialize the VOT struct, parse the document and save the
# summary.  We return the VOT struct pointer itself, the caller is
# responsible for accessing the VOT_ROOT to get the raw root handle

pointer procedure votinit (votable)

char	votable[ARB]				#i VOTable file name

pointer	vot, vot_handle

#  Declare the libVOTable functions we'll be using.
int	vx_openVOTABLE(), vx_getRESOURCE(), vx_getTABLE(), vx_getDATA()
int	vx_getTABLEDATA(), vx_getFIELD(), vx_getINFO(), vx_getPARAM()
int	vx_getNCols(), vx_getNRows(), vx_getLength()

begin
	# Allocate the structure.
	call calloc (vot, SZ_VOT_STRUCT, TY_STRUCT)

	# Open and parse the votable.
	vot_handle = vx_openVOTABLE (votable)
	if (vot_handle <= 0) {
	    call eprintf ("Cannot open file: '%s'\n")
		call pargstr (votable)
	    return (NULL)
	}
	VOT_ROOT(vot) = vot_handle

	# Now get various handles from the table.
	VOT_RES(vot)   = vx_getRESOURCE (vot_handle)
	VOT_TAB(vot)   = vx_getTABLE (VOT_RES(vot))
	VOT_DATA(vot)  = vx_getDATA (VOT_TAB(vot))
	VOT_TDATA(vot) = vx_getTABLEDATA (VOT_DATA(vot))

    	VOT_INFO(vot)  = vx_getINFO (VOT_RES(vot))
    	VOT_PARAM(vot) = vx_getPARAM (VOT_RES(vot))
    	VOT_FIELD(vot) = vx_getFIELD (VOT_TAB(vot))

	VOT_NRES(vot)  = vx_getLength (VOT_RES(vot))
	VOT_NCOLS(vot) = vx_getNCols (VOT_TDATA(vot))
	VOT_NROWS(vot) = vx_getNRows (VOT_TDATA(vot))

	return (vot) 				# return the struct pointer
end


# VOTCLOSE -- Close the VOT struct and free any resources.

procedure votclose (vot)

pointer	vot					#i VOT struct pointer

begin
	call vx_closeVOTABLE (VOT_ROOT(vot))
	call mfree (vot, TY_STRUCT)
end

# IS_VOTABLE --  Utility routine to determine if the named file is a VOTable
# XML document.

define	VOT_MAXLINES		10

bool procedure is_votable (fname)

char	fname[ARB]				#i local filename

int	i, fd, nchars
bool	stat
char	buf[SZ_LINE]

int	open (), access (), getline (), strsearch ()

begin
	stat = FALSE

	if (access (fname, 0, 0) == NO)
	    return (stat)

	iferr {
            fd = open (fname, READ_ONLY, TEXT_FILE)

	    # Look for a "<VOTABLE>" element in the first 10 lines of the file.
	    for (i=0; i < VOT_MAXLINES; i = i + 1) {
                call aclrc (buf, SZ_LINE)
                nchars = getline (fd, buf)
		if (nchars == EOF)
		    break

		call strupr (buf)
                if (strsearch (buf, "<VOTABLE") > 0)
                    stat = TRUE
            }
            call close (fd)
	} then
	    stat = FALSE

	return (stat)
end


# VOT_CONVERT -- Convert a VOTable to some other format.

define VOT_FMTS 	"|ascii|asv|bsv|csv|tsv|html|shtml|fits|xml|raw|votable"

define ASCII   1                       # ascii separated values
define ASV     2                       # ascii separated values
define BSV     3                       # bar separated values
define CSV     4                       # comma separated values
define TSV     5                       # tab separated values
define HTML    6                       # standalone HTML document
define SHTML   7                       # single HTML <table> element
define FITS    8                       # FITS binary table
define XML     9                       # VOTable alias
define RAW     10                      #    "      "
define VOTBL   11                      #    "      "

int procedure vot_convert (in, out, fmt)

char	in[ARB]					#i VOTable file name
char	out[ARB]				#i FITS bintable file name
char	fmt[ARB]				#i format name

pointer	sp, nodename, buf
char	osfn[SZ_PATHNAME], cnvname[SZ_PATHNAME], format[SZ_LINE]
int	vfd, status, ip, opt, delim, infile, outfile

int	vfnopen(), vfnmapu(), access(), ki_gnode(), strdic(), strncmp()
int	open(), getline()
bool	streq()

begin
	call smark (sp)
	call salloc (nodename, SZ_FNAME, TY_CHAR)
	call salloc (buf, SZ_LINE, TY_CHAR)


	# Map input VFN to OSFN.
	ip = 1
	if (strncmp (in, "http://", 7) == 0) {
	    call strcpy (in, osfn, SZ_PATHNAME)
	} else {
	    vfd = vfnopen (in, READ_ONLY)
	    status = vfnmapu (vfd, osfn, SZ_PATHNAME)
	    call vfnclose (vfd, VFN_NOUPDATE)

	    # If the file resides on the local node strip the node name,
	    # returning a legal host system filename as the result.
	    if (ki_gnode (osfn, Memc[nodename], delim) == 0)
	        ip = delim + 1
	}

        # Create a tempfile name for the converted output file.
        call mktemp ("/tmp/vo", cnvname, SZ_PATHNAME)
        call strcat (".", cnvname, SZ_PATHNAME)
        call strcat (fmt, cnvname, SZ_PATHNAME)


	# Validate the format.
	opt = strdic (fmt, format, SZ_LINE, VOT_FMTS)
        if (opt == 0) {
            call eprintf ("Invalid output format '%s'\n")
		call pargstr (fmt)
	    call sfree (sp)
            return (ERR)
        }
	if (opt == VOTBL || opt == XML || opt == RAW)
	    call strcpy ("vot", format, SZ_FNAME)
	if (opt == ASCII)
	    call strcpy ("asv", format, SZ_FNAME)


        # Convert the file from VOTable to FITS bintable.
        call vx_vocopy (5, "-f", format, "-o", cnvname, osfn[ip])

	if (access (cnvname,0,0) == NO) {
	    call eprintf ("Cannot convert %s to '%s'\n")
		call pargstr (osfn[ip])
		call pargstr (fmt)
	    return (ERR)
	}

        # Delete the downloaded XML file, copy the bintable into its
        # place and delete the converted output filename.
	if (streq (in, out))
            call delete (in)


	# Copy converted file to output file.  Works for STDOUT/STDERR as
	# well.
	infile = open (cnvname, READ_ONLY, TEXT_FILE)
	outfile = open (out, NEW_FILE, TEXT_FILE)

	while (getline (infile, Memc[buf]) != EOF)
	    call putline (outfile, Memc[buf])

	call close (infile)
	call close (outfile)

        call delete (cnvname) 		# delete the temporary converted file
	call sfree (sp)
	return (OK)
end


# VOT_TO_FITS -- Convert a VOTable to a FITS bintable.

int procedure vot_to_fits (in, out)

char	in[ARB]					#i VOTable file name
char	out[ARB]				#i FITS bintable file name

pointer	sp, nodename
char	osfn[SZ_PATHNAME], cnvname[SZ_PATHNAME]
int	vfd, status, ip, delim

int	vfnopen(), vfnmapu(), access(), ki_gnode()
bool	streq()

begin
	call smark (sp)
	call salloc (nodename, SZ_FNAME, TY_CHAR)

	# Map input VFN to OSFN.
	vfd = vfnopen (in, READ_ONLY)
	status = vfnmapu (vfd, osfn, SZ_PATHNAME)
	call vfnclose (vfd, VFN_NOUPDATE)

	# If the file resides on the local node strip the node name,
	# returning a legal host system filename as the result.
	if (ki_gnode (osfn, Memc[nodename], delim) == 0)
	    ip = delim + 1
	else
	    ip = 1


        # Create a tempfile name for the converted output file.
        call mktemp ("/tmp/vo", cnvname, SZ_PATHNAME)
        call strcat (".fits", cnvname, SZ_PATHNAME)

        # Convert the file from VOTable to FITS bintable.
        call vx_vocopy (5, "-f", "fits", "-o", cnvname, osfn[ip])

	if (access (cnvname,0,0) == NO)
	    return (ERR)

        # Delete the downloaded XML file, copy the bintable into its
        # place and delete the converted output filename.
	if (streq (in, out))
            call delete (in)

        call fcopy (cnvname, out)   	# copy converted file to output file
        call delete (cnvname) 		# delete the temporary converted file

	call sfree (sp)

	return (OK)
end


# VOT_FROM_FITS -- Convert from a FITS bintable to a VOTable.

int procedure vot_from_fits (in, out)

char	in[ARB]					#i FITS bintable file name
char	out[ARB]				#i VOTable file name

char	osfn[SZ_PATHNAME], cnvname[SZ_PATHNAME]
int	vfd, status

int	vfnopen(), vfnmapu()
bool	streq()

begin
	# Map input VFN to OSFN.
	vfd = vfnopen (in, READ_ONLY)
	status = vfnmapu (vfd, osfn, SZ_PATHNAME)
	call vfnclose (vfd, VFN_NOUPDATE)

        # Create a tempfile name for the converted output file.
        call mktemp ("/tmp/vo", cnvname, SZ_PATHNAME)
        call strcat (".xml", cnvname, SZ_PATHNAME)

        # Convert the file from VOTable to FITS bintable.
        call vx_vocopy (5, "-f", "votable", "-o", cnvname, osfn)

        # Delete the downloaded XML file, copy the bintable into its
        # place and delete the converted output filename.
	if (streq (in, out))
            call delete (in)

        call fcopy (cnvname, out)   	# copy converted file to output file
        call delete (cnvname) 		# delete the temporary converted file

	return (OK)
end