#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[200];     /* name of existing FITS file   */
char card[FLEN_CARD];   /* standard string lengths defined in fitsioc.h */


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("Error in execution of 'view_specific_hdu.c'. Either Filename or hdu number is missing..") ;
      exit(0) ;  
    }  

   if (argc > 3)
    {
      printf("Error in execution of 'view_specific_hdu.c'. Extra Arguments found.") ;
      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]);
    

    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("\nHeader listing for HDU #%d:\n", ii);
        for (jj = 1; jj <= nkeys; jj++)  {
	fits_read_record(fptr, jj, card, &status) ;

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

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


