/*****************************************************************************
*
    PROGRAM:  fillgap.c
    USAGE:    fillgap <dbname> <infile> <outfile>

       This is a block-profile search version of LOGSCAN.C.

       This program reads in each line from the <infile> listing of
       block-profiles that have been detected as bad.  If the tag element
       is "a+n" then the tag line and lines for the next n
       profiles in the database will be written to the
       output file.  If it is "a-n", then the preceding n
       lines will be written along with the tag line.  Any
       line for which the first non-white character is not
       "a" will be passed through.

       J. Ranada - 08/29/88 - University of Hawaii JIMAR
       E. Firing - Thu  09-02-1993  - modified to use the
       "a" tag.
                                                                              *
 *****************************************************************************/

#include "geninc.h"
#include "data_id.h"   /* CODAS oceanographic data variable names & ids      */
#include "ioserv.h"    /* check_fopen(), error_found(), non_blank(), report_msg() */
#include "use_db.h"    /* db*_cnf(), check_blkprf(), write_ymdhms_time() */
#include "dbedit.h"    /* comment() */

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

#if PROTOTYPE_ALLOWED
int log_data(int blkprf[]);
#else
int log_data();
#endif

FILE *fpin, *fpout;

#if PROTOTYPE_ALLOWED
int main(int argc, char *argv[])
#else
int main(argc, argv)
int argc;
char *argv[];
#endif
{
   char             line[255], buff[80], *ch; /* ptr to first non-blank char in line */
   int              i, nprf, ierr = 0;
   int              blkprf[2];
   YMDHMS_TIME_TYPE ptime;
   unsigned int     nt = sizeof(YMDHMS_TIME_TYPE),
		    nb = 2 * sizeof(int);

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

   fpin = check_fopen(infile, "r");
   fpout = check_fopen(outfile, "w");
   set_msg_file(fpout);      /* echo errors/warning to output file */
   if (dbopen_cnf(1, dbname, READ_ONLY, DIR_IN_MEMORY)) exit(-1);

   while (fgets(line, 255, fpin) != NULL)
   {
      ch = non_blank(line);
      if ( ch == NULL || *ch !=  'a' )
      {
         fputs(line, fpout);
      }
      else
      {
         ch++;    /* now it should be '+' or '-' */
         if (! (*ch == '+' || *ch == '-') )
         {
            report_msg(
               "\n%% ERROR: an 'a' tag must be followed by  '-' or '+'\n%  ");
            report_msg(line);
            goto close;
         }


         if (*ch == '+') fputs(line, fpout);
         if (sscanf(ch, "%d %d %d", &nprf, &blkprf[0], &blkprf[1]) != 3)
         {
            report_msg(
               "\n%% ERROR: Scanning tag & block-profile number from line:\n%  ");
            report_msg(line);
            goto close;
         }
         if (*ch == '-')   nprf = - nprf;

         ierr = dbsrch_cnf(BLOCK_PROFILE_SEARCH, (char *) blkprf);
        switch(ierr)
        {
            case 0:  break;
            case NO_SUCH_BLOCK_PROFILE:
               sprintf(buff, "\n%% ERROR: No such block-profile %d %d", blkprf[0], blkprf[1]);
               report_msg(buff);
            default:
               goto close;
         }

         if (*ch == '-')
         {
               if (dbmove_cnf(-nprf))  goto close;
         }
         else
         {
             if (dbmove_cnf(1))      goto close;
         }


         for (i = 0; i < nprf; i++)
         {
            if (dbget_cnf(BLOCK_PROFILE_INDEX, (char *) blkprf, &nb,
	       "getting block-profile index")) goto close;
            if (dbget_cnf(TIME, (char *) &ptime, &nt,
	       "getting profile time")) goto close;

            fprintf(fpout, "  0 %3d %3d ", blkprf[0], blkprf[1]);
            write_ymdhms_time(fpout, &ptime);
            fputs(
               "      32767 32767  -1 255 255     -1.0 32767     0     0     0 32767\n",
               fpout);                    /* default values */
            if (dbmove_cnf(1)) goto close;
         }
         if (*ch == '-')   fputs(line, fpout); /* copy end-of-gap log line to output file */
      }
   } /* end while fgets*/

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


