#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 createtable( int type, int rows, int cols, char *ttype, char *tform,  char *filename, char *extname);


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 type, rows, cols ;
  if (argc != 8)
    {
      printf("This program requires 7 command line arguments\n") ;
      exit(0) ;
    }
    type = (int)atoi(argv[1]) ;
    rows = (int)atoi(argv[2]) ;
    cols = (int)atoi(argv[3]) ;
    createtable(type, rows, cols, argv[4], argv[5],  argv[6], argv[7]);
    printf("\nThe  table is written successfully.\n");
    return(0);
}

/*--------------------------------------------------------------------------*/
void createtable(int type, int rows, int cols, char *ttype, char *tform,  char *filename , char *extname)
{
    fitsfile *fptr;       /* pointer to the FITS file, defined in fitsio.h */
    char **coltype ;
    char **form ;
    char **unit ;

    char *field ;
    int status,i,j,k,ii, hdutype, hdunum;
    
    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_close_file(fptr, &status) ;       /* close the FITS file */
      if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }  
    
    field  = (char *)malloc(80) ;

    coltype = (char **)malloc(cols * sizeof(char *)) ;
    for (ii = 0; ii < cols; ii++)     
        coltype[ii] = (char *) malloc(80); 

    form = (char **)malloc(cols * sizeof(char *)) ;
    for (ii = 0; ii < cols; ii++)     
        form[ii] = (char *) malloc(80); 

    unit = (char **)malloc(cols * sizeof(char *)) ;
    for (ii = 0; ii < cols; ii++)     
        unit[ii] = (char *) malloc(80); 

 

    /* Separate out the comma separated arguments into one array of strings.*/
    i=0 ;
    for(j = 0 ; j < cols; j++)
    { 
      
      k = 0 ;
      while(ttype[i] != ',')
	{
          field[k] = ttype[i] ;
          i++ ;
          k++ ; 
        }
      field[k] = '\0' ;
      printf("\n%s\n", field) ;
      i++ ;
      strcpy(coltype[j], field) ;
    }

    /* Separate out the comma separated arguments into one array of strings.*/
    i=0 ;
    for(j = 0 ; j < cols; j++)
    { 
      
      k = 0 ;
      while(tform[i] != ',')
	{
          field[k] = tform[i] ;
          i++ ;
          k++ ; 
        }
      field[k] = '\0' ;
      printf("\n%s\n", field) ;
      i++ ;
      strcpy(form[j], field) ;
    }

    /* Separate out the comma separated arguments into one array of strings.*/
    i=0 ;
    for(j = 0 ; j < cols; j++)
    {
      strcpy(unit[j], "") ;
    } 


    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) ; 
      }  
    


    /* append a new empty ASCII table onto the FITS file */
    if(type == 1)
    {
    
     fits_create_tbl( fptr, ASCII_TBL, rows, cols, coltype, form,
		      unit, extname, &status) ;
      if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }  
    }
     
    if(type == 2)
    {
        
       fits_create_tbl( fptr, BINARY_TBL, rows, cols, coltype, form,
			   unit, extname, &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;
}

