/*
*************************************************************
*                                                           *
*  WOODS HOLE OCEANOGRAPHIC INSTITUTION                     *
*  UPPER OCEAN PROCESSES GROUP                              *
*  PHYSICAL OCEANOGRAPHY DEPT.                              *
*  WOODS HOLE, MASSACHUSETTS USA  02543                     *
*                                                           *
*  File: lograsc.c               Model: -                   *
*                                                           *
*  Function: LOGR53 binary data file to ASCII converter     *
*                                                           *
*  Project: LOGR53                                          *
*                                                           *
*  Programmer: G.A.                                         *
*                                                           *
*  Copyright (c) 2000 Woods Hole Oceanographic Institution  *
*                                                           *
*************************************************************
 
*/

/* Additional reserved words not recognized by Source Print.
   <e> uchar
*/

/*
*   Program to read and convert to ascii the binary output file from
*   the LOGR53 FLASH memory card
*
*
*   Usage: lograsc datafile outputfile
*          datafile   -  binary data file name
*          outputfile - ascii data file name
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

/* this is the usage string */
char usage[] = "\nUsage: lograsc datafile outputfile\n
			 datafile   - LOGR53 binary data file name\n
			 outputfile - ascii data file name\n\n";

/* LOGR 64 byte packed data record structure for storage in FLASH */
struct LOGR_record
   {
   unsigned char hour;   /* time is not packed for ease of verifying */
   unsigned char min;
   unsigned char day;
   unsigned char mon;
   unsigned char year;   /* year is offset from 2000 (no good after year 2255 :-) */
   unsigned short record;   /* sequential record number from startup */
   unsigned char mux_parm;  /* which option parameter in this record */
   short we,wn;   /* wind speed m/sec */
                  /* (short)(we * 100) ==> +/- 327.67 m/s */
   unsigned short wsavg,wmax,wmin;      /* (ushort)(wsavg * 100) ==> 0 - 655.35 m/s */

   short vdavg,compass;  /* vane average degrees, last compass degrees */
                         /* (short)(vdavg * 10) ==> +/- 3276.7 degrees */

   unsigned short bp;   /* barometer millibars */
               /* (ushort)((bp - 900.0) * 100) ==> 900.00 - 1555.35 mbar */

   short rh;  /* humidity %, deg C */
              /* (short)(rh * 100) ==> +/- 327.67 %RH */
   unsigned short th;  /* (ushort)((th + 20.0) * 1000) ==> -20.000 to +45.535 degC */

   unsigned short ta;  /* air temp deg C */
                       /* (ushort)((ta + 20.0) * 1000) ==> -20.000 to +45.535 degC */

   unsigned short ts;  /* sea temp deg C */
                       /* (ushort)((ts + 20.0) * 1000) ==> -20.000 to +45.535 degC */

   short sr;  /* short wave w/m^2 */
              /* (ushort)(sr * 10) ==> +/- 3276.7 w/m^2 */

   unsigned short dome,body;  /* long wave dome and body thermistors
                                 deg Kelvin, thermopile microvolts */
                              /* (ushort)(dome * 100) ==> 0 - 655.35 degK */
                              /* (ushort)(body * 100) ==> 0 - 655.35 degK */
   short tpile;               /* (short)(tpile * 10) ==> +/- 3276.7 microvolts */

   short lwflux;  /* lwr flux */
                  /* (short)(lwflux * 10) ==> +/-3276.7 w/m^2 */

   short prlev;  /* precipitation values */
                 /* (short)(prlev * 100) ==> +/-327.67 mm */

   short bat1,bat2,bat3,bat4;  /* misc. battery */
                               /* (short)(bat1 * 1000) ==> +/- 32.767 VDC */

   unsigned long opt_parm;     /* optional value indicated in rec_mux */
   unsigned short spare1,spare2,spare3; /* optional spare values */
   unsigned short used;         /* set to 0xA5A5 upon record write */
   };

struct LOGR_record LOGR_data;
   
