/********************************************************
              .AVE .wb57 -> .tap File Converter  
               Pat Carmichael, ARIM/NCAR, 9-20-04
               Written in GNU Emacs, GCC 3.3.1,
               Cygming special.
               For converting .wb57 files to ARIM .tap files.
----------------------------------------------------------
    Versions:
               B1:  Working converter, no datestamp.
               RC1:  Current release canidate, working datestamp.
----------------------------------------------------------
    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<fstream>
#include<cstring>
#include<cstdlib>

using namespace std;

const int MAX_LINE = 512;


int main(int argc, char *argv[])
{
  char *utc, *ilat, *ilong, *thead, *pict, *roll, *gspeed, *trackangle, *inertialwind;
  char *inertialwinddir, *bodylonga, *bodylata, *bnorma, *trackanglerate, *pitchrate, *rollrate;
  char *inertialvertical, *gpsalt, *gpslat, *gpslong, *staticpressure;
  char *totalpressure, *diffpressure, *totaltemp, *statictemp;
  char *barometricalt, *mach, *trueairspeed, *windspeed, *winddir, *sunz;
  char *sunaz;
  char *analog[12];
  char *fix;
  char *hours, *min, *sec;

  char *inputfile, *outputfile;
  char *buffer;
  FILE *ip;
  FILE *op;

  ofstream ccout;

  buffer = (char *)calloc(512, sizeof(char));

  if (argc < 2 || argc > 2)
    {
      cout<<"Usage:  "<<argv[0]<<" inputfile"<<endl;
      exit(0);
    }

  inputfile = argv[1];
  outputfile = (char *)calloc(512, sizeof(char));
  //memcpy(outputfile, argv[1] + 2*sizeof(char), 8);
  memcpy(outputfile, argv[1], 6);
  strcat(outputfile, ".tap");
  strcat(outputfile, "\0");
  //BUILD OUTPUT FILE DONE
 

  ip = fopen(inputfile, "r");
  op = fopen(outputfile, "w");
  ccout.open(outputfile);

  //HEADER WRITING HERE
  //TOTAL LINES = COMMENT + VARIABLE + 3 (for the number lines)
  ccout<<"17 # NH = total number of header lines"<<endl;
  ccout<<"4 # NC = number of comment lines"<<endl;
  ccout<<"10 # NV = number of variable lines"<<endl;
  ccout<<"FILENAME"<<endl;
  ccout<<"ARIM Group, ACD/NCAR"<<endl;
  ccout<<"Preliminary fileconverter tests"<<endl;
  ccout<<"AVE"<<endl;
  
  //VAR NAMES
  ccout<<"Time,secs,1,0,0,0,999,0"<<endl;
  ccout<<"Temperature, degC, 1, 0, 0, 0, 999, 0"<<endl;
  ccout<<"Pressure, mb, 1, 0, 0, 0, 999, 0"<<endl;
  ccout<<"Latitude, deg, 1, 0, 0, 0, 999, 0"<<endl;
  ccout<<"Longitude, deg, 1, 0, 0, 0, 99, 0"<<endl;
  ccout<<"Altitude, m, 1, 0, 0, 0, 999, 0"<<endl;
  ccout<<"Pitch, deg, 1, 0, 0, 0, 999, 0"<<endl;
  ccout<<"Roll, deg, 1, 0, 0, 0, 999, 0"<<endl;
  ccout<<"SuneleG, deg, 1, 0, 0, 0, 999, 0"<<endl;
  ccout<<"SunAzm, deg, 1, 0, 0, 0, 999, 0"<<endl;
  //MADE UP HEADER DONE


  char *test = (char *)calloc(50,sizeof(char));
  gpslong = (char *)calloc(50,sizeof(char));
  
  //END HEADER WRITING

  fgets(buffer, 512, ip);//INITIAL CRAP LINE
  fgets(buffer, 512, ip);//HEADER
  fgets(buffer, 512, ip);//FIRST DATA LINE
  int counter = 0;
  while(!feof(ip))
    {
      counter++;
      strtok(buffer, " ");
      utc = strtok(NULL, " ");
      ilat = strtok(NULL, " ");
      ilong = strtok(NULL, " ");
      strcpy(test, ilong);
      if (!strncmp(test, "W\0", 2))
	{
	  ilong = strtok(NULL, " ");
	}
      //fix = strtok(NULL, " ");
      //strcat(ilong, fix);
      thead = strtok(NULL, " ");
      pict = strtok(NULL, "  ");
      roll = strtok(NULL, "  ");
      gspeed = strtok(NULL, "    ");
      trackangle = strtok(NULL, " ");
      inertialwind = strtok(NULL, "  ");
      inertialwinddir = strtok(NULL, " ");
      bodylonga = strtok(NULL, " ");
      bodylata = strtok(NULL, "  ");
      bnorma = strtok(NULL, " ");
      trackanglerate = strtok(NULL, "  ");
      pitchrate = strtok(NULL, "  ");
      rollrate = strtok(NULL, "  ");
      inertialvertical = strtok(NULL, "     ");
      gpsalt = strtok(NULL, "  ");
      gpslat = strtok(NULL, " ");
      gpslong = strtok(NULL, " ");
      strcpy(test, gpslong);
      if (!strncmp(test, "W\0", 2))
	{
	  
	  char *mytemp = (char *)calloc(50, sizeof(char));
	  strcpy(gpslong, "-");
	  mytemp = strtok(NULL, " ");
	  
	  strcat(gpslong, mytemp);
	 
	  
	}
      else if(!strncmp(test, "E\0", 2))
	{
	  
	  char *mytemp = (char *)calloc(50, sizeof(char));
	  strcat(gpslong, mytemp);
	}
      else if(!strncmp(test, "W", 1))
	{
	  
	  char *mytemp = (char *)calloc(50, sizeof(char));
	  gpslong ++;
	  strcpy(mytemp, gpslong);
	  
	  gpslong = (char *)calloc(50, sizeof(char));
	  strcpy(gpslong, "-");
	  strcat(gpslong, mytemp);
	  
	}
      else if(!strncmp(test, "E", 1))
	{
	  
	  gpslong++;
	}
      
      staticpressure = strtok(NULL, "  ");
      totalpressure = strtok(NULL, " ");
      diffpressure = strtok(NULL, "  ");
      totaltemp = strtok(NULL, "  ");
      statictemp = strtok(NULL, "  ");
      barometricalt = strtok(NULL, "    ");
      mach = strtok(NULL, " ");
      trueairspeed = strtok(NULL, "   ");
      windspeed = strtok(NULL, "  ");
      winddir = strtok(NULL, "  ");
      sunz = strtok(NULL, "  ");
      sunaz = strtok(NULL, " ");
      for (int i = 0; i < 12; i ++)
	{
	  analog[i] = strtok(NULL, " ");
	}
      

      ///////////////////////////////////LINES  IN.  NOW OUT///////////////////

      strtok(utc, ":");
      hours = strtok(NULL, ":");
      min = strtok(NULL, ":");
      sec = strtok(NULL, " ");
      ccout<<hours<<":"<<min<<":"<<sec<<" ";
      ccout<<totaltemp<<" ";
      ccout<<totalpressure<<" ";
      for(int i = 1; i < 9; i++)
	    ccout<<gpslat[i];
      ccout<<" ";
      ccout<<gpslong<<" ";
      //for(int i = 2; i < 10; i++)
      //ccout<<gpslong[i];
      ccout<<" ";
      ccout<<gpsalt<<" ";
      ccout<<pict<<" ";
      ccout<<roll<<" ";
      ccout<<sunz<<" ";
      ccout<<sunaz<<" ";

      ccout<<endl;
      fgets(buffer, 512, ip);
    }//endwhileloop  all done


  fclose(ip);
  fclose(op);
  return EXIT_SUCCESS;

}


