aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/daophot/group/dpwrtgroup.x
blob: 97b9a7147ecd05598491971eb621b8b42b3edfab (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
include <mach.h>
include <time.h>
include	<tbset.h>
include "../lib/daophotdef.h"
include "../lib/apseldef.h"

define	NCOLUMN 	6

# DP_TGNEWGRP -- Create a new GROUP output ST table.

procedure dp_tgnewgrp (tp, colpoint)

pointer	tp			# pointer to outpu ST table
pointer	colpoint[ARB]		# array of pointers to columns

pointer	sp, colnames, colunits, colformat, col_dtype, col_len

begin
	# Allocate space for table definition.
	call smark (sp)
	call salloc (colnames, NCOLUMN * (SZ_COLNAME + 1), TY_CHAR)
	call salloc (colunits, NCOLUMN * (SZ_COLUNITS + 1), TY_CHAR)
	call salloc (colformat, NCOLUMN * (SZ_COLFMT + 1), TY_CHAR)
	call salloc (col_dtype, NCOLUMN, TY_INT)
	call salloc (col_len, NCOLUMN, TY_INT)

	# Set up the column definitions.
	call strcpy (GROUP, Memc[colnames], SZ_COLNAME)
	call strcpy (ID, Memc[colnames+SZ_COLNAME+1], SZ_COLNAME)
	call strcpy (XCENTER, Memc[colnames+2*SZ_COLNAME+2], SZ_COLNAME)
	call strcpy (YCENTER, Memc[colnames+3*SZ_COLNAME+3], SZ_COLNAME)
	call strcpy (MAG, Memc[colnames+4*SZ_COLNAME+4], SZ_COLNAME)
	call strcpy (SKY, Memc[colnames+5*SZ_COLNAME+5], SZ_COLNAME)

	# Set up the format definitions.
	call strcpy ("%6d", Memc[colformat], SZ_COLFMT)
	call strcpy ("%6d", Memc[colformat+SZ_COLFMT+1], SZ_COLFMT)
	call strcpy ("%10.3f", Memc[colformat+2*SZ_COLFMT+2], SZ_COLFMT)
	call strcpy ("%10.3f", Memc[colformat+3*SZ_COLFMT+3], SZ_COLFMT)
	call strcpy ("%12.3f", Memc[colformat+4*SZ_COLFMT+4], SZ_COLFMT)
	call strcpy ("%15.7g", Memc[colformat+5*SZ_COLFMT+5], SZ_COLFMT)

	# Set up the unit definitions.
	call strcpy ("##", Memc[colunits], SZ_COLUNITS)
	call strcpy ("##", Memc[colunits+SZ_COLUNITS+1], SZ_COLUNITS)
	call strcpy ("PIXELS", Memc[colunits+2*SZ_COLUNITS+2], SZ_COLUNITS)
	call strcpy ("PIXELS", Memc[colunits+3*SZ_COLUNITS+3], SZ_COLUNITS)
	call strcpy ("MAGNITUDES", Memc[colunits+4*SZ_COLUNITS+4], SZ_COLUNITS)
	call strcpy ("ADC", Memc[colunits+5*SZ_COLUNITS+5], SZ_COLUNITS)

	# Set up the data type definitions.
	Memi[col_dtype] = TY_INT
	Memi[col_dtype+1] = TY_INT
	Memi[col_dtype+2] = TY_REAL
	Memi[col_dtype+3] = TY_REAL
	Memi[col_dtype+4] = TY_REAL
	Memi[col_dtype+5] = TY_REAL

	# Define the column lengths.
	Memi[col_len] = 1
	Memi[col_len+1] = 1
	Memi[col_len+2] = 1
	Memi[col_len+3] = 1
	Memi[col_len+4] = 1
	Memi[col_len+5] = 1
	
	# Define and create the table.
	call tbcdef (tp, colpoint, Memc[colnames], Memc[colunits],
	    Memc[colformat], Memi[col_dtype], Memi[col_len], NCOLUMN)
	call tbtcre (tp)

	call sfree (sp)
end


# DP_XGGRPPARS -- Write out the parameters to the header of the GROUP text
# output file.

procedure dp_xggrppars (dao, tp)

pointer	dao			# pointer to the DAOPHOT structure
int	tp			# the output file descriptor

pointer	sp, outstr, date, time, psffit
int	envfind()

begin
	# Allocate working space.
	call smark (sp)
	call salloc (outstr, SZ_LINE, TY_CHAR)
	call salloc (date, SZ_DATE, TY_CHAR)
	call salloc (time, SZ_DATE, TY_CHAR)

	psffit = DP_PSFFIT(dao)

	# Write the id.
	if (envfind ("version", Memc[outstr], SZ_LINE) <= 0)
	    call strcpy ("NOAO/IRAF", Memc[outstr], SZ_LINE)
	call dp_rmwhite (Memc[outstr], Memc[outstr], SZ_LINE)
	call dp_sparam (tp, "IRAF", Memc[outstr], "version", "")
	if (envfind ("userid", Memc[outstr], SZ_LINE) > 0)
	    call dp_sparam (tp, "USER", Memc[outstr], "name", "")
	call gethost (Memc[outstr], SZ_LINE)
	call dp_sparam (tp, "HOST", Memc[outstr], "computer", "")
	call dp_date (Memc[date], Memc[time], SZ_DATE)
	call dp_sparam (tp, "DATE", Memc[date], "yyyy-mm-dd", "")
	call dp_sparam (tp, "TIME", Memc[time], "hh:mm:ss", "")
	call dp_sparam (tp, "PACKAGE", "daophot", "name", "")
	call dp_sparam (tp, "TASK", "group", "name", "")

	# Write the file name parameters.
	call dp_imroot (DP_INIMAGE(dao), Memc[outstr], SZ_LINE)
	call dp_sparam (tp, "IMAGE", Memc[outstr], "imagename", "")
	call dp_froot (DP_INPHOTFILE(dao), Memc[outstr], SZ_LINE)
	call dp_sparam (tp, "PHOTFILE", Memc[outstr], "filename", "")
	call dp_imroot (DP_PSFIMAGE(dao), Memc[outstr], SZ_LINE)
	call dp_sparam (tp, "PSFIMAGE", Memc[outstr], "imagename", "")
	call dp_froot (DP_OUTPHOTFILE(dao), Memc[outstr], SZ_LINE)
	call dp_sparam (tp, "GRPFILE", Memc[outstr], "filename", "")

	# Write out relevant data parameters.
	call dp_rparam (tp, "SCALE", DP_SCALE(dao), "units/pix", "")
	call dp_rparam (tp, "DATAMIN", DP_MINGDATA(dao), "counts", "")
	call dp_rparam (tp, "DATAMAX", DP_MAXGDATA(dao), "counts", "")
	call dp_rparam (tp, "GAIN", DP_PHOTADU(dao), "number", "")
	call dp_rparam (tp, "READNOISE", DP_READNOISE(dao), "electrons", "")

	# Write out the observing parameters.
	call dp_sparam (tp, "OTIME", DP_OTIME(dao), "timeunit", "")
	call dp_rparam (tp, "XAIRMASS", DP_XAIRMASS(dao), "number", "")
	call dp_sparam (tp, "IFILTER", DP_IFILTER(dao), "filter", "")

	# Write out the daophot parameters.
	call dp_rparam (tp, "PSFRAD", DP_SPSFRAD(dao), "scaleunit", "")
	call dp_rparam (tp, "FITRAD", DP_SFITRAD(dao), "scaleunit", "")
	call dp_rparam (tp, "PSFMAG", DP_PSFMAG(psffit), "magnitude", "")
	call dp_rparam (tp, "CRITSNRATIO", DP_CRITSNRATIO(dao), "sigma", "")
	call dp_iparam (tp, "MAXGROUP", DP_MAXGROUP(dao), "number", "")
	#call dp_iparam (tp, "MAXNSTAR", DP_MAXNSTAR(dao), "number", "")

	# Write out the group size parameters.
	# call dp_iparam (tp, "MINSZGROUP", 1, "number", "")
	# call dp_iparam (tp, "MAXSZGROUP", MAX_INT, "number", "")

	call sfree(sp)
end


# DP_TGGRPPARS -- Write out the parameters to the header of the GROUP output
# ST table file.

procedure dp_tggrppars (dao, tp)

pointer	dao			# pointer to the DAOPHOT structure
pointer	tp			# pointer to the output table

pointer	sp, outstr, date, time, psffit
int	envfind()

begin
	# Allocate working space.
	call smark (sp)
	call salloc (outstr, SZ_LINE, TY_CHAR)
	call salloc (date, SZ_DATE, TY_CHAR)
	call salloc (time, SZ_DATE, TY_CHAR)

	psffit = DP_PSFFIT(dao)

	# Write the id.
	if (envfind ("version", Memc[outstr], SZ_LINE) <= 0)
	    call strcpy ("NOAO/IRAF", Memc[outstr], SZ_LINE)
	call dp_rmwhite (Memc[outstr], Memc[outstr], SZ_LINE)
	call tbhadt (tp, "IRAF", Memc[outstr])
	if (envfind ("userid", Memc[outstr], SZ_LINE) > 0)
	    call tbhadt (tp, "USER", Memc[outstr])
	call gethost (Memc[outstr], SZ_LINE)
	call tbhadt (tp, "HOST", Memc[outstr])
	call dp_date (Memc[date], Memc[time], SZ_DATE)
	call tbhadt (tp, "DATE", Memc[date])
	call tbhadt (tp, "TIME", Memc[time])
	call tbhadt (tp, "PACKAGE", "daophot")
	call tbhadt (tp, "TASK", "group")

	# Write the file name parameters.
	call dp_imroot (DP_INIMAGE(dao), Memc[outstr], SZ_LINE)
	call tbhadt (tp, "IMAGE", Memc[outstr])
	call dp_froot (DP_INPHOTFILE(dao), Memc[outstr], SZ_LINE)
	call tbhadt (tp, "PHOTFILE", Memc[outstr])
	call dp_imroot (DP_PSFIMAGE(dao), Memc[outstr], SZ_LINE)
	call tbhadt (tp, "PSFIMAGE", Memc[outstr])
	call dp_froot (DP_OUTPHOTFILE(dao), Memc[outstr], SZ_LINE)
	call tbhadt (tp, "GRPFILE", Memc[outstr])

	# Write out relevant data parameters.
	call tbhadr (tp, "SCALE", DP_SCALE(dao))
	call tbhadr (tp, "DATAMIN", DP_MINGDATA(dao))
	call tbhadr (tp, "DATAMAX", DP_MAXGDATA(dao))
	call tbhadr (tp, "GAIN", DP_PHOTADU(dao))
	call tbhadr (tp, "READNOISE", DP_READNOISE(dao))

	# Write out the observing parameters.
	call tbhadt (tp, "OTIME", DP_OTIME(dao))
	call tbhadr (tp, "XAIRMASS", DP_XAIRMASS(dao))
	call tbhadt (tp, "IFILTER", DP_IFILTER(dao))

	# Write out the daophot parameters.
	call tbhadr (tp, "PSFRAD", DP_SPSFRAD(dao))
	call tbhadr (tp, "FITRAD", DP_SFITRAD(dao))
	call tbhadr (tp, "PSFMAG", DP_PSFMAG(psffit))
	call tbhadr (tp, "CRITSNRATIO", DP_CRITSNRATIO(dao))
	call tbhadi (tp, "MAXGROUP", DP_MAXGROUP(dao))
	#call tbhadi (tp, "MAXNSTAR", DP_MAXNSTAR(dao))

	# call tbhadi (tp, "MINSZGROUP", 1)
	# call tbhadi (tp, "MAXSZGROUP", MAX_INT)

	call sfree(sp)
end


# DP_WRTGROUP -- Write out each group into a text file or an ST table.

procedure dp_wrtgroup (dao, im, grp, number, index, group_size, maxgroup)

pointer	dao			# pointer to daophot structure
pointer	im			# the input image descriptor
int	grp			# the output file descriptor
int	number[ARB]		# number in group of each size
int	index[ARB]		# index to results
int	group_size[ARB]		# size of groups
int	maxgroup		# maximum group size

begin
	if (DP_TEXT(dao) == YES)
	    call dp_xwrtgroup (dao, im, grp, number, index, group_size,
	        maxgroup)
	else
	    call dp_twrtgroup (dao, im, grp, number, index, group_size,
	        maxgroup)
end


define GR_NAMESTR "#N%4tGROUP%10tID%16tXCENTER%26tYCENTER%36tMAG%48tMSKY\
%80t\\\n"
define GR_UNITSTR "#U%4t##%10t##%16tpixels%26tpixels%36tmagnitudes%48tcounts\
%80t\\\n"
define GR_FORMATSTR "#F%4t%%-9d%10t%%-6d%16t%%-10.3f%26t%%-10.3f%36t%%-12.3f\
%48t%%-15.7g%80t \n"
define GR_DATASTR "%-9d%10t%-6d%16t%-10.3f%26t%-10.3f%36t%-12.3f%48t%-15.7g\
%80t \n"


# DP_XWRTGROUP -- Write each group into the GROUP output ST table.

procedure dp_xwrtgroup (dao, im, grp, number, index, group_size, maxgroup)

pointer	dao			# pointer to the daophot structure
pointer	im			# the input image descriptor
int	grp			# the output file descriptor
int	number[ARB]		# number in group of each size
int	index[ARB]		# index to results
int	group_size[ARB]		# size of groups
int	maxgroup		# maximum group size

int	i, j, k, id, ngroup, nstars, first_ingrp
pointer	apsel
real	x, y, mag, sky

begin
	# Get the daophot pointer.
	apsel = DP_APSEL(dao)

	# Print results to the standard output.
	if (DP_VERBOSE(dao) == YES) {
	    call printf (" Size of     Number of\n")
	    call printf ("  group       groups\n\n")
	    do i = 1, maxgroup {
		if (number[i] <= 0)
		    next
	        call printf ("%8d    %9d\n")
	    	    call pargi (i)
	    	    call pargi (number[i])
	    }
	}

	# Add header parameters to the table.
	call dp_xggrppars (dao, grp)

	# Write out the banner.
	call fprintf (grp, "#\n")
	call fprintf (grp, GR_NAMESTR)
	call fprintf (grp, GR_UNITSTR)
	call fprintf (grp, GR_FORMATSTR)
	call fprintf (grp, "#\n")

	# Write out each group.
	ngroup = 1
	first_ingrp = 1
	while (first_ingrp <= DP_APNUM(apsel)) {

	    do j = first_ingrp, first_ingrp + group_size[first_ingrp] - 1 {

		# Test the center.
		k = index[j]
		x = Memr[DP_APXCEN(apsel)+k-1]
		y = Memr[DP_APYCEN(apsel)+j-1]
		if (IS_INDEFR(x) || IS_INDEFR(y))
		    break
		call dp_wout (dao, im, x, y, x, y, 1)

		# Get the rest of the numbers.
		id = Memi[DP_APID(apsel)+k-1]
		mag = Memr[DP_APMAG(apsel)+k-1]
		sky = Memr[DP_APMSKY(apsel)+k-1]

		# Write the results.
		call fprintf (grp, GR_DATASTR)
		    call pargi (ngroup)
		    call pargi (id)
		    call pargr (x)
		    call pargr (y)
		    call pargr (mag)
		    call pargr (sky)
	    }

	    ngroup = ngroup + 1
	    first_ingrp = first_ingrp + group_size[first_ingrp]
	}

	# Compute the number of groups and the number of stars.
	ngroup = 0
	nstars = 0
	do i = 1, maxgroup {
	    if (number[i] <= 0)
		next
	    nstars = nstars + i * number[i]
	    ngroup = ngroup + number[i]
	}

	if (DP_VERBOSE(dao) == YES) {
	    call printf ("\nTotal of %d stars in %d groups\n")
	         call pargi (nstars)
	         call pargi (ngroup)
	}
end


# DP_TWRTGROUP -- Write each group into the GROUP output text file.

procedure dp_twrtgroup (dao, im, grp, number, index, group_size, maxgroup)

pointer	dao			# pointer to the daophot structure
pointer	im			# the input image descriptor
pointer	grp			# pointer to group output file
int	number[ARB]		# number in group of each size
int	index[ARB]		# index to results
int	group_size[ARB]		# size of groups
int	maxgroup		# maximum group size

int	row, first_ingrp, ngroup, nstars, i, j, k, id
pointer	apsel, sp, colpoint
real	x, y, mag, sky

begin
	# Get the daophot pointer.
	apsel = DP_APSEL(dao)

	# Allocate space for the column pointers.
	call smark( sp)
	call salloc (colpoint, NCOLUMN, TY_INT)

	# Get the necessary info to create the ST table.
	call dp_tgnewgrp  (grp, Memi[colpoint])

	# Add header parameters to the ST table.
	call dp_tggrppars (dao, grp)

	# Optionally print results to the standard output.
	if (DP_VERBOSE(dao) == YES) {
	    call printf (" Size of     Number of\n")
	    call printf ("  group        groups\n\n")
	    do i = 1, maxgroup {
		if (number[i] <= 0)
		    next
	        call printf ("  %d        %d\n")
	    	    call pargi (i)
	    	    call pargi (number[i])
	    }
	}

	# Initialize for writing.
	ngroup = 1
	row = 0

	# Initialize for reading.
	first_ingrp = 1

	# Write out the data for all the groups.
	while (first_ingrp <= DP_APNUM(apsel)) {

	    do j = first_ingrp, first_ingrp + group_size[first_ingrp] - 1 {

		# Test the center.
		k = index[j]
		x = Memr[DP_APXCEN(apsel)+k-1]
		y = Memr[DP_APYCEN(apsel)+j-1]
		if (IS_INDEFR(x) || IS_INDEFR(y))
		    break
		call dp_wout (dao, im, x, y, x, y, 1)

		# Get the rest of the values.
		id = Memi[DP_APID(apsel)+k-1]
		mag = Memr[DP_APMAG(apsel)+k-1]
		sky = Memr[DP_APMSKY(apsel)+k-1]

		# Copy the values to the correct table row.
		row = row + 1
		call tbrpti (grp, Memi[colpoint], ngroup, 1, row)
		call tbrpti (grp, Memi[colpoint+1], id, 1, row)
	    	call tbrptr (grp, Memi[colpoint+2], x, 1, row)
	    	call tbrptr (grp, Memi[colpoint+3], y, 1, row)
	    	call tbrptr (grp, Memi[colpoint+4], mag, 1, row)
	    	call tbrptr (grp, Memi[colpoint+5], sky, 1, row)
	    }

	    ngroup = ngroup + 1
	    first_ingrp = first_ingrp + group_size[first_ingrp]
	}

	# Compute the number of groups and the number of stars.
	ngroup = 0
	nstars = 0
	do i = 1, maxgroup {
	    if (number[i] <= 0)
		next
	    nstars = nstars + i * number[i]
	    ngroup = ngroup + number[i]
	}

	# Optionally print out a summary of the results.
	if (DP_VERBOSE(dao) == YES) {
	    call printf ("\nTotal of %d stars in %d groups\n")
	         call pargi (nstars)
	         call pargi (ngroup)
	}

	call sfree (sp)
end