aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/apphot/radprof/apfrprof.x
blob: 093e90bd844ac192118d4953b11bbfa6ccbb0708 (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
include <mach.h>
include <gset.h>
include <math.h>

include <math/curfit.h>
include <math/iminterp.h>
include "../lib/apphotdef.h"
include "../lib/noisedef.h"
include "../lib/fitskydef.h"
include "../lib/photdef.h"
include "../lib/radprofdef.h"
include "../lib/apphot.h"
include "../lib/phot.h"
include "../lib/radprof.h"

# AP_FRPROF -- Compute the radial profile of an object.

int procedure ap_frprof (ap, im, wx, wy, pier)

pointer	ap		# pointer to the apphot structure
pointer	im		# pointer to the IRAF image
real	wx, wy		# object coordinates
int	pier		# photometry error

int	i, ier, fier, nxpts, nypts, nrpts, order
pointer	sky, rprof, cv, asi
real	datamin, datamax, step, rmin, rmax, inorm, tinorm
int	ap_rpbuf(), ap_rmag(), ap_rpmeasure(), ap_rpiter()
real	asigrl(), cveval(), ap_rphalf()

errchk	cvinit(), cvfit(), cvvector(), cveval(), cvfree()
errchk	asinit(), asifit(), asigrl(), asifree()
errchk	ap_rpmeasure(), ap_rpiter()

begin
	# Set up some apphot pointers.
	sky = AP_PSKY(ap)
	rprof = AP_RPROF(ap)

	# Initialize.
         AP_RPXCUR(rprof) = wx
         AP_RPYCUR(rprof) = wy
	call ap_rpindef (ap)
        if (IS_INDEFR(wx) || IS_INDEFR(wy)) {
            AP_ORPXCUR(rprof) = INDEFR
            AP_ORPYCUR(rprof) = INDEFR
        } else {
            switch (AP_WCSOUT(ap)) {
            case WCS_WORLD, WCS_PHYSICAL:
                call ap_ltoo (ap, wx, wy, AP_ORPXCUR(rprof),
		    AP_ORPYCUR(rprof), 1)
            case WCS_TV:
                call ap_ltov (im, wx, wy, AP_ORPXCUR(rprof),
		    AP_ORPYCUR(rprof), 1)
            default:
                AP_ORPXCUR(rprof) = wx
                AP_ORPYCUR(rprof) = wy
            }
        }

	# Get the pixels and check for error conditions.
	if (IS_INDEFR(wx) || IS_INDEFR(wy)) {
	    pier = AP_APERT_NOAPERT
	    return (AP_RP_NOPROFILE)
	} else if (IS_INDEFR(AP_SKY_MODE(sky))) {
	    pier = AP_APERT_NOSKYMODE
	    return (AP_RP_NOSKYMODE)
	} else if (ap_rpbuf (ap, im, wx, wy) == AP_RP_NOPROFILE) {
	    pier = AP_APERT_NOAPERT
	    return (AP_RP_NOPROFILE)
	}

	# Do the photometry.
	pier = ap_rmag (ap)

	# Initialize some common variables.
	nxpts = AP_RPNX(rprof)
	nypts = AP_RPNY(rprof)
	nrpts = AP_RPNPTS(rprof)

	# Initialize the radial profile curve fitting.
	step = AP_SCALE(ap) * AP_RPSTEP(rprof)
	order = max (1, min (AP_RPORDER(rprof), nxpts * nypts - 3))
	rmin = 0.0
	rmax = (nrpts - 1) * step
	if (IS_INDEFR(AP_DATAMIN(ap)))
	    datamin = -MAX_REAL
	else
	    datamin = AP_DATAMIN(ap) - AP_SKY_MODE(sky)
	if (IS_INDEFR(AP_DATAMAX(ap)))
	    datamax = MAX_REAL
	else
	    datamax = AP_DATAMAX(ap) - AP_SKY_MODE(sky)

	# Fit the curve.
	call cvinit (cv, SPLINE3, order, rmin, rmax) 
	call asubkr (Memr[AP_RPIX(rprof)], AP_SKY_MODE(sky),
	    Memr[AP_RPIX(rprof)], nxpts * nypts)
	fier = ap_rpmeasure (cv, Memr[AP_RPIX(rprof)], nxpts, nypts,
	    AP_RPXC(rprof), AP_RPYC(rprof), datamin, datamax, rmax,
	    AP_RPNDATA(rprof), AP_RPNBAD(rprof))

	# Perform the rejection cycle.
	if (fier != NO_DEG_FREEDOM && AP_RPNREJECT(rprof) > 0 &&
	    AP_RPKSIGMA(rprof) > 0.0)
	    AP_RPNDATAREJ(rprof) = ap_rpiter (cv, Memr[AP_RPIX(rprof)], nxpts,
	        nypts, AP_RPXC(rprof), AP_RPYC(rprof), rmax, datamin, datamax,
		AP_RPNREJECT(rprof), AP_RPKSIGMA(rprof), fier)
	else
	    AP_RPNDATAREJ(rprof) = 0

	AP_RPNDATA(rprof) = AP_RPNDATA(rprof) - AP_RPNDATAREJ(rprof)
	AP_RPNDATAREJ(rprof) = AP_RPNDATAREJ(rprof) + AP_RPNBAD(rprof)

	# Evaluate the fit.
	if (fier != NO_DEG_FREEDOM) {

	    # Evaluate the profile.
	    do i = 1, nrpts
	        Memr[AP_RPDIST(rprof)+i-1] = (i - 1) * step
	    call cvvector (cv, Memr[AP_RPDIST(rprof)],
	        Memr[AP_INTENSITY(rprof)], nrpts)

	    # Evaluate the integral.
	    call asiinit (asi, II_SPLINE3)
	    call amulr (Memr[AP_RPDIST(rprof)], Memr[AP_INTENSITY(rprof)],
	        Memr[AP_TINTENSITY(rprof)], nrpts)
	    call asifit (asi, Memr[AP_TINTENSITY(rprof)], nrpts)
	    Memr[AP_TINTENSITY(rprof)] = 0.0
	    do i = 2, nrpts
	        Memr[AP_TINTENSITY(rprof)+i-1] = Memr[AP_TINTENSITY(rprof)+
		    i-2] + asigrl (asi, real (i - 1), real (i))
	    call amulkr (Memr[AP_TINTENSITY(rprof)], real (TWOPI) * step,
	        Memr[AP_TINTENSITY(rprof)], nrpts)
	    call asifree (asi)

	    # Normalize the radial profile.
	    inorm = cveval (cv, 0.0)
	    if (inorm != 0.0)
	        call adivkr (Memr[AP_INTENSITY(rprof)], inorm,
	            Memr[AP_INTENSITY(rprof)], nrpts)
	    call apsetr (ap, INORM, inorm)

	    # Normalize the total intensity.
	    tinorm = Memr[AP_TINTENSITY(rprof)+AP_RPNPTS(rprof)-1]
	    if (tinorm != 0.0)
	        call adivkr (Memr[AP_TINTENSITY(rprof)], tinorm,
	            Memr[AP_TINTENSITY(rprof)], nrpts)
	    call apsetr (ap, TNORM, tinorm)

	    # Compute the FWHMPSF.
	    call apsetr (ap, RPFWHM, 2.0 * ap_rphalf (Memr[AP_RPDIST(rprof)],
	        Memr[AP_INTENSITY(rprof)], nrpts))
	}

	# Set the error code and return.
	call cvfree (cv)
	if (fier == NO_DEG_FREEDOM)
	    ier = AP_RP_NPTS_TOO_SMALL
	else if (fier == SINGULAR)
	    ier = AP_RP_SINGULAR

	# Free space.
	return (ier)
end


# AP_RMAG -- Compute the magnitudes for the radial profile

int procedure ap_rmag (ap)

pointer	ap		# pointer to the apphot structure

int	pier, nap
pointer	sp, nse, sky, phot, rprof, aperts
real	datamin, datamax, zmag

begin
	# Initialize some apphot pointers.
	nse = AP_NOISE(ap)
	sky = AP_PSKY(ap)
	phot = AP_PPHOT(ap)
	rprof = AP_RPROF(ap)

	# Allocate working space.
	call smark (sp)
	call salloc (aperts, AP_NAPERTS(phot), TY_REAL)

	# Check for out of bounds apertures.
	call ap_maxap (ap, pier)

	# Define the good data minimum and maximum for photometry.
	if (IS_INDEFR(AP_DATAMIN(ap)))
	    datamin = -MAX_REAL
	else
	    datamin = AP_DATAMIN(ap)
	if (IS_INDEFR(AP_DATAMAX(ap)))
	    datamax = MAX_REAL
	else
	    datamax = AP_DATAMAX(ap)

	# Compute the sums.
	call ap_arrayr (ap, APERTS, Memr[aperts])
	call amulkr (Memr[aperts], AP_SCALE(ap), Memr[aperts], AP_NAPERTS(phot))
	if (IS_INDEFR(AP_DATAMIN(ap)) && IS_INDEFR(AP_DATAMAX(ap))) {
	    call ap_rmmeasure (Memr[AP_RPIX(rprof)], AP_RPNX(rprof),
	        AP_RPNY(rprof), AP_RPXC(rprof), AP_RPYC(rprof), Memr[aperts],
		Memd[AP_SUMS(phot)], Memd[AP_AREA(phot)], AP_NMAXAP(phot))
	    AP_NMINAP(phot) = AP_NMAXAP(phot) + 1
	} else
	    call ap_brmmeasure (Memr[AP_RPIX(rprof)], AP_RPNX(rprof),
	        AP_RPNY(rprof), AP_RPXC(rprof), AP_RPYC(rprof), datamin,
		datamax, Memr[aperts], Memd[AP_SUMS(phot)],
		Memd[AP_AREA(phot)], AP_NMAXAP(phot), AP_NMINAP(phot))

	# Check for bad pixels.
	if ((pier == AP_OK) && (AP_NMINAP(phot) <= AP_NMAXAP(phot)))
	    pier = AP_APERT_BADDATA
	nap = min (AP_NMINAP(phot) - 1, AP_NMAXAP(phot))

	# Compute the magnitudes.
	zmag = AP_ZMAG(phot) + 2.5 * log10 (AP_ITIME(ap))
	if (AP_POSITIVE(ap) == YES)
	    call apcopmags (Memd[AP_SUMS(phot)], Memd[AP_AREA(phot)],
	        Memr[AP_MAGS(phot)], Memr[AP_MAGERRS(phot)], nap,
		AP_SKY_MODE(sky), AP_SKY_SIG(sky), AP_NSKY(sky), zmag,
		AP_NOISEFUNCTION(nse), AP_EPADU(nse))
	else
	    call apconmags (Memd[AP_SUMS(phot)], Memd[AP_AREA(phot)],
	        Memr[AP_MAGS(phot)], Memr[AP_MAGERRS(phot)], nap,
		AP_SKY_MODE(sky), AP_SKY_SIG(sky), AP_NSKY(sky), zmag,
		AP_NOISEFUNCTION(nse), AP_EPADU(nse), AP_READNOISE(nse))

	call sfree (sp)

	return (pier)
end


# AP_RPMEASURE -- Procedure to measure the flux and effective area in a set of
# apertures and accumulate the fit to the radial profile.

int procedure ap_rpmeasure (cv, pixels, nx, ny, wx, wy, datamin, datamax,
	rmax, ndata, nbad)

pointer	cv			# pointer to curfit structure
real	pixels[nx,ARB]		# subraster pixel values
int	nx, ny			# dimensions of the subraster
real	wx, wy			# center of subraster
real	datamin			# minimum good data value
real	datamax			# maximum good data value
real	rmax			# the maximum radius
int	ndata			# the number of ok data points
int	nbad			# the number of bad data points

int	i, j, ier
real	weight, dy2, r2, rcv

begin
	# Initialize.
	ndata = 0
	nbad = 0
	call cvzero (cv)

	# Loop over the pixels.
	do j = 1, ny {
	    dy2 = (j - wy) ** 2
	    do i = 1, nx {
		r2 = (i - wx) ** 2 + dy2
		rcv = sqrt (r2)
		if (rcv > rmax)
		    next
		if (pixels[i,j] < datamin || pixels[i,j] > datamax) {
		    nbad = nbad + 1
		} else {
		    call cvaccum (cv, rcv, pixels[i,j], weight, WTS_UNIFORM)
		    ndata = ndata + 1
		}
	    }
	}

	call cvsolve (cv, ier)
	return (ier)
end


# AP_RPITER -- Procedure to reject pixels from the fit.

int procedure ap_rpiter (cv, pixels, nx, ny, wx, wy, rmax, datamin, datamax,
	niter, ksigma, fier)

pointer	cv		# pointer to the curfit structure
real	pixels[nx,ARB]	# pixel values
int	nx, ny		# dimensions of image subraster
real	wx, wy		# x and y coordinates of the center
real	rmax		# maximum radius value
real	datamin		# minimum good data value
real	datamax		# maximum good data value
int	niter		# maximum number of rejection cycles
real	ksigma		# ksigma rejection limit
int	fier		# fitting error code

int	i, j, k, npts, ntreject, nreject
pointer	sp, rtemp, w, ptr
real	chisqr, diff, locut, hicut
real	cveval()
errchk	cveval, cvrject, cvsolve

begin
	# Allocate the necessary space.
	call smark (sp)
	call salloc (rtemp, nx, TY_REAL)
	call salloc (w, nx * ny, TY_REAL)
	call amovkr (1.0, Memr[w], nx * ny)

	# Set the weights of out of range and bad data points to 0.0.
	ptr = w
	do j = 1, ny {
	    call ap_ijtor (Memr[rtemp], nx, j, wx, wy)
	    do k = 1, nx {
		if (Memr[rtemp+k-1] > rmax || pixels[k,j] < datamin ||
		    pixels[k,j] > datamax)
		    Memr[ptr+k-1] = 0.0
	    }
	    ptr = ptr + nx
	}

	ntreject = 0
	do i = 1, niter { 

	    # Compute the chisqr.
	    chisqr = 0.0
	    npts = 0
	    ptr = w
	    do j = 1, ny {
	    	call ap_ijtor (Memr[rtemp], nx, j, wx, wy)
		do k = 1, nx {
		    if (Memr[ptr+k-1] <= 0.0)
			next
		    chisqr = chisqr + (pixels[k,j] - cveval (cv,
		        Memr[rtemp+k-1])) ** 2
		    npts = npts + 1
		}
		ptr = ptr + nx
	    }

	    # Compute the new limits.
	    if (npts > 1)
	        chisqr = sqrt (chisqr / (npts - 1))
	    else
		chisqr = 0.0
	    locut = - ksigma * chisqr
	    hicut = ksigma * chisqr

	    # Reject pixels from the fit.
	    nreject = 0
	    ptr = w
	    do j = 1, ny {
	    	call ap_ijtor (Memr[rtemp], nx, j, wx, wy)
		do k = 1, nx {
		    if (Memr[ptr+k-1] <= 0.0)
			next
		    diff = pixels[k,j] - cveval (cv, Memr[rtemp+k-1])
		    if (diff >= locut && diff <= hicut)
			next
		    call cvrject (cv, Memr[rtemp+k-1], pixels[k,j], 1.0)
		    nreject = nreject + 1
		    Memr[ptr+k-1] = 0.0
		}
		ptr = ptr + nx
	    }

	    if (nreject == 0)
	        break
	    ntreject = ntreject + nreject

	    # Recompute the fit.
	    call cvsolve (cv, fier)
	    if (fier == NO_DEG_FREEDOM)
		break
	}


	call sfree (sp)

	return (ntreject)
end


# AP_RPHALF -- Compute the FWHM of the PSF.

real procedure ap_rphalf (radius, intensity, npts)

real	radius[ARB]		# radius in pixels
real	intensity[ARB]		# profile intensity
int	npts			# number of points

int	i
real	halfp

begin
	# Seach for the appropriate interval.
	do i = 1, npts
	    if (intensity[i] < 0.5)
		break

	# Compute the full width half maximum.
	if (i == 1)
	    halfp = radius[1]
	else if (i == npts && intensity[npts] >= 0.5)
	    halfp = radius[npts]
	else
	    halfp = (radius[i] * (0.5 - intensity[i-1]) + radius[i-1] *
		(intensity[i] - 0.5)) / (intensity[i] - intensity[i-1])

	return (halfp)
end