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


#include "fitsio.h"

int main( int argc, char *argv[] );
void tabview(char *filename, int cols);



int main(int argc, char *argv[])
{
/*************************************************************************
   This is a simple main program that calls the following routines:
   readtable     - read columns of data from ASCII and binary tables

**************************************************************************/
  int cols ;
   if (argc < 3)
    {
      printf("Error in execution of 'rd_col_name.c'. Either Filename or hdu number is missing..") ;
      exit(0) ;  
    }  

   if (argc > 3)
    {
      printf("Error in execution of 'rd_col_name.c'. Extra Arguments found.") ;
      exit(0) ;  
    }    
 
  cols = (int)atoi(argv[2]) ;
  tabview(argv[1], cols) ;
  return(0);
}



void  tabview(char *filename, int cols) 
{
    fitsfile *fptr;       /* pointer to the FITS file, defined in fitsio.h */
    int status=0,nfound,hdutype,ii, hdunum ;  
    char **ttype ;
 
    fits_open_file(&fptr, filename, READONLY, &status) ;
  if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }  

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

  /* Finding out ttype of each field. */ 
  ttype = (char **)malloc(cols * sizeof(char *)) ;
  for (ii = 0; ii < cols; ii++)      /* allocate space for the column labels */
        ttype[ii] = (char *) malloc(FLEN_VALUE);  /* max label length = 69 */
  
   fits_read_keys_str(fptr, "TTYPE", 1, cols, ttype, &nfound, &status) ;
    if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }  

   for(ii=0; ii < cols; ii++)
     {
       printf("\n%s\n", ttype[ii]) ;
     }

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

}



