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

   TIMELGRID.C

   This program produces a list of subset of time range
   based on the time_range and time_interval specified by the user.
			      
			      Willa Zhu
			      91/05/28

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

control file structure:                                         

     output: (file name)
     time_interval: (in minutes)
     time_range:
        (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*() */


/*
Main routine
*/

#if PROTOTYPE_ALLOWED
void do_it(FILE *fp_cnt)
#else
void do_it(fp_cnt)
FILE *fp_cnt;
#endif
{
   FILE *fp_out;
   YMDHMS_TIME_TYPE start, end, current_date;
   double start_day, end_day, current_day;
   static   int   year_base;
   static   FILE_NAME_TYPE  out_filename;
   static   double   interval;
   static   PARAMETER_LIST_ENTRY_TYPE parameters[] =
   {
      { " output:",         " %79s", out_filename,  TYPE_STRING },
      { " time_interval:",  " %lf",   &interval,     TYPE_DOUBLE },
      { " time_range:",     NULL,    NULL,          0           },
      { NULL,               NULL,    NULL,          0           }
   };                   /* parameters[] */

   sin(0.1);

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

   if ((fp_out = fopen(out_filename, "w")) == NULL)
   {
      printf("\n ERROR: Cannot open file %s.\n",out_filename);
      exit(-1);
   }

   fprintf(fp_out, "/* TimeGrid:\n");
   print_parameters(fp_out, parameters, NULL, "    ");
   fprintf(fp_out, " */\n");
   while ((fscanf(fp_cnt,"%hd/%hd/%hd %hd:%hd:%hd  to %hd/%hd/%hd %hd:%hd:%hd",
      &start.year, &start.month, &start.day, &start.hour, &start.minute,
      &start.second, &end.year, &end.month, &end.day, &end.hour,
      &end.minute,&end.second)) == 12) 
   {

      start.year = yr4digit(start.year);
      end.year   = yr4digit(end.year);
      year_base = start.year;
      fprintf(fp_out, "/* time_range: ");
      write_ymdhms_time(fp_out, &start);
      fprintf(fp_out, "  to  ");
      write_ymdhms_time(fp_out, &end);

      fprintf(fp_out, " */");

      start_day = year_day(&start, year_base);
      end_day = year_day(&end, year_base);
      current_day = start_day;
      fprintf(fp_out, "\n");
      while((end_day - current_day) > (1./(24.*60.)))
      {
         fprintf(fp_out, "   ");
         yd_to_ymdhms_time(current_day, year_base, &current_date);
         write_ymdhms_time(fp_out, &current_date);
         fprintf(fp_out, "  to  ");
         current_day += interval/(24.*60.);
         yd_to_ymdhms_time(current_day, year_base, &current_date);
         if(current_day > end_day)
            write_ymdhms_time(fp_out, &end);
         else
            write_ymdhms_time(fp_out, &current_date);
         fprintf(fp_out, "\n");
      }
   }
   fclose(fp_out);
   return;
}

