File: dfits.c

package info (click to toggle)
qfits 6.2.0-8
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 2,976 kB
  • ctags: 1,019
  • sloc: ansic: 11,631; sh: 8,330; makefile: 131
file content (377 lines) | stat: -rw-r--r-- 11,025 bytes parent folder | download
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
/* $Id: dfits.c,v 1.9 2006/09/21 14:20:06 yjung Exp $
 *
 * This file is part of the ESO QFITS Library
 * Copyright (C) 2001-2004 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Author: yjung $
 * $Date: 2006/09/21 14:20:06 $
 * $Revision: 1.9 $
 * $Name: qfits-6_2_0 $
 */

/*-----------------------------------------------------------------------------
                                Includes
 -----------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "qfits_card.h"

/*-----------------------------------------------------------------------------
                                Define
 -----------------------------------------------------------------------------*/

#define BLOCK_SIZE  2880
#define LGTH        80
#define MAGIC       "SIMPLE  ="
#define MAGICEXT    "XTENSION="

/* If compiled against zlib, include support for gzipped files */
#if HAVE_ZLIB
#include "zlib.h"
#define FILE            gzFile
#define fopen           gzopen
#define fclose          gzclose
#define fread(b,s,n,f)  gzread(f,b,n*s)
#endif

/*-----------------------------------------------------------------------------
                                Function prototypes
 -----------------------------------------------------------------------------*/

void usage(char * pname) ;
int parse_cmd_line(int, char **, int *, int *, int *) ;
int dump_fits_filter(FILE *, int) ;
int dump_hdr_filter(FILE *, int) ;
int dump_fits(char *, int) ;
char * rstrip(char *) ;

/*-----------------------------------------------------------------------------
                                Main
 -----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
    int        xtnum ;
    int        c_arg ;
    int        filter ;
    int        err ;

    /* Parse command-line options */
    if (!parse_cmd_line(argc, argv, &xtnum, &filter, &c_arg))
        usage(argv[0]);

    /* Filter mode: process data received from stdin */
    if (filter) {
#if HAVE_ZLIB
        printf("filter mode does not support gzipped files\n");
        printf("use: gunzip -c file.fits | dfits -\n");
        return 1 ;
#else
        return dump_fits_filter(stdin, xtnum);
#endif
    }

    /* Normal mode: loop on all file names given on command-line */
    err = 0 ;
    while (c_arg < argc) {
        err += dump_fits(argv[c_arg], xtnum);
        c_arg++;
    }
    return err ; /* Returns number of errors during process */
}

void usage(char * pname)
{
    printf(
"\n\n"
"usage: %s [-x xtnum] <list of FITS files>\n"
"usage: %s [-x xtnum] -\n"
"\n"
"The former version expects file names.\n"
"The latter expects data coming in from stdin.\n"
"\n"
"-x xtnum specifies the extension header to print\n"
"-x 0     specifies main header + all extensions\n"
"\n\n",
    pname, pname);

#if HAVE_ZLIB
    printf(
"This program was compiled against zlib %s\n"
"This means you can use it with gzipped FITS files\n"
"as with uncompressed FITS files.\n"
"NB: this does not apply to the '-' option (input from stdin)\n"
"\n\n", ZLIB_VERSION);
#endif
    exit(1) ;
}

int parse_cmd_line(
        int         argc,
        char    **  argv,
        int     *    xtnum,
        int     *    filter,
        int     *    c_arg)
{
    *filter = 0 ;
    *xtnum  = -1 ;
    *c_arg  = argc-1 ;

    /* No arguments, return an error */
    if (argc<2) return 0;

    /* If '-' is on the command-line, it must be the last argument */
    if (!strcmp(argv[argc-1], "-")) *filter = 1 ;
    /* If -x xtnum is on the command-line, it must be the first two arguments */
    if (!strcmp(argv[1], "-x")) {
        char *endptr;
        if (argc < 3) return 0;
        *xtnum = strtol(argv[2], &endptr, 10);
        if (*endptr != 0) return 0;
        *c_arg = 3 ;
    } else {
        *c_arg = 1 ;
    }
    return 1;
}

/* Strip off all blank characters in a string from the right-side. */
char * rstrip(char * s)
{
    int len ;
    if (s==NULL) return s ;
    len = strlen(s);
    if (len<1) return s ;
    len -- ;
    while (s[len]== ' ') {
        s[len]=(char)0 ;
        len --;
        if (len<0) break ;
    }
    return s ;
}

/* Dump the requested header (main or extension) from a filename. */
int dump_fits(char * name, int xtnum)
{
    FILE    *    in ;
    int            err ;

    if ((in=fopen(name, "r"))==NULL) {
        fprintf(stderr, "error: cannot open file [%s]\n", name);
        return 1 ;
    }

    printf("====> file %s (main) <====\n", name) ;
    err = dump_fits_filter(in, xtnum);
    fclose(in);
    return err ;
}

