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

     PROGRAM:  FIXUNITS.C
     USAGE:    FIXUNITS <dbname>

     This program is used to fix the bottom track structure definition
     in ADCP databases where units for the bottom track velocities have
     been misspecified as mm/s.  These include databases loaded using
     version 3 LOADPING.EXE with the producer definition file CODAS3.DEF
     dated earlier than 03 November 1989 (the producer definition file
     had the units misspecified).  These also include databases that
     have been converted from version 2 to version 3 using DB2TODB3.EXE
     with CODAS3.DEF or some external structure definition file for
     which the units are specified as mm/s, and for which the bottom
     track velocities have been rescaled to m/s using BT2TOBT3.EXE.

*/

#include "geninc.h"
#include "use_db.h"    /* db*_cnf() */
#include "ioserv.h"    /* error_found() */

#if PROTOTYPE_ALLOWED
void main(int argc, char *argv[])
#else
void main(argc, argv)
int argc;
char *argv[];
#endif
{
   static struct
   {
      STRUCT_DEF_HDR_TYPE h;
      STRUCT_DEF_ELEM_TYPE e[3];
   } bt_sd[] =
   {
      "BOTTOM_TRACK", 3    , "",
      "u"           , "m/s", FLOAT_VALUE_CODE, 1, "",
      "v"           , "m/s", FLOAT_VALUE_CODE, 1, "",
      "depth"       , "m"  , FLOAT_VALUE_CODE, 1, ""
   };
   FILE_NAME_TYPE dbname;
   int  iblkprf[2], ierr = 0;
   unsigned int nbytes = sizeof(bt_sd);

   iblkprf[0] = iblkprf[1] = 0;
   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   /* for each block file */
   {
      if (dbput_cnf(STRUCTURE_DEF, (char *) bt_sd, &nbytes,
         "storing updated structure definition")) goto close;
      iblkprf[0]++;
      error_found(
         ( ((ierr = dbsrch_cnf(BLOCK_PROFILE_SEARCH, (char *) iblkprf)) != 0)
         && (ierr != NO_SUCH_BLOCK_PROFILE) ), "searching for next block");
   } while (!ierr);
   printf("\n\n UNITS FIXED");

   close:
      dbclose_cnf();
}
