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

/*
  Every program which uses the CFITSIO interface must include the
  the fitsio.h header file.  This contains the prototypes for all
  the routines and defines the error status values and other symbolic
  constants used in the interface.  
*/
#include "fitsio.h"
int main( int argc, char *argv[]);
void readheader( char *hdunum );


fitsfile *fptr;       /* pointer to the FITS file, defined in fitsio.h */

int status=0, nkeys, keypos, hdutype, ii, jj;
char filename[80];     /* name of existing FITS file   */
char keyname[80], value[80], comment[80] ;


int main(int argc, char *argv[])
{
/*************************************************************************
   This is a simple main program that calls the following routines:

   
    readheader    - read and print the header keywords in every extension
   

**************************************************************************/
  if(argc != 3)
    {
       printf("report_error_status:102\n") ;
       exit(0) ;
     } 
 
 
 strcpy(filename, argv[1]) ;
 
 fits_open_file(&fptr, filename, READONLY, &status) ;
   if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }  

         
   
    readheader(argv[2]);
    if (status == END_OF_FILE)   /* status values are defined in fitsioc.h */
        status = 0;              /* got the expected EOF error; reset = 0  */
    else
    {
       printf("report_error_status:%d\n", status) ;
       exit(0) ; 
    }     /* got an unexpected error                */

    fits_close_file(fptr, &status) ;
    if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }     
        


    printf("\n\n");
    return(0);
}

/*--------------------------------------------------------------------------*/
void readheader ( char *hdunum)

    /**********************************************************************/
    /* Print out all the header keywords in all extensions of a FITS file */
    /**********************************************************************/
{
   
  ii = (int)atoi(hdunum) ;
    
    /* attempt to move to next HDU, until we get an EOF error */
    if ( !(fits_movabs_hdu(fptr, ii, &hdutype, &status))) 
    {
        /* get no. of keywords */
      fits_get_hdrpos(fptr, &nkeys, &keypos, &status) ;
        if(status)
        {
          printf("report_error_status:%d\n", status) ;
          exit(0) ; 
        }   

        printf("Header listing for HDU #%d:\n", ii);
        for (jj = 1; jj <= nkeys; jj++)  {
	  fits_read_keyn(fptr, jj, keyname,value, comment,  &status) ;
             if(status)
              {
                 printf("report_error_status:%d\n", status) ;
                 exit(0) ; 
              }   

            printf("%s = %s / %s\n", keyname, value, comment); /* print the keyword card */
        }
        printf("END\n\n");  /* terminate listing with END */
    }
    else
      {
           
             printf("report_error_status:%d\n", status) ;
             exit(0) ; 
             
      }
    return;
}


