aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/daophot/psf/dpispstars.x
blob: 85a4c66946e85dbb1019696e361b1cb14723a830 (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
include <fset.h>
include <ctype.h>
include <gset.h>
include "../lib/daophotdef.h"
include "../lib/apseldef.h"
include "../lib/psfdef.h"

define	HELPFILE	"daophot$psf/mkpsflist.key"

# DP_IPFSTARS -- Construct a stellar PSF from one or more stars in an image
# frame.

int procedure dp_ipfstars (dao, im, maxnpsf, lolimit, radius, gd, mgd, id,
	mkstars, interactive, showplots)

pointer	dao			# pointer to DAOPHOT structure
pointer im			# pointer to IRAF image
int	maxnpsf			# maximum number of psf stars
real	lolimit			# lower magnitude limit for stars
real	radius			# the confusion radius for good psf stars
pointer gd			# pointer to graphics descriptor
pointer mgd			# pointer to the metacode file
pointer id			# pointer to image display stream
bool	mkstars			# marked the selected and deleted psf stars
bool	interactive		# interactive mode
bool	showplots		# show the plots

int	key, nxstar, wcs, idnum, istar, ip
pointer apsel, sp, cmd, str
real	wx, wy

real	dp_pstatr()
int	clgcur(), dp_pqverify(), dp_locstar(), dp_idstar(), dp_nxstar()
int	dp_pstati(), ctoi(), dp_addstar(), dp_delstar()

begin
	# Get some pointers
	apsel = DP_APSEL(dao)

	# Allocate some working space.
	call smark (sp)
	call salloc (str, SZ_LINE, TY_CHAR)
	call salloc (cmd, SZ_LINE, TY_CHAR)

	# Initialize some variables.
	key = 'a'
	nxstar = 0
	call dp_pseti (dao, PNUM, 0)

	# Begin to build the PSF.
	while (clgcur ("icommands", wx, wy, wcs, key, Memc[cmd], SZ_LINE) !=
	    EOF) {

	    # Correct for tv coordinates if necessary.
	    call dp_vtol (im, wx, wy, wx, wy, 1)

	    switch (key) {

	    # Quit the interactive cursor loop.
	    case 'q':
		if (interactive) {
		    if (dp_pqverify() == YES)
			break
		} else
		    break

	    # Print the help page.
	    case '?':
		if (id == NULL)
		    call pagefile (HELPFILE, "")
		else
		    call gpagefile (id, HELPFILE, "")

	    # Add next candidate star to the PSF.
	    case 'n':
		if (dp_pstati (dao, PNUM) >= maxnpsf) {
		    call printf ("Number of psf stars is >= to maxnpsf\n")
		    next
		}
		idnum = dp_nxstar (Memi[DP_APID(apsel)], Memr[DP_APXCEN(apsel)],
		    Memr[DP_APYCEN(apsel)], Memr[DP_APMSKY(apsel)],
		    Memr[DP_APMAG(apsel)], DP_APNUM(apsel), nxstar, lolimit,
		    radius)
		if (idnum <= 0) {
		    call printf ("No more good psf stars in photometry list\n")
		    next
		}
		if (dp_addstar (dao, im, wx, wy, INDEFR, idnum, gd, mgd,
		    showplots) == ERR)
		    next
		if (mkstars && id != NULL) {
		    call gmark (id, dp_pstatr (dao, CUR_PSFX), dp_pstatr (dao,
		        CUR_PSFY), GM_PLUS, -5.0, -5.0)
		    if (gd == id)
		        call gflush (id)
		    else
		        call gframe (id)
		}

	    # Add the star nearest the cursor position to the PSF.
	    case 'a':
		if (dp_pstati (dao, PNUM) >= maxnpsf) {
		    call printf ("Number of psf stars is >= to maxnpsf\n")
		    next
		}
		if (dp_addstar (dao, im, wx, wy, INDEFR, 0, gd, mgd,
		    showplots) == ERR)
		    next
		if (mkstars && id != NULL) {
		    call gmark (id, dp_pstatr (dao, CUR_PSFX), dp_pstatr (dao,
		        CUR_PSFY), GM_PLUS, -5.0, -5.0)
		    if (gd == id)
		        call gflush (id)
		    else
		        call gframe (id)
		}

	    # Delete the star nearest the cursor position from the PSF.
	    case 'd':
		if (dp_delstar (dao, im, wx, wy, 0, gd, showplots) == ERR)
		    next
		if (mkstars && id != NULL) {
		    call gmark (id, dp_pstatr (dao, CUR_PSFX), dp_pstatr (dao,
		        CUR_PSFY), GM_CROSS, -5.0, -5.0)
		    if (gd == id)
			call gflush (id)
		    else
			call gframe (id)
		}

	    # List all the current psf stars.
	    case 'l':
		if (interactive)
		    call dp_listpsf (dao, im)

	    # Locate the star in the aperture photometry file and print out
	    # the photometry.
	    case 'p':
		if (interactive)
		    istar = dp_locstar (dao, im, wx, wy)
		else
		    next
		if (istar > 0)
		    call dp_pshow (dao, im, istar)
		else if (istar == 0)
		    call printf ("Star not found in the photometry file\n")
		else
		    call printf (
		        "Star off or too near the edge of the image\n")

	    # Colon command mode.
	    case ':':
		for (ip = 1; IS_WHITE(Memc[cmd+ip-1]); ip = ip + 1)
		    ;
		switch (Memc[cmd+ip-1]) {

		case 'p':
		    if (! interactive)
			next
		    ip = ip + 1
		    if (ctoi (Memc[cmd], ip, idnum) <= 0)
			istar = dp_locstar (dao, im, wx, wy)
		    else
			istar = dp_idstar (dao, im, idnum)
		    if (istar > 0)
			call dp_pshow (dao, im, istar)
		    else if (istar == 0)
		        call printf (
		                "Star not found in the photometry file\n")
		    else
		        call printf (
		            "Star is off or too near the edge of the image.\n")

		case 'a':
		    if (dp_pstati (dao, PNUM) >= maxnpsf) {
		        call printf (
			    "Number of psf stars is >= to maxnpsf\n")
			next
		    }
		    ip = ip + 1
		    if (ctoi (Memc[cmd], ip, idnum) <= 0)
		        idnum = 0
		    if (dp_addstar (dao, im, wx, wy, INDEFR, idnum, gd, mgd,
		        showplots) == ERR) 
			next
		    if (mkstars && id != NULL) {
		        call gmark (id, dp_pstatr (dao, CUR_PSFX),
			    dp_pstatr (dao, CUR_PSFY), GM_PLUS, -5.0, -5.0)
			if (gd == id)
			    call gflush (id)
			else
			    call gframe (id)
		    }

		case 'd':
		    ip = ip + 1
		    if (ctoi (Memc[cmd], ip, idnum) <= 0)
			idnum = 0
		    if (dp_delstar (dao, im, wx, wy, idnum, gd, showplots) ==
		        ERR)
			next
		    if (mkstars && id != NULL) {
			call gmark (id, dp_pstatr (dao, CUR_PSFX),
			    dp_pstatr (dao, CUR_PSFY), GM_CROSS, -5.0, -5.0)
			if (gd == id)
			    call gflush (id)
			else
			    call gframe (id)
		    }

		default:
		    call printf ("Unknown keystroke command\n")
		}

	    default:
		call printf ("Unknown keystroke command\n")
	    }
	}

	# Free up memory. 
	call sfree (sp)

	return (dp_pstati (dao, PNUM))
end


define	QUERY "[Hit return to continue, q to quit]"

# DP_PQVERIFY -- Print a message in the status line asking the user if they
# really want to quit, returning YES if they really want to quit, NO otherwise.

int procedure dp_pqverify ()

int	ch
pointer	tty
int	getci()
pointer	ttyodes()

begin
	# Open terminal and print query.
	tty = ttyodes ("terminal")
	call ttyclearln (STDOUT, tty)
	call ttyso (STDOUT, tty, YES)
	call printf (QUERY)
	call flush (STDOUT)

	# Get character.
	call fseti (STDIN, F_RAW, YES)
	if (getci (STDIN, ch) == EOF)
	    ;

	# Reset and close terminal.
	call fseti (STDIN, F_RAW, NO)
	call ttyso (STDOUT, tty, NO)
	call ttyclearln (STDOUT, tty)
	call printf ("\n")
	call flush (STDOUT)
	call ttycdes (tty)

	# Return YES for the quit command, otherwise NO.
	if (ch == 'q') {
	    return (YES)
	} else {
	    return (NO)
	}
end



# DP_NXSTAR -- Select the next psf star form the photometry list.

int procedure dp_nxstar (ids, xcen, ycen, sky, mag, nstar, nxstar, lolimit,
	radius) 

int	ids[ARB]		# array of star ids
real	xcen[ARB]		# array of x coordinates
real	ycen[ARB]		# array of y coordinates
real	sky[ARB]		# array of sky values
real	mag[ARB]		# array of magnitudes
int	nstar			# the number of stars
int	nxstar			# the current star
real	lolimit			# lower data limit
real	radius			# minimum separation

bool	omit
int	istar, jstar
real	radsq, dy2, dr2

begin
	radsq = radius * radius

	# Check the first star.
	if ((mag[1] > lolimit) && (nxstar == 0)) {
	    nxstar = 1
	    return (ids[1])
	}

	# Loop over the candidate psf stars.
	do istar = nxstar + 1, nstar {

	    # Test that the candidate psf stars are not saturated and
	    # are sufficiently far from the edge of the frame.

	    if (mag[istar] <= lolimit)
		next

	    # Text that there are no brighter stars with a distance squared
	    # of radsq.
	    omit = false
	    do jstar = 1, istar - 1 {
		dy2 = abs (ycen[jstar] - ycen[istar])
		if (dy2 >= radius)
		    next
		dr2 = (xcen[jstar] - xcen[istar]) ** 2 + dy2 ** 2
		if (dr2 >= radsq)
		    next
		omit = true
		break
	    }

	    if (omit)
		next

	    nxstar = istar
	    return (ids[istar])
	}

	return (0)
end