/********************************************************
               AVE Houston File converter program.  
               Pat Carmichael, ARIM/NCAR, 6-25-04
               Written in GNU Emacs, GCC 3.3.1,
               Cygming special.
               For converting CAFS .xls files to data archive type
----------------------------------------------------------
    Versions:
               RC1:  Initial version, no working filenmame system
----------------------------------------------------------
    Stipulations on use:
               None, this program is released under the GNU Public liscense.
               No warranty, express or implied, or even of usability.
               You may include this program with any data set/etc,
               but you may NOT edit this header without explicit
               permission of Pat Carmichael.
----------------------------------------------------------
    Contact info:
               Pat Carmichael, ARIM/NCAR
               1800 Table Mesa Dr
               Boulder, CO, 80301
               lopoetve@ucar.edu, 303-497-1674.
*********************************************************/



#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<fstream>
#include<string>
#include<cstdlib>
#include<time.h>
#include<ctype.h>

using namespace std;
char *WHITESPACE = "\t";  //CAFS uses a tab for whitespace

char* itoa(int num) //int to ascii
{
  int i,n1,n,nlen;
  char *str;
  str = (char *)calloc(1024, sizeof(char));
  nlen=1;
  n=10;
  while (num/n!=0)
    {
      nlen++;
      n*=10;
    }
  n1=num;
  i=0;
  
  
  do{
    n = n1 % 10;
    n1 = n1 / 10;
    str[nlen-i-1]='0'+n;
    i++;
  }while(n1!=0);
  str[i]='\0';
  return str;
}

