aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/apphot/daofind/apconvolve.x
blob: 0c6f58aa929cf85e8073f4e1d5d5471edaa3560f (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
include <imhdr.h>
include <imset.h>
include "../lib/find.h"

# AP_FCONVOLVE --  Solve for the density enhancement image and optionally
# the sky enhancement image in the case where datamin and datamax are not
# defined.

procedure ap_fconvolve (im, den, sky, kernel1, kernel2, skip, nxk, nyk, const2)

pointer	im			# pointer to the input image
pointer	den			# pointer to the output density image
pointer	sky			# pointer to the output sky image
real	kernel1[nxk,nyk]	# the first convolution kernel
real	kernel2[nxk,nyk]	# the second convolution kernel
int	skip[nxk,nyk]		# the skip array
int	nxk, nyk		# dimensions of the kernel
real	const2			# subtraction constant for the skyimage

int	i, ncols, nlines, col1, col2, inline, outline
pointer	sp, lineptrs, outbuf1, outbuf2
pointer	imgs2r(), impl2r()
errchk	imgs2r, impl2r, imflush

begin
	# Set up an array of linepointers.
	call smark (sp)
	call salloc (lineptrs, nyk, TY_POINTER)

	# Set the number of image buffers.
	call imseti (im, IM_NBUFS, nyk)

	ncols = IM_LEN(den,1)
	nlines = IM_LEN(den,2)

	# Set input image column limits.
	col1 = 1 - nxk / 2
	col2 = IM_LEN(im,1) + nxk / 2

	# Initialise the line buffers.
	inline = 1 - nyk / 2
	do i = 1 , nyk - 1 {
	    Memi[lineptrs+i] = imgs2r (im, col1, col2, inline, inline)
	    inline = inline + 1
	}

	# Generate the output image line by line.
	do outline = 1, nlines {

	    # Scroll the input buffers.
	    do i = 1, nyk - 1
		Memi[lineptrs+i-1] = Memi[lineptrs+i]

	    # Read in new image line.
	    Memi[lineptrs+nyk-1] = imgs2r (im, col1, col2, inline,
	        inline)

	    # Get first output image line.
	    outbuf1 = impl2r (den, outline)
	    if (outbuf1 == EOF)
		call error (0, "Error writing first output image.")

	    # Generate first output image line.
	    call aclrr (Memr[outbuf1], ncols)
	    do i = 1, nyk
		call ap_skcnvr (Memr[Memi[lineptrs+i-1]], Memr[outbuf1],
		    ncols, kernel1[1,i], skip[1,i], nxk)

	    if (sky != NULL) {

	        # Get second output image line.
	        outbuf2 = impl2r (sky, outline)
	        if (outbuf2 == EOF)
		    call error (0, "Error writing second output image.")

	        # Generate second output image line.
	        call aclrr (Memr[outbuf2], ncols)
	        do i = 1, nyk
		    call ap_skcnvr (Memr[Memi[lineptrs+i-1]], Memr[outbuf2],
		        ncols, kernel2[1,i], skip[1,i], nxk)
		call ap_w1sur (Memr[outbuf2], Memr[outbuf1], Memr[outbuf2],
		    ncols, -const2)
	    }

	    inline = inline + 1
	}

	# Flush the output image(s).
	call imflush (den)
	if (sky != NULL)
	    call imflush (sky)

	# Free the image buffer pointers.
	call sfree (sp)
end


# AP_GCONVOLVE --  Solve for the density enhancement image and optionally
# the sky enhancement image in the case where datamin and datamax are defined.

procedure ap_gconvolve (im, den, sky, kernel1, skip, nxk, nyk, gsums,
	datamin, datamax)

pointer	im			# pointer to the input image
pointer	den			# pointer to the output density image
pointer	sky			# pointer to the output sky image
real	kernel1[nxk,nyk]	# the first convolution kernel
int	skip[nxk,nyk]		# the sky array
int	nxk, nyk		# dimensions of the kernel
real	gsums[ARB]		# array of kernel sums
real	datamin, datamax	# the good data minimum and maximum

int	i, ncols, nlines, col1, col2, inline, outline
pointer	sp, lineptrs, sd, sgd, sg, sgsq, p, outbuf2
pointer	imgs2r(), impl2r()
errchk	imgs2r, impl2r, imflush

begin
	# Set up an array of linepointers.
	call smark (sp)
	call salloc (lineptrs, nyk, TY_POINTER)

	# Set the number of image buffers.
	call imseti (im, IM_NBUFS, nyk)

	ncols = IM_LEN(den,1)
	nlines = IM_LEN(den,2)

	# Allocate some working space.
	call salloc (sd, ncols, TY_REAL)
	call salloc (sgsq, ncols, TY_REAL)
	call salloc (sg, ncols, TY_REAL)
	call salloc (p, ncols, TY_REAL)

	# Set input image column limits.
	col1 = 1 - nxk / 2
	col2 = IM_LEN(im,1) + nxk / 2

	# Initialise the line buffers.
	inline = 1 - nyk / 2
	do i = 1 , nyk - 1 {
	    Memi[lineptrs+i] = imgs2r (im, col1, col2, inline, inline)
	    inline = inline + 1
	}

	# Generate the output image line by line.
	do outline = 1, nlines {

	    # Scroll the input buffers.
	    do i = 1, nyk - 1
		Memi[lineptrs+i-1] = Memi[lineptrs+i]

	    # Read in new image line.
	    Memi[lineptrs+nyk-1] = imgs2r (im, col1, col2, inline,
	        inline)

	    # Get first output image line.
	    sgd = impl2r (den, outline)
	    if (sgd == EOF)
		call error (0, "Error writing first output image.")

	    # Generate first output image line.
	    call aclrr (Memr[sgd], ncols)
	    call aclrr (Memr[sd], ncols)
	    call amovkr (gsums[GAUSS_SUMG], Memr[sg], ncols)
	    call amovkr (gsums[GAUSS_SUMGSQ], Memr[sgsq], ncols)
	    call amovkr (gsums[GAUSS_PIXELS], Memr[p], ncols)
	    do i = 1, nyk
		call ap_gdsum (Memr[Memi[lineptrs+i-1]], Memr[sgd], Memr[sd],
		    Memr[sg], Memr[sgsq], Memr[p], ncols, kernel1[1,i],
		    skip[1,i], nxk, datamin, datamax)
	    call ap_gdavg (Memr[sgd], Memr[sd], Memr[sg], Memr[sgsq],
	        Memr[p], ncols, gsums[GAUSS_PIXELS], gsums[GAUSS_DENOM],
		gsums[GAUSS_SGOP])

	    if (sky != NULL) {

	        # Get second output image line.
	        outbuf2 = impl2r (sky, outline)
	        if (outbuf2 == EOF)
		    call error (0, "Error writing second output image.")

	        # Generate second output image line.
		call ap_davg (Memr[sd], Memr[sgd], Memr[sg], Memr[p],
		    Memr[outbuf2], ncols)
	    }

	    inline = inline + 1
	}

	# Flush the output image(s).
	call imflush (den)
	if (sky != NULL)
	    call imflush (sky)

	# Free the image buffer pointers.
	call sfree (sp)
end


# AP_SKCNVR -- Compute the convolution kernel using a skip array.

procedure ap_skcnvr (in, out, npix, kernel, skip, nk)

real	in[npix+nk-1]		# the input vector
real	out[npix]		# the output vector
int	npix			# the size of the vector
real	kernel[ARB]		# the convolution kernel
int	skip[ARB]		# the skip array
int	nk			# the size of the convolution kernel

int	i, j
real	sum

begin
	do i = 1, npix {
	    sum = out[i]
	    do j = 1, nk {
		if (skip[j] == YES)
		    next
		sum = sum + in[i+j-1] * kernel[j]
	    }
	    out[i] = sum
	}
end


# AP_GDSUM -- Compute the vector sums required to do the convolution.

procedure  ap_gdsum (in, sgd, sd, sg, sgsq, p, npix, kernel, skip, nk,
	datamin, datamax)

real	in[npix+nk-1]		# the input vector
real	sgd[ARB]		# the computed input/output convolution vector
real	sd[ARB]			# the computed input/output sum vector
real	sg[ARB]			# the input/ouput first normalization factor
real	sgsq[ARB]		# the input/ouput second normalization factor
real	p[ARB]			# the number of points vector
int	npix			# the size of the vector
real	kernel[ARB]		# the convolution kernel
int	skip[ARB]		# the skip array
int	nk			# the size of the convolution kernel
real	datamin, datamax	# the good data limits.

int	i, j
real	data

begin
	do i = 1, npix {
	    do j = 1, nk {
		if (skip[j] == YES)
		    next
		data = in[i+j-1]
		if (data < datamin || data > datamax) {
		    sgsq[i] = sgsq[i] - kernel[j] ** 2
		    sg[i] = sg[i] - kernel[j]
		    p[i] = p[i] - 1.0
		} else {
		    sgd[i] = sgd[i] + kernel[j] * data 
		    sd[i] = sd[i] + data
		}
	    }
	}
end


# AP_GDAVG -- Compute the vector averages required to do the convolution.

procedure  ap_gdavg (sgd, sd, sg, sgsq, p, npix, pixels, denom, sgop)

real	sgd[ARB]		# the computed input/output convolution vector
real	sd[ARB]			# the computed input/output sum vector
real	sg[ARB]			# the input/ouput first normalization factor
real	sgsq[ARB]		# the input/ouput second normalization factor
real	p[ARB]			# the number of points vector
int	npix			# the size of the vector
real	pixels			# number of pixels
real	denom			# kernel normalization factor
real	sgop			# kernel normalization factor

int	i

begin
	do i = 1, npix {
	    if (p[i] > 1.5) {
		if (p[i] < pixels) {
		    sgsq[i] = sgsq[i] - (sg[i] ** 2) / p[i]
		    if (sgsq[i] != 0.0)
			sgd[i] = (sgd[i] - sg[i] * sd[i] / p[i]) / sgsq[i]
		    else
			sgd[i] = 0.0
		} else
		    sgd[i] = (sgd[i] - sgop * sd[i]) / denom
	    } else
		sgd[i] = 0.0
	}
end


# AP_DAVG -- Generate the results the optional sky output image.

procedure ap_davg (sd, sgd, sg, p, out, npix)

real	sd[ARB]			# the computed input/output sum vector
real	sgd[ARB]		# the computed input/output convolution vector
real	sg[ARB]			# the input/ouput first normalization factor
real	p[ARB]			# the number of points vector
real	out[ARB]		# the output array
int	npix			# the size of the vector

int	i

begin
	do i = 1, npix {
	    if (p[i] > 0.0)
	        out[i] = (sd[i] - sgd[i] * sg[i]) / p[i]
	    else
		out[i] = 0.0
	}
end