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
577
578
579
580
581
582
583
584
585
586
587
|
include <error.h>
include <pkg/mef.h>
include <imhdr.h>
define SZ_RANGE 100 # Size of range list
# XT_EXTNS -- Expand template of files into a list of extensions.
#
# This supports all MEF extension types. If IMAGE type or any type is
# requested this will also return non-FITS images as well.
#
# This differs from XT_EXTNS1 in that extension zero is not returned
# unless it is a simple image and, in that case, the extension is removed.
int procedure xt_extns (files, exttype, index, extname, extver, lindex, lname,
lver, dataless, ikparams, err, imext)
char files[ARB] #I List of MEF files
char exttype[ARB] #I Extension type (or null for all)
char index[ARB] #I Range list of extension indexes
char extname[ARB] #I Patterns for extension names
char extver[ARB] #I Range list of extension versions
int lindex #I List index number?
int lname #I List extension name?
int lver #I List extension version?
int dataless #I Include dataless image headers?
char ikparams[ARB] #I Image kernel parameters
int err #I Print errors?
int imext #O Image extensions?
int list #O Image list
int i, j, nphu, nextns, fd
pointer sp, temp, patbuf, fname, image, im, immap()
int xt_extns1(), patmake(), gpatmatch(), imtopen(), imtgetim(), open()
errchk xt_extns1, open, immap, delete
begin
call smark (sp)
call salloc (temp, SZ_FNAME, TY_CHAR)
call salloc (patbuf, SZ_FNAME, TY_CHAR)
call salloc (fname, SZ_FNAME, TY_CHAR)
call salloc (image, SZ_FNAME, TY_CHAR)
# Get the list.
list = xt_extns1 (files, exttype, index, extname, extver, lindex,
lname, lver, ikparams, err)
# Check and edit the list.
i = patmake ("\[[01]\]", Memc[patbuf], SZ_FNAME)
nphu = 0
nextns = 0
call mktemp ("@tmp$iraf", Memc[temp], SZ_FNAME)
fd = open (Memc[temp+1], NEW_FILE, TEXT_FILE)
while (imtgetim (list, Memc[fname], SZ_FNAME) != EOF) {
if (dataless == NO) {
iferr (im = immap (Memc[fname], READ_ONLY, 0))
im = NULL
if (im != NULL) {
if (IM_NDIM(im) == 0 || IM_LEN(im,1) == 0) {
call imunmap (im)
next
}
call imunmap (im)
}
}
if (gpatmatch (Memc[fname], Memc[patbuf], i, j) > 0) {
call strcpy (Memc[fname], Memc[image], SZ_FNAME)
call strcpy (Memc[image+j], Memc[image+i-1], SZ_FNAME)
ifnoerr (im = immap (Memc[image], READ_ONLY, 0)) {
call strcpy (Memc[image], Memc[fname], SZ_FNAME)
call imunmap (im)
nphu = nphu + 1
}
}
nextns = nextns + 1
call fprintf (fd, "%s\n")
call pargstr (Memc[fname])
}
call close (fd)
# Return new list and extension flag.
imext = YES
if (nphu == nextns)
imext = NO
call imtclose (list)
list = imtopen (Memc[temp])
call delete (Memc[temp+1])
call sfree (sp)
return (list)
end
# XT_IMEXTNS -- Expand a template of MEF files into a list of image extensions.
int procedure xt_imextns (files, index, extname, extver, lindex, lname, lver,
ikparams, err)
char files[ARB] #I List of MEF files
char index[ARB] #I Range list of extension indexes
char extname[ARB] #I Patterns for extension names
char extver[ARB] #I Range list of extension versions
int lindex #I List index number?
int lname #I List extension name?
int lver #I List extension version?
char ikparams[ARB] #I Image kernel parameters
int err #I Print errors?
int list #O Image list
int xt_extns1()
errchk xt_extns1
begin
list = xt_extns1 (files, "IMAGE", index, extname, extver, lindex,
lname, lver, ikparams, err)
return (list)
end
# XT_EXTNS1 -- Expand a template of MEFs into a list of extensions.
int procedure xt_extns1 (files, exttype, index, extname, extver, lindex,
lname, lver, ikparams, err)
char files[ARB] #I List of MEFs
char exttype[ARB] #I Desired extension type (or null for all)
char index[ARB] #I Range list of extension indexes
char extname[ARB] #I Patterns for extension names
char extver[ARB] #I Range list of extension versions
int lindex #I List index number?
int lname #I List extension name?
int lver #I List extension version?
char ikparams[ARB] #I Image kernel parameters
int err #I Print errors?
int list #O Image list
int i, fd
pointer sp, temp, fname, rindex, rextver, ikp, str
int imtopen(), imtgetim()
int ix_decode_ranges(), decode_ranges(), nowhite(), open()
errchk open, xt_extn, delete
begin
call smark (sp)
call salloc (temp, SZ_FNAME, TY_CHAR)
call salloc (fname, SZ_FNAME, TY_CHAR)
call salloc (ikp, SZ_LINE, TY_CHAR)
call salloc (str, SZ_LINE, TY_CHAR)
# Expand parameters.
list = imtopen (files)
call salloc (rindex, 3*SZ_RANGE, TY_INT)
if (ix_decode_ranges (index, Memi[rindex], SZ_RANGE, i) == ERR)
call error (1, "Bad index range list")
rextver = NULL
if (nowhite (extver, Memc[str], SZ_LINE) > 0) {
call salloc (rextver, 3*SZ_RANGE, TY_INT)
if (decode_ranges (Memc[str], Memi[rextver], SZ_RANGE, i)==ERR)
call error (1, "Bad extension version range list")
}
i = nowhite (ikparams, Memc[ikp], SZ_LINE)
# Expand MEFs into list of extensions in a temp file.
call mktemp ("@tmp$iraf", Memc[temp], SZ_FNAME)
fd = open (Memc[temp+1], NEW_FILE, TEXT_FILE)
while (imtgetim (list, Memc[fname], SZ_FNAME) != EOF) {
call xt_extn (fd, Memc[fname], exttype, rindex, extname,
rextver, lindex, lname, lver, Memc[ikp], err)
}
call imtclose (list)
call close (fd)
# Return list.
list = imtopen (Memc[temp])
call delete (Memc[temp+1])
call sfree (sp)
return (list)
end
# XT_EXTN -- Expand a single MEF into a list of extensions.
# The extensions are written to the input file descriptor.
procedure xt_extn (fd, fname, exttype, indices, extname, extver, lindex,
lname, lver, ikparams, err)
int fd #I File descriptor for list
char fname[SZ_FNAME] #I File name
char exttype[ARB] #I Extension type (or null for all)
pointer indices #I Range list of extension indexes
char extname[ARB] #I Pattern for extension names
pointer extver #I Range list of extension versions
int lindex #I List index number?
int lname #I List extension name?
int lver #I List extension version?
char ikparams[ARB] #I Image kernel parameters
int err #I Print errors?
int i, j, n, index, ver, stat
pointer sp, clust, ksec, imsec, name, str
pointer mef, im
bool streq(), is_in_range(), xt_extmatch()
int mef_rdhdr_exnv(), mef_rdhdr_gn(), ix_get_next_number()
pointer mefopen(), immap()
errchk mefopen, mef_rdhdr_exnv, mef_rdhdr_gn, immap
begin
call smark (sp)
call salloc (clust, SZ_FNAME, TY_CHAR)
call salloc (ksec, SZ_FNAME, TY_CHAR)
call salloc (imsec, SZ_FNAME, TY_CHAR)
call salloc (name, SZ_LINE, TY_CHAR)
call salloc (str, SZ_LINE, TY_CHAR)
# Parse the file name syntax.
call imparse (fname, Memc[clust], SZ_FNAME, Memc[ksec],
SZ_FNAME, Memc[imsec], SZ_FNAME, index, ver)
# Open the file and check the error status.
iferr (mef = mefopen (Memc[clust], READ_ONLY, 0)) {
if (exttype[1] == EOS || !streq (exttype, "IMAGE"))
call fprintf (fd, fname)
else if (streq (exttype, "IMAGE")) {
ifnoerr (im = immap (fname, READ_ONLY, 0)) {
call imunmap (im)
call fprintf (fd, fname)
}
}
return
}
# Loop through extensions.
if (Memc[ksec] != EOS || index >= 0)
n = 1
else
n = ARB
j = index
do i = 1, n {
iferr {
# If a kernel section is given look for the extension/extver.
if (Memc[ksec] != EOS) {
call mef_ksection (Memc[ksec], Memc[name], ver)
stat = mef_rdhdr_exnv (mef, Memc[name], ver)
# If an index is given then look for the indexed extension
} else if (j >= 0)
stat = mef_rdhdr_gn (mef, j)
# If neither is given look for list of indices.
else {
stat = ix_get_next_number (Memi[indices], index)
if (stat != EOF)
stat = mef_rdhdr_gn (mef, index)
}
} then {
# Check if file is an IRAF image.
if (exttype[1] == EOS || !streq (exttype, "IMAGE"))
call fprintf (fd, fname)
else if (streq (exttype, "IMAGE")) {
ifnoerr (im = immap (fname, READ_ONLY, 0)) {
call imunmap (im)
call fprintf (fd, fname)
}
}
stat = EOF
}
# Finish if EOF is encountered in either indices or file.
if (stat == EOF)
break
# Check the extension type.
if (exttype[1] != EOS && !streq (exttype, MEF_EXTTYPE(mef))) {
if (!streq (exttype, "IMAGE") ||
!streq (MEF_EXTTYPE(mef), "SIMPLE")) {
# Check for PLIO mask which is a kind of image.
if (streq (MEF_EXTTYPE(mef), "BINTABLE")) {
call sprintf (Memc[str], SZ_LINE, "%s[%d]")
call pargstr (Memc[clust])
call pargi (MEF_CGROUP(mef))
iferr (im = immap (Memc[str], READ_ONLY, 0))
im = NULL
if (im == NULL)
next
else
call imunmap (im)
}
}
}
# Check the extension name.
if (!xt_extmatch (MEF_EXTNAME(mef), extname))
next
# Check the extension version.
if (extver != NULL) {
if (IS_INDEFI(MEF_EXTVER(mef)))
next
if (!is_in_range (Memi[extver], MEF_EXTVER(mef)))
next
}
# Set the extension name and version.
if (lname == YES)
call strcpy (MEF_EXTNAME(mef), Memc[name], SZ_LINE)
else
Memc[name] = EOS
if (lver == YES)
ver = MEF_EXTVER(mef)
else
ver = INDEFI
# Output the file name with the desired elements.
call fprintf (fd, Memc[clust])
if (lindex == YES || (Memc[name] == EOS && IS_INDEFI(ver))) {
call fprintf (fd, "[%d]")
call pargi (MEF_CGROUP(mef))
}
if (Memc[name] != EOS) {
call fprintf (fd, "[%s")
call pargstr (Memc[name])
if (!IS_INDEFI(ver)) {
call fprintf (fd, ",%d")
call pargi (ver)
}
if (ikparams[1] != EOS) {
call fprintf (fd, ",%s")
call pargstr (ikparams)
}
call fprintf (fd, "]")
} else if (!IS_INDEFI(ver)) {
call fprintf (fd, "[extver=%d")
call pargi (ver)
if (ikparams[1] != EOS) {
call fprintf (fd, ",%s")
call pargstr (ikparams)
}
call fprintf (fd, "]")
} else if (ikparams[1] != EOS) {
call fprintf (fd, "[%s]")
call pargstr (ikparams)
}
if (Memc[imsec] != EOS) {
call fprintf (fd, "%s")
call pargstr (Memc[imsec])
}
call fprintf (fd, "\n")
}
# Finish up.
call mefclose (mef)
call sfree (sp)
end
include <mach.h>
include <ctype.h>
define FIRST 0 # Default starting range
define LAST MAX_INT # Default ending range
define STEP 1 # Default step
define EOLIST -1 # End of list
# IX_DECODE_RANGES -- Parse a string containing a list of integer numbers or
# ranges, delimited by either spaces or commas. Return as output a list
# of ranges defining a list of numbers, and the count of list numbers.
# Range limits must be positive nonnegative integers. ERR is returned as
# the function value if a conversion error occurs. The list of ranges is
# delimited by EOLIST.
int procedure ix_decode_ranges (range_string, ranges, max_ranges, nvalues)
char range_string[ARB] # Range string to be decoded
int ranges[3, max_ranges] # Range array
int max_ranges # Maximum number of ranges
int nvalues # The number of values in the ranges
int ip, nrange, first, last, step, ctoi()
begin
ip = 1
nvalues = 0
do nrange = 1, max_ranges - 1 {
# Defaults to all nonnegative integers
first = FIRST
last = LAST
step = STEP
# Skip delimiters
while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
ip = ip + 1
# Get first limit.
# Must be a number, '-', 'x', or EOS. If not return ERR.
if (range_string[ip] == EOS) { # end of list
if (nrange == 1) {
# Null string defaults
ranges[1, 1] = first
ranges[2, 1] = last
ranges[3, 1] = step
ranges[1, 2] = EOLIST
nvalues = MAX_INT
return (OK)
} else {
ranges[1, nrange] = EOLIST
return (OK)
}
} else if (range_string[ip] == '-')
;
else if (range_string[ip] == 'x')
;
else if (IS_DIGIT(range_string[ip])) { # ,n..
if (ctoi (range_string, ip, first) == 0)
return (ERR)
} else
return (ERR)
# Skip delimiters
while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
ip = ip + 1
# Get last limit
# Must be '-', or 'x' otherwise last = first.
if (range_string[ip] == 'x')
;
else if (range_string[ip] == '-') {
ip = ip + 1
while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
ip = ip + 1
if (range_string[ip] == EOS)
;
else if (IS_DIGIT(range_string[ip])) {
if (ctoi (range_string, ip, last) == 0)
return (ERR)
} else if (range_string[ip] == 'x')
;
else
return (ERR)
} else
last = first
# Skip delimiters
while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
ip = ip + 1
# Get step.
# Must be 'x' or assume default step.
if (range_string[ip] == 'x') {
ip = ip + 1
while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
ip = ip + 1
if (range_string[ip] == EOS)
;
else if (IS_DIGIT(range_string[ip])) {
if (ctoi (range_string, ip, step) == 0)
;
if (step == 0)
return (ERR)
} else if (range_string[ip] == '-')
;
else
return (ERR)
}
# Output the range triple.
ranges[1, nrange] = first
ranges[2, nrange] = last
ranges[3, nrange] = step
nvalues = nvalues + abs (last-first) / step + 1
}
return (ERR) # ran out of space
end
# IX_GET_NEXT_NUMBER -- Given a list of ranges and the current file number,
# find and return the next file number. Selection is done in such a way
# that list numbers are always returned in monotonically increasing order,
# regardless of the order in which the ranges are given. Duplicate entries
# are ignored. EOF is returned at the end of the list.
int procedure ix_get_next_number (ranges, number)
int ranges[ARB] # Range array
int number # Both input and output parameter
int ip, first, last, step, next_number, remainder
begin
# If number+1 is anywhere in the list, that is the next number,
# otherwise the next number is the smallest number in the list which
# is greater than number+1.
number = number + 1
next_number = MAX_INT
for (ip=1; ranges[ip] != EOLIST; ip=ip+3) {
first = min (ranges[ip], ranges[ip+1])
last = max (ranges[ip], ranges[ip+1])
step = ranges[ip+2]
if (step == 0)
call error (1, "Step size of zero in range list")
if (number >= first && number <= last) {
remainder = mod (number - first, step)
if (remainder == 0)
return (number)
if (number - remainder + step <= last)
next_number = number - remainder + step
} else if (first > number)
next_number = min (next_number, first)
}
if (next_number == MAX_INT)
return (EOF)
else {
number = next_number
return (number)
}
end
# XT_EXTMATCH -- Match extname against a comma-delimited list of patterns.
bool procedure xt_extmatch (extname, patterns)
char extname[ARB] #I Extension name to match
char patterns[ARB] #I Comma-delimited list of patterns
bool stat #O Match?
int i, j, k, sz_pat, strlen(), patmake(), patmatch(), nowhite()
pointer sp, patstr, patbuf
begin
if (patterns[1] == EOS)
return (true)
stat = false
sz_pat = strlen (patterns)
if (sz_pat == 0)
return (stat)
sz_pat = sz_pat + SZ_LINE
call smark (sp)
call salloc (patstr, sz_pat, TY_CHAR)
call salloc (patbuf, sz_pat, TY_CHAR)
i = nowhite (patterns, Memc[patstr], sz_pat)
if (i == 0)
stat = true
else if (i == 1 && Memc[patstr] == '*')
stat = true
else {
i = 1
for (j=i;; j=j+1) {
if (patterns[j] != ',' && patterns[j] != EOS)
next
if (j > 0 && patterns[j] == ',' && patterns[j-1] == '\\')
next
if (j - i > 0) {
if (j-i == 1 && patterns[i] == '*') {
stat = true
break
}
call strcpy (patterns[i], Memc[patstr+1], j-i)
Memc[patstr] = '^'
Memc[patstr+j-i+1] = '$'
Memc[patstr+j-i+2] = EOS
k = patmake (Memc[patstr], Memc[patbuf], sz_pat)
if (patmatch (extname, Memc[patbuf]) > 0) {
stat = true
break
}
}
if (patterns[j] == EOS)
break
i = j + 1
}
}
call sfree (sp)
return (stat)
end
|