#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; 
    unsigned int  uint_value ;
    unsigned int *uint_colval ;
    long int *long_colval, long_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"))
    flag = 2 ;

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

  if ( strstr(tform[colno-1],"E") || strstr(tform[colno-1],"e"))
    flag = 4 ;  
 
  if ( strstr(tform[colno-1],"J") || strstr(tform[colno-1],"j"))
    flag = 5 ;  

  if ( strstr(tform[colno-1],"B") || strstr(tform[colno-1],"b"))
    flag = 6 ;  

  if ( strstr(tform[colno-1],"L") || strstr(tform[colno-1],"l"))
    flag = 7 ;  

   if ( strstr(tform[colno-1],"X") || strstr(tform[colno-1],"x")  )
    flag = 8 ;
  

    printf("\n%d\n", flag) ;  
    field = (char *)malloc(80);
 
 
    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)) ;
    float_colval = (float *)malloc(rows * sizeof(float)) ;
    uint_colval = (unsigned int *)malloc(rows * sizeof(unsigned int)) ;
    long_colval = (long *)malloc(rows * sizeof(long)) ;
  
 
  if(flag == 2 || flag == 7) 
  {
    /* 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) ;
    }
    }
    

    if(prog == 1)
    {
      if(flag == 2) 
        fits_write_col(fptr, TSTRING, colno, 1, 1, rows, colval,
       &status);
      
      if(flag == 7)
        fits_write_col(fptr, TLOGICAL, colno, 1, 1, rows, colval,
       &status);
    }
    else
    { 
      if(flag == 2) 
	{
          fits_read_col(fptr, TSTRING, colno, 1, 1, rows, NULL, colval, NULL, &status) ; 
          strcpy(colval[rows-1] ,colvalues) ;
          fits_write_col(fptr, TSTRING, colno,  1, 1,rows, colval,
       &status);
        }

      if(flag == 7)
	{
          fits_read_col(fptr, TLOGICAL, colno, 1, 1, rows, NULL, colval, NULL, &status) ; 
          strcpy(colval[rows-1] ,colvalues) ;
          fits_write_col(fptr, TLOGICAL, colno,  1, 1,rows, 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) ;
    }

    if(prog == 1)
      {
        fits_write_col(fptr, TINT, colno, 1, 1, rows,int_colval,
       &status);
      }
      else
      {
        fits_read_col(fptr, TINT, colno, 1, 1, rows, NULL, int_colval, NULL, &status) ; 
        int_colval[rows-1] = int_value ;
        /* printf("\\%d//\n",int_colval[1]) ;*/
        fits_write_col(fptr, TINT, colno, 1,1, rows, 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 = atof(field) ;
      /*printf("\n%lf\n",double_value) ;*/
      i++ ;
      double_colval[j] = double_value ;
    }
    }
    else
    {
      double_value = atof(colvalues) ;
      /*printf("\\%lf//",double_value) ;*/
    
    }

    if(prog == 1) 
       fits_write_col(fptr, TDOUBLE, colno, 1, 1, rows,double_colval,
       &status);
    else
     {
       fits_read_col(fptr, TDOUBLE, colno, 1, 1, rows, NULL, double_colval, NULL, &status) ; 
       double_colval[rows-1] = double_value ;
       fits_write_col(fptr, TDOUBLE, colno,1, 1, rows,  double_colval,
       &status);
     }

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

   if(flag == 4) 
  {
    /* 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) ;
      float_value = (float)atof(field) ;
      printf("\n%f\n",float_value) ;
      i++ ;
      float_colval[j] = float_value ;
    }
    }
    else
    {
      float_value = (float)atof(colvalues) ;
      
    }

    if(prog == 1) 
       fits_write_col(fptr, TFLOAT, colno, 1, 1, rows,float_colval,
       &status);
    else
      {
        fits_read_col(fptr, TFLOAT, colno, 1, 1, rows, NULL, float_colval, NULL, &status) ; 
        float_colval[rows-1] = float_value ;
        fits_write_col(fptr, TFLOAT, colno, 1, 1,rows, float_colval,
       &status);
      } 

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

  if(flag == 5) 
  {
    /* 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) ;
      long_value = (long int)atol(field) ;
      printf("\n%ld\n",long_value) ;
      i++ ;
      long_colval[j] = long_value ;
    }
    }
    else
    {
      long_value = (long int)atol(colvalues) ;
    }

    if(prog == 1) 
     {
       printf("//%ld\\\n",long_colval[0]); 
       fits_write_col(fptr, TLONG, colno, 1, 1, rows,long_colval,
       &status);
     }
    else
     { 
       fits_read_col(fptr, TLONG, colno, 1, 1, rows, NULL, long_colval, NULL, &status) ; 
       long_colval[rows-1] = long_value ;
       printf("//%ld\\\n",long_colval[rows-1]); 
       fits_write_col(fptr, TLONG, colno,  1, 1, rows,  long_colval, &status);
     }

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

  if(flag == 6) 
  {
    /* 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) ;
      uint_value = (unsigned int)atoi(field) ;
      printf("\n%c\n",uint_value) ;
      i++ ;
      uint_colval[j] = uint_value ;
      printf("\\\%c////",uint_colval[j]) ; 
      }
    }
    else
    {
      uint_value = (unsigned int)atoi(colvalues) ;
     
    }

    if(prog == 1)
         fits_write_col(fptr, TBYTE, colno, 1, 1, rows, uint_colval,
       &status) ;
    else
     {
       fits_read_col(fptr, TBYTE, colno, 1, 1, rows, NULL, uint_colval, NULL, &status) ; 
       uint_colval[rows-1] = uint_value ;
       fits_write_col(fptr, TBYTE, colno, 1, 1,rows, uint_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) ; 
      }  

}




