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

#define NDAT 500

void printerror( int status);

int main(int argc,char *argv[])
{
  fitsfile *fp;
  int i,j,status,nfound,anynull,imax;
  long naxes[2]={NDAT, NDAT};
  float nullval;
  char array[NDAT],*buffer=array;

  if(argc!=2){
    printf("Usage: %s input\n",argv[0]);
    exit(1);
  }
  fprintf(stderr,"Read %s  ... ",argv[1]);
  status=0;
  if(fits_open_table(&fp, argv[1], READONLY, &status))
    printerror(status);
  if(fits_read_keys_lng(fp, "NAXIS", 1, 2, naxes, &nfound, &status)) 
    printerror(status);
  fprintf(stderr,"%ld x %ld format \n",naxes[0],naxes[1]);
  nullval = 0;
  if(fits_read_col(fp, TSTRING, 16, 1, 1, 1LL, &nullval, &buffer, &anynull, &status)) imax=14;
  else imax=16;
  status=0;
  for (j = 1; j <= naxes[1]; j++) {
  for(i=1;i<=imax;i++){
    if (fits_read_col(fp, TSTRING, i, j, 1, 1LL, &nullval, &buffer, &anynull, &status))
      printerror(status);
    printf("%s |", buffer);
    if(imax==14 && i==8) printf("     0.0 |     0.0 |");
  }
  printf("\n");
  }
  if(fits_close_file(fp, &status))
    printerror(status);
  fprintf(stderr,"OK\n");
  return(0);
}

void printerror( int status)
{
    /*****************************************************/
    /* Print out cfitsio error messages and exit program */
    /*****************************************************/

    char status_str[FLEN_STATUS], errmsg[FLEN_ERRMSG];
  
    if (status)
      fprintf(stderr, "\n*** Error occurred during program execution ***\n");

    fits_get_errstatus(status, status_str);   /* get the error description */
    fprintf(stderr, "\nstatus = %d: %s\n", status, status_str);

    /* get first message; null if stack is empty */
    if ( fits_read_errmsg(errmsg) ) 
    {
         fprintf(stderr, "\nError message stack:\n");
         fprintf(stderr, " %s\n", errmsg);

         while ( fits_read_errmsg(errmsg) )  /* get remaining messages */
             fprintf(stderr, " %s\n", errmsg);
    }

    exit( status );       /* terminate the program, returning error status */
}
