/*
*************************************************************
*                                                           *
*  WOODS HOLE OCEANOGRAPHIC INSTITUTION                     *
*  UPPER OCEAN PROCESSES GROUP                              *
*  PHYSICAL OCEANOGRAPHY DEPT.                              *
*  WOODS HOLE, MASSACHUSETTS USA  02543                     *
*                                                           *
*  File: vwndasc.c              Model: -                    *
*                                                           *
*  Function: ASIMET WND binary data file to ASCII converter    *
*                                                           *
*  Project: ASIMET
*                                                           *
*  Programmer: G.A.                                         *
*                                                           *
*  Copyright (c) 2002 Woods Hole Oceanographic Institution  *
*                                                           *
*************************************************************
 
*/

/* Additional reserved words not recognized by Source Print.
   <e> uchar
*/

/*
*   Program to read the processed binary output file of the VOS C51 logger
*   and convert to ASCII - run vwndswab first.
*
*   Usage: vwndasc datafile outputfile
*          datafile   - binary data file name
*          outputfile - ascii data file name


The ASCII output file is formatted with 1 hour's records per line,
comma-separated value, as follows:

hour, minute, second, day, day-of-week, month, 4 digit year,
60 float values of Wind VEast in m/s,
60 float values of Wind VNorth in m/s,
60 float values of Wind SpeedAvg in m/s,
60 float values of Wind SpeedMAx in m/s,
60 float values of Last Vane in degrees,
60 float values of Last Compass in degrees,
60 float values of Tilt X in degrees,
60 float values of Tilt Y in degrees,
"used" value (0xA5A5 = 42405)
CRC (unused; always zero)
crlf


**************************************************************** */

/* a structure to hold the RTC register times. */
// struct time_type
//   {
//   unsigned char hour;
//   unsigned char min;
//   unsigned char sec;
//   unsigned char day;
//   unsigned char dow;   /* day of week - Sunday = 1 */
//   unsigned char mon;
//   unsigned int year;
//   };

/* this is the WND data record structure, 740 bytes (values are packed) */
// struct WND_record
//   {
//   struct time_type time1;      /* 8 bytes of time */
//   short Ve[60];   		/* 60 minutes of Vel East data */
//   short Vn[60];   		/* 60 minutes of Vel North data */
//   unsigned char WSpeed[60];	/* 60 minutes of WS data */
//   unsigned char WSMax[60];	/* 60 minutes of Max WS data */
//   unsigned short LastVane[60];	/* 60 minutes of Last Vane data */
//   unsigned short LastCompass[60];	/* 60 minutes of Last Compass data */
//   char TiltX[60];              /* 60 minutes of Tilt X data */
//   char TiltY[60];              /* 60 minutes of Tilt Y data */
//   unsigned char spare[8];
//   unsigned short used;     /* set to 0xA5A5 upon record write */
//   unsigned short wnd_CRC;  /* CRC of previous bytes */
//   };


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

/* this is the usage string */
char usage[] = "\nUsage: vwndasc datafile outputfile\n
	 datafile   - binary data file name\n
	 outputfile - ascii data file name\n\n";

/* main program */
void main(int argc,char *argv[])
{
	FILE *fd1,*fd2;
	char outbuf[100];
	char buffer;
	short intbuf;
	unsigned short used;
	long lbuf;
	float fvar;
	int ret;
	int j;
	struct tm *tptr;

   if (argc <  3)
      {
      printf("%s",usage);
      exit(-1);
      }

   /* input file name */
   fd1 = fopen(argv[1],"rb");
   if ( fd1 == NULL )
      {
      printf("\nUnable to open input file %s\n",argv[1]);
      printf("%s",usage);
      exit(-1);
      }

   /* output file name */
   fd2 = fopen(argv[2],"w");
   if ( fd2 == NULL )
      {
      printf("\nUnable to open output file %s\n",argv[2]);
      printf("%s",usage);
      exit(-1);
      }

   /* stay here until EOF, error, or last record aborts */
   while (1)
      {
      /* first are 8 time bytes, first six are char */
      for (j = 0; j < 6; j++)
         {
         /* read the byte, put a byte */
         ret = fread(&buffer,sizeof(char),1,fd1);
         if ( ret != 1 )
            goto end;

         /* transfer it to ASCII file */
         fprintf(fd2,"%.2u,",(unsigned int)buffer); 
         }

      /* next read the 2 "year" bytes */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to ASCII file */
      fprintf(fd2,"%.2u,",intbuf);

      /* next read 60 minutes of Ve and Vn data */
      for ( j = 0; j < 120; j++ )
         {
         /* read a value */
         ret = fread(&intbuf,sizeof(short),1,fd1);
         if ( ret != 1 )
            goto end;
         /* transfer it to ASCII file */
/*         fprintf(fd2,"%.2d,",intbuf);  */
         fprintf(fd2,"%6.2f,",((float)intbuf) / 100.0); 
         }

      /* next are 60 minutes of Wspeed and WspeedMax in unsigned char */
      for (j = 0; j < 120; j++)
         {
         /* read the byte, put a byte */
         ret = fread(&buffer,sizeof(char),1,fd1);
         if ( ret != 1 )
            goto end;

         /* transfer it to ASCII file */
/*         fprintf(fd2,"%.2u,",(unsigned int)buffer);  */
         fprintf(fd2,"%5.1f,",((float)buffer) / 5.0 );
         }

      /* next read 60 minutes of LastVane and LastCompass data */
      for ( j = 0; j < 120; j++ )
         {
         /* read a value */
         ret = fread(&intbuf,sizeof(short),1,fd1);
         if ( ret != 1 )
            goto end;
         /* transfer it to ASCII file */
/*         fprintf(fd2,"%.2u,",intbuf); */
         fprintf(fd2,"%6.1f,",((float)intbuf) / 10.0); 
         }

      /* next are 60 minutes of TiltX and TiltY in unsigned char */
      for (j = 0; j < 120; j++)
         {
         /* read the byte, put a byte */
         ret = fread(&buffer,sizeof(char),1,fd1);
         if ( ret != 1 )
            goto end;

         /* transfer it to ASCII file */
/*         fprintf(fd2,"%.2d,",(int)buffer); */
         fprintf(fd2,"%6.1f,",((float)buffer) / 5.0); 
         }

      /* next are 8 unused bytes for now - just skip */
      for (j = 0; j < 8; j++)
         {
         /* read the byte, but discard */
         ret = fread(&buffer,sizeof(char),1,fd1);
         if ( ret != 1 )
            goto end;
         }

      /* next read the 2 "used" bytes - they should be A5A5 in a good
         record */
      ret = fread(&used,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;

      /* transfer "used" to ASCII file */
      fprintf(fd2,"%.2u,",used);

      /* next read the 2 CRC bytes - unused set to 0 for now */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;

      /* transfer CRC to ASCII file */
      fprintf(fd2,"%.2u",(unsigned short)intbuf);

		/* end each line with crlf */
      fprintf(fd2,"\n");

      /* was this a good record ? */
      if (used != 0xA5A5)
         goto end;
      } /* end while */

end:
   printf("\n");
   fclose(fd1);
   fclose(fd2);
}
