/******************************************************************************
*
    PROGRAM:  unbadbin.c
    USAGE:    unbadbin <dbname> <infile>

       This program undoes the effects of badbin on the database given
       by <dbname>.  It reads in the time, block no. and profile no.
       from the <infile>, then unmarks as bad the indicated bins:
       it does this by unsetting the PROFILE_FLAGS bit.  (For
       databases that do not have PROFILE_FLAGS as a variable, badbin
       actually throws out the original velocity values by setting the
       velocitied to BADSHORT.  In this case, unbadbin will not be
       useful and exits right away.)

       The <infile> looks exactly like the input file for the badbin program.
       Each line begins with the ymdhms-time of the bad profile, its block and
       profile no., the no. of bad bins flagged followed by the nos. of the
       bad bins.

       J. Ranada - 01/21/93 - University of Hawaii JIMAR

       2000/09/20 - EF - removed get_time() function; the new one in
                   use_db/time_io.c can be used instead.
                                                                              *


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

#include "geninc.h"
#include <time.h>
#include "data_id.h"    /* U, V, W definitions */
#include "ioserv.h"     /* check_fopen(), error_found(), report_msg() */
#include "use_db.h"     /* db*_cnf(), set_db_dpmask() */
#include "dpmask.h"     /* GLITCHES_REMOVED */
#include "adcpflag.h"   /* GLITCH_BIT */

#define MAX_NBINS 128
#define dbname argv[1]
#define ascname argv[2]

#if PROTOTYPE_ALLOWED
int main(int argc, char *argv[])
#else
int main(argc, argv)
int argc;
char *argv[];
#endif
{
   unsigned int npf;
   int badcount, unbadbin, i;
   UBYTE profile_flags[MAX_NBINS];
   YMDHMS_TIME_TYPE prftime;
   FILE *fpin, *fplog;
   FILE_NAME_TYPE logname;
   char buff[255];
   time_t time_stamp;

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

   fpin = check_fopen(ascname, "r");
   if (dbopen_cnf(1, dbname, READ_WRITE, 0)) exit(-1);

   strcat(strcpy(logname, dbname), ".bbn");
   fplog = check_fopen(logname, "w+t");
   set_msg_file(fplog);
   time(&time_stamp);
   fprintf(fplog, "\n unbadbin run on %s\n\n", ctime(&time_stamp));

   while (!get_time(fpin, &prftime))
   {
      report_msg("\n...Processing ");
      write_ymdhms_time(stdout, &prftime);
      write_ymdhms_time(fplog, &prftime);

      if (dbsrch_cnf(TIME_SEARCH, (char *) &prftime)) goto close;

      npf = MAX_NBINS * sizeof(UBYTE);
      if (dbget_cnf(PROFILE_FLAGS, (char *) profile_flags, &npf,
         "getting profile flags")) goto close;
      if (npf == 0)
      {
         report_msg("\n ERROR:  unbadbin will not work if there is no");
         report_msg("\n         PROFILE_FLAGS variable in the database.\n\n");
         goto close;
      }

      if (error_found( (fscanf(fpin, "%*d %*d %d", &badcount) < 1),
         "reading no. of bad bins")) goto close;
      sprintf(buff, "...badcount = %2d", badcount);
      report_msg(buff);

      for (i = 0; i < badcount; i++)
      {
         if ( (fscanf(fpin, "%d", &unbadbin) != 1) || unbadbin < 1 || unbadbin > npf )
         {
            sprintf(buff, "\n%%%% ERROR: Reading bad bin no. %d\n\n", i+1);
            report_msg(buff);
            goto close;
         }
         sprintf(buff, " %3d", unbadbin);
         report_msg(buff);
         unbadbin--;                     /* adjust for C array indexing from 0 */
         profile_flags[unbadbin] ^= GLITCH_BIT;
      }
      if (dbput_cnf(PROFILE_FLAGS, (char *) profile_flags, &npf,
         "storing profile flags")) goto close;
   }

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


