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


#include "fitsio.h"

int main( int argc, char *argv[] );
void writecolumn(char *filename, int rows,int cols,  int colno, char *colvalues, int prog);


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 rows,colno,cols, prog ;
  if(argc != 7)
    {
      printf("report_error_status:102\n") ;
      exit(0) ;
    } 

  rows = (int)atoi(argv[2]) ;
  cols = (int)atoi(argv[3]) ;
  colno = (int)atoi(argv[4]) ;
  prog = (int)atoi(argv[6]) ; 

  writecolumn(argv[1], rows,cols,colno, argv[5],prog ) ;
  return(0);
}

void  writecolumn(char *filename, int rows,int cols, int colno,  char *colvalues, int prog) 
{
    fitsfile *fptr;       /* pointer to the FITS file, defined in fitsio.h */
    int status=0 ,i,j,hdunum,hdutype,ii,k,nfound,*int_colval,flag=0,int_value;  
    double *double_colval, double_value ;
    char **colval, *field, **tform,*comment ;
    float *float_colval, float_value ; 
 
     fits_open_file(&fptr, filename, READWRITE, &status) ;
     if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }  

  tform = (char **)malloc(cols * sizeof(char *)) ;
  for(i=0; i < cols; i++)
     tform[i] = (char *)malloc(80) ;
   
  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_read_keys_str(fptr, "TFORM", 1, cols, tform, &nfound, &status);
  if(status)
      {
        printf("report_error_status:%d\n", status) ;
        exit(0) ; 
      }  
 
  if ( strstr(tform[colno-1],"I") || strstr(tform[colno-1],"i") )
    flag = 1 ;

  if ( strstr(tform[colno-1],"A") || strstr(tform[colno-1],"a")|| strstr(tform[colno-1],"D") || strstr(tform[colno-1],"d")|| strstr(tform[colno-1],"F") || strstr(tform[colno-1],"f") )
    flag = 2 ;

  if ( strstr(tform[colno-1],"E") || strstr(tform[colno-1],"e") )
    flag = 3 ;

  printf("\n%d\n", flag) ;  
  field = (char *)malloc(80);
 
  if(prog == 1){
     colval = (char **)malloc(rows * sizeof(char *)) ;
      for (ii = 0; ii < rows; ii++)      /* allocate space for the column labels */
          colval[ii] = (char *) malloc(FLEN_VALUE);  /* max label length = 69 */
 
     int_colval = (int *)malloc(rows * sizeof(int)) ;
     double_colval = (double *)malloc(rows * sizeof(double)) ;
  }
  else
  {
     colval = (char **)malloc(1 * sizeof(char *)) ;    
     colval[0] = (char *)malloc(FLEN_VALUE);  /* max label length = 69 */
     int_colval = (int *)malloc(1 * sizeof(int)) ;
     double_colval = (double *)malloc(1 * sizeof(double)) ;
  } 

  if(flag == 2) 
  {
    /* Separate out the comma separated arguments into one array of strings.*/
    if(prog == 1){
    i=0 ;
    for(j = 0 ; j < rows; j++)
    { 
      
      k = 0 ;
      while(colvalues[i] != ',')
	{
          field[k] = colvalues[i] ;
          i++ ;
          k++ ; 
        }
      field[k] = '\0' ;
      printf("\n%s\n", field) ;
      i++ ;
      strcpy(colval[j], field) ;
    }
    }
    else
    {
      strcpy(colval[0], colvalues) ;
    }

    if(prog == 1)
      fits_write_col(fptr, TSTRING, colno, 1, 1, rows, colval,
       &status);
    else
      fits_write_col(fptr, TSTRING, colno, rows, 1, 1, colval,
       &status);

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

  if(flag == 1) 
  {
    /* Separate out the comma separated arguments into one array of strings.*/
    if(prog == 1){
    i=0 ;
    for(j = 0 ; j < rows; j++)
    { 
      
      k = 0 ;
      while(colvalues[i] != ',')
	{
          field[k] = colvalues[i] ;
          i++ ;
          k++ ; 
        }
      field[k] = '\0' ;
      printf("\n%s\n", field) ;
      int_value = (int)atoi(field) ;
      printf("\n%d\n", int_value) ;
      i++ ;
      int_colval[j] = int_value ;
    }
    }
    else
    {
      int_value = (int)atoi(colvalues) ;
      int_colval[0] = int_value ;
    }

    if(prog == 1)
       fits_write_col(fptr, TINT, colno, 1, 1, rows,int_colval,
       &status);
    else
      fits_write_col(fptr, TINT, colno, rows, 1, 1, int_colval,
        &status);
      

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

  if(flag == 3) 
  {
    /* Separate out the comma separated arguments into one array of strings.*/
    if(prog == 1){
    i=0 ;
    for(j = 0 ; j < rows; j++)
    { 
      
      k = 0 ;
      while(colvalues[i] != ',')
	{
          field[k] = colvalues[i] ;
          i++ ;
          k++ ; 
        }
      field[k] = '\0' ;
      printf("\n%s\n", field) ;
      double_value = (double)atof(field) ;
      printf("\n%lf\n",double_value) ;
      i++ ;
      double_colval[j] = double_value ;
    }
    }
    else
    {
      double_value = (double)atof(colvalues) ;
      double_colval[0] = double_value ;
    }

    if(prog == 1) 
       fits_write_col(fptr, TDOUBLE, colno, 1, 1, rows,double_colval,
       &status);
    else
       fits_write_col(fptr, TINT, colno, rows, 1, 1, double_colval,
       &status);

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

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

}




