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

   LST_HDG.C

   Within a time range, this program produces a list of profile
   times and ancil1.mn_heading (if available) and ancil2.last_heading.

                       WILLA ZHU
                       01-23-1991
--------------------------------------------------------------------

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 "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, print_mean_heading = 0;
   YMDHMS_TIME_TYPE start, end,            /* for time range */
                    this_time;             /* current profile time */
   ANCILLARY_1_TYPE ancil1;
   ANCILLARY_2_TYPE ancil2;


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

   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.
*/
   fputs("%", fp_out);
   print_parameters(fp_out, parameters, NULL, "  ");
   fputs("\n%\n%     days      mean     last\n%              heading  heading",
      fp_out);
/*
---> 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");

      /* check if mean heading data are available */
      if (check_dbget(ANCILLARY_1, (char *) &ancil1, sizeof(ancil1),
         "getting ANCILLARY_1") == sizeof(ancil1))
         print_mean_heading = (ancil1.mn_heading != 0.0);

     ierr = 0;
/*
---> Start the loop through profiles within the time range.
*/
      while (!ierr && check_time(&this_time, &start, &end))
      {
         if (check_dbget(ANCILLARY_2, (char *) &ancil2, sizeof(ancil2),
            "getting ANCILLARY_2") == sizeof(ancil2))
         {
            if (print_mean_heading &&
               check_dbget(ANCILLARY_1, (char *) &ancil1, sizeof(ancil1),
               "getting ANCILLARY_1") == sizeof(ancil1))
            {
               fprintf(fp_out, "%12.6f   %6.2f   %6.2f\n",
                  year_day(&this_time, year_base),
                  ancil1.mn_heading,
                  ancil2.last_heading);
            }
            else
            {
               fprintf(fp_out, "%12.6f      %12.2f\n",
                  year_day(&this_time, year_base),
                  ancil2.last_heading);
            }
         }
         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_hdg completed.\n");
   return;
}

