/*
*************************************************************
*                                                           *
*  WOODS HOLE OCEANOGRAPHIC INSTITUTION                     *
*  UPPER OCEAN PROCESSES GROUP                              *
*  PHYSICAL OCEANOGRAPHY DEPT.                              *
*  WOODS HOLE, MASSACHUSETTS USA  02543                     *
*                                                           *
*  File: vlwrswab.c             Model: -                    *
*                                                           *
*  Function: VOS LWR binary data file byte swap utility     *
*                                                           *
*  Project: VOS                                             *
*                                                           *
*  Programmer: G.A.                                         *
*                                                           *
*  Copyright (c) 1999 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 VOS LWR
*   data format...
*
*   This program was designed to run on
*   binary MET file before producing ascii files using
*   "vlwrasc" in the PC environment.
*
*   Usage:   vlwrswab inputfile outputfile
*/

#include <stdio.h>
#include <stdlib.h>

main(int argc,char *argv[])
{
   short ret;
   unsigned short i;
   short j;
   char buff[4];
   FILE *fd1,*fd2;

   /* check correct calling sequence */
   if (argc < 3)
      {
      printf("Usage: vlwrswab inputfile outputfile\n");
      exit(-1);
      }

   /* handle input file */
   fd1 = fopen(argv[1],"rb");
   if ( fd1 == NULL )
      {
      printf("Usage: vlwrswab inputfile outputfile\n");
      exit(-1);
      }

   /* handle output file */
   fd2 = fopen(argv[2],"wb");
   if ( fd2 == NULL )
      {
      printf("Usage: vlwrswab inputfile outputfile\n");
      exit(-1);
      }

   /* main loop */
   while(1)
      {
      /* first 8 bytes are time stuff - only swap last 2 bytes (year) */
      for (j=0; j<6; 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);
         }

      /* swap year bytes */
      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 240 bytes are short int - swap 2 on 120 shorts;  get bytes in 2,1 order */
      for (j=0; j<120; 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 240 bytes are little-endian float - don't swap */
      for (j=0; j<240; 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 120 bytes are short int - swap 2 on 60 shorts;  get bytes in 2,1 order */
      for (j=0; j<60; 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);
         }

      /* last 4 bytes are short int - swap 2 on 2 shorts;  get bytes in 2,1 order */
      for (j=0; j<2; 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);
}
