/*
*************************************************************
*                                                           *
*  WOODS HOLE OCEANOGRAPHIC INSTITUTION                     *
*  UPPER OCEAN PROCESSES GROUP                              *
*  PHYSICAL OCEANOGRAPHY DEPT.                              *
*  WOODS HOLE, MASSACHUSETTS USA  02543                     *
*                                                           *
*  File: autoswab3.c              Model: -                   *
*                                                           *
*  Function: AUTOIMET binary data file byte swapper
*            for Version 3.xx firmware
*            see Line 75 below for XMODEM vs Direct Files
*                                                           *
*  Project: AUTOIMET                                        *
*                                                           *
*  Programmer: G.A.                                         *
*                                                           *
*  Copyright (c) 2004 Woods Hole Oceanographic Institution  *
*                                                           *
*************************************************************

*/

/* Additional reserved words not recognized by Source Print.
   <e> uchar
*/

/*
*   Program to do two-byte and four-byte swapping on an input
*   file and produce a binary output file.  Must match LOGR53
*   data format...
*
*   This program was designed to run on a 
*   binary .dat file before producing ascii files using
*   "autoasc" in the PC environment.
*
*   Usage:   autoswab inputfile outputfile
*/

#include <stdio.h>
#include <stdlib.h>

void main(argc,argv)
int argc;
char *argv[];
{
   int ret;
   unsigned long i;
   int j;
   char buff[4];
   FILE *fd1,*fd2;

   if (argc < 3)
      {
      printf("Usage: autoswab inputfile outputfile\n");
      exit(-1);
      }

   /* handle input file */
   fd1 = fopen(argv[1],"rb");
   if ( fd1 == NULL )
      {
      printf("Usage: autoswab inputfile outputfile\n");
      exit(-1);
      }

   /* handle output file */
   fd2 = fopen(argv[2],"wb");
   if ( fd2 == NULL )
      {
      printf("Usage: autoswab inputfile outputfile\n");
      exit(-1);
      }

   /* throw away the first 128k bytes (reserved space in FLASH)
      if the FLASH card was read directly in a PC PCMCIA slot;
      preferred is using the dd command while reading the card.  For 
      example:

      dd bs=1024 count=16384 skip=128 if=/dev/mem0c0c of=your_filename 
      
      reads the first 16MB of the card to a file, skipping the first 128K
      bytes. 
      */
   /* REMOVE THIS LOOP IF YOUR RAW DATA FILE WAS CREATED BY XMODEM DUMP
      or YOU SKIPPED USING dd AS ABOVE */
   //   for (j=0; j < 0x20000; j++)
   //     {
       /* get bytes */
   //         ret = fread(&buff[0],sizeof(char),1,fd1);
   //         if ( ret != 1 )
   //            goto end;
   //     }


   /* main loop */
   while(1)
      {
      /* first 5 bytes are time stuff */
      for (j=0; j<5; j++)
         {
         /* read a character */
         ret = fread(&buff[0],sizeof(char),1,fd1);
         if ( ret != 1 )
            goto end;

         /* write the character */
         fwrite(&buff[0],sizeof(char),1,fd2);
         }

      /* next 2 bytes are short int - swap 2 on 1 short;  get bytes in 2,1 order */
      ret = fread(&buff[1],sizeof(char),1,fd1);
      if ( ret != 1 )
         break;
      ret = fread(&buff[0],sizeof(char),1,fd1);
      if ( ret != 1 )
         break;

      /* write bytes in 1,2 order */
      fwrite(&buff[0],sizeof(char),1,fd2);
      fwrite(&buff[1],sizeof(char),1,fd2);

		/* next byte is unsigned char - muxed parameter number */
      ret = fread(&buff[0],sizeof(char),1,fd1);
      if ( ret != 1 )
         goto end;

      /* write the character */
      fwrite(&buff[0],sizeof(char),1,fd2);

      /* next 36 bytes are short int - swap 2 on 18 shorts;  get bytes in 2,1 order */
      for (j=0; j<18; j++)
         {
         ret = fread(&buff[1],sizeof(char),1,fd1);
         if ( ret != 1 )
            break;
         ret = fread(&buff[0],sizeof(char),1,fd1);
         if ( ret != 1 )
            break;

         /* write bytes in 1,2 order */
         fwrite(&buff[0],sizeof(char),1,fd2);
         fwrite(&buff[1],sizeof(char),1,fd2);
         }

      /* next 8 bytes are long int - swap 4 bytes on 2 long */
      for (j=0; j<2; j++)
	 {
	 ret = fread(&buff[3],sizeof(char),1,fd1);
	 if ( ret != 1 )
	    break;
         ret = fread(&buff[2],sizeof(char),1,fd1);
         if ( ret != 1 )
            break;
         ret = fread(&buff[1],sizeof(char),1,fd1);
         if ( ret != 1 )
            break;
         ret = fread(&buff[0],sizeof(char),1,fd1);
         if ( ret != 1 )
            break;

	 /* write bytes in 1,2,3,4 order */
	 fwrite(&buff[0],sizeof(char),1,fd2);
	 fwrite(&buff[1],sizeof(char),1,fd2);
	 fwrite(&buff[2],sizeof(char),1,fd2);
	 fwrite(&buff[3],sizeof(char),1,fd2);
	 }

      /* next 31 bytes are little-endian float and char - don't swap */
      for (j=0; j<31; j++)
         {
         /* read a character */
         ret = fread(&buff[0],sizeof(char),1,fd1);
         if ( ret != 1 )
            goto end;

         /* write the character */
         fwrite(&buff[0],sizeof(char),1,fd2);
         }

      /* next 4 bytes are long int - swap 4 bytes on 1 long */
      for (j=0; j<1; j++)
	 {
	 ret = fread(&buff[3],sizeof(char),1,fd1);
	 if ( ret != 1 )
	    break;
         ret = fread(&buff[2],sizeof(char),1,fd1);
         if ( ret != 1 )
            break;
         ret = fread(&buff[1],sizeof(char),1,fd1);
         if ( ret != 1 )
            break;
         ret = fread(&buff[0],sizeof(char),1,fd1);
         if ( ret != 1 )
            break;

	 /* write bytes in 1,2,3,4 order */
	 fwrite(&buff[0],sizeof(char),1,fd2);
	 fwrite(&buff[1],sizeof(char),1,fd2);
	 fwrite(&buff[2],sizeof(char),1,fd2);
	 fwrite(&buff[3],sizeof(char),1,fd2);
	 }

      /* next 7 bytes are spare */
      for (j=0; j<7; j++)
         {
         /* read a character */
         ret = fread(&buff[0],sizeof(char),1,fd1);
         if ( ret != 1 )
            goto end;

         /* write the character */
         fwrite(&buff[0],sizeof(char),1,fd2);
         }

      
      /* next 2 bytes are short int - swap 2 on 1 shorts;  get bytes in 2,1 order */
      for (j=0; j<1; j++)
         {
         ret = fread(&buff[1],sizeof(char),1,fd1);
         if ( ret != 1 )
            break;
         ret = fread(&buff[0],sizeof(char),1,fd1);
         if ( ret != 1 )
            break;

         /* write bytes in 1,2 order */
         fwrite(&buff[0],sizeof(char),1,fd2);
         fwrite(&buff[1],sizeof(char),1,fd2);
         }

      }

end:
   fclose(fd1);
   fclose(fd2);
}
