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
|
include <imhdr.h>
include <pkg/gtools.h>
include <pkg/xtanswer.h>
# T_RESPONSE -- Determine the response function for 2D spectra.
#
# A calibration image is divided by a normalization spectrum to form
# a response image. The normalization spectrum is derived by averaging
# the normalization image across dispersion. The normalization spectrum
# is then smoothed by curve fitting. The smoothed normalization
# spectrum is divided into the calibration image to form the response
# function image. The curve fitting may be performed interactively
# using the icfit package. A response function is determined for each
# input image. Image sections in the calibration image may be used to determine
# the response for only part of an image such as with multiple slits.
# CL callable task.
#
# The images are given by image templates. The number of images must
# in each list must match. Image sections are allowed in the calibration
# image.
procedure t_response ()
int list1 # List of calibration images
int list2 # List of normalization images
int list3 # List of response images
real threshold # Response threshold
int naverage # Sample averaging size
int order # Order of curve fitting function
real low_reject, high_reject # Rejection thresholds
int niterate # Number of rejection iterations
real grow # Rejection growing radius
int interactive # Interactive?
pointer cal, norm, resp, ic, gt
pointer sp, image1, image2, image3, history
int clgeti(), imtopen(), imtgetim(), imtlen(), gt_init(), ic_geti()
bool clgetb()
real clgetr(), ic_getr()
pointer immap()
errchk immap, ls_immap
begin
call smark (sp)
call salloc (image1, SZ_LINE, TY_CHAR)
call salloc (image2, SZ_LINE, TY_CHAR)
call salloc (image3, SZ_LINE, TY_CHAR)
call salloc (history, SZ_LINE, TY_CHAR)
# Get the calibration, normalization, and response image lists and
# check that the they match.
call clgstr ("calibration", Memc[image1], SZ_LINE)
call clgstr ("normalization", Memc[image2], SZ_LINE)
call clgstr ("response", Memc[image3], SZ_LINE)
list1 = imtopen (Memc[image1])
list2 = imtopen (Memc[image2])
list3 = imtopen (Memc[image3])
if ((imtlen(list1)!=imtlen(list3)) || (imtlen(list2)!=imtlen(list3))) {
call imtclose (list1)
call imtclose (list2)
call imtclose (list3)
call error (0, "Image lists do not match")
}
# Get remaining parameters and initialize the curve fitting package.
threshold = clgetr ("threshold")
call clgstr ("sample", Memc[image1], SZ_LINE)
naverage = clgeti ("naverage")
call clgstr ("function", Memc[image2], SZ_LINE)
order = clgeti ("order")
low_reject = clgetr ("low_reject")
high_reject = clgetr ("high_reject")
niterate = clgeti ("niterate")
grow = clgetr ("grow")
if (clgetb ("interactive"))
interactive = YES
else
interactive = ALWAYSNO
# Set the ICFIT pointer structure.
call ic_open (ic)
call ic_pstr (ic, "sample", Memc[image1])
call ic_puti (ic, "naverage", naverage)
call ic_pstr (ic, "function", Memc[image2])
call ic_puti (ic, "order", order)
call ic_putr (ic, "low", low_reject)
call ic_putr (ic, "high", high_reject)
call ic_puti (ic, "niterate", niterate)
call ic_putr (ic, "grow", grow)
call ic_pstr (ic, "ylabel", "")
gt = gt_init()
call gt_sets (gt, GTTYPE, "line")
# Create the response image for each calibration image.
while ((imtgetim (list1, Memc[image1], SZ_LINE) != EOF) &&
(imtgetim (list2, Memc[image2], SZ_LINE) != EOF) &&
(imtgetim (list3, Memc[image3], SZ_LINE) != EOF)) {
# Map the images. If the response image does not exist it
# is created and initialized to unit response everywhere.
# If the calibration image is an image section then the response
# image is opened as a section also.
call ls_immap (Memc[image1], Memc[image3], cal, resp)
norm = immap (Memc[image2], READ_ONLY, 0)
# Determine whether the normalization spectrum is to be fit
# interactively and if so set the graphics title.
call sprintf (Memc[image2], SZ_LINE,
"Fit the normalization spectrum for %s interactively")
call pargstr (Memc[image1])
call xt_answer (Memc[image2], interactive)
if ((interactive == YES) || (interactive == ALWAYSYES)) {
call sprintf (Memc[image2], SZ_LINE,
"Fit the normalization spectrum for %s\n%s")
call pargstr (Memc[image1])
call pargstr (IM_TITLE(cal))
call gt_sets (gt, GTTITLE, Memc[image2])
}
# Make the response.
call re_make (cal, norm, resp, ic, gt, threshold, interactive)
# Document the fit.
call ic_gstr (ic, "sample", Memc[history], SZ_LINE)
call clpstr ("sample", Memc[history])
naverage = ic_geti (ic, "naverage")
call clputi ("naverage", naverage)
call ic_gstr (ic, "function", Memc[history], SZ_LINE)
call clpstr ("function", Memc[history])
order = ic_geti (ic, "order")
call clputi ("order", order)
low_reject = ic_getr (ic, "low")
call clputr ("low_reject", low_reject)
high_reject = ic_getr (ic, "high")
call clputr ("high_reject", high_reject)
niterate = ic_geti (ic, "niterate")
call clputi ("niterate", niterate)
grow = ic_getr (ic, "grow")
call clputr ("grow", grow)
call imaddr (resp, "ccdmean", 1.)
call sprintf (Memc[history], SZ_LINE,
"Response determined from %s.")
call pargstr (Memc[image2])
call xt_phistory (resp, Memc[history])
call imunmap (cal)
call imunmap (norm)
call imunmap (resp)
}
# Finish up.
call ic_closer (ic)
call imtclose (list1)
call imtclose (list2)
call imtclose (list3)
call gt_free (gt)
call sfree (sp)
end
# RE_MAKE -- Given the calibration image determine the response.
procedure re_make (cal, norm, resp, ic, gt, threshold, interactive)
pointer cal # Calibration IMIO pointer
pointer norm # Normalization IMIO pointer
pointer resp # Response IMIO pointer
pointer ic # ICFIT pointer
pointer gt # GTOOLS pointer
real threshold # Response threshold
int interactive # Interactive?
char graphics[SZ_FNAME] # Graphics output device
int laxis, paxis, npts
pointer cv, gp, sp, wavelengths, spectrum, wts
pointer gopen()
errchk get_daxis
begin
# Determine the dispersion axis and set the axis labels.
call get_daxis (cal, laxis, paxis)
switch (laxis) {
case 1:
call ic_pstr (ic, "xlabel", "Column")
case 2:
call ic_pstr (ic, "xlabel", "Line")
}
# Get the normalization spectrum.
call ls_aimavg (norm, laxis, 1, IM_LEN(norm, 1), 1, IM_LEN(norm, 2),
wavelengths, spectrum, npts)
# Allocate memory for the fit.
call smark (sp)
call salloc (wts, npts, TY_REAL)
call amovkr (1., Memr[wts], npts)
# Smooth the normalization spectrum.
call ic_putr (ic, "xmin", Memr[wavelengths])
call ic_putr (ic, "xmax", Memr[wavelengths+npts-1])
if ((interactive == YES) || (interactive == ALWAYSYES)) {
call clgstr ("graphics", graphics, SZ_FNAME)
gp = gopen (graphics, NEW_FILE, STDGRAPH)
call icg_fit (ic, gp, "cursor", gt, cv, Memr[wavelengths],
Memr[spectrum], Memr[wts], npts)
call gclose (gp)
} else {
call ic_fit (ic, cv, Memr[wavelengths], Memr[spectrum], Memr[wts],
npts, YES, YES, YES, YES)
}
call cvvector (cv, Memr[wavelengths], Memr[spectrum], npts)
call cvfree (cv)
# Compute the response image by normalizing the calibration
# image by the normalization spectrum.
call re_normalize (cal, resp, laxis, threshold, Memr[spectrum], npts)
# Free allocated memory.
call sfree (sp)
call mfree (wavelengths, TY_REAL)
call mfree (spectrum, TY_REAL)
end
# RE_NORMALIZE -- Divide each calibration image pixel by the normalization
# spectrum at that pixel.
procedure re_normalize (cal, resp, axis, threshold, spectrum, npts)
pointer cal # Calibration IMIO pointer
pointer resp # Response IMIO pointer
int axis # Dispersion axis
real threshold # Normalization treshold
real spectrum[npts] # Pointer to normalization spectrum
int npts # Number of points in spectrum
int i, j, ncols, nlines
real norm
pointer datain, dataout
pointer imgl2r(), impl2r()
begin
ncols = IM_LEN (cal, 1)
nlines = IM_LEN (cal, 2)
# Compute the response image.
if (IS_INDEF (threshold)) {
do i = 1, nlines {
datain = imgl2r (cal, i)
dataout = impl2r (resp, i)
switch (axis) {
case 1:
call adivr (Memr[datain], spectrum, Memr[dataout], ncols)
case 2:
call adivkr (Memr[datain], spectrum[i], Memr[dataout],
ncols)
}
}
} else {
do i = 1, nlines {
datain = imgl2r (cal, i)
dataout = impl2r (resp, i)
switch (axis) {
case 1:
do j = 1, ncols {
norm = spectrum[j]
if (norm < threshold || Memr[datain] < threshold)
Memr[dataout] = 1.
else
Memr[dataout] = Memr[datain] / norm
datain = datain + 1
dataout = dataout + 1
}
case 2:
norm = spectrum[i]
if (norm < threshold)
call amovkr (1., Memr[dataout], ncols)
else {
do j = 1, ncols {
if (Memr[datain] < threshold)
Memr[dataout] = 1.
else
Memr[dataout] = Memr[datain] / norm
datain = datain + 1
dataout = dataout + 1
}
}
}
}
}
end
|