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

   FILE:  DEPTHCNG.C
   USAGE: DEPTHCNG

          This program gets the DEPTH array (within the time range specified
          by the user) from CODAS database and adds on a certain constant
          number (also specified by user) to the DEPTH, then stores new DEPTH
          array in the database.  It also updates value of conf1.tr_depth
          if necessary.

                             Hui Zhu
                             88-11-04

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

#include "geninc.h"      /* READ_WRITE, DIR_IN_MEMORY */
#include "use_db.h"      /* db*_cnf() */
#include "data_id.h"     /* DEPTH */
#include "adcp.h"        /* CONFIGURATION_1_TYPE structure declaration */

#define MAX_NBINS 128

#if PROTOTYPE_ALLOWED
int main(void)
#else
int main()
#endif
{
   FILE_NAME_TYPE dbname;
   unsigned int nbytes;
   int iblkprf[2], end_blk, i, nvalues;
   SHORT depth[MAX_NBINS];
   float add_dep, tr_dep;
   CONFIGURATION_1_TYPE conf;
   DEPTH_RANGE_TYPE dr;
   unsigned int n = sizeof(CONFIGURATION_1_TYPE);

   printf("\n ENTER DATABASE NAME: ");
   gets(dbname);

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

   printf("\n ENTER BLOCK # RANGE (BLK#  BLK#) ==> ");
   scanf("%d %d", &iblkprf[0], &end_blk);
   iblkprf[1] = 0;

   printf("\n ENTER depth to be added (m) ==> ");
   scanf("%f", &add_dep);

   printf("\n ENTER correct value of transducer depth (m) if it needs to be updated");
   printf("\n     (or -1 otherwise) ==> ");
   scanf("%f", &tr_dep);

   while (iblkprf[0] <= end_blk)
   {
      if (dbsrch_cnf(BLOCK_PROFILE_SEARCH, (char *) iblkprf)) goto close_db;
      nbytes = MAX_NBINS * sizeof(SHORT);
      if (dbget_cnf(DEPTH, (char *) depth, &nbytes, "getting depth"))
         goto close_db;
      if (tr_dep >= 0)
      {
         if (dbget_cnf(CONFIGURATION_1, (char *) &conf, &n, "getting configuration"))
            goto close_db;
         conf.tr_depth = tr_dep;
         if (dbput_cnf(CONFIGURATION_1, (char *) &conf, &n, "putting configuration"))
            goto close_db;
      }
      nvalues = nbytes / sizeof(SHORT);
      if (nvalues > 0)
      {
         printf("\n BLOCK # :  %d", iblkprf[0]);
         for (i = 0; i < nvalues; i++) depth[i] += add_dep;
         if (dbput_cnf(DEPTH, (char *) depth, &nbytes, "putting depth"))
            goto close_db;
         /* 94/08/09: jr+ update depth range if needed */
         for (i = 0; i < get_nprofs(); i++)
         {
            nbytes = sizeof(DEPTH_RANGE_TYPE);
            if (dbget_cnf(DEPTH_RANGE, (char *) &dr, &nbytes, "getting depth range"))
               goto close_db;
            if (depth[0] > dr.min_depth) dr.min_depth = depth[0];
            if (depth[nvalues] < dr.max_depth) dr.max_depth = depth[nvalues];
            if (dbput_cnf(DEPTH_RANGE, (char *) &dr, &nbytes, "putting depth range"))
               goto close_db;
            if (dbmove_cnf(1))
               goto close_db;
         }
      }

      iblkprf[0]++;
   }

   close_db:
      dbclose_cnf();
      return(0);
}

