File: matchcat.c

package info (click to toggle)
wcstools 3.9.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,680 kB
  • sloc: ansic: 113,336; sh: 553; makefile: 246; lisp: 86; sed: 1
file content (221 lines) | stat: -rw-r--r-- 5,901 bytes parent folder | download | duplicates (3)
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
/* File matchcat.c
 * January 10, 2007
 * By Jessica Mink, Harvard-Smithsonian Center for Astrophysics
 * Send bug reports to jmink@cfa.harvard.edu

   Copyright (C) 2006-2007
   Smithsonian Astrophysical Observatory, Cambridge, MA USA

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "libwcs/wcscat.h"
#include "libwcs/wcs.h"

#define NMAXCAT 1500000

static int verbose = 0;         /* verbose/debugging flag */
static double matchrad = 5.0;	/* Initial match radius is 5 arcseconds */
int version = 0;		/* If 1, print only program name and version */
static void usage();

static char *RevMsg = "MATCHCAT WCSTools 3.9.7, 26 April 2022, Jessica Mink (jmink@cfa.harvard.edu)";

main (ac, av)
int ac;
char **av;
{
    char *fn;
    char *str;
    char outfile[256];
    FILE *fcat;
    double delta, dist;

    double ra[NMAXCAT], dec[NMAXCAT];
    double ra2[NMAXCAT], dra[NMAXCAT], ddec[NMAXCAT];
    float as[NMAXCAT], v[NMAXCAT], r[NMAXCAT];
    float dv[NMAXCAT], dr[NMAXCAT], class[NMAXCAT];
    int id[NMAXCAT];
    char line[1000];
    char line1[1000];
    char line2[1000];
    int ncmax = 1000;
    struct Tokens *tokens;
    int ltok, ntok;
    int i, num, j, ij, nid;
    char rastr[32], decstr[32], mvstr[8], mrstr[8], clstr[8];

    /* Check for help or version command first */
    str = *(av+1);
    if (!str || !strcmp (str, "help") || !strcmp (str, "-help"))
	usage();
    if (!strcmp (str, "version") || !strcmp (str, "-version")) {
        version = 1;
	usage();
	}

    /* crack arguments */
    for (av++; --ac > 0 && *(str = *av) == '-'; av++) {
        char c;
        while (c = *++str)
        switch (c) {

        case 'v':       /* more verbosity */
            verbose++;
            break;

        case 'r':       /* match radius in arcseconds */
            if (ac > 1)
		matchrad = atof (*++av);
            break;

        default:
            usage();
            break;
        }
    }

    /* There are ac remaining file names starting at av[0] */
    if (ac == 0)
        usage ();

    while (ac-- > 0) {
        fn = *av++;

	/* Open ASCII catalog */
	if (!(fcat = fopen (fn, "r"))) {
	    fprintf (stderr,"ASCII catalog %s cannot be read\n", fn);
	    exit (1);
	    }

	/* Ignore first two lines */
	if (fgets (line1, ncmax, fcat) == NULL) {
	    fprintf (stderr,"Cannot read 1st line of ASCII catalog %sd\n", fn);
	    fclose (fcat);
	    exit (1);
	    }
	if (fgets (line2, ncmax, fcat) == NULL) {
	    fprintf (stderr,"Cannot read 2nd line of ASCII catalog %sd\n", fn);
	    fclose (fcat);
	    exit (1);
	    }

	/* Read position information into arrays */
	i = 0;
	while (fgets (line, ncmax, fcat) != NULL) {
	    ntok = setoken (tokens, line, NULL);
	    ltok = getoken (tokens, 1, rastr, 32);
	    ltok = getoken (tokens, 2, decstr, 32);
	    ltok = getoken (tokens, 3, mvstr, 8);
	    ltok = getoken (tokens, 4, mrstr, 8);
	    ltok = getoken (tokens, 5, clstr, 8);
	    as[i] = -1;
	    v[i] = atof (mvstr);
	    r[i] = atof (mrstr);
	    class[i] = atoi (clstr);
	    dec[i] = str2dec (decstr);
	    ra[i] = str2ra (rastr) * cosdeg (dec[i]);
	    ra2[i] = str2ra (rastr);
	    i++;
	    if (i > NMAXCAT)
		break;
	    }
	fclose (fcat);

	/* Set match radius in degrees */
	delta = 5.0 / 3600.0;
	num = i - 1;
	nid = 0;

	/* Read through file */
	for (i = 0; i < num; i++) {

	    /* If current star has not been matched */
	    if (id[i] <= 0) {
		nid = nid + 1;
		id[i] = nid;

		/* Check for matching stars */
		for (j = 1; j < 1000; j++) {
		    ij = i + j;
		    if (ij >= num)
			break;

		    /* Check RA distance first */
		    if ((ra[ij] - ra[i]) > delta)
			break;

		    /* If RA close enough, check radial distance */
		    dist = wcsdist (ra2[i],dec[i],ra2[ij],dec[ij]);
		    if (dist <= delta) {
			id[ij] = nid;
			as[ij] = 3600.0 * dist;
			dv[ij] = v[i] - v[ij];
			dr[ij] = r[i] - r[ij];
			dra[ij] = 3600.0 * (ra[i] - ra[ij]);
			ddec[ij] = 3600.0 * (dec[i] - dec[ij]);
			}
		    }
		}
	    }

	/* Open output file */
	strcpy (outfile, fn);
	strcat (outfile, ".match");
	fcat = fopen (outfile, "w");
        if (fcat == NULL) {
            fprintf (stderr, "Cannot write file %s\n", outfile);
	    exit (1);
	    }

	/* Print header lines */
	fprintf (fcat, "%s", line1);
	fprintf (fcat, "%s", line2);

	/* Print catalog */
	for (i = 0; i < num; i++) {
	    ra2str (rastr, 32, ra2[i], 3);
	    dec2str (decstr, 32, dec[i], 2);
	    fprintf (fcat, "%7d %s %s %6.3f %6.3f %6.3f %6.3f %6.3f ",
		     id[i], rastr, decstr, r[i], v[i], dr[i], dv[i], class[i]);
	    fprintf (fcat, "%9.6f %9.6f %9.6f %9.6f %9.6f %7d\n",
		     as[i], ra2[i], dec[i], dra[i], ddec[i], i);
	    }

	fclose (fcat);
	}
    exit (0);
}

static void
usage ()
{
    fprintf (stderr,"%s\n",RevMsg);
    if (version)
        exit (-1);
    fprintf (stderr,"Match catalog entries after combining, RA-sorting them\n");
    fprintf (stderr,"usage: matchcat [-v][-r rad] file.cat ...\n");
    fprintf (stderr,"  -r rad: Combine entries within this arcsecond distance\n");
    fprintf (stderr,"  -v: Verbose\n");
    exit (1);
}
/*
 * Jan 10 2007	Declare RevMsg static, not const
 */