/******************************************************************************

    FILE:  BT2TOBT3.C
    USAGE: BT2TOBT3 <dbname>

           This utility program should be run after converting CODAS version 2
           ADCP databases with bottom track data to version 3 using the
           DB2TODB3.EXE program.  Version 3 ADCP databases and support
           programs have bottom track velocities expressed in m/s while
           version 2 databases and support programs use mm/s.  This program
           performs the rescaling from mm/s to m/s after converting versions.

*/

#include "geninc.h"
#include "use_db.h"                /* db*_cnf() */
#include "data_id.h"               /* BOTTOM_TRACK */
#include "adcp.h"                  /* BOTTOM_TRACK_TYPE */

#if PROTOTYPE_ALLOWED
void main(int argc, char *argv[])
#else
void main(argc, argv)
int argc;
char *argv[];
#endif
{
   FILE_NAME_TYPE       dbname;
   BOTTOM_TRACK_TYPE    bt;
   int                  bt_present, nsteps = 1, ierr = 0;
   unsigned int         n_bt;

   if (argc < 2)
   {
      printf("\n Enter database name ==> ");
      scanf(" %79s", dbname);
   }
   else
      strncpy(dbname, argv[1], 79);
   dbname[79] = '\0';

   if (dbopen_cnf(1, dbname, READ_WRITE, DIR_IN_MEMORY)) exit(-1);

   do
   {
      /*------------------------------------------------
          PERFORM RESCALING OF BOTTOM TRACK VELOCITIES
        ------------------------------------------------*/
      n_bt = sizeof(BOTTOM_TRACK_TYPE);
      bt_present = (!dbget_cnf(BOTTOM_TRACK, (char *) &bt, &n_bt,
         "getting BOTTOM_TRACK") && (n_bt != 0));

      if (bt_present)
      {
         if (bt.u < ADJ_BADFLOAT)
            bt.u /= 1000;            /* convert from mm/s to m/s */
         if (bt.v < ADJ_BADFLOAT)
            bt.v /= 1000;
         if ( (bt.u < ADJ_BADFLOAT) || (bt.v < ADJ_BADFLOAT) )
            if (dbput_cnf(BOTTOM_TRACK, (char *) &bt, &n_bt,
               "putting bottom track")) goto close;
      }

      /*------------------------
          MOVE TO NEXT PROFILE
        ------------------------*/
      DBMOVE(&nsteps, &ierr);
      if (ierr)
      {
         if (ierr != SEARCH_BEYOND_END)
            DBERROR(&ierr, " In DBMOVE\n\n");
         else
            puts("\n Bottom track velocities rescaled\n\n");
      }
   } while (!ierr);

   close:
      dbclose_cnf();
}

