/*
*************************************************************
*                                                           *
*  WOODS HOLE OCEANOGRAPHIC INSTITUTION                     *
*  UPPER OCEAN PROCESSES GROUP                              *
*  PHYSICAL OCEANOGRAPHY DEPT.                              *
*  WOODS HOLE, MASSACHUSETTS USA  02543                     *
*                                                           *
*  File: autoasc.c               Model: -                   *
*                                                           *
*  Function: AUTOIMET binary data file to ASCII converter   *
*                                                           *
*  Project: AUTOIMET                                        *
*                                                           *
*  Programmer: G.A.                                         *
*                                                           *
*  Copyright (c) 2002 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: autoasc 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: autoasc datafile outputfile\n
			 datafile   - LOGR53 binary data file name\n
			 outputfile - ascii data file name\n\n";

/* AUTOIMET 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 */

   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 */

   unsigned short sct; /* SeaCat sea temp deg C */
                       /* (ushort)((sct + 5.0) * 1000) ==> -5.000 to +60.535 degC */
   unsigned short scc; /* SeaCat conductivity Siemens */
                       /* (ushort)(scc * 1000) ==> 0.000 to +6.5535 Siemens/meter */

   unsigned long opt_parm;     /* optional value indicated in rec_mux */
   unsigned long gps_time;     /* UNIX t_time value from NOAA-SEAS GPS bridge system */
   float latitude;             /* lat & lon from NOAA-SEAS GPS bridge system */
   float longitude;
   unsigned short spare;       /* optional spare value */
   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;
	float fvar;
	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 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 SeaCat Temp value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.sct = intbuf; 

      /* next read SeaCat Cond value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.scc = 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 GPS_TIME data value */
      ret = fread(&lbuf,sizeof(long),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.gps_time = lbuf;

      /* next read LATITUDE data */
      ret = fread(&fvar,sizeof(float),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage file */
      LOGR_data.latitude = fvar;

      /* next read LONGITUDE data */
      ret = fread(&fvar,sizeof(float),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage file */
      LOGR_data.longitude = fvar;

      /* next read SPARE data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      LOGR_data.spare = 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);

      /* 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);

      /* SeaCat Temp and Cond */
      fprintf(fd2,"%7.3f,",(((float)LOGR_data.sct) / 1000.0) -5.0);
      fprintf(fd2,"%7.4f,",((float)LOGR_data.scc) / 10000.0);

      /* OPT_PARM */
      fprintf(fd2,"%lu,",LOGR_data.opt_parm);

      /* GPS time (UNIX time_t), latitude, longitude */
      fprintf(fd2,"%lu,",LOGR_data.gps_time);
      fprintf(fd2,"%8.4f,",LOGR_data.latitude);
      fprintf(fd2,"%8.4f,",LOGR_data.longitude);

      /* SPARE - NOT USED */
      fprintf(fd2,"%u\r\n",LOGR_data.spare);

      /* look for EOf */
      if (feof(fd1))
         break;
      } /* end while */

end:
   printf("\n");
   fclose(fd1);
   fclose(fd2);
}
