aboutsummaryrefslogtreecommitdiff
path: root/noao/imred/dtoi/spotlist.x
blob: 8f4ffeee7331ce6246e1dd8e97dbbb1b3ab2b28d (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
include	<error.h>
include	<imhdr.h>
include	<mach.h>
include	<fset.h>

# T_SPOTLIST -- calculates the densities and standard deviations of calibration
# spots read in as IRAF images.  Entries for density, sdev, spot number
# and number of pixels used in the average are made in the output database.
# These values are entered for all spots and the fog level.  The
# fog level is calculated but not subtracted in this procedure.

procedure t_spotlist ()

pointer	db, sp, den, ngpix, sdev, dloge, option, npix
int	spot_fd, fog_fd, fngpix, nspots, maxad, fnpix
real	scale, sigma, fsdev, fog
double	maxden

pointer	ddb_map()
int	imtopenp(), imtlen(), clgeti(), strncmp()
real	clgetr()

begin
	call smark (sp)
	call salloc (dloge, SZ_FNAME, TY_CHAR)
	call salloc (option, SZ_FNAME, TY_CHAR)

	# Get parameters and open file name templates
	spot_fd = imtopenp ("spots")
	fog_fd  = imtopenp ("fogs")
	call clgstr ("database", Memc[dloge], SZ_FNAME)
	scale = clgetr ("scale")
	maxad = clgeti ("maxad")
	sigma = clgetr ("sigma")
	call clgstr ("option", Memc[option], SZ_FNAME)

	maxden = maxad * double (scale)

	# Allocate space for density, standard deviation and ngpix arrays
	nspots = imtlen (spot_fd)
	call salloc ( den,  nspots, TY_REAL)
	call salloc (sdev,  nspots, TY_REAL)
	call salloc (ngpix, nspots, TY_INT)
	call salloc ( npix, nspots, TY_INT)

	# Calculate densities depending on algorithm option.  The 
	# number of saturated pixels per spot is also calculated now.

	if (strncmp (Memc[option], "median", 3) == 0)
	    call hd_median (spot_fd, Memr[den], Memr[sdev], Memi[ngpix],
		nspots, scale, Memi[npix])
	else
	    call hd_mean (spot_fd, Memr[den], Memr[sdev], Memi[ngpix], 
		nspots, scale, sigma, Memi[npix])

	# Calculate fog level and count saturated pixels
	call hd_fogcalc (fog_fd, fog, fsdev, fngpix, scale, sigma, 
	    Memc[option], fnpix)

	# Now print results to stdout
	call hd_printit (Memr[den], Memr[sdev], Memi[npix], Memi[ngpix], 
	    fog, fsdev, fnpix, fngpix, nspots)
	    
	# Open output database file and write spot information
	db = ddb_map (Memc[dloge], APPEND)
	call hd_wspotdb (db, Memr[den], Memr[sdev], Memi[ngpix], nspots)

	# Write fog information to database as single record
	call ddb_prec (db, "fog")
	call ddb_putr (db, "density", fog)
	call ddb_putr (db, "sdev", fsdev)
	call ddb_puti (db, "ngpix", fngpix)

	# Scale info gets written to database also (very precisely!)
	call ddb_prec (db, "common")
	call ddb_putr (db, "scale", scale)
	call ddb_putd (db, "maxden", maxden)
	call ddb_pstr (db, "option", Memc[option])

	call ddb_unmap (db)

	call imtclose (spot_fd)
	call imtclose (fog_fd)
	call sfree (sp)
end


# HD_MEAN -- Calculate mean density of calibration spots.

procedure hd_mean (spot_fd, den, sdev, ngpix, nspots, scale, sigma, npix)

int	spot_fd		# File descriptor for list of spots
real	den[ARB]	# Mean density values - filled on return
real	sdev[ARB]	# Standard deviation array - filled on return
int	ngpix[ARB]	# Number of unrejected pixels - filled on return
int	nspots		# Number of spots in list
real	scale		# Scale for voltage to density conversion
real	sigma		# Rejection criteria set by user
int	npix[ARB]	# Number of pixels per spot

pointer	im, spot, sp, pix
int	i, junk, ncols, nlines
pointer	immap(), imgs2r()
int	imtgetim(), hd_aravr()
errchk	imgs2r, amulkr, hd_aravr

begin
	call smark (sp)
	call salloc (spot, SZ_FNAME, TY_CHAR)

	# Loop over spot rasters.  Calculate density and standard deviation.
	for (i = 1; i <= nspots; i = i + 1) {
	    junk = imtgetim (spot_fd, Memc[spot], SZ_FNAME)
	    iferr (im = immap (Memc[spot], READ_ONLY, 0)) {
		call erract (EA_WARN)
		next
	    }

	    ncols = IM_LEN(im,1)
	    nlines = IM_LEN(im,2)
	    npix[i] = ncols * nlines

	    # For all pixels in the image, scale the a/d value to density and
	    # calculate the mean value, using a mean rejection algorithm.

	    pix = imgs2r (im, 1, ncols, 1, nlines)
	    call amulkr (Memr[pix], scale, Memr[pix], npix[i])
	    ngpix[i] = hd_aravr (Memr[pix], npix[i], den[i], sdev[i], sigma)
	    call imunmap (im)
	}

	call sfree (sp)
end


# HD_MEDIAN -- Calculate median density of calibration spots.

procedure hd_median (spot_fd, den, sdev, ngpix, nspots, scale, npix)

int	spot_fd		# File descriptor for list of spots
real	den[ARB]	# Mean density values - filled on return
real	sdev[ARB]	# Standard deviation of input spots
int	ngpix[ARB]	# Number of pixels not rejected
int	nspots		# Number of spots in list
real	scale		# Scale for voltage to density conversion
int	npix[ARB]	# Number of pixels per spot

pointer	im, spot, sp, pix
int	i, junk, ncols, nlines
real	junk_mean
pointer	immap(), imgs2r()
int	imtgetim()
real	amedr()
errchk	imgs2r, amulkr, amedr

begin
	call smark (sp)
	call salloc (spot, SZ_FNAME, TY_CHAR)

	# Loop over spot rasters.  Calculate density and standard deviation.
	for (i = 1; i <= nspots; i = i + 1) {
	    junk = imtgetim (spot_fd, Memc[spot], SZ_FNAME)
	    iferr (im = immap (Memc[spot], READ_ONLY, 0)) {
		call erract (EA_WARN)
		next
	    }

	    ncols = IM_LEN(im,1)
	    nlines = IM_LEN(im,2)
	    npix[i] = ncols * nlines

	    # For all pixels in the image, scale the a/d value to density and
	    # calculate the median value.  For the user's information, the
	    # sigma is also calculated in a call to aavgr.

	    pix = imgs2r (im, 1, ncols, 1, nlines)
	    call amulkr (Memr[pix], scale, Memr[pix], npix[i])
	    den[i] = amedr (Memr[pix], npix[i])
	    ngpix[i] = npix[i]
	    call aavgr (Memr[pix], npix[i], junk_mean, sdev[i])
	    call imunmap (im)
	}

	call sfree (sp)
end


# HD_FOGCALC -- Calculate fog density.

procedure hd_fogcalc (fog_fd, fog, fsdev, fngpix, scale, sigma, option, nfpix)

int	fog_fd		# File descriptor for list of fog spots
real	fog		# Mean fog value - returned
real	fsdev		# Standard deviation - returned
int	fngpix		# Number of pixels used - returned
real	scale		# Voltage to density scaling factor
real	sigma		# Rejection criteria - set by user
char	option[ARB]     # User's choice of mean/median algorithm
int	nfpix		# Total number of fog pixels

pointer	pix, im, sp, fogfile, ptr
int	nfog, maxfog, i, junk, ncols, nlines, npix, total_pix, op
real	junk_mean
pointer	immap(), imgs2r()
int	imtlen(), imtgetim(), hd_aravr(), strncmp()
real	amedr()
errchk	calloc, imgs2r, aaddr, amulkr, hd_aravr

begin
	call smark (sp)
	call salloc (fogfile, SZ_FNAME, TY_CHAR)

	pix = NULL
	total_pix = 0
	op = 0
	nfog = imtlen (fog_fd)
	maxfog = nfog
	do i = 1, maxfog {
	    junk = imtgetim (fog_fd, Memc[fogfile], SZ_FNAME)
	    iferr (im = immap (Memc[fogfile], READ_ONLY, 0)) {
		call erract (EA_WARN)
		nfog = nfog - 1
		next
	    }

	    ncols = IM_LEN(im,1)
	    nlines = IM_LEN(im,2)
	    npix = ncols * nlines
	    total_pix = total_pix + npix

	    if (pix == NULL)
		# Initialize space for accumulating pixels
		call calloc (pix, npix, TY_REAL)
	    else
	       # Increase space for accumulating pixels
	       call realloc (pix, total_pix, TY_REAL)

	    # Build up vector of fog pixels
	    ptr = imgs2r (im, 1, ncols, 1, nlines)
	    call amovr (Memr[ptr], Memr[pix+op], npix)
	    op = op + npix

	    call imunmap (im)
	}

	# Scale values to density and calculate fog and std deviation

	if (nfog > 0) {
# 7 Sept 1989, S. Rooke:  in Suzanne's absence, made following bugfix after
# bug reported by Steve Majewski that fog values are off by 1/n images where
# multiple fog images are used in a single run.  The total_pix already contains
# the sum of all pixel values, so the fog pixel values should not be divided
# by nfog.  This should be verified by Suzanne on her return, and these comments
# removed.
#	    call amulkr (Memr[pix], scale / real (nfog), Memr[pix], total_pix)
	    call amulkr (Memr[pix], scale, Memr[pix], total_pix)
	    if (strncmp (option, "median", 3) == 0) {
		fog = amedr (Memr[pix], total_pix)
		fngpix = total_pix
		call aavgr (Memr[pix], total_pix, junk_mean, fsdev)
	    } else
	        fngpix = hd_aravr (Memr[pix], total_pix, fog, fsdev, sigma)
	}  else {
	    fog = 0.0
	    fsdev = 0.0
	    fngpix = 0
	}
	nfpix = total_pix

	call mfree (pix, TY_REAL)
	call sfree (sp)
end


# HD_WSPOTDB -- Write spot information to database file.  Values are first
# sorted in order of increasing density.

procedure hd_wspotdb (db, density, sdev, ngpix, nspots)

pointer	db			# Pointer to database
real	density[ARB]		# Array of densities
real	sdev [ARB]		# Array of standard deviations
int	ngpix [ARB]		# Array of npix used in calculations
int	nspots			# Number of density spots

begin
	if (density[1] > density[nspots]) {
	    # Need to reorder arrays
	    call hd_reorderr (density, nspots)
	    call hd_reorderr (   sdev, nspots)
	    call hd_reorderi (  ngpix, nspots)
	}

	call ddb_ptime (db)

	# Density record
	call ddb_prec (db, "density")
	call ddb_par (db, "den_val", density, nspots)

	# Standard deviation of density is written to a record
	call ddb_prec (db, "standard deviation")
	call ddb_par (db, "sdev_val", sdev, nspots)

	# Record for npix_used
	call ddb_prec (db, "ngpix")
	call ddb_pai (db, "npix_val", ngpix, nspots)
end


# HD_REORDERR - Flip order of real array in place.

procedure hd_reorderr (array, nvals)

real	array[ARB]		# Real array to be reordered
int	nvals			# Number of elements in array

pointer	sp, tmp
int	i

begin
	call smark (sp)
	call salloc (tmp, nvals, TY_REAL)

	call amovr (array, Memr[tmp], nvals)
	do i = 1, nvals
	    array[i] = Memr[tmp+nvals-i]

	call sfree (sp)
end


# HD_REORDERI -- Flip order of integer array in place.

procedure hd_reorderi (array, nvals)

int	array[ARB]		# Integer array to be ordered
int	nvals			# Number of elements in array

pointer	sp, tmp
int	i

begin
	call smark (sp)
	call salloc (tmp, nvals, TY_INT)

	call amovi (array, Memi[tmp], nvals)
	do i = 1, nvals
	    array[i] = Memi[tmp+nvals-i]

	call sfree (sp)
end


# HD_PRINTIT -- Neatly printout all the accumulated information.

procedure hd_printit (den, sdev, npix, ngpix, fog, fsdev, fnpix, fngpix, nspots)

real	  den[ARB]	# density array 
real	 sdev[ARB]	# std deviation array 
int	 npix[ARB]      # npix array
int	ngpix[ARB]      # ngoodpix array
real	fog		# fog value
real	fsdev           # std deviation of fog
int     fnpix		# npix in fog
int	fngpix		# ngoodpix in fog
int	nspots		# number of spots
int	i

begin

	call fseti (STDOUT, F_FLUSHNL, YES)

	call printf ("\n                                         # Number of P")
	call printf ("ixels\n")
	call printf ("# Spot Number  Density   Std Deviation  Total  Used  Rej")
	call printf ("ected \n")

	do i = 1, nspots {
	    call printf ("    %d %17t%.4f %27t%.4f %43t%d %5d %6d \n")
	        call pargi (i)
	        call pargr (den[i])
	        call pargr (sdev[i])
	        call pargi (npix[i])
	        call pargi (ngpix[i])
	        call pargi (npix[i] - ngpix[i])
	}

	call printf ("   FOG %17t%.4f %27t%.4f %43t%d %5d %6d \n")
	    call pargr (fog)
	    call pargr (fsdev)
	    call pargi (fnpix)
	    call pargi (fngpix)
	    call pargi (fnpix - fngpix)

end