aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/daophot/psf/dpmkpsf.x
blob: 2af7dbed934b36fb4908de616b860684f3d5b777 (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
include <gset.h>
include <ctype.h>
include	<imhdr.h>
include "../lib/daophotdef.h"
include "../lib/apseldef.h"
include "../lib/psfdef.h"

define	HELPFILE	"daophot$psf/mkpsf.key"

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

procedure dp_mkpsf (dao, im, psfim, opst, psfgr, gd, mgd, id, mkstars,
	interactive, showplots)

pointer	dao			# pointer to the main daophot structure
pointer im			# pointer to the input image
pointer	psfim			# pointer to the output psfimage
int	opst			# the psf star list file descriptor
int	psfgr			# the psf group file descriptor
pointer gd			# pointer to graphics descriptor
pointer mgd			# pointer to the metacode file
pointer id			# pointer to image display stream
bool	mkstars			# mark the added and deleted psf stars
bool	interactive		# interactive mode
bool	showplots		# show the plots

real	wx, wy
pointer apsel, sp, cmd, str
int	i, key, wcs, idnum, istar, ip, npsf, verbose
bool	psf_new, psf_computed, psf_written

real	dp_pstatr()
int	clgcur(), dp_qverify(), dp_locstar(), dp_idstar(), dp_stati()
int	dp_pstati(), ctoi(), dp_addstar(), dp_delstar(), dp_subpsf()
int	dp_fitpsf()
bool	dp_updatepsf()

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'
	if (dp_pstati (dao, PNUM) > 0)
	    psf_new = false
	else
	    psf_new = true
	psf_computed = false
	psf_written = false

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

	    # Convert 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_qverify (dao, im, psfim, opst, psfgr, psf_new,
		        psf_computed, psf_written) == NO) 
			next
		} else if (dp_updatepsf (dao, im, psfim, opst, psfgr, psf_new,
		    psf_computed, psf_written)) {
		    psf_computed = true
		    psf_written = true
		}

		# Delete any empty psf and group files lying around.
		if (! psf_written)
		    call dp_rmpsf (dao, psfim, opst, psfgr)

		# Delete the PSF stars. 
		call dp_pseti (dao, PNUM, 0)

		break

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

	    # Add the star nearest the cursor position to the PSF.
	    case 'a':
		if (dp_addstar (dao, im, wx, wy, INDEFR, 0, gd, mgd,
		    showplots) == ERR) 
		    next
		psf_new = false
		psf_computed = false
		psf_written = false
                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)
		}

	    # Subtract the star nearest the cursor position from the PSF
	    # using the current best fit.
	    case 's':
		if (psf_new) {
		    call printf ("The PSF star list is empty\n")
		    next
		}
		if (! psf_computed) {
		    call printf ("The PSF is not uptodate\n")
		    next
		}
		if (dp_subpsf (dao, im, wx, wy, 0, gd, mgd, showplots) == ERR)
		    next
		if (dp_pstati (dao, PNUM) <= 0)
		    psf_new = true
		psf_computed = false
		psf_written = false

	    # 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 (dp_pstati (dao, PNUM) <= 0)
		    psf_new = true
		psf_computed = false
		psf_written = false
                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)

	    # Fit the PSF.
	    case 'f':

		# Reread the stars is necessary.
		if (dp_pstati (dao, PNUM) > 0 && (psf_new || psf_computed)) {
		    npsf = dp_pstati (dao, PNUM)
		    call dp_pseti (dao, PNUM, 0)
		    call dp_reinit (dao)
		    verbose = dp_stati (dao, VERBOSE)
		    call dp_seti (dao, VERBOSE, NO)
		    do i = 1, npsf
		        if (dp_addstar (dao, im, wx, wy, INDEFR,
		            Memi[DP_APID(apsel)+i-1], gd, mgd,
			    false) == OK)
			    psf_new = false
		    call dp_seti (dao, VERBOSE, verbose)
		}

		# Fit the psf.
		if (psf_new) {
		    call printf ("The PSF star list is empty\n")
		} else if (dp_fitpsf (dao, im, Memc[str], SZ_LINE) == OK) {
		    psf_computed = true
		} else {
		    call printf ("%s\n")
			call pargstr (Memc[str])
		}

	    # Review the fit. 
	    case 'r':
		if (psf_new) {
		    call printf ("The PSF star list is empty\n")
		} else if (! psf_computed) {
		    call printf ("The PSF is not uptodate\n")
		} else {
		    i = 1
		    repeat {
		        if (dp_subpsf (dao, im, wx, wy,
			    Memi[DP_APID(apsel)+i-1], gd, mgd,
			    showplots) == OK) {
		            if (dp_pstati (dao, PNUM) <= 0)
		                psf_new = true
		            psf_computed = false
		            psf_written = false
			} else
			    i = i + 1
		    } until (i > dp_pstati(dao, PNUM))
		}

	    # Rebuild the PSF from scratch.
	    case 'z':

		# Print message.
		if (interactive) {
		    call dp_stats (dao, OUTPHOTFILE, Memc[str], SZ_FNAME)
		    call printf (
		        "PSF star list, image, and output files deleted\n")
		}

		# Delete the previous psf an dgroup files if any.
		call dp_rmpsf (dao, psfim, opst, psfgr)

		# Delete the PSF stars. 
		call dp_pseti (dao, PNUM, 0)

		# Reopen the psf image and group files.
		call dp_oppsf (dao, psfim, opst, psfgr)

		# Reset the reduction flags.
		psf_new = true
		psf_computed = false
		psf_written = false

	    # Write out the PSF.
	    case 'w':
		if (dp_updatepsf (dao, im, psfim, opst, psfgr, psf_new,
		    psf_computed, psf_written)) {
		    psf_computed = true
		    psf_written = true
		}

	    # 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")

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

		case 'p':
		    if (Memc[cmd+ip] != EOS && Memc[cmd+ip] != ' ') {
			call dp_pcolon (dao, psfim, opst, psfgr, Memc[cmd],
			    psf_new, psf_computed, psf_written)
			next
		    }
		    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 (Memc[cmd+ip] != EOS && Memc[cmd+ip] != ' ') {
			call dp_pcolon (dao, psfim, opst, psfgr, Memc[cmd],
			    psf_new, psf_computed, psf_written)
			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
		    psf_new = false
		    psf_computed = false
		    psf_written = false
                    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 's':
		    if (Memc[cmd+ip] != EOS && Memc[cmd+ip] != ' ') {
			call dp_pcolon (dao, psfim, opst, psfgr, Memc[cmd],
			    psf_new, psf_computed, psf_written)
			next
		    }
		    ip = ip + 1
		    if (ctoi (Memc[cmd], ip, idnum) <= 0)
			idnum = 0
		    if (! psf_computed) {
		        call printf ("The PSF has not been fit\n")
			next
		    }
		    if (! psf_computed) {
		        call printf ("Warning: The PSF is not uptodate\n")
			next
		    }
		    if (dp_subpsf (dao, im, wx, wy, idnum, gd, mgd,
		        showplots) == ERR)
			next
		    if (dp_pstati (dao, PNUM) <= 0)
		        psf_new = true
		    psf_computed = false
		    psf_written = false

		case 'd':
		    if (Memc[cmd+ip] != EOS && Memc[cmd+ip] != ' ') {
			call dp_pcolon (dao, psfim, opst, psfgr, Memc[cmd],
			    psf_new, psf_computed, psf_written)
			next
		    }
		    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 (dp_pstati (dao, PNUM) <= 0)
		        psf_new = true
		    psf_computed = false
		    psf_written = false
                    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 dp_pcolon (dao, psfim, opst, psfgr, Memc[cmd],
		        psf_new, psf_computed, psf_written)
		}

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

	# Free up memory. 
	call sfree (sp)
end