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
|
include <error.h>
include <mach.h>
include "apertures.h"
# Data structure for user aperture id table.
define IDS_LEN 4 # Length of ID structure
define IDS_NIDS Memi[$1] # Number of aperture IDs
define IDS_APS Memi[$1+1] # Aperture numbers (pointer)
define IDS_BEAMS Memi[$1+2] # Beam numbers (pointer)
define IDS_TITLES Memi[$1+3] # Titles (pointer)
# AP_GIDS -- Get user aperture ID's.
procedure ap_gids (ids)
pointer ids # ID structure
int nids, ap, beam, fd, nalloc
double ra, dec
pointer sp, key, str, aps, beams, titles, im, list
int nowhite(), open(), fscan(), nscan()
pointer immap(), imofnlu(), imgnfn()
errchk open
begin
call smark (sp)
call salloc (key, SZ_FNAME, TY_CHAR)
call salloc (str, SZ_LINE, TY_CHAR)
nids = 0
nalloc = 0
call apgstr ("apidtable", Memc[key], SZ_FNAME)
if (nowhite (Memc[key], Memc[key], SZ_FNAME) > 0) {
iferr {
# Read aperture information from an image.
ifnoerr (im = immap (Memc[key], READ_ONLY, 0)) {
list = imofnlu (im, "SLFIB[0-9]*")
while (imgnfn (list, Memc[key], SZ_FNAME) != EOF) {
call imgstr (im, Memc[key], Memc[str], SZ_LINE)
call sscan (Memc[str])
call gargi (ap)
if (nscan() == 0)
next
if (ap < 1) {
call imcfnl (list)
call imunmap (im)
call error (1,
"Aperture numbers in apidtable must be > 0")
}
if (nalloc == 0) {
nalloc = 50
call malloc (aps, nalloc, TY_INT)
call malloc (beams, nalloc, TY_INT)
call malloc (titles, nalloc, TY_POINTER)
} else if (nids == nalloc) {
nalloc = nalloc + 50
call realloc (aps, nalloc, TY_INT)
call realloc (beams, nalloc, TY_INT)
call realloc (titles, nalloc, TY_POINTER)
}
Memi[aps+nids] = ap
call gargi (Memi[beams+nids])
call gargd (ra)
call gargd (dec)
if (nscan() != 4) {
call reset_scan ()
call gargi (ap)
call gargi (beam)
Memc[str] = EOS
call gargstr (Memc[str], SZ_LINE)
call xt_stripwhite (Memc[str])
if (Memc[str] == EOS)
Memi[titles+nids] = NULL
else {
call malloc (Memi[titles+nids], SZ_APTITLE,
TY_CHAR)
call strcpy (Memc[str], Memc[Memi[titles+nids]],
SZ_APTITLE)
}
} else {
Memc[str] = EOS
call gargstr (Memc[str], SZ_LINE)
call xt_stripwhite (Memc[str])
call malloc (Memi[titles+nids], SZ_APTITLE, TY_CHAR)
if (Memc[str] == EOS) {
call sprintf (Memc[Memi[titles+nids]],
SZ_APTITLE, "(%.2h %.2h)")
call pargd (ra)
call pargd (dec)
} else {
call sprintf (Memc[Memi[titles+nids]],
SZ_APTITLE, "%s (%.2h %.2h)")
call pargstr (Memc[str])
call pargd (ra)
call pargd (dec)
}
}
nids = nids + 1
}
call imcfnl (list)
call imunmap (im)
# Read aperture information from a file.
} else {
fd = open (Memc[key], READ_ONLY, TEXT_FILE)
while (fscan (fd) != EOF) {
call gargi (ap)
if (nscan() == 0)
next
if (ap < 1) {
call close (fd)
call error (1,
"Aperture numbers in apidtable must be > 0")
}
if (nalloc == 0) {
nalloc = 50
call malloc (aps, nalloc, TY_INT)
call malloc (beams, nalloc, TY_INT)
call malloc (titles, nalloc, TY_POINTER)
} else if (nids == nalloc) {
nalloc = nalloc + 50
call realloc (aps, nalloc, TY_INT)
call realloc (beams, nalloc, TY_INT)
call realloc (titles, nalloc, TY_POINTER)
}
Memi[aps+nids] = ap
Memi[beams+nids] = ap
Memc[str] = EOS
call gargi (beam)
if (nscan() == 2)
Memi[beams+nids] = beam
call gargstr (Memc[str], SZ_LINE)
call xt_stripwhite (Memc[str])
if (Memc[str] == EOS)
Memi[titles+nids] = NULL
else {
call malloc (Memi[titles+nids], SZ_APTITLE, TY_CHAR)
call strcpy (Memc[str], Memc[Memi[titles+nids]],
SZ_APTITLE)
}
nids = nids + 1
}
call close (fd)
}
} then
call erract (EA_WARN)
}
if (nalloc > nids) {
call realloc (aps, nids, TY_INT)
call realloc (beams, nids, TY_INT)
call realloc (titles, nids, TY_INT)
}
if (nids > 0) {
call malloc (ids, IDS_LEN, TY_STRUCT)
IDS_NIDS(ids) = nids
IDS_APS(ids) = aps
IDS_BEAMS(ids) = beams
IDS_TITLES(ids) = titles
}
call sfree (sp)
end
procedure ap_fids (ids)
pointer ids # ID structure
int i
begin
if (ids != NULL) {
do i = 1, IDS_NIDS(ids)
call mfree (Memi[IDS_TITLES(ids)+i-1], TY_CHAR)
call mfree (IDS_APS(ids), TY_INT)
call mfree (IDS_BEAMS(ids), TY_INT)
call mfree (IDS_TITLES(ids), TY_POINTER)
call mfree (ids, TY_STRUCT)
}
end
# AP_IDS -- Set aperture IDs
# Do not allow negative or zero aperture numbers.
procedure ap_ids (aps, naps, ids)
pointer aps[ARB] # Aperture pointers
int naps # Number of apertures
int ids # ID structure
int i, j, k, l, m, axis, nids, ap, beam, skip, nused
real maxsep, apgetr()
pointer sp, used, a, b
begin
if (naps < 1)
return
axis = AP_AXIS(aps[1])
maxsep = apgetr ("maxsep")
# Dereference ID structure pointers.
if (ids != NULL) {
nids = IDS_NIDS(ids)
a = IDS_APS(ids)
b = IDS_BEAMS(ids)
} else
nids = 0
# Make a list of used aperture numbers
call smark (sp)
call salloc (used, naps, TY_INT)
nused = 0
do i = 1, naps
if (!IS_INDEFI(AP_ID(aps[i]))) {
Memi[used+nused] = AP_ID(aps[i])
nused = nused + 1
}
# Find first aperture with a defined aperture number.
for (i=1; i<=naps && IS_INDEFI(AP_ID(aps[i])); i=i+1)
;
# If there are no defined aperture numbers start with 1 or first
# aperture in the ID table.
if (i > naps) {
i = 1
if (nids > 0) {
ap = Memi[a]
beam = Memi[b]
} else {
ap = i
beam = ap
}
AP_ID(aps[i]) = ap
AP_BEAM(aps[i]) = beam
Memi[used+nused] = ap
nused = nused + 1
} else {
ap = AP_ID(aps[i])
for (l = 1; l <= nids && ap != Memi[a+l-1]; l = l + 1)
;
if (l <= nids)
AP_BEAM(aps[i]) = Memi[b+l-1]
else
AP_BEAM(aps[i]) = ap
}
# Work backwards through the undefined apertures.
for (j = i - 1; j > 0; j = j - 1) {
skip = abs (AP_CEN(aps[j],axis)-AP_CEN(aps[j+1],axis)) / maxsep
if (ids != NULL) {
ap = AP_ID(aps[j+1])
for (l = 1; l <= nids && ap != Memi[a+l-1]; l = l + 1)
;
if (nids <= naps)
skip = 0
m = l - skip
if (l > nids) {
l = 1
for (k = 2; k <= nids; k = k + 1)
if (abs (ap - Memi[a+k-1]) < abs (ap - Memi[a+l-1]))
l = k
m = l - skip + 1
}
repeat {
m = m - 1
if (m > 0) {
ap = Memi[a+m-1]
beam = Memi[b+m-1]
} else {
ap = Memi[a+l-1] + m
beam = max (0, Memi[b+l-1] + m)
}
if (ap == 0)
next
for (k = 0; k < nused && abs(ap) != Memi[used+k]; k = k + 1)
;
if (k == nused)
break
}
} else {
ap = AP_ID(aps[j+1]) - skip
repeat {
ap = ap - 1
beam = abs (ap)
if (ap == 0)
next
for (k = 0; k < nused && abs(ap) != Memi[used+k]; k = k + 1)
;
if (k == nused)
break
}
}
ap = abs (ap)
AP_ID(aps[j]) = ap
AP_BEAM(aps[j]) = beam
Memi[used+nused] = ap
nused = nused + 1
}
# Work forwards through the undefined apertures.
for (i = i + 1; i <= naps; i = i + 1) {
if (IS_INDEFI(AP_ID(aps[i]))) {
skip = abs (AP_CEN(aps[i],axis)-AP_CEN(aps[i-1],axis)) / maxsep
if (nids > 0) {
ap = AP_ID(aps[i-1])
for (l = 1; l <= nids && ap != Memi[a+l-1]; l = l + 1)
;
if (nids <= naps)
skip = 0
m = l + skip
if (l > nids) {
l = 1
for (k = 2; k <= nids; k = k + 1)
if (abs (ap-Memi[a+k-1]) < abs (ap-Memi[a+l-1]))
l = k
m = l + skip - 1
}
m = nids - m + 1
repeat {
m = m - 1
if (m > 0) {
ap = Memi[a+nids-m]
beam = Memi[b+nids-m]
} else {
ap = Memi[a+l-1] - m
beam = max (0, Memi[b+l-1] - m)
}
if (ap == 0)
next
for (k=0; k<nused && abs(ap)!=Memi[used+k]; k=k+1)
;
if (k == nused)
break
}
} else {
ap = AP_ID(aps[i-1]) + skip
repeat {
ap = ap + 1
beam = abs (ap)
if (ap == 0)
next
for (k=0; k<nused && abs(ap)!=Memi[used+k]; k=k+1)
;
if (k == nused)
break
}
}
ap = abs(ap)
AP_ID(aps[i]) = ap
AP_BEAM(aps[i]) = beam
Memi[used+nused] = ap
nused = nused + 1
}
}
call sfree (sp)
end
procedure ap_titles (aps, naps, ids)
pointer aps[ARB] # Aperture pointers
int naps # Number of apertures
pointer ids # ID structure
int i, j, nids
pointer a, titles, title
begin
if (ids == NULL)
return
nids = IDS_NIDS(ids)
a = IDS_APS(ids)
titles = IDS_TITLES(ids)
do i = 1, naps {
if (AP_TITLE(aps[i]) != NULL)
next
do j = 1, nids {
if (AP_ID(aps[i]) == Memi[a+j-1]) {
title = Memi[titles+j-1]
if (title != NULL) {
if (AP_TITLE(aps[i]) == NULL)
call malloc (AP_TITLE(aps[i]), SZ_APTITLE, TY_CHAR)
call strcpy (Memc[title], Memc[AP_TITLE(aps[i])],
SZ_APTITLE)
} else if (AP_TITLE(aps[i]) != NULL)
call mfree (AP_TITLE(aps[i]), TY_CHAR)
}
}
}
end
|