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

/* 2B OZONE 32 byte packed data record structure for storage in FLASH */

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

   float ozone;          /* 2B ozone value in ppbv */
   float cell_temp;               /* 2B cell temperature in degrees C */
   float cell_pressure;           /* 2B cell pressure in mbar */

   float wsavg;                    /* wind speed m/s */
   unsigned char rain_detect;      /* rain detector 1 = RAIN */

   unsigned char spare2;
   unsigned short curr_elapsed;    /* elapsed minutes on the current sample */
   unsigned char system_status;    /* status byte */
   unsigned char maincpu_status;   /* status byte */
   unsigned char OZONE_status;     /* status byte for 2B OZONE (see OZONE data protocol) */

   unsigned short used;   /* set to 0xA5A5 upon record write */
   };


struct OZONE_record OZONE_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); 
      OZONE_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 */
      OZONE_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 */
      OZONE_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 */
      OZONE_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 */
      OZONE_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 */
      OZONE_data.record = intbuf; 

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

      /* next read cell temperature data value */
      ret = fread(&fvar,sizeof(float),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage file */
      OZONE_data.cell_temp = fvar;

      /* next read cell pressure data value */
      ret = fread(&fvar,sizeof(float),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage file */
      OZONE_data.cell_pressure = fvar;

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

      /* next read rain detect data value */
      ret = fread(&buffer,sizeof(char),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      OZONE_data.rain_detect = (unsigned char)buffer;

      /* next read spare2 data value */
      ret = fread(&buffer,sizeof(char),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      OZONE_data.spare2 = (unsigned char)buffer;

      /* next read current elapsed time data value */
      ret = fread(&intbuf,sizeof(short),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      OZONE_data.curr_elapsed = intbuf; 

      /* next read system status byte */
      ret = fread(&buffer,sizeof(char),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      OZONE_data.system_status = (unsigned char)buffer;

      /* next read maincpu status byte */
      ret = fread(&buffer,sizeof(char),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      OZONE_data.maincpu_status = (unsigned char)buffer;

      /* next read OZONE status byte */
      ret = fread(&buffer,sizeof(char),1,fd1);
      if ( ret != 1 )
         goto end;
      /* transfer it to temp storage */
      OZONE_data.OZONE_status = (unsigned char)buffer;

      /* 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 */
      OZONE_data.used = used;

      /* now process and write out the record */
      /* first do time */
      fprintf(fd2,"%.2u,",(unsigned int)OZONE_data.hour); 
      fprintf(fd2,"%.2u,",(unsigned int)OZONE_data.min);  // minute
      fprintf(fd2,"%.2u,",(unsigned int)OZONE_data.day); 
      fprintf(fd2,"%.2u,",(unsigned int)OZONE_data.mon); 
      fprintf(fd2,"%.4u,",((unsigned int)OZONE_data.year + 2000));
		
      /* sequential record number */
      fprintf(fd2,"%.5u,",(unsigned int)OZONE_data.record);
		
      /* now ozone, cell temp, cell pressure */
      fprintf(fd2,"%5.1f,",OZONE_data.ozone);
      fprintf(fd2,"%4.1f,",OZONE_data.cell_temp);
      fprintf(fd2,"%6.1f,",OZONE_data.cell_pressure);

      /* wind speed and rain detect */
      fprintf(fd2,"%5.2f,",OZONE_data.wsavg);
      fprintf(fd2,"%.2u,",(unsigned int)OZONE_data.rain_detect);

      /* current elapsed time */
      fprintf(fd2,"%.5u,",OZONE_data.curr_elapsed);
      
      /* status bytes */
      fprintf(fd2,"%.2X,",(unsigned int)OZONE_data.system_status); 
      fprintf(fd2,"%.2X,",(unsigned int)OZONE_data.maincpu_status);
      fprintf(fd2,"%.2X\r\n",(unsigned int)OZONE_data.OZONE_status); 

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

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