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

  FILE:   REF2MAT.C
  USAGE:  REF2MAT <input file> <output file> [start day] [end day] [ncol]

          Program REF2MAT reads an input data file (the output from
          REFABS.EXE) and transfer it into matlab formated data
          file from the 'start day' to the 'end day' if they are
          given, or transfer the whole data file if no date range
          is given.

          Initially, the program limits the matlab data size only
          hold up to 8000 elements for PC matlab version.  Then it
          is revised so that there is no limitation on the data size
          since the 386 matlab is in use.

                                08/27/90

          94/07 - JR - changed to save matrices in column-wise fashion to suit 
		       Matlab version 4 (in particular, 4.2); this requires a 
		       change in refplot.m to transpose the array upon loading

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

#include "common.h"
#include "ioserv.h"
#include "matfile.h"
#include "cal.h"
#include "smoothr.h"

#define VBUFSIZE 8192
/* #define MAX_MAT_ARRAY 8000 */  /* for PC */
#define NOT 0 ==

#if PROTOTYPE_ALLOWED
void main(int argc, char **argv)
#else
void main(argc, argv)
int argc;
char **argv;
#endif
{
   int nr;
   int finished = 0;
   double day0 = 0.0, day1 = 600.0;
   int ncol = 9;
/*   int i, imax;  removed, with no data size limitation */

   FILE *fp_in, *fp_mat;
   char line_buffer[120];
   MAT_ARRAY_FP_TYPE *mat_ptr;
   REFABS_TYPE ref;

   if (argc >= 6)
   {
      ncol = max_val(atoi(argv[5]), 1);
      ncol = min_val(ncol, 9);
   }
   if (argc >= 5)
      day1 = atof(argv[4]);
   if (argc >= 4)
      day0 = atof(argv[3]);
   if (argc < 3)
   {
      printf("\nUsage: ref2mat source destination start_day end_day ncol\n");
      printf("  Only the first two arguments are required.\n");
      exit(-1);
   }
   fp_in = check_fopen(argv[1], "r");
   fp_mat = check_fopen(argv[2], "wb");
#ifdef VBUF
    check_error((   setvbuf(fp_in,  NULL, _IOFBF, VBUFSIZE)
                 || setvbuf(fp_mat, NULL, _IOFBF, VBUFSIZE) ),
                 "Error in setvbuf in main.");
#endif

   mat_ptr = start_mat_array(fp_mat, 'd', "ref", 'c', ncol);

/*  imax = MAX_MAT_ARRAY / ncol;    removed, with no data size limitation */
   do
      finished = (getline_nc(fp_in, line_buffer, 120) == EOF) ||
                 (sscanf(line_buffer, " %lf", &ref.t) != 1);
   while (! finished && ref.t < day0);

/*   for (i=0; i<imax && !finished; i++)  removed, with no size limitation */
   while (! finished)
   {
      nr = sscanf(line_buffer, " %lf %lf %lf %lf %lf %lf %lf %lf %lf",
               &ref.t, &ref.x, &ref.y, &ref.u, &ref.v, &ref.dt,
               &ref.gap_u, &ref.gap_v, &ref.gap_t);
      finished = (nr != 9 || ref.t > day1);
      if (! finished)
      {
         tonan_d((double *)&ref, (double *)&ref, ncol);
         add_to_mat_array(mat_ptr, (char *)&ref);
         finished = (getline_nc(fp_in, line_buffer, 120) == EOF);
      }
   }

   end_mat_array(mat_ptr);
   fclose(fp_in);
   fclose(fp_mat);
}

