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
|
include <math/iminterp.h>
include <mach.h>
include "apertures.h"
# Background fitting types
define BACKGROUND "|none|average|median|minimum|fit|"
define B_NONE 1
define B_AVERAGE 2
define B_MEDIAN 3
define B_MINIMUM 4
define B_FIT 5
define NSAMPLE 20 # Maximum number of background sample regions
# AP_SKYEVAL -- Evaluate sky within aperture.
#
# The sky pixels specified by the background sample string are used to
# determine a sky function at each line which is then evaluated for each
# pixel in the aperture as given by the SBUF array with starting offsets
# given by XS. The fitting consists of either a straight average or a
# function fit using ICFIT. The sky regions are specified relative to the
# aperture center. To avoid systematics due to shifting of the aperture
# relative to the integer pixel positions the sky regions are linearly
# interpolated. The average uses the integral of the interpolation
# function within the sample region endpoints. The fit samples the
# interpolation on a pixel grid with the aperture exactly centered on
# a pixel. A crude sky variance is computed for each line based solely
# on the variance model and the square root of the number of "pixels"
# used for the fit. This variance is used to boost the variance of
# the sky subtracted spectrum during variance weighting. Because sky
# noise may be significant in short slits a box car smoothing may be
# used giving a lower variance per pixel but bad errors near sky lines.
# An unweighted aperture sum of the sky is returned in case the user
# wants to save the subtracted 1D sky spectrum.
procedure ap_skyeval (im, ap, dbuf, nc, nl, c1, l1, sbuf, svar, sky, nx, ny,
xs, ys, nsubaps, rdnoise)
pointer im # IMIO pointer
pointer ap # Aperture structure
pointer dbuf # Data buffer
int nc, nl # Size of data buffer
int c1, l1 # Origin of data buffer
real sbuf[nx,ny] # Sky values
real svar[ny] # Sky variances
real sky[ny,nsubaps] # Extracted sky (out)
int nx, ny # Size of profile array
int xs[ny], ys # Origin of profile array
int nsubaps # Number of subapertures
real rdnoise # Readout noise in RMS data numbers.
int bkg # Background type
int skybox # Sky box car smoothing
int i, j, ix1, ix2, nsample, nsky, nfit, ix, iy
real center, xmin, xmax, a, b, c, s, avg
pointer ic, cv, cv1, asi, sp, str, data, as, bs, x, y, w
int apgwrd(), apgeti(), ctor()
real ic_getr(), ap_cveval(), asieval(), asigrl(), amedr()
errchk salloc, ic_fit
begin
call smark (sp)
call salloc (str, SZ_LINE, TY_CHAR)
# Get CL parameters and set shift and fitting function pointers.
bkg = apgwrd ("background", Memc[str], SZ_LINE, BACKGROUND)
skybox = apgeti ("skybox")
cv = AP_CV(ap)
ic = AP_IC(ap)
# Set center and maximum limits relative to data buffer.
# The limits are required to overlap the aperture and include
# an extra point at each end for interpolation. Shifts
# and boundary limits will be enforced later.
i = AP_AXIS(ap)
center = AP_CEN(ap,i)
xmin = center + min (AP_LOW(ap,i), ic_getr (ic, "xmin"))
xmax = center + max (AP_HIGH(ap,i), ic_getr (ic, "xmax"))
ix1 = nint (xmin) - 1
ix2 = nint (xmax) + 1
nfit = ix2 - ix1 + 1
# Allocate memory and parse sample string.
# The colons in the sample string must be changed to avoid
# sexigesimal interpretation.
call salloc (as, NSAMPLE, TY_REAL)
call salloc (bs, NSAMPLE, TY_REAL)
call ic_gstr (ic, "sample", Memc[str], SZ_LINE)
for (i=str; Memc[i]!=EOS; i=i+1)
if (Memc[i] == ':')
Memc[i] = '$'
nsample = 0
for (i=1; Memc[str+i-1]!=EOS; i=i+1) {
if (ctor (Memc[str], i, a) > 0) {
i = i - 1
if (Memc[str+i] == '$') {
i = i + 2
if (ctor (Memc[str], i, b) > 0) {
i = i - 1
Memr[as+nsample] = center + min (a, b)
Memr[bs+nsample] = center + max (a, b)
nsample = nsample + 1
if (nsample == NSAMPLE)
break
}
}
}
}
if (nsample == 0) {
Memr[as] = xmin
Memr[bs] = xmax
nsample = 1
}
if (bkg == B_MEDIAN)
call salloc (y, nfit, TY_REAL)
else if (bkg == B_FIT) {
call salloc (x, nfit, TY_REAL)
call salloc (y, nfit, TY_REAL)
call salloc (w, nfit, TY_REAL)
}
# Initialize the image interpolator.
call asiinit (asi, II_LINEAR)
# Determine sky at each dispersion point.
call aclrr (svar, ny)
do iy = 1, ny {
# Fit image interpolation function including extra points
# and apply image boundary limits.
i = iy + ys - 1
s = ap_cveval (cv, real (i))
ix1 = max (c1, nint (xmin + s) - 1)
ix2 = min (c1+nc-1, nint (xmax + s) + 1)
nfit = ix2 - ix1 + 1
if (nfit < 3) {
call aclrr (sbuf[1,iy], nx)
svar[iy] = 0.
next
}
data = dbuf + (i - l1) * nc + ix1 - c1
if (bkg == B_AVERAGE || bkg == B_FIT) {
iferr (call asifit (asi, Memr[data], nfit)) {
call aclrr (sbuf[1,iy], nx)
svar[iy] = 0.
next
}
}
# Determine background
switch (bkg) {
case B_AVERAGE:
# The background is computed by integrating the interpolator
avg = 0.
nsky = 0
c = 0.
for (i=0; i < nsample; i=i+1) {
a = max (real (ix1), Memr[as+i] + s) - ix1 + 1
b = min (real (ix2), Memr[bs+i] + s) - ix1 + 1
if (b - a > 0.) {
avg = avg + asigrl (asi, a, b)
c = c + b - a
nsky = nsky + nint (b) - nint(a) + 1
}
}
if (c > 0.)
avg = avg / c
call amovkr (avg, sbuf[1,iy], nx)
if (nsky > 1)
svar[iy] = max (0., (rdnoise + avg) / (nsky - 1))
case B_MEDIAN:
# The background is computed by the median pixel
avg = 0.
nsky = 0
for (i=0; i < nsample; i=i+1) {
a = max (real (ix1), Memr[as+i] + s) - ix1 + 1
b = min (real (ix2), Memr[bs+i] + s) - ix1 + 1
do j = nint (a), nint (b) {
Memr[y+nsky] = Memr[data+j-1]
nsky = nsky + 1
}
}
if (nsky > 0)
avg = amedr (Memr[y], nsky)
call amovkr (avg, sbuf[1,iy], nx)
if (nsky > 1)
svar[iy] = max (0., (rdnoise + avg) / (nsky - 1))
case B_MINIMUM:
# The background is computed by the minimum pixel
avg = MAX_REAL
nsky = 0
for (i=0; i < nsample; i=i+1) {
a = max (real (ix1), Memr[as+i] + s) - ix1 + 1
b = min (real (ix2), Memr[bs+i] + s) - ix1 + 1
do j = nint (a), nint (b) {
avg = min (avg, Memr[data+j-1])
nsky = nsky + 1
}
}
if (nsky == 0)
avg = 0
call amovkr (avg, sbuf[1,iy], nx)
if (nsky > 1)
svar[iy] = max (0., (rdnoise + avg) / (nsky - 1))
case B_FIT:
# The fitting is done in a coordinate system relative to
# aperture center.
c = center + s
a = ix1 + c - int (c)
do i = 1, nfit-1 {
Memr[x+i-1] = nint (1000. * (a - c)) / 1000.
Memr[y+i-1] = asieval (asi, a-ix1+1)
Memr[w+i-1] = 1.
a = a + 1.
}
iferr {
call ic_fit (ic, cv1, Memr[x], Memr[y], Memr[w], nfit-1,
YES, YES, YES, YES)
avg = 0.
do i = 1, nx {
a = xs[iy] + i - 1
b = ap_cveval (cv1, a - c)
avg = avg + b
sbuf[i,iy] = b
}
avg = avg / nx
} then {
avg = 0.
call aclrr (sbuf[1,iy], nx)
}
nsky = 0.
for (i=0; i < nsample; i=i+1) {
a = max (real (ix1), Memr[as+i] + s) - ix1 + 1
b = min (real (ix2), Memr[bs+i] + s) - ix1 + 1
nsky = nsky + nint (b) - nint (a) + 1
}
if (nsky > 1)
svar[iy] = max (0., (rdnoise + avg) / (nsky - 1))
}
}
# Do box car smoothing if desired.
if (skybox > 1) {
ix2 = skybox ** 2
avg = 0.
a = 0.
iy = 1
for (i=1; i<=skybox; i=i+1) {
avg = avg + sbuf[1,i]
a = a + svar[i]
}
for (; i<=ny; i=i+1) {
b = sbuf[1,iy]
c = svar[iy]
sbuf[1,iy] = avg / skybox
svar[iy] = a / ix2
avg = avg + sbuf[1,i] - b
a = a + svar[i] - c
iy = iy + 1
}
sbuf[1,iy] = avg / skybox
svar[iy] = a / ix2
i = ny - skybox + 1
for (iy=ny; iy > ny-skybox/2; iy=iy-1)
svar[iy] = svar[i]
for (; i > 1; i=i-1) {
svar[iy] = svar[i]
iy = iy - 1
}
for (; iy > 1; iy=iy-1)
svar[iy] = svar[1]
switch (bkg) {
case B_AVERAGE, B_MEDIAN, B_MINIMUM:
i = ny - skybox + 1
for (iy=ny; iy > ny-skybox/2; iy=iy-1)
call amovkr (sbuf[1,i], sbuf[1,iy], nx)
for (; i > 1; i=i-1) {
call amovkr (sbuf[1,i], sbuf[1,iy], nx)
iy = iy - 1
}
for (; iy > 1; iy=iy-1)
call amovkr (sbuf[1,1], sbuf[1,iy], nx)
case B_FIT:
i = ny - skybox + 1
for (iy=ny; iy > ny-skybox/2; iy=iy-1)
sbuf[1,iy] = sbuf[1,i]
for (; i > 1; i=i-1) {
sbuf[1,iy] = sbuf[1,i]
iy = iy - 1
}
for (; iy > 1; iy=iy-1)
sbuf[1,iy] = sbuf[1,1]
do ix1 = 2, nx {
avg = 0.
iy = 1
for (i=1; i<=skybox; i=i+1)
avg = avg + sbuf[ix1,i]
for (; i<=ny; i=i+1) {
b = sbuf[ix1,iy]
sbuf[ix1,iy] = avg / skybox
avg = avg + sbuf[ix1,i] - b
iy = iy + 1
}
sbuf[ix1,iy] = avg / skybox
i = ny - skybox + 1
for (iy=ny; iy > ny-skybox/2; iy=iy-1)
sbuf[ix1,iy] = sbuf[ix1,i]
for (; i > 1; i=i-1) {
sbuf[ix1,iy] = sbuf[ix1,i]
iy = iy - 1
}
for (; iy > 1; iy=iy-1)
sbuf[ix1,iy] = sbuf[ix1,1]
}
}
}
# Compute the unweighted aperture sky spectrum.
i = AP_AXIS(ap)
a = AP_CEN(ap,i) + AP_LOW(ap,i)
b = AP_CEN(ap,i) + AP_HIGH(ap,i)
c = (b - a) / nsubaps
do iy = 1, ny {
data = dbuf + (iy + ys - 1 - l1) * nc + xs[iy] - c1 - 1
s = ap_cveval (cv, real (iy + ys - 1)) - c1 + 1
do i = 1, nsubaps {
xmin = max (0.5, a + (i - 1) * c + s) + c1 - xs[iy]
xmax = min (nc + 0.49, a + i * c + s) + c1 - xs[iy]
if (xmin >= xmax) {
sky[iy,i] = 0.
next
}
ix1 = nint (xmin)
ix2 = nint (xmax)
if (ix1 == ix2)
sky[iy,i] = (xmax - xmin) * sbuf[ix1,iy]
else {
sky[iy,i] = (ix1 - xmin + 0.5) * sbuf[ix1,iy]
sky[iy,i] = sky[iy,i] + (xmax - ix2 + 0.5) * sbuf[ix2,iy]
}
do ix = ix1+1, ix2-1
sky[iy,i] = sky[iy,i] + sbuf[ix,iy]
}
}
if (bkg == B_FIT)
call cvfree (cv1)
call asifree (asi)
call sfree (sp)
end
|