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

   LST_PROF.C

   Within a time range, this program produces a list of
   profile times and positions.  It is a minimal conversion
   from LLGRID.C.


                     Eric Firing
                     Tue  08-09-1988

   Thu  01-04-90  Modified to print longitudes within
                  -179 to +180 range (JR)

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

control file structure:                                         

     dbname:    (e.g. WEP)
     output: (file name)
     output_type: <time_pos> <time_time>
     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*() */

#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 */
   HUN_LL_TYPE    this_position;

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

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

*/

static   FILE_NAME_TYPE  dbname, out_filename;
static   CHAR out_type[10];
static   int   STEPS;
int      time_pos = 0;


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


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

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

   check_dbopen(1, dbname, READ_ONLY, DIR_ON_DISK);
   fp_out = check_fopen(out_filename,"w");

   check_error( (fscanf(fp_cnt, " time_ranges:") != 0),
         "reading time range start");

/*
---> output type
*/
   if( strcmp( out_type, "time_pos" ) == 0 ) time_pos = 1;
/*
---> Print the header.
*/
   if( time_pos )
   {
      fprintf(fp_out, "/*  Profile times and positions for:\n");
      print_parameters(fp_out, parameters, NULL, "    ");
      fprintf(fp_out, "\n*/");
   }
/*
---> Start the loop through time ranges listed in the control file.
*/
   while (get_time_range(fp_cnt, &start, &end) != EOF)
   {
      check_dbsrch(TIME_SEARCH, (char *)&start);

      if( time_pos )
      {
         fprintf(fp_out, "\n/* ");
         print_time_range(fp_out, &start, &end);
         print_time_range(stdout, &start, &end);
         fprintf(fp_out, " */");
      }
      IERR = 0;
/*
---> Start the loop through profiles within the time range.
*/
      while (!IERR && check_time(&this_time, &start, &end))
      {
         fprintf(fp_out, "\n");
         write_ymdhms_time(fp_out, &this_time);
         if( time_pos )
         {
            get_hun_latlon(&this_position);
            if (this_position.lon == BADLONG)
               fprintf(fp_out, "  /*    1E38      1E38 */");
            else
            {
               if (this_position.lon > 64800000L) /* 180 degrees */
               {
                  /* convert to -179 to 180 range */
                  this_position.lon -= 129600000L;
                  if (this_position.lon > 64800000L)
                     this_position.lon -= 129600000L;
               }
               fprintf(fp_out, "  /* %7.3lf  %8.3lf */",
                  to_degrees(this_position.lat),
                  to_degrees(this_position.lon));
            }
         }
         else
         {
            fprintf(fp_out, " to ");
            write_ymdhms_time(fp_out, &this_time);
         }

         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("\nlst_prof completed.\n");
   return;
}

