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

   FIX_HDG.C

   Within given time ranges, this program adds a constant value
   (in degree) to ship's heading in CODAS database 
   (ancil2.last_heading).

                       Willa Zhu
                       03-07-1990


  Modified 91/10/27 - JR - to also update ancillary_1.mn_heading
                           if loaded properly (nonzero)

------------------------------------------------------------------

control file structure:                                         

     dbname:    (e.g. WEP)
     degree_add_to_heading=
     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"

#if PROTOTYPE_ALLOWED
void do_it(FILE *fp_cnt)
#else
void do_it(fp_cnt)
FILE *fp_cnt;
#endif
{
   int              IERR, STEP=1;
   unsigned int     n_ancil1, n_ancil2;
   YMDHMS_TIME_TYPE start, end,            /* for time range */
                    this_time;             /*  current profile time */
   ANCILLARY_2_TYPE ancil2;
   ANCILLARY_1_TYPE ancil1;

   static   FILE_NAME_TYPE dbname;
   static   double d_hdg;

   static   PARAMETER_LIST_ENTRY_TYPE parameters[] =
   {
      { " dbname:"               , " %79s", dbname, TYPE_STRING },
      { " degree_add_to_heading=", " %lf" , &d_hdg, TYPE_DOUBLE },
      { 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_WRITE, DIR_IN_MEMORY);
   check_error( (fscanf(fp_cnt, " time_ranges:") != 0),
         "reading time range start");
/*
---> 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);
      IERR = 0;
/*
---> Start the loop through profiles within the time range.
*/
      while (!IERR && check_time(&this_time, &start, &end))
      {
	 n_ancil1 = sizeof(ANCILLARY_1_TYPE);
	 n_ancil2 = sizeof(ANCILLARY_2_TYPE);
         if ( dbget_cnf(ANCILLARY_1, (char *) &ancil1, &n_ancil1,
            "getting ANCILLARY_1")
            || dbget_cnf(ANCILLARY_2, (char *) &ancil2, &n_ancil2,
            "getting ANCILLARY_2") ) goto close_db;
	 printf(" n_ancil2 = %d\n", n_ancil2);
         if (ancil1.mn_heading != 0.0)       /* only if loaded properly */
         {
            ancil1.mn_heading += d_hdg;
            if (ancil1.mn_heading > 360.0) ancil1.mn_heading -= 360.0;
            if (ancil1.mn_heading <   0.0) ancil1.mn_heading += 360.0;
            if (dbput_cnf(ANCILLARY_1, (char *) &ancil1, &n_ancil1,
               "storing ANCILLARY_1")) goto close_db;
         }
         ancil2.last_heading += d_hdg;
         if (ancil2.last_heading > 360.0) ancil2.last_heading -= 360.0;
         if (ancil2.last_heading <   0.0) ancil2.last_heading += 360.0;
         if ( dbput_cnf(ANCILLARY_2, (char *) &ancil2, &n_ancil2,
            "storing ANCILLARY_2") ) goto close_db;
         DBMOVE(&STEP, &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 */

close_db:
   DBCLOSE(&IERR);
   check_error(IERR, "DBCLOSE");
   printf("\n fix_hdg completed.\n\n");
   return;
}

