#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( int argc, char *argv[] );


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 < 2)
    {
      printf("Error in execution of 'checkimage.c'. Filename not found..") ;
      exit(0) ;  
    }  

   if (argc > 2)
    {
      printf("Error in execution of 'checkimage.c'. Extra Arguments found. Filename might have spaces..") ;
      exit(0) ;  
    }   
   
    readheader(argc, argv);
    printf("\n\n");
    return(0);
}

/*--------------------------------------------------------------------------*/
void readheader ( int argc, char *argv[])

    /**********************************************************************/
    /* Print out all the header keywords in all extensions of a FITS file */
    /**********************************************************************/
{
    fitsfile *fptr;       /* pointer to the FITS file, defined in fitsio.h */

    int status, hdutype, ii, count, hdunum;
    char filename[150] ;
     /* name of existing FITS file   */
    
    char dimension[80] , extensionname[80], naxis1[80], naxis2[80],type[80],naxis[80] ;
    char comment[80] ;
    int flag=0  ;
    strcpy(filename , argv[1]); 
    status = 0;
    
    printf("\n%s\n", argv[1]) ;
    fits_open_file(&fptr, argv[1], READONLY, &status) ;
    if(status)
      {
        printf("\n///report_error_status:%d\n", status) ;
        exit(0) ; 
      }
    /*    printf("\n\tIndex\tType\t\tExtension\tDimension\n") ;*/
     
     fits_get_num_hdus(fptr, &hdunum,&status) ;
   if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }  

    /* attempt to move to next HDU, until we get an EOF error */
    count = 0 ;  
    for (ii = 1; ii<= hdunum; ii++) 
    {  
      fits_movabs_hdu(fptr, ii, &hdutype, &status) ;
      if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }
      strcpy (dimension, " ")  ;
        if (ii == 1)   /*If it is a primary header. */
	{
	  
           fits_read_key(fptr, TSTRING, "NAXIS",naxis,comment, &status ) ;
           if(status)
           {
              printf("report_error_status:%d\n", status) ;
              exit(0) ; 
           }
           printf("%s",naxis) ;
        }
    }

     fits_close_file(fptr, &status) ;       /* close the FITS file */
      if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }  
        
}


