File: daoread.c

package info (click to toggle)
wcstools 3.9.7-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,684 kB
  • sloc: ansic: 113,336; sh: 553; makefile: 245; lisp: 86; sed: 1
file content (211 lines) | stat: -rw-r--r-- 5,667 bytes parent folder | download | duplicates (8)
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
/*** File libwcs/daoread.c
 *** January 11, 2007
 *** By Jessica Mink, jmink@cfa.harvard.edu
 *** Harvard-Smithsonian Center for Astrophysics
 *** Copyright (C) 1996-2007
 *** Smithsonian Astrophysical Observatory, Cambridge, MA, USA

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

    This library 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
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Correspondence concerning WCSTools should be addressed as follows:
           Internet email: jmink@cfa.harvard.edu
           Postal address: Jessica Mink
                           Smithsonian Astrophysical Observatory
                           60 Garden St.
                           Cambridge, MA 02138 USA
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "wcs.h"
#include "fitsfile.h"
#include "wcscat.h"

static int nlines;	/* Number of lines in catalog */
#define ABS(a) ((a) < 0 ? (-(a)) : (a))

static char newline = 10;
static char *daobuff;

/* DAOREAD -- Read DAOFIND file of star positions in an image */

int
daoread (daocat, xa, ya, ba, pa, nlog)

char	*daocat;	/* Name of DAOFIND catalog file */
double	**xa, **ya;	/* X and Y coordinates of stars, array returned */
double	**ba;		/* Instrumental magnitudes of stars, array returned */
int	**pa;		/* Peak counts of stars in counts, array returned */
int	nlog;		/* 1 to print each star's position */
{
    int nstars;
    double xi, yi, magi;
    double flux;
    int iline;
    char *line;

    line = 0;
    iline = 0;
    nstars = 0;

    if (daoopen (daocat) > 0) {
	line = daobuff;

    /* Loop through catalog */
	for (iline = 1; iline <= nlines; iline++) {
	    line = daoline (iline, line);
	    if (line == NULL) {
		fprintf (stderr,"DAOREAD: Cannot read line %d\n", iline);
		break;
		}
	    else if (line[0] != '#') {

		/* Extract X, Y, magnitude  */
		sscanf (line,"%lg %lg %lg", &xi, &yi, &magi);

		/* Save star position, scaled flux, and magnitude in table */
		nstars++;
		*xa= (double *) realloc(*xa, nstars*sizeof(double));
		*ya= (double *) realloc(*ya, nstars*sizeof(double));
		*ba= (double *) realloc(*ba, nstars*sizeof(double));
		*pa= (int *) realloc(*pa, nstars*sizeof(int));
		(*xa)[nstars-1] = xi;
		(*ya)[nstars-1] = yi;
		(*ba)[nstars-1] = magi;
		flux = pow (10.0, (-magi / 2.5));
		(*pa)[nstars-1] = (int) flux;

		if (nlog == 1)
		    fprintf (stderr,"DAOREAD: %6d: %9.5f %9.5f %15.4f %6.2f\n",
			   nstars,xi,yi,flux,magi);
		}

	    /* Log operation */
	    if (nlog > 0 && iline%nlog == 0)
		fprintf (stderr,"DAOREAD: %5d / %5d / %5d stars from catalog %s\r",
			nstars, iline, nlines, daocat);

	    /* End of star loop */
	    }

	/* End of open catalog file */
	}

/* Summarize search */
    if (nlog > 0)
	fprintf (stderr,"DAOREAD: Catalog %s : %d / %d / %d found\n",
		 daocat, nstars, iline, nlines);

    free (daobuff);

    return (nstars);
}


/* DAOOPEN -- Open DAOFIND catalog, returning number of entries */

int
daoopen (daofile)

char *daofile;	/* DAOFIND catalog file name */
{
    FILE *fcat;
    int nr, lfile;
    char *daonew;
    
/* Find length of DAOFIND catalog */
    lfile = getfilesize (daofile);
    if (lfile < 2) {
	fprintf (stderr,"DAOOPEN: DAOFIND catalog %s has no entries\n",daofile);
	return (0);
	}

/* Open DAOFIND catalog */
    if (!(fcat = fopen (daofile, "r"))) {
	fprintf (stderr,"DAOOPEN: DAOFIND catalog %s cannot be read\n",daofile);
	return (0);
	}

/* Allocate buffer to hold entire catalog and read it */
    if ((daobuff = malloc (lfile)) != NULL) {
	nr = fread (daobuff, 1, lfile, fcat);
	if (nr < lfile) {
	    fprintf (stderr,"DAOOPEN: read only %d / %d bytes of file %s\n",
		     nr, lfile, daofile);
	    (void) fclose (fcat);
	    return (0);
	    }

    /* Enumerate entries in DAOFIND catalog by counting newlines */
	daonew = daobuff;
	nlines = 0;
	while ((daonew = strchr (daonew, newline)) != NULL) {
	    daonew = daonew + 1;
	    nlines = nlines + 1;
	    }
	}

    (void) fclose (fcat);
    return (nlines);
}


/* DAOLINE -- Get DAOFIND catalog entry for one star; return 0 if successful */

char *
daoline (iline, line)

int iline;	/* Star sequence number in DAOFIND catalog */
char *line;	/* Pointer to iline'th entry (returned updated) */
{
    char *nextline;
    int i;

    if (iline > nlines) {
	fprintf (stderr, "DAOSTAR:  %d is not in catalog\n",iline);
	return (NULL);
	}
    else if (iline < 1 && line) {
	nextline = strchr (line, newline) + 1;
	}
    else {
	nextline = daobuff;
	for (i = 1; i < iline; i++) {
	    nextline = strchr (nextline, newline) + 1;
	    }
	}

    return (nextline);
}

/* Dec 11 1996	New subroutines
 *
 * Mar 20 1997	Removed unused variables, fixed logging after lint
 *
 * Jul 20 2001	Return magnitude as well as flux
 *
 * May 27 2003	Use getfilesize() to get length of file
 *
 * Aug  3 2004	Move daoopen() and daoline() declarations to wcscat.h
 * Aug 30 2004	Include fitsfile.h
 *
 * Jun 19 2006	Initialized uninitialized  variable iline
 *
 * Jan 10 2007	Include wcs.h
 * Jan 11 2007	Include fitsfile.h
 */