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

   >>>>>>>obsolete!!<<<<<<<<

    PROGRAM:  UPDSCAN.C
    USAGE:    UPDSCAN <dbname> <infile>

       This program accesses the CODAS database given by <dbname> for updating.
       The <infile> contains block and profile numbers of profiles for which
       bottom interference has been detected in earlier editing routines
       (FLAG.EXE and LOGSCAN.EXE).

       For those profiles tagged as 0 in the <infile>,

          (1)  the max_amp_bin field in ANCILLARY_2 is set to indicate
               the bin number at which the ocean bottom has been encountered,
               corresponding to the bin at which the amplitude signal reaches
               a maximum (the max_amp_bin field in <infile>);

          (2)  the last_good_bin field in ANCILLARY_2 is set to MAXSHORT
               for further updating under separate programs (BOTMPAS3.EXE
               and LAST_85.EXE).

          (3)  the last_good_bin field in ACCESS_VARIABLES is set to the
               same value as the last_good_bin field in ANCILLARY_2 if
               the latter has a smaller value.

       For those profiles tagged as 1 in the <infile>,
       all the bins are considered bad so

          (1)  max_amp_bin is set to the whatever value that field has in the
               <infile> (usually MAXSHORT) and

          (2)  last_good_bin is set to -1 for both ANCILLARY_2 and
               ACCESS_VARIABLES.

       This editing procedure does not set the data_processing mask.  It is
       primarily an intermediate step in the bottom detection process which
       is completed by the program BOTMPAS3.EXE.  This latter program turns
       on bit 9 of the mask.

       J. Ranada - 03/08/88 - University of Hawaii JIMAR

       Modified 10/17/89 to perform two passes:  Pass 1 checks the
       max_amp_bin field for the <infile> whenever the tag is 0 to
       see if it has been properly set (not a MAXSHORT) and aborts
       the program if there are errors in the <infile>;  otherwise,
       Pass 2 continues with the actual updating of the database as
       described above. - JR
                                                                              *
******************************************************************************/

#include "geninc.h"
#include "data_id.h"            /* CODAS variable names and IDs   */
#include "adcp.h"               /* ADCP data structure typedefs   */
#include "ioserv.h"             /* non_blank(), check_fopen()     */
#include "use_db.h"             /* db*_cnf(), write_ymdhms_time() */
#include "dbedit.h"             /* comment(), update_acc_var()    */

#define dbname argv[1]
#define infile argv[2]

#if PROTOTYPE_ALLOWED
int main(int argc, char *argv[])
#else
int main(argc, argv)
int argc;
char *argv[];
#endif
{
   char             buff[255];
   int              tag, max_amp_bin, file_OK = 1;
   YMDHMS_TIME_TYPE time;
   ANCILLARY_2_TYPE anc;
   CONFIGURATION_1_TYPE conf1;
   FILE             *fpin;
   unsigned int     n;

   if (argc != 3)
   {
      printf("\n ERROR: Invalid number of arguments on command line.");
      printf("\n USAGE: UPDSCAN <dbname> <infile>\n\n");
      exit(-1);
   }

   fpin = check_fopen(infile, "r");
   printf("\n\n PASS 1: Checking input file %s\n\n", infile);
   while (fgets(buff, 255, fpin))
   {
      if (!comment(non_blank(buff)))
      {
         if (sscanf(buff,"%d %*d %*d %*d/%*d/%*d %*d:%*d:%*d %*s %*d %d",
            &tag, &max_amp_bin) != 2)
         {
            printf("\n ERROR: Scanning <infile> line:\n %s\n", buff);
            exit(-1);
         }
         if ( (tag == 0) && (max_amp_bin == MAXSHORT) )
         {
            file_OK = 0;
            buff[79] = '\0';
            printf("%s\n", buff);
         }
      }
   }

   if (!file_OK)
   {
      printf("\n ERROR: max_amp_bin not properly set for above line(s)");
      printf("\n UPDSCAN aborted\n\n");
      exit(-1);
   }

   printf("\n\n PASS 2: Updating database %s\n\n", dbname);
   if (dbopen_cnf(1, dbname, READ_WRITE, DIR_IN_MEMORY)) exit(-1);
   rewind(fpin);
   while (fgets(buff, 255, fpin))
   {
      if (!comment(non_blank(buff)))
      {
         if (sscanf(buff,"%d %*d %*d %hd/%hd/%hd %hd:%hd:%hd %*s %*d %d",
            &tag, &(time.year), &(time.month), &(time.day),
            &(time.hour), &(time.minute), &(time.second), &max_amp_bin) != 8)
         {
            printf("\n ERROR: Scanning <infile> line:\n  %s", buff);
            goto close_db;
         }
         time.year = yr4digit(time.year) ;
         printf("\n...Processing ");
         write_ymdhms_time(stdout, &time);

         if (dbsrch_cnf(TIME_SEARCH, (char *) &time)) goto close_db;

         n = sizeof(CONFIGURATION_1_TYPE);
         if (dbget_cnf(CONFIGURATION_1, (char *) &conf1, &n, "getting configuration data"))
            goto close_db;

         n = sizeof(ANCILLARY_2_TYPE);
         if (dbget_cnf(ANCILLARY_2, (char *) &anc, &n, "getting ancillary data"))
            goto close_db;

         anc.max_amp_bin = max_amp_bin;

         if (tag)
            anc.last_good_bin = -1;                         /* all bins are bad */
         else
            anc.last_good_bin = conf1.num_bins;      /* reset to default;
                                                      alter later */

         if (dbput_cnf(ANCILLARY_2, (char *) &anc, &n, "storing ancillary data"))
            goto close_db;
         if (update_acc_var(anc.last_good_bin)) goto close_db;
      }
   }

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