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

   GETNAV.C

   Within a time range, this program produces a list of
   profile times and positions.  

                          Willa Zhu
			  06/04/91
----------------------------------------------------------------

control file structure:                                         

     dbname:    (e.g. WEP)
     output: (file name)
     step_size:  (number of profiles to advance)
     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"  /* PARAMETER_LIST_ENTRY_TYPE, get_parameters(), ... */
#include "use_db.h"  /* check_db*() */
#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 */
   NAVIGATION_TYPE  navigation;
   /*---------- 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, 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[] */


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

   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.
*/
/*
---> 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);

      fprintf(fp_out, "\n%% Time range: ");
      write_ymdhms_time(fp_out, &start);
      fprintf(fp_out, " to ");
      write_ymdhms_time(fp_out, &end);
      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))
      {
       if ((check_dbget(NAVIGATION, (char *)&navigation, sizeof(navigation), 
	       "getting NAVIGATION") == sizeof(navigation))) { 
          fprintf(fp_out, "\n");
	  if(good_float(navigation.longitude) && 
		 good_float(navigation.latitude)) {
	     fprintf(fp_out, "%13.7f   %13.7f %13.7f",
	              year_day(&this_time,year_base),
		      navigation.longitude, navigation.latitude);
	  }
          else 
	     fprintf(fp_out, "%% %13.7f         1E+38         1E+38",
            	  year_day(&this_time,year_base));

         DBMOVE(&STEPS, &IERR);   /* IERR is checked at the start of the loop */
	 }

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

      fprintf(fp_out, "\n\n");

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

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