/* main program */
void main(argc,argv)
int argc;
char *argv[];
{

	FILE *fd1,*fd2;
	char buffer;
	short int intbuf;
	long int lbuf;
	unsigned short used;
	int ret;
	int i;

   if (argc <  2)
      {
      printf("%s",usage);
      exit(-1);
      }

	/* index to input filename */
   i = 1;

   /* input file name */
   fd1 = fopen(argv[i],"rb");
   if ( fd1 == NULL ) {
      printf("\nUnable to open input file %s\n",argv[i]);
      printf("%s",usage);
      exit(-1);
   }
   printf("\nOpening input file %s\n",argv[i]);
   i++;

   /* output file name */
   fd2 = fopen(argv[i],"w");
   if ( fd2 == NULL ) {
      printf("\nUnable to open output file %s\n",argv[i]);
      printf("%s",usage);
      exit(-1);
   }
   printf("\nOpening output file %s\n",argv[i]);

   /* stay here until EOF, error, or last record aborts */
   while (1)
      {
      /* first are 5 time bytes - char */
      /* read the byte, put a byte */
      ret = fread(&buffer,sizeof(char),1,fd1);
      if ( ret != 1 )
         goto end;

      /* transfer it to temp storage */
//      fprintf(fd2,"%.2u,",(unsigned int)buffer); 
      LOGR_data.hour = (unsigned char)buffer;

      /* read the byte, put a byte */
      ret = fread(&buffer,sizeof(char),1,fd1);
      if ( ret != 1 )
         goto end;

      /* transfer it to temp storage */
      LOGR_data.min = (unsigned char)buffer;

      /* read the byte, put a byte */
      ret = fread(&buffer,sizeof(char),1,fd1);
      if ( ret != 1 )
         goto end;

      /* transfer it to temp storage */
      LOGR_data.day = (unsigned char)buffer;

      /* read the byte, put a byte */
      ret = fread(&buffer,sizeof(char),1,fd1);
      if ( ret != 1 )
         goto end;

      /* transfer it to temp storage */
      LOGR_data.mon = (unsigned char)buffer;

      /* next read the "year" byte */
      ret = fread(&buffer,sizeof(char),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.year = (unsigned char)buffer;

      /* next read record number data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.record = intbuf; 

      /* next read the "mux_parm" byte */
      ret = fread(&buffer,sizeof(char),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.mux_parm = (unsigned char)buffer;

      /* next read We data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.we = intbuf; 

      /* next read Wn data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.wn = intbuf; 

      /* next read WSavg data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.wsavg = intbuf; 

      /* next read Wmax data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.wmax = intbuf; 

      /* next read Wmin data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.wmin = intbuf; 

      /* next read VDavg data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.vdavg = intbuf; 

      /* next read Compass data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.compass = intbuf; 

      /* next read BP data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.bp = intbuf; 

      /* next read RH% data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.rh = intbuf; 

      /* next read TH data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.th = intbuf; 

      /* next read TA data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.ta = intbuf; 

      /* next read TS data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.ts = intbuf; 

      /* next read SWR data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.sr = intbuf; 

      /* next read LWR Dome data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.dome = intbuf; 

      /* next read LWR Body data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.body = intbuf; 

      /* next read LWR thermopile data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.tpile = intbuf; 

      /* next read LWFlux data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.lwflux = intbuf; 

      /* next read PRC level data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.prlev = intbuf; 

      /* next read BAT1 data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.bat1 = intbuf; 

      /* next read BAT2 data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.bat2 = intbuf; 

      /* next read BAT3 data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.bat3 = intbuf; 

      /* next read BAT4 data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.bat4 = intbuf; 

      /* next read OPT_PARM data value */
      ret = fread(&lbuf,sizeof(long),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.opt_parm = lbuf; 

      /* next read SPARE1 data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.spare1 = intbuf; 

      /* next read SPARE2 data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.spare2 = intbuf; 

      /* next read SPARE3 data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.spare3 = intbuf; 

      /* 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 temp storage */
      LOGR_data.used = used;

      /* now process and write out the record */
      /* first do time */
      fprintf(fd2,"%.2u,",(unsigned int)LOGR_data.hour); 
      fprintf(fd2,"%.2u,",(unsigned int)LOGR_data.min);  // minute
      fprintf(fd2,"%.2u,",(unsigned int)LOGR_data.day); 
      fprintf(fd2,"%.2u,",(unsigned int)LOGR_data.mon); 
      fprintf(fd2,"%.4u,",((unsigned int)LOGR_data.year + 2000));
		
      /* sequential record number */
      fprintf(fd2,"%.5u,",(unsigned int)LOGR_data.record);
		
      /* muxed parameter number */      fprintf(fd2,"%.3u,",(unsigned int)LOGR_data.mux_parm); 

      /* now We, Wn, WSavg, Wmax, Wmin */
      fprintf(fd2,"%7.2f,",((float)LOGR_data.we) / 100.0); 
      fprintf(fd2,"%7.2f,",((float)LOGR_data.wn) / 100.0);
      fprintf(fd2,"%7.2f,",((float)LOGR_data.wsavg) / 100.0);
      fprintf(fd2,"%7.2f,",((float)LOGR_data.wmax) / 100.0);
      fprintf(fd2,"%7.2f,",((float)LOGR_data.wmin) / 100.0);

      /* vane average degrees and last compass */
      fprintf(fd2,"%6.2f,",((float)LOGR_data.vdavg) / 10.0);
      fprintf(fd2,"%6.2f,",((float)LOGR_data.compass) / 10.0);

      /* BPR */
      fprintf(fd2,"%7.2f,",(((float)LOGR_data.bp) / 100.0) + 900.0);

      /* HRH */      fprintf(fd2,"%7.3f,",((float)LOGR_data.rh) / 100.0);
      fprintf(fd2,"%7.3f,",(((float)LOGR_data.th) / 1000.0) - 20.0);

      /* AIR TMP */
      fprintf(fd2,"%7.3f,",(((float)LOGR_data.ta) / 1000.0) - 20.0);

      /* SEA TMP */
      fprintf(fd2,"%7.3f,",(((float)LOGR_data.ts) / 1000.0) - 20.0);

      /* SWR */
      fprintf(fd2,"%6.1f,",((float)LOGR_data.sr) / 10.0);

      /* LWR dome, body, thermopile, flux */
      fprintf(fd2,"%7.3f,",((float)LOGR_data.dome) / 100.0);
      fprintf(fd2,"%7.3f,",((float)LOGR_data.body) / 100.0);
      fprintf(fd2,"%7.1f,",((float)LOGR_data.tpile) / 10.0);
      fprintf(fd2,"%6.1f,",((float)LOGR_data.lwflux) / 10.0);

      /* PRC level */
      fprintf(fd2,"%7.2f,",((float)LOGR_data.prlev) / 100.0);

      /* battery voltage 1 to 4 - NOT USED */
      fprintf(fd2,"%6.2f,",((float)LOGR_data.bat1) / 1000.0);
      fprintf(fd2,"%6.2f,",((float)LOGR_data.bat2) / 1000.0);
      fprintf(fd2,"%6.2f,",((float)LOGR_data.bat3) / 1000.0);
      fprintf(fd2,"%6.2f,",((float)LOGR_data.bat4) / 1000.0);

      /* OPT_PARM - NOT USED */
      fprintf(fd2,"%lu,",LOGR_data.opt_parm);

      /* SPARES 1 to 3 - NOT USED */
      fprintf(fd2,"%u,",LOGR_data.spare1);
      fprintf(fd2,"%u,",LOGR_data.spare2);
      fprintf(fd2,"%u\r\n",LOGR_data.spare3);

      /* look for EOf */
      if (feof(fd1))
         break;
      } /* end while */

end:
   printf("\n");
   fclose(fd1);
   fclose(fd2);
}
