aboutsummaryrefslogtreecommitdiff
path: root/noao/imred/ccdred/src/scancor.x
blob: 6a5eb84caa752e07fe15ed1a8a88d23ca157a2a2 (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
include	<imhdr.h>
include	<imset.h>

define	SCANTYPES	"|shortscan|longscan|"
define	SHORTSCAN	1	# Short scan accumulation, normal readout
define	LONGSCAN	2	# Long scan continuous readout

# SCANCOR -- Create a scanned image from an unscanned image.

procedure scancor (input, output, nscan, minreplace)

char	input[ARB]		# Input image
char	output[ARB]		# Output image (must be new image)
int	nscan			# Number of scan lines
real	minreplace		# Minmum value of output

int	scantype		# Type of scan format
int	readaxis		# Readout axis

int	clgwrd()
pointer	sp, str, in, out, immap()
errchk	immap

begin
	call smark (sp)
	call salloc (str, SZ_LINE, TY_CHAR)

	# Determine readout axis and create the temporary output image.
	scantype = clgwrd ("scantype", Memc[str], SZ_LINE, SCANTYPES)
	readaxis = clgwrd ("readaxis", Memc[str], SZ_LINE, "|lines|columns|")

	# Make the output scanned image.
	in = immap (input, READ_ONLY, 0)
	call set_output (in, out, output)

	switch (scantype) {
	case SHORTSCAN:
	    call shortscan (in, out, nscan, minreplace, readaxis)
	case LONGSCAN:
	    call longscan (in, out, readaxis)
	}
		
	# Log the operation.
	switch (scantype) {
	case SHORTSCAN:
	    call sprintf (Memc[str], SZ_LINE,
		"Converted to shortscan from %s with nscan=%d")
	    call pargstr (input)
	    call pargi (nscan)
	    call hdmputi (out, "nscanrow", nscan)
	case LONGSCAN:
	    call sprintf (Memc[str], SZ_LINE, "Converted to longscan from %s")
	    call pargstr (input)
	}
	call timelog (Memc[str], SZ_LINE)
	call ccdlog (out, Memc[str])
	call hdmpstr (out, "scancor", Memc[str])

	call imunmap (in)
	call imunmap (out)

	call sfree (sp)
end


# SHORTSCAN -- Make a shortscan mode image by using a moving average.
#
# NOTE!! The value of nscan used here is increased by 1 because the
# current information in the image header is actually the number of
# scan steps and NOT the number of rows.

procedure shortscan (in, out, nscan, minreplace, readaxis)

pointer	in		# Input image
pointer	out		# Output image
int	nscan		# Number of lines scanned before readout
real	minreplace	# Minimum output value
int	readaxis	# Readout axis

bool	replace
real	nscanr, sum, mean, asumr()
int	i, j, k, l, len1, len2, nc, nl, nscani, c1, c2, cs, l1, l2, ls
pointer	sp, str, bufs, datain, dataout, data, imgl2r(), impl2r()
long	clktime()
errchk	malloc, calloc

begin
	call smark (sp)
	call salloc (str, SZ_LINE, TY_CHAR)

	# The default data section is the entire image.
	len1 = IM_LEN(in,1)
	len2 = IM_LEN(in,2)
	c1 = 1
	c2 = len1
	cs = 1
	l1 = 1
	l2 = len2
	ls = 1
	call hdmgstr (in, "datasec", Memc[str], SZ_LINE)
	call ccd_section (Memc[str], c1, c2, cs, l1, l2, ls)
	if ((c1<1)||(c2>len1)||(l1<1)||(l2>len2)||(cs!=1)||(ls!=1))
	    call error (0, "Error in DATASEC parameter")
	nc = c2 - c1 + 1
	nl = l2 - l1 + 1

	# Copy initial lines.
	do i = 1, l1 - 1
	    call amovr (Memr[imgl2r(in,i)], Memr[impl2r(out,i)], len1)

	replace = !IS_INDEF(minreplace)
	mean = 0.
	switch (readaxis) {
	case 1:
	    nscani = max (1, min (nscan, nl) + 1)
	    nscanr = nscani
	    call imseti (in, IM_NBUFS, nscani)
	    call malloc (bufs, nscani, TY_INT)
	    call calloc (data, nc, TY_REAL)
	    j = 1
	    k = 1
	    l = 1

	    # Ramp up
	    while (j <= nscani) {
		i = j + l1 - 1
		datain = imgl2r (in, i)
		if (nc < len1)
		    call amovr (Memr[datain], Memr[impl2r(out,i)], len1)
		datain = datain + c1 - 1
		Memi[bufs+mod(j,nscani)] = datain
	        call aaddr (Memr[data], Memr[datain], Memr[data], nc)
	        j = j + 1
	    }
	    dataout = impl2r (out, l+l1-1) + c1 - 1
	    call adivkr (Memr[data], nscanr, Memr[dataout], nc)
	    if (replace)
		call amaxkr (Memr[dataout], minreplace, Memr[dataout], nc)
	    mean = mean + asumr (Memr[dataout], nc)
	    l = l + 1

	    # Moving average
	    while (j <= nl) {
		datain = Memi[bufs+mod(k,nscani)]
	        call asubr (Memr[data], Memr[datain], Memr[data], nc)
		i = j + l1 - 1
		datain = imgl2r (in, i)
		if (nc < len1)
		    call amovr (Memr[datain], Memr[impl2r(out,i)], len1)
		datain = datain + c1 - 1
		Memi[bufs+mod(j,nscani)] = datain
	        call aaddr (Memr[data], Memr[datain], Memr[data], nc)
		dataout = impl2r (out, l+l1-1) + c1 - 1
		call adivkr (Memr[data], nscanr, Memr[dataout], nc)
		if (replace)
		    call amaxkr (Memr[dataout], minreplace, Memr[dataout], nc)
		mean = mean + asumr (Memr[dataout], nc)

	        j = j + 1
	        k = k + 1
	        l = l + 1
	    }

	    # Ramp down.
	    while (l <= nl) {
		datain = Memi[bufs+mod(k,nscani)]
	        call asubr (Memr[data], Memr[datain], Memr[data], nc)
		dataout = impl2r (out, l+l1-1) + c1 - 1
		call adivkr (Memr[data], nscanr, Memr[dataout], nc)
		if (replace)
		    call amaxkr (Memr[dataout], minreplace, Memr[dataout], nc)
		mean = mean + asumr (Memr[dataout], nc)

	        k = k + 1
	        l = l + 1
	    }

	    call mfree (bufs, TY_INT)
	    call mfree (data, TY_REAL)

	case 2:
	    nscani = max (1, min (nscan, nc) + 1)
	    nscanr = nscani
	    do i = 1, nl {
	        datain = imgl2r (in, i + l1 - 1)
		datain = datain + c1 - 1
	        data = impl2r (out, i + l1 - 1)
		call amovr (Memr[datain], Memr[data], len1)
		datain = datain + c1 - 1
		data = data + c1 - 1
		sum = 0
		j = 0
		k = 0
		l = 0

		# Ramp up
		while (j < nscani) {
		    sum = sum + Memr[datain+j]
		    j = j + 1
		}
		if (replace)
		    Memr[data] = max (minreplace, sum / nscani)
		else
		    Memr[data] = sum / nscani
		mean = mean + Memr[data]
		l = l + 1

		# Moving average
		while (j < nl) {
		    sum = sum + Memr[datain+j] - Memr[datain+k]
		    if (replace)
			Memr[data+l] = max (minreplace, sum / nscani)
		    else
			Memr[data+l] = sum / nscani
		    mean = mean + Memr[data+l]
		    j = j + 1
		    k = k + 1
		    l = l + 1
		}

		# Ramp down
		while (l < nl) {
		    sum = sum - Memr[datain+k]
		    if (replace)
			Memr[data+l] = max (minreplace, sum / nscani)
		    else
			Memr[data+l] = sum / nscani
		    mean = mean + Memr[data+l]
		    k = k + 1
		    l = l + 1
		}
	    }
	}

	# Copy final lines.
	do i = l2+1, len2
	    call amovr (Memr[imgl2r(in,i)], Memr[impl2r(out,i)], len1)

	mean = mean / nc / nl
	call hdmputr (out, "ccdmean", mean)
	call hdmputi (out, "ccdmeant", int (clktime (long (0))))

	call sfree (sp)
end


# LONGSCAN -- Make a longscan mode readout flat field correction by averaging
# across the readout axis.

procedure longscan (in, out, readaxis)

pointer	in		# Input image
pointer	out		# Output image
int	readaxis	# Readout axis

int	i, nc, nl, c1, c2, cs, l1, l2, ls
int	in_c1, in_c2, in_l1, in_l2, ccd_c1, ccd_c2, ccd_l1, ccd_l2
real	mean, asumr()
long	clktime()
pointer	sp, str, data, imgl2r(), impl2r(), imps2r()

begin
	call smark (sp)
	call salloc (str, SZ_LINE, TY_CHAR)

	# The default data section is the entire image.
	nc = IM_LEN(in,1)
	nl = IM_LEN(in,2)
	c1 = 1
	c2 = nc
	cs = 1
	l1 = 1
	l2 = nl
	ls = 1
	call hdmgstr (in, "datasec", Memc[str], SZ_LINE)
	call ccd_section (Memc[str], c1, c2, cs, l1, l2, ls)
	if ((c1<1)||(c2>nc)||(l1<1)||(l2>nl)||(cs!=1)||(ls!=1))
	    call error (0, "Error in DATASEC parameter")
	in_c1 = c1
	in_c2 = c2
	in_l1 = l1
	in_l2 = l2

	# The default ccd section is the data section.
	call hdmgstr (in, "ccdsec", Memc[str], SZ_LINE)
	call ccd_section (Memc[str], c1, c2, cs, l1, l2, ls)
	if ((cs != 1) || (ls != 1))
	    call error (0, "Error in CCDSEC parameter")
	ccd_c1 = c1
	ccd_c2 = c2
	ccd_l1 = l1
	ccd_l2 = l2
	if ((in_c2-in_c1 != ccd_c2-ccd_c1) || (in_l2-in_l1 != ccd_l2-ccd_l1))
	    call error (0, "Size of DATASEC and CCDSEC do not agree")

	switch (readaxis) {
	case 1:
	    IM_LEN(out,2) = 1
	    data = impl2r (out, 1)
	    call aclrr (Memr[data], nc)
	    nc = in_c2 - in_c1 + 1
	    nl = in_l2 - in_l1 + 1
	    data = data + in_c1 - 1
	    do i = in_l1, in_l2
		call aaddr (Memr[imgl2r(in,i)+in_c1-1], Memr[data],
		    Memr[data], nc)
	    call adivkr (Memr[data], real (nl), Memr[data], nc)
	    call sprintf (Memc[str], SZ_LINE, "[%d:%d,1:1]")
		call pargi (in_c1)
		call pargi (in_c2)
	    call hdmpstr (out, "datasec", Memc[str])
	    call sprintf (Memc[str], SZ_LINE, "[%d:%d,*]")
		call pargi (ccd_c1)
		call pargi (ccd_c2)
	    call hdmpstr (out, "ccdsec", Memc[str])
	    mean = asumr (Memr[data], nc) / nl
	case 2:
	    IM_LEN(out,1) = 1
	    data = imps2r (out, 1, 1, 1, nl)
	    call aclrr (Memr[data], nl)
	    nc = in_c2 - in_c1 + 1
	    nl = in_l2 - in_l1 + 1
	    do i = in_l1, in_l2
		Memr[data+i-1] = asumr (Memr[imgl2r(in,i)+in_c1-1], nc) / nc
	    call sprintf (Memc[str], SZ_LINE, "[1:1,%d:%d]")
		call pargi (in_l1)
		call pargi (in_l2)
	    call hdmpstr (out, "datasec", Memc[str])
	    call sprintf (Memc[str], SZ_LINE, "[*,%d:%d]")
		call pargi (ccd_l1)
		call pargi (ccd_l2)
	    call hdmpstr (out, "ccdsec", Memc[str])
	    mean = asumr (Memr[data], nl) / nc
	}

	call hdmputr (out, "ccdmean", mean)
	call hdmputi (out, "ccdmeant", int (clktime (long (0))))

	call sfree (sp)
end