aboutsummaryrefslogtreecommitdiff
path: root/vendor/voclient/libvotable/examples/votpos.c
blob: a4c87150f4717ac1c4d751ba5696f3926477da17 (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
/*
 *  VOTPOS  
 *
 *  Extract the main RA/Dec columns from a VOTable.
 *
 *    Usage:
 *		votpos -o <outname> <votable>
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "votParse.h"


int  vot	= 0;			/* VOTable handle		*/
char *oname	= NULL;			/* Output file name		*/

int  verbose	= 0;			/* options			*/
int  warn	= 0;			/* warning options		*/

int
main (int argc, char **argv)
{
    char  *fname, *ucd;
    int   res, tab, data, tdata, field;
    int   i, ncols, nrows;
    int   ra_col=-1, dec_col=-1, number=1;
    FILE *fd = stdout;


    if (argc < 2) {
	fprintf (stderr, "Usage:  votinfo <votable>\n");
	return (1);

    } else if (argc >= 2) {
	for (i=1; i < argc; i++) {
	    if (argv[i][0] == '-') {
		switch (argv[i][1]) {
		case 'o':  oname = argv[++i];		break;
		case 'v':  verbose++;			break;
		case 'w':  warn++;			break;
		case 'n':  number--;			break;
		default:
		    fprintf (stderr, "Invalid option '%s'\n", argv[i]);
		    return (1);
		}
	    } else {
		fname = argv[i];
		break;
	    }
	}
    }

    if (oname) {
	if ((fd = fopen (oname, "w+")) == (FILE *) NULL)
	    return (1);
    }

    /* Open the table.  This also parses it.
    */
    vot_setWarnings (warn);
    if ( (vot = vot_openVOTABLE (fname) ) <= 0) {
	fprintf (stderr, "Error opening VOTable '%s'\n", argv[1]);
	return (1);
    }


    res   = vot_getRESOURCE (vot);	/* get handles		*/
    tab   = vot_getTABLE (res);
    data  = vot_getDATA (tab);
    tdata = vot_getTABLEDATA (data);
    nrows = vot_getNRows (tdata);
    ncols = vot_getNCols (tdata);


    /*  Find the columns.
    */
    for (i=0, field=vot_getFIELD(tab); field; field=vot_getNext (field),i++) {
	if ((ucd  = vot_getAttr (field, "ucd"))) {
	  if ((strcmp (ucd, "POS_EQ_RA_MAIN") == 0)  ||		/* UCD 1     */
	      (strcmp (ucd, "pos.eq.ra;meta.main") == 0))	/* UCD 1+    */
		ra_col = i;
	  if ((strcmp (ucd, "POS_EQ_DEC_MAIN") == 0) ||		/* UCD 1     */
	      (strcmp (ucd, "pos.eq.dec;meta.main") == 0))	/* UCD 1+    */
		dec_col = i;
	}
    }
		
    /*  Print the position cells.
     */
    for (i=0; i < nrows; i++) {
	if (number)
	    fprintf (fd, "%d %s %s\n", i,
	        vot_getTableCell (tdata, i, ra_col),
	        vot_getTableCell (tdata, i, dec_col));
	else
	    fprintf (fd, "%s %s\n", 
	        vot_getTableCell (tdata, i, ra_col),
	        vot_getTableCell (tdata, i, dec_col));
    }

    if (fd != stdout)
	fclose (fd);
	    
    vot_closeVOTABLE (vot);		/* close the table  	*/
    return (0);
}