/*
*************************************************************
*                                                           *
*  WOODS HOLE OCEANOGRAPHIC INSTITUTION                     *
*  UPPER OCEAN PROCESSES GROUP                              *
*  PHYSICAL OCEANOGRAPHY DEPT.                              *
*  WOODS HOLE, MASSACHUSETTS USA  02543                     *
*                                                           *
*  File: vprcasc.c              Model: -                    *
*                                                           *
*  Function: ASIMET PRC 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 vprcswab first.
*
*   Usage: vprcasc 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 PRC,
"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 PRC data record structure, 256 bytes */
// struct PRC_record
//   {
//   struct time_type time1;      /* 8 bytes of time */
//   float prc_cal[60];           /* 60 minutes of PRC data */
//   unsigned char unused[4];
//   unsigned short used;     /* set to 0xA5A5 upon record write */
//   unsigned short prc_CRC;  /* CRC of previous 254 bytes */
//   };


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

/* this is the usage string */
char usage[] = "\nUsage: vprcasc 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 int intbuf;
	unsigned short used;
	long lbuf;
	float fvar;
	short int ret;
	short int j;
	struct tm *tptr;

//   printf("Program vprcasc - if you have garbled output, try running");
//   printf(" the file through program vprcswab first to swap the bytes\n\n");

   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 precip data */
      for ( j = 0; j < 60; j++ )
         {
         /* read a value */
         ret = fread(&fvar,sizeof(float),1,fd1);
         if ( ret != 1 )
            goto end;

         /* transfer it to ASCII file */
         fprintf(fd2,"%7.3f,",fvar); 
         }

      /* next are 4 unused bytes for now - just skip */
      for (j = 0; j < 4; 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);
}
