/* ROVCONV.C  Rover data file conversion routine */

#include "\rover\soft\14\rovsys.h"

#include <stdio.h>
#include <time.h>

short rotate_short(short inbuf);
unsigned long rotate_ulong(unsigned long inbuf);

main(void){
  char filein[80], fileout[80];
  FILE *in, *out;
  short  filetype;
  unsigned long record_count, record_num;
  unsigned long thistime, time_correct;
  float battvolt, hpcurr, lpcurr, gfvolt, tiltx, tilty, cmvane, auxana;
  float heading, curr_speed, curr_dir;

  struct file_header{
    unsigned long record_count;
  } file_head;

  struct datafile{
    short filetype;
    time_t caltime;
    short analog[8];
    short heading;
    short curr_speed;
    short curr_dir;
  } datain;

/*
  printf("\nStruct file_header size: %d", sizeof(struct file_header));
  printf("\nStruct datafile size: %d", sizeof(struct datafile));
*/
  printf("\n\nEnter Rover data file name: ");
  gets(filein);

  printf("\nEnter Output file name: ");
  gets(fileout);

  if((in=fopen(filein, "rb")) == NULL) {
    printf("Cannot open source file\n");
    return(1);
  }

  if((out=fopen(fileout, "w")) == NULL) {
    printf("Cannot open destination file\n");
    return(1);
  }

  if(fread(&file_head, sizeof(struct file_header), 1, in) != 1){
    printf("\n file read error");
    return(1);
  }


  time_correct = 66*365+17;  /* Days between 1904 and 1970 */
  time_correct *= 24;        /* Hours   "      "   "    "  */
  time_correct *= 3600;      /* Seconds "      "   "    "  */

  record_count = rotate_ulong(file_head.record_count);

  for(record_num = 1; record_num <= record_count; record_num++){
    fread(&datain, sizeof(struct datafile), 1, in);

    filetype = rotate_short(datain.filetype);
    thistime = rotate_ulong(datain.caltime);
    battvolt = (float)rotate_short(datain.analog[0])/BATTCAL;
    hpcurr = (float)rotate_short(datain.analog[1])/HPCURRCAL;
    lpcurr = (float)rotate_short(datain.analog[2])/LPCURRCAL;
    gfvolt = (float)rotate_short(datain.analog[3])/GFVOLTCAL;
    tiltx = (float)rotate_short(datain.analog[4])/TILTX_CAL-TILTX_OFFSET;
    tilty = (float)rotate_short(datain.analog[5])/TILTY_CAL-TILTY_OFFSET;
    cmvane = (float)rotate_short(datain.analog[6])/CMVANECAL;
    auxana = (float)rotate_short(datain.analog[7])/AUXANACAL;
    heading = (float)rotate_short(datain.heading);
    curr_speed = (float)rotate_short(datain.curr_speed);
    curr_dir = (float)rotate_short(datain.curr_dir);

    thistime -= time_correct;  /* Correct for Mac/DOS time difference 1904/1970 */

    fprintf(out, "%6d %s %6.2f %6.2f %6.2f %6.2f %5.1f %5.1f %5.1f %6.2f %5.1f %5.1f %5.1f\n",
	    filetype, asctime(gmtime(&(time_t)thistime)), battvolt, hpcurr, lpcurr,
	    gfvolt, tiltx, tilty, cmvane, auxana, heading, curr_speed,
	    curr_dir);
  }

  if(fclose(in)) printf("\nfile close error");
  if(fclose(out)) printf("\nfile close error");
  printf("\n%lu records converted\n", record_count);

  return 0;
}

/**************************************************/

short rotate_short(short inbuf){
  unsigned short leftbuf, rightbuf;
  short outbuf;

  leftbuf = (unsigned short)inbuf;
  rightbuf = (unsigned short)inbuf;

  rightbuf = rightbuf >> 8;
  leftbuf = leftbuf << 8;
  outbuf = (short)(leftbuf | rightbuf);
  return outbuf;
}

/**************************************************/


unsigned long rotate_ulong(unsigned long inbuf){
  union ulong_buf{
    unsigned long word;
    unsigned char byte[4];
  } tempbuf1, tempbuf2;

  tempbuf1.word = inbuf;
  tempbuf2.byte[0] = tempbuf1.byte[3];
  tempbuf2.byte[1] = tempbuf1.byte[2];
  tempbuf2.byte[2] = tempbuf1.byte[1];
  tempbuf2.byte[3] = tempbuf1.byte[0];

  return tempbuf2.word;
}