/* Dump the requested header (main or extension) from a FILE * */
int dump_fits_filter(FILE * in, int xtnum)
{
    int            n_xt ;
    char        buf[LGTH+2];
    int            err ;
    int         data_bytes, naxis ;
    char    *   read_val ;
    int         skip_blocks ;
    int         seeked ;
    int         hdr, hdr_type ;
    int         i ;

    /* Try getting the first LGTH+1 chars */
    memset(buf, 0, LGTH+2);
    if (fread(buf, sizeof(char), LGTH+1, in)!=LGTH+1) {
        fprintf(stderr, "error reading input\n");
        return 1;
    }
    rewind(in) ;
    
    /* If the (char) 10 (LINE FEED) is there, the file is a HDR file */
    hdr = 0 ;
    for (i=0 ; i<LGTH+2 ; i++) {
        if ((int)(buf[i]) == 10) {
            hdr = 1 ;
            break ;
        }
    }
    if (hdr) {
        if (!strncmp(buf, MAGIC, strlen(MAGIC))) {
            hdr_type = 0 ;
        } else if (!strncmp(buf, MAGICEXT, strlen(MAGICEXT))) {
            hdr_type = 1 ;
        } else {
            fprintf(stderr, "not a HDR file\n");
            return 1 ;
        }
        dump_hdr_filter(in, hdr_type) ;
        return 0 ;
    }
    
    /* Check that it is indeed FITS */
    if (strncmp(buf, MAGIC, strlen(MAGIC))) {
        fprintf(stderr, "not a FITS file\n");
        return 1 ;
    }
    naxis = 0 ;
    data_bytes = 1 ;
    memset(buf, 0, LGTH+2);
    if (xtnum<1) {
        /* Output main header */
        data_bytes = 1 ;
        naxis = 0 ;
        while ((err=fread(buf, sizeof(char), LGTH, in))==LGTH) {
            printf("%s\n", rstrip(buf));
            /* Look for BITPIX keyword */
            if (buf[0]=='B' &&
                buf[1]=='I' &&
                buf[2]=='T' &&
                buf[3]=='P' &&
                buf[4]=='I' &&
                buf[5]=='X' &&
                buf[6]==' ') {
                read_val = qfits_getvalue(buf);
                data_bytes *= (int)atoi(read_val) / 8 ;
                if (data_bytes<0) data_bytes *= -1 ;
            } else
            /* Look for NAXIS keyword */
            if (buf[0]=='N' &&
                buf[1]=='A' &&
                buf[2]=='X' &&
                buf[3]=='I' &&
                buf[4]=='S') {

                if (buf[5]==' ') {
                    /* NAXIS keyword */
                    read_val = qfits_getvalue(buf);
                    naxis = (int)atoi(read_val);
                } else {
                    /* NAXIS?? keyword (axis size) */
                    read_val = qfits_getvalue(buf);
                    data_bytes *= (int)atoi(read_val);
                }
            } else
            /* Look for END keyword */
            if (buf[0]=='E' &&
                buf[1]=='N' &&
                buf[2]=='D') {
                break ;
            }
        }
        if (err!=LGTH) return 1 ;
    }
    if (xtnum<0) return 0 ;

    n_xt=0 ;
    while (1) {
        /*
         * Skip the previous data section if pixels were declared
         */
        if (naxis>0) {
            /* Skip as many blocks as there are declared pixels */
            skip_blocks = data_bytes/BLOCK_SIZE ;
            if ((data_bytes % BLOCK_SIZE)!=0) skip_blocks ++ ;
            seeked = fseek(in, skip_blocks*BLOCK_SIZE, SEEK_CUR);
            if (seeked<0) return -1 ;
        }

        /* Look for next XTENSION keyword */
        while ((err=fread(buf, sizeof(char), LGTH, in))==LGTH) {
            if (buf[0]=='X' &&
                buf[1]=='T' &&
                buf[2]=='E' &&
                buf[3]=='N' &&
                buf[4]=='S' &&
                buf[5]=='I' &&
                buf[6]=='O' &&
                buf[7]=='N') break ;
        }
        if (err==0)    break ;
        if (err!=LGTH) return 1 ;

        n_xt++ ;
        
        if (xtnum==0 || xtnum==n_xt) {
            printf("===> xtension %d\n", n_xt) ;
            printf("%s\n", rstrip(buf));
        }

        data_bytes = 1 ;
        naxis = 0 ;
        while ((err=fread(buf, sizeof(char), LGTH, in))==LGTH) {
            if (xtnum==0 || xtnum==n_xt) printf("%s\n", rstrip(buf));

            /* Look for BITPIX keyword */
            if (buf[0]=='B' &&
                buf[1]=='I' &&
                buf[2]=='T' &&
                buf[3]=='P' &&
                buf[4]=='I' &&
                buf[5]=='X' &&
                buf[6]==' ') {
                read_val = qfits_getvalue(buf);
                data_bytes *= (int)atoi(read_val) / 8 ;
                if (data_bytes<0) data_bytes *= -1 ;
            } else
            /* Look for NAXIS keyword */
            if (buf[0]=='N' &&
                buf[1]=='A' &&
                buf[2]=='X' &&
                buf[3]=='I' &&
                buf[4]=='S') {

                if (buf[5]==' ') {
                    /* NAXIS keyword */
                    read_val = qfits_getvalue(buf);
                    naxis = (int)atoi(read_val);
                } else {
                    /* NAXIS?? keyword (axis size) */
                    read_val = qfits_getvalue(buf);
                    data_bytes *= (int)atoi(read_val);
                }
            } else 
            /* Look for END keyword */
            if (buf[0]=='E' &&
                buf[1]=='N' &&
                buf[2]=='D') break ;
        }
        if (n_xt==xtnum) break ;
    }
    return 0 ;
}

/* Dump the requested header (main or extension) from a HDR FILE * */
int dump_hdr_filter(FILE * in, int type)
{
    char        buf[1];
    int         err ;

    if (type == 0)      {}
    else if (type == 1) printf("===> xtension HDR\n") ;
    else return 1 ;
    
    while ((err=fread(buf, sizeof(char), 1, in))==1) {
        printf("%c", buf[0]);
    }
    return 0 ;
}