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

   NAV2DIR.C

   Within a time range, this program copies latitude and
   longitude from the NAVIGATION data type into the
   directory.


                     Eric Firing
                     88-06-12
----------------------------------------------------------------

control file structure:                                         

     dbname:    (e.g. WEP)
     time_ranges:
     (list of YMDHMS time pairs:) 
        (yy/mm/dd hh:mm:ss) to (yy/mm/dd hh:mm:ss)
        .
        .
****************************************************************/

#include <math.h>
#include "geninc.h"
#include "ioserv.h" /* PARAMETER_LIST_ENTRY_TYPE, get_parameters(), check_error() */
#include "use_db.h" /* check_db*(), get_time_range(), check_time(), print_time_range() */
#include "data_id.h"

typedef struct
{
   double lat, lon, vel, dir;
} NAVIGATION_TYPE;

/*
Main routine
*/

#if PROTOTYPE_ALLOWED
void do_it(FILE *fp_cnt)
#else
void do_it(fp_cnt)
FILE *fp_cnt;
#endif
{
   int   STEPS = 1;      /* It would not make sense to do
                              this while skipping profiles. */
   int      IERR, ierr;
   int      good_count = 0, bad_count = 0;
   unsigned int nbytes;

   YMDHMS_TIME_TYPE start, end,            /* for time range */
                    this_time;  /* current profile time */
   NAVIGATION_TYPE nav;
   DMSH_POSITION_TYPE position[2];
   int pos_type = POSITION;
   long hun_lat, hun_lon;

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

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

*/

static   FILE_NAME_TYPE  dbname;

static   PARAMETER_LIST_ENTRY_TYPE parameters[] =
   {
      { " dbname:",     " %79s", dbname,                 TYPE_STRING },
      { NULL,           NULL,    NULL,                   0           }
   };                   /* parameters[] */


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

   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))
      {
         check_dbget(NAVIGATION, (char *)&nav, sizeof(nav),
                              "reading navigation");
         if ( nav.lon > -360.0 && nav.lon <= 360.0 &&
              nav.lat >  -90.0 && nav.lat <=  90.0  )
         {
            hun_lon = (long) (nav.lon * 360000.0);
            hun_lat = (long) (nav.lat * 360000.0);
            HUNPOS(  position,      &hun_lon);
            HUNPOS( (position + 1), &hun_lat);
            nbytes = sizeof(position);
            DBPUT(&pos_type, (char *) position, &nbytes, &ierr);
            if (error_found(ierr, "DBPUT position --> stop NAV2DIR"))
               goto close_all;
            good_count++;
         }
         else
         {
            bad_count++;
         }
         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 */

close_all:
   DBCLOSE(&IERR);
   check_error(IERR, "DBCLOSE");
   printf("\nNAV2DIR completed, %d positions written in directory.\n", good_count);
   printf("     %d invalid positions found and not written.\n", bad_count);
   return;
}

