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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
include <imhdr.h>
include <error.h>
include <pkg/gtools.h>
include "apertures.h"
define APFIND 1
define APRECENTER 2
define APRESIZE 3
define APEDIT 4
define APTRACE 5
define APSUM 6
define APNORM 7
define APSCAT 8
define APALL 9
define APFIT 10
define APFLAT 11
define APMASK 12
define APSCRIPT 13
define APSLITPROC 14
define APNOISE 15
# APEXTRACT TASK ENTRY POINTS
#
# The entry point for each task selects the operations to be performed
# and initializes the pset to be used for the algorithm parameters.
procedure t_apfind ()
begin
call apall (APFIND)
end
procedure t_aprecenter ()
begin
call apall (APRECENTER)
end
procedure t_apresize ()
begin
call apall (APRESIZE)
end
procedure t_apedit ()
begin
call apall (APEDIT)
end
procedure t_aptrace ()
begin
call apall (APTRACE)
end
procedure t_apsum ()
begin
call apall (APSUM)
end
procedure t_apnorm ()
begin
call apall (APNORM)
end
procedure t_apscatter ()
begin
call apall (APSCAT)
end
procedure t_apall ()
begin
call apall (APALL)
end
procedure t_apflat ()
begin
call apall (APFLAT)
end
procedure t_apfit ()
begin
call apall (APFIT)
end
procedure t_apmask ()
begin
call apall (APMASK)
end
procedure t_apscript ()
begin
call apall (APSCRIPT)
end
procedure t_apslitproc ()
begin
call apall (APSLITPROC)
end
procedure t_apnoise ()
begin
call apall (APNOISE)
end
# APALL -- Master aperture definition and extraction procedure.
procedure apall (ltask)
int ltask # Logical task
bool find # Find apertures?
bool recenter # Recenter apertures?
bool resize # Resize apertures?
bool edit # Edit apertures?
bool trace # Trace apertures?
bool extract # Extract apertures?
bool fit # Extract fit?
bool norm # Normalize spectra?
bool flat # Flatten spectra?
bool scat # Subtract scattered light?
bool mask # Aperture mask?
bool noise # Noise calculation?
int input # List of input spectra
int refs # List of reference spectra
int out # List of output spectra
pointer format # Output format or fit type
int scatout # List of scattered light images
int profs # List of profile spectra
int line # Dispersion line
int nsum # Lines to sum
pointer aps # Pointer to array of aperture pointers
int naps # Number of apertures
char nullstr[1]
int i
pointer sp, image, output, reference, profiles, str, str1
bool clgetb(), apgetb(), streq(), ap_answer(), apgans(), apgansb()
int imtopenp(), clgeti(), ap_getim(), ap_dbaccess(), strncmp()
errchk ap_dbacess, ap_dbread, ap_find, ap_recenter, ap_resize, ap_edit
errchk ap_trace, ap_plot, ap_extract, ap_scatter, ap_mask, ap_dbwrite
data nullstr /0,0/
begin
# Allocate memory for the apertures, filenames, and strings.
call smark (sp)
call salloc (image, SZ_FNAME, TY_CHAR)
call salloc (output, SZ_FNAME, TY_CHAR)
call salloc (reference, SZ_FNAME, TY_CHAR)
call salloc (format, SZ_LINE, TY_CHAR)
call salloc (profiles, SZ_FNAME, TY_CHAR)
call salloc (str, SZ_LINE, TY_CHAR)
call salloc (str1, SZ_LINE, TY_CHAR)
switch (ltask) {
case APALL:
call apopset ("apall1")
case APFIT:
call apopset ("apfit1")
case APFLAT:
call apopset ("apflat1")
case APNORM:
call apopset ("apnorm1")
case APSCRIPT:
call apopset ("apscript")
case APSLITPROC:
call apopset ("apslitproc")
case APNOISE:
call apopset ("apnoise1")
default:
call apopset ("apparams")
}
input = imtopenp ("input")
refs = imtopenp ("references")
line = clgeti ("line")
nsum = clgeti ("nsum")
out = NULL
profs = NULL
scatout = NULL
switch (ltask) {
case APSUM, APALL, APFIT, APNORM, APFLAT, APSCAT,
APMASK, APSCRIPT, APSLITPROC:
out = imtopenp ("output")
}
switch (ltask) {
case APSUM, APALL:
profs = imtopenp ("profiles")
call apgstr ("format", Memc[format], SZ_LINE)
case APFIT:
call clgstr ("fittype", Memc[format], SZ_LINE)
case APNORM:
call strcpy ("normalize", Memc[format], SZ_LINE)
case APFLAT:
call strcpy ("flatten", Memc[format], SZ_LINE)
case APSCAT:
scatout = imtopenp ("scatter")
case APSCRIPT, APSLITPROC:
scatout = imtopenp ("scatter")
profs = imtopenp ("profiles")
call apgstr ("format", Memc[format], SZ_LINE)
case APNOISE:
call strcpy ("noise", Memc[format], SZ_LINE)
}
trace = false
extract = false
fit = false
norm = false
flat = false
scat = false
mask = false
noise = false
if (apgetb ("initialize")) {
find = clgetb ("find")
recenter = clgetb ("recenter")
resize = clgetb ("resize")
edit = clgetb ("edit")
switch (ltask) {
case APTRACE, APSUM, APALL, APFIT, APNORM,
APFLAT, APSCAT, APMASK, APSCRIPT, APSLITPROC, APNOISE:
trace = clgetb ("trace")
}
switch (ltask) {
case APSUM, APALL:
extract = clgetb ("extract")
case APFIT:
fit = clgetb ("fit")
case APNORM:
norm = clgetb ("normalize")
case APFLAT:
flat = clgetb ("flatten")
case APSCAT:
scat = clgetb ("subtract")
case APMASK:
mask = clgetb ("mask")
case APSCRIPT, APSLITPROC:
extract = clgetb ("extract")
scat = clgetb ("subtract")
if (extract && scat)
call error (1,
"APSCRIPT: Can't combine scattered light and extraction")
case APNOISE:
noise = true
}
call ap_init (find, recenter, resize, edit, trace, extract, fit,
norm, flat, scat, mask, noise)
} else {
find = apgans ("ansfind")
recenter = apgans ("ansrecenter")
resize = apgans ("ansresize")
edit = apgans ("ansedit")
switch (ltask) {
case APTRACE, APSUM, APALL, APFIT, APNORM,
APFLAT, APSCAT, APMASK, APSCRIPT, APSLITPROC, APNOISE:
trace = apgans ("anstrace")
}
switch (ltask) {
case APSUM, APALL:
extract = apgans ("ansextract")
case APFIT:
fit = apgans ("ansfit")
case APNORM:
norm = apgans ("ansnorm")
case APFLAT:
flat = apgans ("ansflat")
case APSCAT:
scat = apgans ("ansscat")
case APMASK:
mask = apgans ("ansmask")
case APSCRIPT, APSLITPROC:
extract = apgans ("ansextract")
scat = apgans ("ansscat")
if (extract && scat)
call error (1,
"APSCRIPT: Can't combine scattered light and extraction")
}
}
# Initialize the apertures.
naps = 0
Memc[reference] = EOS
Memc[profiles] = EOS
call malloc (aps, 100, TY_POINTER)
# Process the apertures from each input image.
while (ap_getim (input, Memc[image], SZ_FNAME) != EOF) {
if (ap_getim (refs, Memc[str], SZ_LINE) != EOF)
call strcpy (Memc[str], Memc[reference], SZ_FNAME)
if (extract || fit || flat || norm || scat || mask)
if (ap_getim (out, Memc[output], SZ_FNAME) == EOF)
Memc[output] = EOS
# Get apertures.
call appstr ("ansdbwrite1", "no")
if (streq (Memc[reference], nullstr) ||
streq (Memc[reference], Memc[image])) {
if (clgetb ("verbose"))
call printf ("Searching aperture database ...\n")
iferr (call ap_dbread (Memc[image], aps, naps))
;
} else if (streq (Memc[reference], "OLD")) {
iferr (call ap_dbread (Memc[image], aps, naps))
next
} else {
if (strncmp (Memc[reference], "NEW", 3) == 0) {
if (ap_dbaccess (Memc[image]) == YES)
next
call strcpy (Memc[reference+3], Memc[reference], SZ_FNAME)
}
if (clgetb ("verbose"))
call printf ("Searching aperture database ...\n")
iferr (call ap_dbread (Memc[reference], aps, naps)) {
call eprintf (
"WARNING: Reference image (%s) apertures not found\n")
call pargstr (Memc[reference])
next
}
if (naps > 0)
call appstr ("ansdbwrite1", "yes")
}
call clgstr ("apertures", Memc[str], SZ_LINE)
call ap_select (Memc[str], Memi[aps], naps)
iferr {
# Find apertures.
if (find && naps == 0)
call ap_find (Memc[image], line, nsum, aps, naps)
# Recenter apertures.
else if (recenter)
call ap_recenter (Memc[image], line, nsum, Memi[aps], naps,
NO)
# Resize apertures.
if (resize)
call ap_resize (Memc[image], line, nsum, Memi[aps], naps,
NO)
# Edit apertures.
if (edit)
call ap_edit (Memc[image], line, nsum, aps, naps)
# Trace apertures.
if (trace)
call ap_trace (Memc[image], line, Memi[aps], naps, NO)
# Write database and make aperture plot.
if (apgansb ("ansdbwrite1")) {
call clgstr ("database", Memc[str1], SZ_LINE)
call sprintf (Memc[str], SZ_LINE,
"Write apertures for %s to %s")
call pargstr (Memc[image])
call pargstr (Memc[str1])
if (ap_answer ("ansdbwrite", Memc[str]))
call ap_dbwrite (Memc[image], aps, naps)
}
iferr (call ap_dbwrite ("last", aps, naps))
;
iferr (call ap_plot (Memc[image], line, nsum, Memi[aps], naps))
call erract (EA_WARN)
# Extract 1D spectra but do not extract negative beams
if (extract) {
do i = 1, naps {
if (AP_BEAM(Memi[aps+i-1]) < 0)
AP_SELECT(Memi[aps+i-1]) = NO
}
if (ap_getim (profs, Memc[str1], SZ_LINE) != EOF)
call strcpy (Memc[str1], Memc[profiles], SZ_FNAME)
call sprintf (Memc[str], SZ_LINE,
"Extract aperture spectra for %s?")
call pargstr (Memc[image])
if (ap_answer ("ansextract", Memc[str])) {
call sprintf (Memc[str], SZ_LINE,
"Review extracted spectra from %s?")
call pargstr (Memc[image])
if (ap_answer ("ansreview", Memc[str])) {
call apgstr ("ansreview", Memc[str], SZ_LINE)
call appstr ("ansreview1", Memc[str])
} else
call appstr ("ansreview1", "NO")
call ap_extract (Memc[image], Memc[output],
Memc[format], Memc[profiles], Memi[aps], naps)
}
}
# Fit apertures.
if (fit) {
call sprintf (Memc[str], SZ_LINE,
"Fit apertures in %s?")
call pargstr (Memc[image])
if (ap_answer ("ansfit", Memc[str])) {
call ap_extract (Memc[image], Memc[output],
Memc[format], nullstr, Memi[aps], naps)
}
}
# Normalize apertures.
if (norm) {
call sprintf (Memc[str], SZ_LINE,
"Normalize apertures in %s?")
call pargstr (Memc[image])
if (ap_answer ("ansnorm", Memc[str])) {
call sprintf (Memc[str], SZ_LINE,
"Fit spectra from %s interactively?")
call pargstr (Memc[image])
if (ap_answer ("ansfitspec", Memc[str])) {
call apgstr ("ansfitspec", Memc[str], SZ_LINE)
call appstr ("ansfitspec1", Memc[str])
} else
call appstr ("ansfitspec1", "NO")
call ap_extract (Memc[image], Memc[output],
Memc[format], nullstr, Memi[aps], naps)
}
}
# Flatten apertures.
if (flat) {
call sprintf (Memc[str], SZ_LINE,
"Flatten apertures in %s?")
call pargstr (Memc[image])
if (ap_answer ("ansflat", Memc[str])) {
call sprintf (Memc[str], SZ_LINE,
"Fit spectra from %s interactively?")
call pargstr (Memc[image])
if (ap_answer ("ansfitspec", Memc[str])) {
call apgstr ("ansfitspec", Memc[str], SZ_LINE)
call appstr ("ansfitspec1", Memc[str])
} else
call appstr ("ansfitspec1", "NO")
call ap_extract (Memc[image], Memc[output],
Memc[format], nullstr, Memi[aps], naps)
}
}
# Substract scattered light.
if (scat) {
if (ap_getim (scatout, Memc[str1], SZ_LINE) == EOF)
Memc[str1] = EOS
if (Memc[output] == EOS ||
streq (Memc[image], Memc[output])) {
call mktemp ("tmp", Memc[str], SZ_LINE)
call ap_scatter (Memc[image], Memc[str],
Memc[str1], Memi[aps], naps, line)
call imdelete (Memc[image])
call imrename (Memc[str], Memc[image])
} else
call ap_scatter (Memc[image], Memc[output],
Memc[str1], Memi[aps], naps, line)
}
# Make a aperture mask.
if (mask)
call ap_mask (Memc[image], Memc[output], Memi[aps], naps)
# Fit noise.
if (noise)
call ap_extract (Memc[image], nullstr,
Memc[format], nullstr, Memi[aps], naps)
} then
call erract (EA_WARN)
# Free memory.
for (i = 1; i <= naps; i = i + 1)
call ap_free (Memi[aps+i-1])
naps = 0
}
# Free memory and finish up.
call imtclose (input)
call imtclose (refs)
if (out != NULL)
call imtclose (out)
if (profs != NULL)
call imtclose (profs)
if (norm || flat)
call ap_fitfree ()
if (scat) {
if (scatout != NULL)
call imtclose (scatout)
call scat_free ()
}
call ap_gclose ()
call ap_trfree ()
call apcpset ()
call sfree (sp)
end
procedure ap_init (find, recenter, resize, edit, trace, extract, fit,
norm, flat, scat, mask, noise)
bool find, recenter, resize, edit, trace
bool extract, fit, norm, flat, scat, mask, noise
pointer sp, str
bool clgetb()
begin
call smark (sp)
call salloc (str, SZ_LINE, TY_CHAR)
if (find)
call appans ("ansfind", find, find)
if (recenter)
call appans ("ansrecenter", recenter, recenter)
if (resize)
call appans ("ansresize", resize, resize)
if (edit)
call appans ("ansedit", edit, false)
if (trace) {
call appans ("anstrace", trace, trace)
call appans ("ansfittrace", clgetb ("fittrace"), false)
}
if (extract) {
call appans ("ansextract", extract, extract)
call appans ("ansreview", clgetb ("review"), false)
}
if (fit) {
call appans ("ansfit", fit, fit)
call appstr ("ansreview1", "NO")
}
if (norm) {
call appans ("ansnorm", norm, norm)
call appans ("ansfitspec", clgetb ("fitspec"), false)
call appstr ("ansreview1", "NO")
}
if (flat) {
call appans ("ansflat", flat, flat)
call appans ("ansfitspec", clgetb ("fitspec"), false)
call appstr ("ansreview1", "NO")
}
if (scat) {
call appans ("ansscat", scat, scat)
call appans ("anssmooth", clgetb ("smooth"), clgetb ("smooth"))
call appans ("ansfitscatter", clgetb ("fitscatter"), false)
call appans ("ansfitsmooth", clgetb ("fitsmooth"), false)
}
if (mask)
call appans ("ansmask", mask, mask)
if (noise)
call appstr ("ansreview1", "NO")
if (extract || fit || norm || flat) {
if (clgetb ("interactive"))
call appstr ("ansclobber", "no")
else
call appstr ("ansclobber", "NO")
}
call apgstr ("dbwrite", Memc[str], SZ_LINE)
if (clgetb ("interactive"))
call appstr ("ansdbwrite", Memc[str])
else {
if (Memc[str] == 'y' || Memc[str] == 'Y')
call appstr ("ansdbwrite", "YES")
else
call appstr ("ansdbwrite", "NO")
}
call sfree (sp)
end
|