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

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

          Program SM2MAT reads an input data file (the output from
          SMOOTHR.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 - EF - 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"   /* check_fopen() */
#include "matfile.h"
#include "cal.h"
#include "smoothr.h"

#define VBUFSIZE 8192

/* #define MAX_MAT_ARRAY 8000  */  /* for PC */

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

   FILE *fp_in, *fp_mat;
   double output_line[8];
   MAT_ARRAY_FP_TYPE *mat_ptr;
   SMOOTHR_OUT_TYPE *sm_ptr;


   if (argc >= 7)
   {
      ncol = max_val(atoi(argv[6]), 1);
      ncol = min_val(ncol, 8);
   }
   if (argc >= 6)
      step = max_val(atoi(argv[5]), 1);
   if (argc >= 5)
      day1 = atof(argv[4]);
   if (argc >= 4)
      day0 = atof(argv[3]);
   if (argc < 3)
   {
      printf("\n USAGE: SM2MAT <source> <destination> [start day] [end day] [step] [ncol]\n");
      printf("  Only the first two arguments are required.\n");
      exit(-1);
   }
   fp_in = check_fopen(argv[1], "rb");
   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

   sm_ptr = (SMOOTHR_OUT_TYPE *)calloc(step, sizeof(SMOOTHR_OUT_TYPE));
   check_error(sm_ptr==NULL, "out of memory for smoothr line buffer");
   mat_ptr = start_mat_array(fp_mat, 'd', "sm", 'c', ncol);

/*  imax = MAX_MAT_ARRAY / ncol;    removed, with no data size limitation */
   do
      finished = fread(sm_ptr, sizeof(SMOOTHR_OUT_TYPE), step, fp_in) != step;
   while (!finished && sm_ptr[0].t < day0);

/*   for (i=0; i<imax && !finished; i++)  removed, with no size limitation */
   while (! finished)
   {
      tonan_d(output_line, (double *)sm_ptr, ncol);
      add_to_mat_array(mat_ptr, (char *)output_line);
      finished = ( (sm_ptr[step-1].t >= day1) ||
         (fread(sm_ptr, sizeof(SMOOTHR_OUT_TYPE), step, fp_in) != step) );
   }

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