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
|
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
include <error.h>
include <imhdr.h>
include "imsurfit.h"
# T_IMSURFIT -- Fit a surface function to an image
#
# 1. A user selected function is fit to each surface.
# 2. Only the selected regions of the image are fit.
# 3. Deviant pixels may be rejected from the fit.
# 4. The user selects the type of output image. The choices are:
# a. the fitted image.
# b. the input image with deviant pixels replaced by
# the fitted values
# c. the input image minus the fitted image.
# d. the ratio of the input image and the fit where
# pixels less than div_min are set to a ratio of 1.
procedure t_imsurfit ()
char imtlist1[SZ_LINE] # Input image list
char imtlist2[SZ_LINE] # Output image list
char image1[SZ_FNAME] # Input image
char image2[SZ_FNAME] # Output image
char str[SZ_LINE], region_str[SZ_LINE]
int list1, list2, region_type
pointer im1, im2, imfit, gl, sp
bool clgetb()
int imtopen(), imtgetim(), imtlen(), btoi(), clgeti(), clgwrd()
pointer immap()
real clgetr()
begin
# Allocate space for imfit structure.
call smark (sp)
call salloc (imfit, LEN_IMSFSTRUCT, TY_STRUCT)
# Get task parameters.
call clgstr ("input", imtlist1, SZ_FNAME)
call clgstr ("output", imtlist2, SZ_FNAME)
TYPE_OUTPUT(imfit) = clgwrd ("type_output", str, SZ_LINE,
",fit,clean,residual,response,")
DIV_MIN(imfit) = clgetr ("div_min")
# Get surface ftting parameters.
SURFACE_TYPE(imfit) = clgwrd ("function", str, SZ_LINE,
",legendre,chebyshev,spline3,spline1,")
XORDER(imfit) = clgeti ("xorder")
YORDER(imfit) = clgeti ("yorder")
CROSS_TERMS(imfit) = btoi (clgetb ("cross_terms"))
# Get median processing parameters.
XMEDIAN(imfit) = clgeti ("xmedian")
YMEDIAN(imfit) = clgeti ("ymedian")
MEDIAN_PERCENT(imfit) = clgetr ("median_percent")
if (XMEDIAN(imfit) > 1 || YMEDIAN(imfit) > 1)
MEDIAN(imfit) = YES
else
MEDIAN(imfit) = NO
# Get rejection cycle parameters.
NITER(imfit) = clgeti ("niter")
LOWER(imfit) = clgetr ("lower")
UPPER(imfit) = clgetr ("upper")
NGROW(imfit) = clgeti ("ngrow")
if (MEDIAN(IMFIT) == YES) {
REJECT(imfit) = NO
NITER(imfit) = 0
} else if (NITER(imfit) > 0 && (LOWER(imfit) > 0. || UPPER(imfit) > 0.))
REJECT(imfit) = YES
else {
REJECT(imfit) = NO
NITER(imfit) = 0
}
# Checking sigmas for cleaning.
if (TYPE_OUTPUT(imfit) == CLEAN && MEDIAN(imfit) == YES)
call error (0,
"T_IMSURFIT: Clean option and median processing are exclusive.")
if (TYPE_OUTPUT(imfit) == CLEAN && NITER(imfit) <= 0)
call error (0, "T_IMSURFIT: Clean option requires non-zero niter.")
if (TYPE_OUTPUT(imfit) == CLEAN && LOWER(imfit) <= 0. &&
UPPER(imfit) <= 0.)
call error (0, "T_IMSURFIT: Clean option requires non-zero sigma.")
# Get regions to be fit.
gl = NULL
region_type = clgwrd ("regions", str, SZ_LINE,
",all,columns,rows,border,sections,circle,invcircle,")
switch (region_type) {
case ALL:
;
case BORDER:
call clgstr ("border", region_str, SZ_LINE)
case SECTIONS:
call clgstr ("sections", region_str, SZ_LINE)
case COLUMNS:
call clgstr ("columns", region_str, SZ_LINE)
case ROWS:
call clgstr ("rows", region_str, SZ_LINE)
case CIRCLE, INVCIRCLE:
call clgstr ("circle", region_str, SZ_LINE)
}
# Expand the input and output image lists.
list1 = imtopen (imtlist1)
list2 = imtopen (imtlist2)
if (imtlen (list1) != imtlen (list2)) {
call imtclose (list1)
call imtclose (list2)
call error (0, "Number of input and output images not the same.")
}
# Do each set of input and output images.
while ((imtgetim (list1, image1, SZ_FNAME) != EOF) &&
(imtgetim (list2, image2, SZ_FNAME) != EOF)) {
im1 = immap (image1, READ_ONLY, 0)
im2 = immap (image2, NEW_COPY, im1)
iferr {
if (region_type != ALL)
call make_good_list (im1, gl, region_type, region_str)
call imsurfit (im1, im2, imfit, gl)
} then
call erract (EA_WARN)
call imunmap (im1)
call imunmap (im2)
call prl_free (gl)
}
# Cleanup.
call sfree (sp)
call imtclose (list1)
call imtclose (list2)
end
# MAKE_GOOD_LIST -- Procedure to make a list of good regions. The program
# returns an error message if no good regions are defined. The good
# list parameter is set to NULL if the whole image is to be fit. This routine
# uses both the ranges and pixlist package which will be replaced by image
# masking.
procedure make_good_list (im, gl, region_type, region_string)
pointer im # pointer to the image
pointer gl # good pixel list descriptor
int region_type # type of good region
char region_string[ARB] # region parameters
int i, ip, zero, nvals, range_min, r2, xdist, max_nranges
int x1, x2, y1, y2, temp, border, xcenter, ycenter, radius
int columns[7]
pointer sp, ranges, list
bool is_in_rangelist()
int is_next_number(), is_decode_ranges(), open(), fscan(), nscan(), ctoi()
errchk open, close
begin
# Determine the maximum number of images.
max_nranges = IM_LEN(im,1)
# Allocate working space.
call smark (sp)
call salloc (ranges, 3 * max_nranges + 1, TY_INT)
# Compute the good pixel list.
switch (region_type) {
case ROWS:
# Decode the row ranges.
if (is_decode_ranges (region_string, Memi[ranges], max_nranges, 1,
int (IM_LEN(im,2)), nvals) == ERR)
call error (0, "MAKE_GOOD_LIST: Error decoding row string.")
if (nvals == 0)
call error (0, "MAKE_GOOD_LIST: no good rows.")
if (nvals == IM_LEN(im,2)) {
call sfree (sp)
return
}
# Intialize the good pixel list.
call prl_init (gl, int (IM_LEN(im,1)), int (IM_LEN(im,2)))
# Set column limits using the ranges format.
columns[1] = 1
columns[2] = IM_LEN(im,1)
columns[3] = 1
columns[4] = NULL
# Set column limits for the specied lines.
zero = 0
range_min = is_next_number (Memi[ranges], zero)
while (range_min != EOF) {
for (i = range_min; i <= IM_LEN(im,2) + 1; i = i + 1) {
if (!is_in_rangelist (Memi[ranges], i) ||
i == IM_LEN(im,2)+1) {
call prl_put_ranges (gl, range_min, i-1, columns)
break
}
}
range_min = is_next_number (Memi[ranges], i)
}
case COLUMNS:
# Set the specified columns.
if (is_decode_ranges (region_string, Memi[ranges], max_nranges, 1,
int (IM_LEN(im,1)), nvals) == ERR)
call error (0, "MAKE_GOOD_LIST: Error decoding column string.")
if (nvals == 0)
call error (0, "MAKE_GOOD_LIST: No good columns.")
if (nvals == IM_LEN(im,1)) {
call sfree (sp)
return
}
# Make the good pixel list.
call prl_init (gl, int (IM_LEN(im,1)), int (IM_LEN(im,2)))
call prl_add_ranges (gl, 1, int (IM_LEN(im,2)), Memi[ranges])
case CIRCLE, INVCIRCLE:
# Get the parameters of the circle.
ip = 1
if (ctoi (region_string, ip, xcenter) <= 0)
call error (0, "MAKE_GOOD_LIST: Error decoding xcenter.")
if (ctoi (region_string, ip, ycenter) <= 0)
call error (0, "MAKE_GOOD_LIST: Error decoding ycenter.")
if (ctoi (region_string, ip, radius) <= 0)
call error (0, "MAKE_GOOD_LIST: Error decoding radius.")
y1 = max (1, ycenter - radius)
y2 = min (int (IM_LEN(im,2)), ycenter + radius)
x1 = max (1, xcenter - radius)
x2 = min (int (IM_LEN(im,1)), xcenter + radius)
if (region_type == CIRCLE) {
if (y1 > IM_LEN(im,2) || y2 < 1 || x1 > IM_LEN(im,1) || x2 < 1)
call error (0, "MAKE_GOOD_LIST: No good regions.")
}
# Create the good pixel list.
call prl_init (gl, int (IM_LEN(im,1)), int (IM_LEN(im,2)))
r2 = radius ** 2
if (region_type == CIRCLE) {
do i = y1, y2 {
xdist = sqrt (real (r2 - (ycenter - i) ** 2))
x1 = max (1, xcenter - xdist)
x2 = min (IM_LEN(im,1), xcenter + xdist)
columns[1] = x1
columns[2] = x2
columns[3] = 1
columns[4] = NULL
call prl_put_ranges (gl, i, i, columns)
}
} else if (region_type == INVCIRCLE) {
do i = 1, y1 - 1 {
columns[1] = 1
columns[2] = IM_LEN(im,1)
columns[3] = 1
columns[4] = NULL
call prl_put_ranges (gl, i, i, columns)
}
do i = y2 + 1, IM_LEN(im,2) {
columns[1] = 1
columns[2] = IM_LEN(im,1)
columns[3] = 1
columns[4] = NULL
call prl_put_ranges (gl, i, i, columns)
}
do i = y1, y2 {
xdist = sqrt (real (r2 - (ycenter - i) ** 2))
x1 = max (1, xcenter - xdist)
x2 = min (IM_LEN(im,1), xcenter + xdist)
if (x1 > 1) {
columns[1] = 1
columns[2] = x1 - 1
columns[3] = 1
if (x2 < IM_LEN(im,1)) {
columns[4] = x2 + 1
columns[5] = IM_LEN(im,1)
columns[6] = 1
columns[7] = NULL
} else
columns[4] = NULL
} else if (x2 < IM_LEN(im,1)) {
columns[1] = x2 + 1
columns[2] = IM_LEN(im,1)
columns[3] = 1
columns[4] = NULL
} else
columns[1] = NULL
call prl_put_ranges (gl, i, i, columns)
}
}
case SECTIONS:
# Open file of sections.
list = open (region_string, READ_ONLY, TEXT_FILE)
call prl_init (gl, int (IM_LEN(im,1)), int (IM_LEN(im,2)))
# Scan the list.
while (fscan (list) != EOF) {
# Fetch parameters from list.
call gargi (x1)
call gargi (x2)
call gargi (y1)
call gargi (y2)
if (nscan() != 4)
next
# Check and correct for out of bounds limits.
x1 = max (1, min (IM_LEN(im,1), x1))
x2 = min (IM_LEN(im,1), max (1, x2))
y1 = max (1, min (IM_LEN(im,2), y1))
y2 = min (IM_LEN(im,2), max (1, y2))
# Check the order.
if (x2 < x1) {
temp = x1
x1 = x2
x2 = temp
}
if (y2 < y1) {
temp = y1
y1 = y2
y2 = temp
}
# If entire image return.
if ((x1 == 1) && (x2 == IM_LEN(im,1)) && (y1 == 1) &&
(y2 == IM_LEN(im,2))) {
call prl_free (gl)
gl = NULL
break
}
# Set ranges.
columns[1] = x1
columns[2] = x2
columns[3] = 1
columns[4] = NULL
call prl_add_ranges (gl, y1, y2, columns)
}
call close (list)
case BORDER:
# Decode border parameter.
ip = 1
if (ctoi (region_string, ip, border) == ERR)
call error (0, "MAKE_GOOD_LIST: Error decoding border string.")
if (border < 1)
call error (0, "MAKE_GOOD_LIST: No border.")
if ((border > IM_LEN(im,1)/2) && (border > IM_LEN(im,2)/2)) {
call sfree (sp)
return
}
# Intialize list.
call prl_init (gl, int (IM_LEN(im,1)), int (IM_LEN(im,2)))
y1 = 1 + border - 1
y2 = IM_LEN(im,2) - border + 1
columns[1] = 1
columns[2] = IM_LEN(im,1)
columns[3] = 1
columns[4] = NULL
# Set ranges for top and bottom edges of image.
call prl_put_ranges (gl, 1, y1, columns)
call prl_put_ranges (gl, y2, int (IM_LEN(im,2)), columns)
columns[1] = 1
columns[2] = y1
columns[3] = 1
columns[4] = NULL
call prl_put_ranges (gl, y1 + 1, y2 - 1, columns)
columns[1] = IM_LEN(im,1) - border + 1
columns[2] = IM_LEN(im,1)
columns[3] = 1
columns[4] = NULL
call prl_add_ranges (gl, y1 + 1, y2 - 1, columns)
}
call sfree (sp)
end
|