aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/ptools/pexamine/t_pexamine.x
blob: 10db07472741e7f17414e2f983c06f61bc064405 (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
include	<fset.h>
include	<error.h>
include "pexamine.h"

# T_PEXAMINE -- Interactively examine and edit APPHOT and DAOPHOT output.

procedure t_pexamine()

pointer	input		# pointer to the name of the catalog
pointer	output		# pointer to the name of the edited catalog
pointer	xcolumn		# pointer to the name of the X column
pointer	ycolumn		# pointer to the name of the Y column
pointer	xposcolumn	# pointer to the name of the X coord column
pointer	yposcolumn	# pointer to the name of the Y coord column
pointer	hcolumn		# pointer to the name of the histogram column
pointer	photcolumns	# pointer to the photometry columns
pointer	usercolumns	# pointer to the user columns
pointer	graphics	# pointer to the name of the graphics device
pointer	image		# pointer to the name of the input image
pointer	reject 		# pointer to the name of the deletions catalog
real	match_radius	# the matching radius

int	numrows, max_nstars, first_star, status
int	apd, apout, aprej, use_display
pointer	sp, key, px, gd, im, deleted

bool	clgetb()
int	fstati(), access(), clgeti(), pt_getphot(), pt_plot(), open(), btoi()
pointer	gopen(), tbtopn(), pt_init(), immap()
real	clgetr()

begin
	# Flush on a newline if the standard output is not redirected.
	if (fstati (STDOUT, F_REDIR) == NO)
	    call fseti (STDOUT, F_FLUSHNL, YES)

	# Get some working memory.
	call smark (sp)
	call salloc (input, SZ_FNAME, TY_CHAR)
	call salloc (output, SZ_FNAME, TY_CHAR)
	call salloc (image, SZ_FNAME, TY_CHAR)
	call salloc (reject, SZ_FNAME, TY_CHAR)
	call salloc (photcolumns, PX_SZCOLNAME * (PX_MAXNCOLS + 1), TY_CHAR)
	call salloc (xcolumn, PX_SZCOLNAME, TY_CHAR)
	call salloc (ycolumn, PX_SZCOLNAME, TY_CHAR)
	call salloc (hcolumn, PX_SZCOLNAME, TY_CHAR)
	call salloc (xposcolumn, PX_SZCOLNAME, TY_CHAR)
	call salloc (yposcolumn, PX_SZCOLNAME, TY_CHAR)
	call salloc (usercolumns, PX_SZCOLNAME * (PX_MAXNCOLS + 1), TY_CHAR)
	call salloc (graphics, SZ_FNAME, TY_CHAR)

	# Fetch the input and output file parameters and the column
	# definition parameters.
	call clgstr ("input", Memc[input], SZ_FNAME)
	call clgstr ("output", Memc[output], SZ_FNAME)
	call clgstr ("image", Memc[image], SZ_FNAME)
	call clgstr ("deletions", Memc[reject], SZ_FNAME)
	call clgstr ("photcolumns", Memc[photcolumns], PX_SZCOLNAME * 
	    (PX_MAXNCOLS + 1))
	call clgstr ("xcolumn", Memc[xcolumn], PX_SZCOLNAME)
	call clgstr ("ycolumn", Memc[ycolumn], PX_SZCOLNAME)
	call clgstr ("hcolumn", Memc[hcolumn], PX_SZCOLNAME)
	call clgstr ("xposcolumn", Memc[xposcolumn], PX_SZCOLNAME)
	call clgstr ("yposcolumn", Memc[yposcolumn], PX_SZCOLNAME)
	call clgstr ("usercolumns", Memc[usercolumns], PX_SZCOLNAME *
	    (PX_MAXNCOLS + 1))

	match_radius = clgetr ("match_radius")
	max_nstars = clgeti ("max_nstars")
	first_star = clgeti ("first_star")

	# Get the graphics and display parameters.
	call clgstr ("icommands.p_filename", Memc[graphics], SZ_FNAME)
	if (Memc[graphics] != EOS)
	    use_display = YES
	else
	    use_display = btoi (clgetb ("use_display"))
	call clgstr ("graphics", Memc[graphics], SZ_FNAME)

	# Initialize the pexamine struture.
	px = pt_init (Memc[photcolumns], Memc[usercolumns], Memc[xcolumn],
	    Memc[ycolumn], Memc[xposcolumn], Memc[yposcolumn], Memc[hcolumn])
	
	# Open the input catalog.
	if (access (Memc[input], 0, TEXT_FILE) == YES) {
	    apd = open (Memc[input], READ_ONLY, TEXT_FILE)
	    call pt_kyinit (key)
	} else {
	    apd = tbtopn (Memc[input], READ_ONLY, 0)
	    key = NULL
	}

	# Open the input image.
	if (Memc[image] == EOS)
	    im = NULL
	else
	    im = immap (Memc[image], READ_ONLY, 0)

	# Allocate buffer space.
	iferr {
	    numrows = min (max_nstars, pt_getphot (px, apd, key, max_nstars,
		first_star))
	    call malloc (deleted, numrows, TY_INT)
	} then
	    call erract (EA_FATAL)

	# Plot the data and enter the interactive cursor loop.
	gd = gopen (Memc[graphics], NEW_FILE, STDGRAPH)
	status = pt_plot (gd, px, apd, key, im, Memi[deleted], numrows,
	    max_nstars, first_star, match_radius, use_display)
	call gclose (gd)

	if (status == PX_EXIT) {

	    # Open the output file.
	    if (Memc[output] != EOS) {
	        if (access (Memc[output], 0, 0) == YES) {
		    call printf ("The catalog %s already exists\n")
			call pargstr (Memc[output])
		    call mktemp ("out", Memc[output], SZ_FNAME)
		    call printf ("The new output catalog is %s\n")
			call pargstr (Memc[output])
	        }
	        if (key == NULL)
	            apout = tbtopn (Memc[output], NEW_COPY, apd)
	        else
		    apout = open (Memc[output], NEW_FILE, TEXT_FILE)
	    } else
		apout = NULL

	    # Open a reject points catalog if required.
	    if (Memc[reject] != EOS) {
	        if (access (Memc[reject], 0, 0) == YES) {
		    call printf ("The catalog %s already exists.\n")
			call pargstr (Memc[reject])
		    call mktemp ("rej", Memc[reject], SZ_FNAME)
		    call printf ("The new rejections catalog is %s\n")
			call pargstr (Memc[reject])
	        }
	        if (key == NULL)
	            aprej = tbtopn (Memc[reject], NEW_COPY, apd)
	        else
		    aprej = open (Memc[reject], NEW_FILE, TEXT_FILE)
	    } else
		aprej = NULL

	    # Write the output catalog file.
	    call pt_wtfile (apd, key, apout, aprej, Memi[deleted], numrows) 

	    # Close the output file.
	    if (apout != NULL) {
	        if (key == NULL)
	            call tbtclo (apout)
 	        else
	            call close (apout)
	    }

	    # Close the rejected points file.
	    if (aprej != NULL) {
	        if (key == NULL)
	            call tbtclo (aprej)
	        else
		    call close (aprej)
	    }

	} else if (status == ERR)
	    call fseti (STDOUT, F_CANCEL, OK)

	# Close the input file.
	if (key != NULL) {
	    call pt_kyfree (key)
	    call close (apd)
	} else if (apd != NULL)
	    call tbtclo (apd)

	# Close the image.
	if (im != NULL)
	    call imunmap(im)

	# Return the buffer space.
	call mfree (deleted, TY_INT)

	# Free the program structures.
	call pt_free (px)
	call sfree (sp)
	if (status == ERR)
	    call erract (EA_ERROR)
end