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
|
/* Fitsy FITS routines to read and write FITS headers.
*/
#include <xos.h>
#include "fitsy.h"
#define MAX_COPY 1024
static int ncopy_file=0;
static File copy_files[MAX_COPY];
static int GetWordType(fptr, tbuf, cflag)
char **fptr;
char *tbuf;
int *cflag;
{
int alpha, digit, other, got;
char *f, *t;
/* start out clean */
*tbuf = '\0';
/* gather up characters until we have a delim */
for(got=0, alpha=0, digit=0, other=0, f=*fptr, *tbuf='\0', t=tbuf;
(*f!='\0') && (*f!=',') && (*f!=']');
got++){
if( isdigit((unsigned int)*f) ){
digit++;
*t++ = *f++;
}
else if( isalpha((unsigned int)*f) || (*f == '_') || (*f == '$') ){
alpha++;
*t++ = *f++;
}
/* special case: extension or index ending with '+' means copy extension */
else if( (*f=='+') && ((*(f+1)=='\0') || (*(f+1)==',') || (*(f+1)==']')) ){
/* don't add '+' to output, but inc past it in input */
f++;
/* we got the 'copy extension' flag */
*cflag = 1;
}
else{
/* - might be part of an extension */
if( (*f=='-') && got && !other )
alpha++;
else
other++;
*t++ = *f++;
}
}
/* null terminate working string */
*t = '\0';
/* update pointer */
*fptr = f;
/* determine what sort of beast we have and return */
if( got == 0 )
return(0);
else if( other >0 )
return(3);
else if( (digit>0) && (alpha==0) )
return(2);
else
return(1);
}
/*
*
* ft_parsefilename -- break a string into name, extension, index, tail
* The possibilities are:
*
* name[extn], name[extn,index], name[index]
* name[extn,tail], name[extn,index,tail], name[index,tail]
*
* where:
* extn is composed of alphanumerics, index is numeric, tail is anything
* and commas are separators.
*
* returns 1 if 'copy flag' was set (+extn, extn+, +index, index+), else 0
*
*
*/
int ft_parsefilename(filename, name, extn, nmaxlen, indx, tail, tmaxlen)
char *filename;
char *name;
char *extn;
int nmaxlen;
int *indx;
char *tail;
int tmaxlen;
{
char *f, *t;
char *tbuf, *tbuf2;
int i, len;
int got;
int cflag;
/* start out clean */
*extn = '\0';
*indx = -1;
*tail = '\0';
cflag = 0;
/* make sure we have something to work with */
if( (filename == NULL) || (*filename == '\0') )
return 0;
/* handle pipe: specification specially */
if( !strncasecmp(filename, "pipe:", 5) ){
strncpy(name, filename, nmaxlen);
return 0;
}
/* basic work buffers */
tbuf = (char *)malloc(strlen(filename)+1);
tbuf2 = (char *)malloc(strlen(filename)+1);
/* this will be our pointer into the filename buffer */
f = filename;
/* maxlen generally is 1 more than we can handle */
nmaxlen--;
tmaxlen--;
/* gather up the name */
for(*tbuf='\0', t=tbuf; (*f!='\0') && (*f!='[') && (*f!=','); )
*t++ = *f++;
/* null terminate working string */
*t = '\0';
/* copy max from the work buffer to the name buffer */
strncpy(name, tbuf, nmaxlen);
name[nmaxlen] = '\0';
/* check for done */
if( *f == '\0' )
goto cleanup;
/* or skip past '[' delim following the name */
else
f++;
/* get token and process type */
switch(GetWordType(&f, tbuf, &cflag)){
/* EOF */
case 0:
break;
/* extn */
case 1:
strcpy(extn, tbuf);
*tbuf = '\0';
if( *f == ',' ){
f++;
switch(GetWordType(&f, tbuf, &cflag)){
case 2:
*indx = strtol(tbuf, NULL, 10);
*tbuf = '\0';
if( *f == ',' )
f++;
break;
default:
break;
}
}
break;
/* index */
case 2:
*indx = strtol(tbuf, NULL, 10);
*tbuf = '\0';
if( *f == ',' )
f++;
break;
/* tail */
case 3:
break;
}
/* at this point, we are processing tail */
strcpy(tbuf2, "[");
if( *tbuf != '\0' )
strcat(tbuf2, tbuf);
if( *f != '\0' )
strcat(tbuf2, f);
strncpy(tail, tbuf2, tmaxlen);
tail[tmaxlen] = '\0';
/* get rid of any empty brackets */
len = strlen(tail);
for(i=0; i<len; i++){
if( (tail[i] == '[') && (i+2 < len) && (tail[i+1] == ']') ){
strcpy(tbuf, &(tail[i+2]));
strcpy(&(tail[i]), tbuf);
}
}
/* but don't bother passing back completely null brackets */
len = strlen(tail);
for(got=0, i=0; i<len; i++){
if( (tail[i] != '[') && (tail[i] != ']') && (tail[i] != ' ') ){
got = 1;
break;
}
}
if( !got ) *tail = '\0';
/* clean up */
cleanup:
if( tbuf != NULL )
free(tbuf);
if( tbuf2 != NULL )
free(tbuf2);
return cflag;
}
/*
*
* ft_cfile -- change the static copy_file handle
* This is used to set/unset the copy handle for when we open a fits file
* (There is no other good place to store this copy handle!)
* returns: old handle
*
*/
int ft_cfile(cfile)
File cfile;
{
if( cfile ){
if( ncopy_file >= MAX_COPY ) return -1;
copy_files[ncopy_file++] = cfile;
}
else{
ncopy_file = 0;
}
return ncopy_file;
}
/* Read a FITS header from the named file -- primitive of ft_fitsheadopen()
*/
File ft_fitsheadread(file, filename, header, mode, iline, ifd)
File file;
char *filename; /* FITS image file name. */
FITSHead *header;
char *mode;
char iline[FT_CARDLEN];
File *ifd;
{
FITSHead prim = NULL;
FITSHead head = NULL;
char name[FT_FILENAME];
char extn[FT_FILENAME];
char tail[FT_FILENAME];
char tbuf[FT_CARDLEN];
char *e;
char *i;
char *tptr;
int indx = 0;
int len = FT_FILENAME;
int tlen;
int n;
int nc;
static int vlaerr=0;
/* if the user called the wrong routine, fix it */
if( !file ) return NULL;
/* initialize return values */
if( iline ) iline[0] = '\0';
if( ifd ) *ifd = file;
/* parse filename into extension, index, and tail */
ft_parsefilename(filename, name, extn, len, &indx, tail, len);
/* make sure this is a FITS file by looking at the first card */
tptr = (iline ? iline : tbuf);
if( ( !ftGets(file, tptr, 10) ) ||
(strncmp(tptr, "SIMPLE =", 9) && strncmp(tptr, "XTENSION=", 9)) ){
/* pass back opened file so someone else can make an attempt */
if( !ifd ) ftClose(file);
return NULL;
}
tlen = strlen(tptr);
if( ftRead(file, &tptr[tlen], FT_CARDLEN-tlen, 1) != 1 ){
/* pass back opened file so someone else can make an attempt */
if( !ifd ) ftClose(file);
return NULL;
}
/* don't return anything anymore, both ifd and line were used already */
if( iline ){
memcpy(tbuf, iline, FT_CARDLEN);
iline[0] = '\0';
}
if( ifd ) *ifd = NULL;
/* read primary header */
if ( (prim = ft_headread0(file, tbuf, FT_CARDLEN)) == NULL ){
ftClose(file);
return NULL;
}
/* Only look in the environment if the primary header
has no data and the user hasn't specified any thing
in the brackets.
*/
if ( ft_databytes(prim) == 0 && !*extn && indx == -1 ) {
if ( (e = (char *)getenv("FITS_EXTNAME")) ) {
strncpy(extn, e, len);
extn[len-1] = '\0';
}
if ( (i = (char *)getenv("FITS_EXTNUM")) ) {
indx = atoi(i);
}
}
head = prim;
if ( *extn ) { /* Extension and possible an index */
do {
char *ext=NULL;
char *extcopy=(char *)strdup(extn);
for(ext=(char *)strtok(extcopy," :;,");
ext!=NULL;
ext=(char *)strtok(NULL," :;,")){
/* look for extname, then hduname, along with indx */
if ( ((ft_name(head) && *ft_name(head) &&
!strcasecmp(ext, ft_name(head))) ||
(ft_hduname(head) && *ft_hduname(head) &&
!strcasecmp(ext, ft_hduname(head)))) &&
(indx == -1 || indx == ft_extn(head)) )
break;
}
if( extcopy )
free(extcopy);
/* if we matched an extension, we are done */
if( ext )
break;
/* copy header, if necessary */
if( ncopy_file ){
for(nc=0; nc<ncopy_file; nc++)
ft_headwrite(copy_files[nc], head);
}
/* skip data, copying header if necessary */
ft_dataskip(file, head, copy_files, ncopy_file);
if ( head != prim ) ft_headfree(head, 1);
} while ( (head = ft_headread(file)) );
if ( !head ) {
ft_headfree(prim, 1);
ftClose(file);
return NULL;
}
} else if ( indx > 0 ) { /* Extension index, not == 0 */
int i;
/* copy header, if necessary */
if( ncopy_file ){
for(nc=0; nc<ncopy_file; nc++)
ft_headwrite(copy_files[nc], head);
}
ft_dataskip(file, prim, copy_files, ncopy_file);
for ( i = 1; i <= indx; i++ ) {
if ( (head = ft_headread(file)) == NULL ) {
ft_headfree(prim, 1);
ftClose(file);
return NULL;
}
if( i != indx ){
/* copy header, if necessary */
if( ncopy_file ){
for(nc=0; nc<ncopy_file; nc++)
ft_headwrite(copy_files[nc], head);
}
/* skip data, copying header if necessary */
ft_dataskip(file, head, copy_files, ncopy_file);
ft_headfree(head, 1);
}
}
}
if ( head != prim ){
ft_primlink(prim, head); /* Link the primary to the header */
/* this primary header only exists as part of head */
prim->rfcount = 1;
}
/* if this is a table and contains a variable length array, issue wanring */
if( head && head->table && !vlaerr ){
for ( n = 1; n <= head->table->tfields; n++ ) {
if ( head->table->col[n-1].vla ){
fprintf(stderr,
"WARNING: The 'variable length array' is not part of the FITS Bintable standard. ");
fprintf(stderr,
"Funtools will pass it along, but will not process it.\n");
vlaerr++;
break;
}
}
}
if( header != NULL ){
*header = head;
}
return file;
}
/* Read a FITS header from the named file.
*/
File ft_fitsheadopen(filename, header, tail, len, mode)
char *filename; /* FITS image file name. */
FITSHead *header;
char *tail;
int len;
char *mode;
{
return ft_fitsheadopenfd(filename, header, tail, len, mode, NULL,
NULL, NULL);
}
/* Read a FITS header from the named file.
*/
File ft_fitsheadopenfd(filename, header, tail, len, mode, file, iline, ifd)
char *filename; /* FITS image file name. */
FITSHead *header;
char *tail;
int len;
char *mode;
File file;
char iline[FT_CARDLEN];
File *ifd;
{
char *xmode;
char name[FT_FILENAME];
char extn[FT_FILENAME];
int indx = 0;
int cflag;
/* initialize return values */
if( iline ) iline[0] = '\0';
if( ifd ) *ifd = NULL;
/* parse filename into extension, index, and tail */
cflag = ft_parsefilename(filename, name, extn, FT_FILENAME, &indx,
tail, len);
/* set mode or use 'r' as default */
if( mode ){
xmode = mode;
}
else
xmode = "r";
/* see if 'CopyExtension' mode was explicitly set in the mode */
if( strchr(xmode, 'C') )
cflag = 1;
/* else see if it was set in the section and is permitted by the mode */
else if( strchr(xmode, 'c') && cflag )
cflag = 1;
/* else we do not copy */
else
cflag = 0;
if( !file ){
if ( (file = ftOpen(name, xmode)) == NULL )
return NULL;
}
/* if the "copy extension" flag was set, we can exit here */
if( cflag ){
/* flag header was not created */
if( header )
*header = NULL;
return file;
}
/* now read the file up to the specified extension */
file = ft_fitsheadread(file, filename, header, xmode, iline, ifd);
/* save filename */
if( file && header && *header && *name ){
if( (*header)->filename ) (void)Free((*header)->filename);
(*header)->filename = strdup(name);
}
return file;
}
|