Subject: FITS header display in ANSI C

From: Nicolas Devillard <nDevil at eso.org>
Date: Mon, 26 Feb 1996 14:54:55 +0100

Following is a ANSI C FITS header viewer. It should work/compile with
any ANSI C compiler. You may have to replace <string.h> by <strings.h>
on your system. Let me know if you have problems with it.

I usually use a bourne-shell script by Steve Allen, but seems like the dd
options are not universal... This should work almost anywhere, I guess.

Enjoy !

Nicolas

/*--------------------------------------------------------------------------*/
/*                      European Southern Observatory                       */
/*--------------------------------------------------------------------------*/
/* File name    :   dfits.c                                                 */
/* Author       :   Nicolas Devillard                                       */
/* Created on   :   Feb 26, 1996                                            */
/* Language     :   ANSI C                                                  */
/* Description  :   FITS header display                                     */
/*--------------------------------------------------------------------------*/

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

#define LGTH    80
#define MAGIC   "SIMPLE  ="
/* Just check for the presence of SIMPLE=, to allow all kind of files, even */
/* incorrect ones such as SIMPLE=T.                                         */

/*--------------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
    FILE    *in ;
    char    *buf ;
    int     n_arg ;

    /* Give use */
    if (argc<2) {
        printf("%s <list of FITS files...>\n", argv[0]) ;
        return(1) ;
    }
    /* Allocate read buffer */
    if ((buf = (char*)calloc(LGTH,sizeof(char))) == (char*)NULL)
        return(-2) ;

    /* loop on all file names given on command line     */
    /* this enables use of jokers like : dfits *.fits   */
    for (n_arg=1 ; n_arg<argc ; n_arg++) {
        /* open file, if impossible, output error msg and exit  */
        if ((in = fopen(argv[n_arg], "r")) == (FILE*)NULL) {
            printf("cannot open file %s\n", argv[n_arg]) ;
            return(-1) ;
        }
        /* Put name of file if multiple files   */
        if (argc>2)
            printf("====> file %s <====\n", argv[n_arg]) ;
        /* Try to validate fits file type, should start with SIMPLE */
        fread(buf, sizeof(char), LGTH, in) ;
        rewind(in) ;
        if (strncmp(buf, MAGIC, 9)!=0)
            printf("is not a FITS file\n") ;
        else
        /* Output FITS header */
        do {
            fread(buf, sizeof(char), LGTH, in) ;
            /* This cuts lines, enabling tools as grep to work on   */
            /* the output.                                          */
            buf[LGTH-1] = (char)0 ;
            printf("%s\n", buf) ;
        }
        while (strncmp(buf, "END", 3) != 0) ;
        fclose(in) ;
    }
    free(buf) ;
}
/*--------------------------------------------------------------------------*/
-- 
 ______________________________________________________________________
/\                                                                     \
\_| Nicolas Devillard               |    E-mail:  nDevil at eso.org        |
  | European Southern Observatory,  |    http://www.eso.org/~ndevilla   |
  | Karl-Schwarzschild-Str. 2,      |    Phone:  +49 89 320-06-340      |
  | D-85748 Garching, Germany       |                                   |
  |   __________________________________________________________________|_
  \_/____________________________________________________________________/