int main(int argc, char *argv[])
{
  
  if (argc != 2)
    {
      cerr<<"Retard"<<endl;
      exit(0);
    }
  
  ofstream ccout;
  ifstream ccin;
  FILE *ip;
  char *buffer;
  buffer = (char *)calloc(8192, sizeof(char));
  int base, final, total;
  char *working;
  working = (char *)calloc(8192, sizeof(char));
  int totallines = 302;
  int header = 1010;
  char *year;
  char *month;
  char *day;
  char *temp;
  year = (char *)calloc(10, sizeof(char));
  month = (char *)calloc(5, sizeof(char));
  day = (char *)calloc(5, sizeof(char));
  temp = (char *)calloc(512, sizeof(char));
  char *namestring = "Shetter, Rick";
  char *orgstring = "ARIM/NCAR";
  char *insstring = "WB57 Spectrometer";
  char *projstring = "AVE_0410";
  char *indep = "FracDoyCPU";  
  int pricount;// = 254;
  int pricountscale = 1;
  char *missing = "999E15";
  char *otherpri = "FracDoyIRIG";
  int auxcount = 0;
  int specialcount = 0;
  int normalcount = 1;
  
  
  //parse old header.  
  char *ccintest;
  ccintest = (char *)calloc(16384, sizeof(char));
  ccin.open(argv[1]);
  ccin.getline(ccintest, 8192);
  ccin.getline(ccintest, 8192);
  ccin.getline(ccintest, 8192);

  //divide up the line for total number of variables
  strcpy(working, ccintest);
  strtok(working, " ");
  strtok(NULL, " ");
  base = atoi(strtok(NULL, " "));
  strtok(NULL, " ");
  final = atoi(strtok(NULL, " "));
  total = final - base;
  cerr<<"base: "<<base<<" final: "<<final<<" total: "<<total<<endl;
  cerr<<ccintest<<endl;
  pricount = total + 1;
  //last header lines
  ccin.getline(ccintest, 8192);
  ccin.getline(ccintest, 8192);
  ccin.getline(ccintest, 8192);
  ccin.getline(ccintest, 8192);
  //header parsing done
  
  
  strcpy(year, "20");
  memcpy(temp, argv[1], 2);
  strcat(year, temp);
  strcat(year, "\0");
  memcpy(temp, argv[1] + 2*sizeof(char), 2);
  strcpy(month, temp);
  memcpy(temp, argv[1] + 4*sizeof(char), 2);
  strcpy(day, temp);
  //RECORDED DATE FORMATTED

  //Make into filename
  char *outfile;
  outfile = (char *)calloc(100, sizeof(char));
  strcpy(outfile, "RS");
  strcat(outfile, "20");
  memcpy(temp, argv[1], 2);
  strcat(outfile, temp);
  strcat(outfile, month);
  strcat(outfile, day);
  strcat(outfile, ".wb57");


  //NOW FOR THE CURRENT date

  char *d2;
  d2 = (char *)calloc(100, sizeof(char));
  time_t current_timestamp = time(NULL);
  struct tm current_time;
  localtime_r(&current_timestamp, &current_time);
  strftime(d2, 100, "%Y %m %d", &current_time);

  int errorcount, scalecount;
  scalecount = pricount / 15;
  if (pricount % 15 != 0)
    scalecount++;
  errorcount = pricount / 15;
  if (pricount % 15 != 0)
    errorcount++;
  cerr<<"COUNTS: "<<endl;
  cerr<<"Primary: "<<pricount<<" Scale: "<<scalecount
      <<" Error: "<<errorcount<<endl;

  //total line calculations
  totallines = 9 + 1 + scalecount + errorcount + pricount + (auxcount + 1)
    + (specialcount + 1) + (normalcount + 1);
  //9 + 1 (pricount line) + scale factors + error factors + pricount (in var)
  //+ (auxcount + 1 (for line)) + (specialcount + 1 (for line))
  //+ (normalcount + 1 (for line))
  cerr<<"Total Header Lines: "<<totallines<<endl;



  //start dumping crap
  ccout.open(outfile);
  ccout<<totallines<<" "<<header<<endl;
  ccout<<namestring<<endl;
  ccout<<orgstring<<endl;
  ccout<<insstring<<endl;
  ccout<<projstring<<endl;
  ccout<<"1 1          (IVOL, NVOL)"<<endl;
  ccout<<year<<" "<<month<<" "<<day<<"  "<<d2<<endl;
  ccout<<"0"<<endl;
  ccout<<indep<<endl;
  ccout<<pricount<<endl;
  int counter = 0;
  for (int i = 0; i < pricount; i++)
    {
    ccout<<pricountscale<<" ";
    counter ++;
    if(counter == 15)
      {
      ccout<<endl; 
      counter = 0;
      }
    }
  ccout<<endl;

  counter = 0;
  for (int i = 0; i < pricount; i++)
    {
      ccout<<missing<<" ";
      counter ++;
      if (counter == 15)
	{
	  ccout<<endl;
	  counter = 0;
	}
    }
  ccout<<endl;

  ccout<<otherpri<<endl;

  char *temp2;
  char *temp3;
  temp2 = (char *)calloc(512, sizeof(char));
  temp3 = (char *)calloc(512, sizeof(char));
  for (int i = 0; i < pricount - 1; i++)
    {
      strcpy(temp2, "Pix");
      strcpy(temp3, itoa(i+base));
      strcat(temp2, temp3);
      strcat(temp2, "\0");
      ccout<<temp2<<endl;
    }
  
  ccout<<auxcount<<"  AUX COUNT"<<endl;
  ccout<<specialcount<<"  # SPECIAL COMMENT"<<endl;
  ccout<<normalcount<<"  # NORMAL COMMENT"<<endl;
  ccout<<"THIS IS MY NORMAL COMMENT"<<endl;


  




  //////////////////////////////////////////////////////////////////////////
  //DATA LOOP DATA LOOP DATA LOOP DATA LOOP DATA LOOP DATA LOOP
  /////////////////////////////////////////////////////////////////////////  
  counter = 0;
  char *countertemp;
  //fgets(buffer, 8192, ip);
  ccin.getline(ccintest, 16383);
  int totalcounter = 0;
  while(ccin.peek() != -1)
    {
      
      strcpy(buffer, ccintest);
      while(counter != pricount + 1)
	{
	  
	  if (counter == 0)
	    {
	     
	      countertemp = strtok(buffer, WHITESPACE);
	     
	    }
	  else
	    {
	      countertemp = strtok(NULL, WHITESPACE);
	      
	    }

	  ccout<<countertemp<<" ";
	  if (counter%6 == 0)
	    ccout<<endl;
	  counter++;
	}
      //cerr<<"Going to next line"<<endl;
      totalcounter ++;
      //char voidit;
      //ccin.get(voidit);
      ccin.getline(ccintest, 16383);
      
            
      //cerr<<"v: "<<voidit<<endl;
      
      if (!ccin.eof())
	{
	 
	  ccout<<endl;
	  
	}
      counter = 0;
      
    };

  cerr<<"File done, exiting."<<endl;
  
  ccout<<endl;
  ccout.close();
  cerr<<"ccout closed"<<endl;
  ccin.close();
  cerr<<"ccin closed"<<endl;
  //fclose(ip);
  cerr<<"fclosed"<<endl;
  return EXIT_SUCCESS;
}
