#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 insertcolumn(char *filename,  char *colname , char *colform, int cols) ;

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

   
    writeascii    - write a FITS ASCII table extension
   
**************************************************************************/
  int  rows, cols ;
  if (argc != 5)
    {
      printf("report_error_status:102\n") ;
      exit(0) ;
    }
   
  cols = (int)atoi(argv[4]) ;
  insertcolumn(argv[1],  argv[2],  argv[3], cols );
  return(0);
}

/*--------------------------------------------------------------------------*/
void insertcolumn(char *filename,  char *colname , char *colform, int cols)
{
  fitsfile *fptr;       /* pointer to the FITS file, defined in fitsio.h */
   

    char *field ;
    int status, hdunum, hdutype;
    
    status=0;

    /* open with write access the FITS file containing a primary array */
    fits_open_file(&fptr, filename, READWRITE, &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) ; 
      }

  fits_insert_col( fptr,  (cols+1), colname, colform,&status);
   if(status)
        {
          printf("report_error_status:%d\n", status) ;
          exit(0) ; 
        }  	 
    

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

