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

   LST_BTRK.C

   Within a time range, this program produces a list of
   profile times and bottom track u, v, and depths.  It
   is a minimal conversion from LST_PROF.C.


                     Eric Firing
                     Wed  04-19-1989

   9/28/89 - JR - modified for CODAS3
   2/09/90 - JR - modified to print the negative of the
                  bottom track U  and V velocities to
                  reflect the ship velocity relative to the ground
--------------------------------------------------------------------

control file structure:                                         

     dbname:    (e.g. WEP)
     output: (file name)
     step_size=  (number of profiles to advance)
     year_base=
     time_ranges:
     (list of YMDHMS time pairs:) 
        (yy/mm/dd hh:mm:ss) to (yy/mm/dd hh:mm:ss)
        .
        .
****************************************************************/

#include "geninc.h"
#include <math.h>
#include <string.h>
#include "ioserv.h"
#include "use_db.h"
#include "adcp.h"
#include "data_id.h"
#define  TRUE  1
#define  FALSE 0

#define  sqr(x)  (x)*(x)
#define  good_float(x)  ( (x) < ADJ_BADFLOAT )



/*
Main routine
*/

#if PROTOTYPE_ALLOWED
void do_it(FILE *fp_cnt)
#else
void do_it(fp_cnt)
FILE *fp_cnt;
#endif
{
   FILE *fp_out;

   int      IERR;

   YMDHMS_TIME_TYPE start, end,            /* for time range */
                    this_time;             /*  current profile time */
   BOTTOM_TRACK_TYPE bt;
   int gap_flagged;
   

   /*---------- end of auto declarations for do_it -----------------*/

/***************************************************************
   static variables needed for initialization of
   the parameter array

*/

static   FILE_NAME_TYPE  dbname, out_filename;
static   int   STEPS;
static   int   year_base;

static   PARAMETER_LIST_ENTRY_TYPE parameters[] =
   {
      { " dbname:",     " %79s", dbname,                 TYPE_STRING },
      { " output:",     " %79s", out_filename,           TYPE_STRING },
      { " step_size=",  " %d",   &STEPS,                 TYPE_INT    },
      { " year_base=",  " %d",   &year_base,             TYPE_INT    },
      { NULL,           NULL,    NULL,                   0           }
   };                   /* parameters[] */


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

   sin(0.0);       /* to force linking of float routines */

   if(get_parameters(fp_cnt, parameters, NULL))  exit(-1);

   check_dbopen(1, dbname, READ_ONLY, DIR_IN_MEMORY);

   fp_out = check_fopen(out_filename,"w");


   check_error( (fscanf(fp_cnt, " time_ranges:") != 0),
         "reading time range start");
/*
---> Print the header.
*/
   fprintf(fp_out, "%%Bottom Track time, u, v, depth for :\n%%");
   print_parameters(fp_out, parameters, NULL, ", ");
   fprintf(fp_out, "\n%%\n");
/*
---> Start the loop through time ranges listed in the control file.
*/
   while (get_time_range(fp_cnt, &start, &end) == 1)
   {
      check_dbsrch(TIME_SEARCH, (char *)&start);

      print_time_range(stdout, &start, &end);
      fprintf(fp_out, "\n");
      gap_flagged = 0;
      IERR = 0;
/*
---> Start the loop through profiles within the time range.
*/
      while (!IERR && check_time(&this_time, &start, &end))
      {
         if (check_dbget(BOTTOM_TRACK, (char *)&bt, sizeof(bt),
            "getting bottom track") == sizeof(bt)
            && bt.depth < ADJ_BADFLOAT && bt.depth > 0)
         {
            fprintf(fp_out, "%12.6f  %7.3f  %7.3f  %5.0f\n",
                     year_day(&this_time, year_base),
                     -(bt.u), -(bt.v), bt.depth);
            gap_flagged = 0;
         }
         else if (!gap_flagged)
         {
            fprintf(fp_out, "%%\n");
            gap_flagged = 1;
         }
         DBMOVE(&STEPS, &IERR);   /* IERR is checked at the start of the loop */

      }  /* end of loop through profiles within a time range */

   }  /* end of loop through time ranges read from control file */

   fclose(fp_out);
   DBCLOSE(&IERR);
   check_error(IERR, "DBCLOSE");
   printf("\nLST_BTRK completed.\n");
   return;
}

