/*

    FILE:  pgbin.c

*/

#include "common.h"
#include "time_.h"    /* YMDHMS_TIME_TYPE */
#include "matfile.h"  /* savemat() */
#include "ioserv.h"

#define NMAX 1000

#if PROTOTYPE_ALLOWED
void main(int argc, char *argv[])
#else
void main(argc, argv)
int argc;
char *argv[];
#endif
{
   int i = 0, max_bin[NMAX];
   FILE *fpin, *fpmat;
   FILE_NAME_TYPE infile;
   double dday[NMAX];
   YMDHMS_TIME_TYPE t;

   if (argc < 2)
   {
      printf("\n Enter input file name ==> ");
      scanf(" %79s", infile);
   }
   else strncpy(infile, argv[1], 79);
   if ( (fpin = fopen(infile, "rt")) == NULL )
   {
      printf("\n ERROR: Cannot open input file %s\n\n", infile);
      return;
   }
   if ( (fpmat = fopen(strcat(strtok(infile, "."), ".mat"), "wb")) == NULL )
   {
      printf("\n ERROR: Cannot open output mat file\n\n");
      return;
   }
   while (fscanf(fpin, "Warning at") != EOF)
   {
      if (fscanf(fpin, " %hd/%hd/%hd  %hd:%hd:%hd bin range is %*d to %d",
         &(t.year), &(t.month), &(t.day),
         &(t.hour), &(t.minute), &(t.second), &max_bin[i]) != 7)
      {
         printf("\n ERROR: Scanning input file\n\n");
         goto close;
      }
      t.year = yr4digit(t.year);
      dday[i] = year_day(&t, t.year);
      if (++i == NMAX)
      {
         printf("\n WARNING: Reached array size limit %d\n\n", NMAX);
         break;
      }
   }
   savemat(fpmat, 'd', "dec_days", 1, i, REAL_ROWS, (char *) dday, (char *) NULL);
   savemat(fpmat, 'i', "max_bin" , 1, i, REAL_ROWS, (char *) max_bin, (char *) NULL);
close:
   fclose(fpmat);
   fclose(fpin);
}

