/*****************************************************************************
Copyright 1998 MBARI                                                      
******************************************************************************
Summary  : Converts binary data files into MATLAB MAT files 
Filename : bin2mat
Author   : Michael B. Matthews                                                
Project  : New ROV                                                     
Version  : 2.0                                                         
Created  : 10.8.98  
Modified :   
Notes    : Reads in file in arg[0] and writes out MAT file with ".mat" suffix 
         : argv[1] - filename	
         : argv[2] - beginning buffer number
         : argv[3] - ending buffer number	
******************************************************************************/

#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "mat.h"
#include "matrix.h"
#include "mbariTypes.h"
#include "bin2mat.h"


void main(int argc, char* argv[])
{
  char outfile[100];
  char string[10];
  char infile[100];
  char* mat_suffix = ".mat";
  FILE* fp1;
  MATFile* fp;
  Matrix* a;
#if 0
  Flt64* pr;
  Flt64 time;
  Int32 i,j=0,k,m;
  Int16 n;
  int nbuff = 0;
  int begin, end;
  Flt32 x[OUTPUT_BUFFER_SIZE][9];
  MotionPak_Output_Buffer p;

  if (argc == 2){
    begin = 1;
    end = -1;
  }
  if (argc == 3){
    begin = atoi(argv[2]);
    end = begin;
  }
  if (argc == 4){
    begin = atoi(argv[2]);
    end = atoi(argv[3]);
  }

  strcpy(infile, argv[1]);
  printf("Binary to MAT file conversion\n");
  printf("V1.2, M.B. Matthews, 10.8.98\n");
  printf("opening %s\n", infile);
  if ( (fp1 = fopen(infile, "r")) == NULL ){
    printf("error opening %s\n", infile);
    exit(0);
  }

  strcpy(outfile, argv[1]);
  strcat(outfile, mat_suffix);
  printf("open MAT file %s\n", outfile);
  fp = matOpen(outfile, "w");

  fread((char*) &time, sizeof(Flt64), 1, fp1);
  printf("start time - %f\n", time);
  fread((char*) &n, sizeof(Int16), 1, fp1); /* disregard this number */
  fread((char*) &n, sizeof(Int16), 1, fp1);
  printf("buffer size - %d\n", n);

  MotionPak_Output_Buffer_Initialize(&p, OUTPUT_BUFFER_SIZE);
  while (MotionPak_Read_Buffer(&p, fp1) == OUTPUT_BUFFER_SIZE*9){
    nbuff++;
  }
  printf("number of buffers - %d\n", nbuff);
  fseek(fp1, 2*sizeof(Int16) + sizeof(Flt64), SEEK_SET);
  if ((end == -1) || (end > nbuff)) end = nbuff;
  printf("convert buffers %d to %d\n", begin, end);
  MotionPak_Output_Buffer_Free(&p);
  MotionPak_Output_Buffer_Initialize(&p, OUTPUT_BUFFER_SIZE);

  a = mxCreateDoubleMatrix(9, OUTPUT_BUFFER_SIZE*(end-begin+1), REAL);
  mxSetName(a, "x");
  pr = mxGetPr(a);
  printf("create matrix x(%d x %d)\n", mxGetM(a), mxGetN(a)); 

  j = 0;

  for (k = 1; k < begin; k++)
    MotionPak_Read_Buffer(&p, fp1);
  
  for (k = begin; k <= end; k++){
    if (MotionPak_Read_Buffer(&p, fp1) != OUTPUT_BUFFER_SIZE*9) break;

    for (i = 0; i < OUTPUT_BUFFER_SIZE; i++){
      pr[j++] = (Flt64) p.time[i];
      pr[j++] = (Flt64) p.ref[i];
      pr[j++] = (Flt64) p.ax[i];
      pr[j++] = (Flt64) p.ay[i];
      pr[j++] = (Flt64) p.az[i];
      pr[j++] = (Flt64) p.rx[i];
      pr[j++] = (Flt64) p.ry[i];
      pr[j++] = (Flt64) p.rz[i]; 
      pr[j++] = (Flt64) p.temp[i]; 
    }
    printf(". ");
  }

  printf("\n");

  matPutMatrix(fp, a);
  matClose(fp);
  mxFreeMatrix(a);
  fclose(fp1);
  MotionPak_Output_Buffer_Free(&p);
  printf("done\n");
  exit(0);
#endif
}

#if 0

/******************************************************************************/
/* Function : MotionPak_Output_Buffer_Initialize                              */
/* Purpose  : Creates an output buffer for MotionPak data for real-time file  */
/*            logging                                                         */
/* Inputs   : Output buffer pointer p and size n of buffer                    */
/* Outputs  : 		                                                      */
/******************************************************************************/

STATUS MotionPak_Output_Buffer_Initialize(MotionPak_Output_Buffer* p, int n)
{
  int i;
  p->state = FALSE;
  p->index = -1;
  p->size = n;
  p->time = (Flt32*) malloc((unsigned) n * sizeof(Flt32));
  p->ref = (Flt32*) malloc((unsigned) n * sizeof(Flt32));
  p->ax = (Flt32*) malloc((unsigned) n * sizeof(Flt32));
  p->ay = (Flt32*) malloc((unsigned) n * sizeof(Flt32));
  p->az = (Flt32*) malloc((unsigned) n * sizeof(Flt32));
  p->rx = (Flt32*) malloc((unsigned) n * sizeof(Flt32));
  p->ry = (Flt32*) malloc((unsigned) n * sizeof(Flt32));
  p->rz = (Flt32*) malloc((unsigned) n * sizeof(Flt32));
  p->temp = (Flt32*) malloc((unsigned) n * sizeof(Flt32));
  for (i = 0; i < n; i++){
    p->time[i] = 0.0;
    p->ref[i] = 0.0;
    p->ax[i] = 0.0;
    p->ay[i] = 0.0;
    p->az[i] = 0.0;
    p->rx[i] = 0.0;
    p->ry[i] = 0.0;
    p->rz[i] = 0.0;
    p->temp[i] = 0.0;
  }
}
 

/******************************************************************************/
/* Function : MotionPak_Output_Buffer_Free                                    */
/* Purpose  :                                                                 */
/* Inputs   :                                                                 */
/* Outputs  : 		                                                      */
/******************************************************************************/

void MotionPak_Output_Buffer_Free(MotionPak_Output_Buffer* p)
{
  free(p->time);
  free(p->ref);
  free(p->ax);
  free(p->ay);
  free(p->az);
  free(p->rx);
  free(p->ry);
  free(p->rz);
  free(p->temp);
}


/******************************************************************************/
/* Function : MotionPak_Read_Buffer                                           */
/* Purpose  :                                                                 */
/* Inputs   :                                                                 */
/* Outputs  : 		                                                      */
/******************************************************************************/
int MotionPak_Read_Buffer(MotionPak_Output_Buffer* p, FILE* fp)
{
  Reg int n = 0;
  n += fread((char*) p->time, sizeof(Flt32), p->size, fp);
  n += fread((char*) p->ref, sizeof(Flt32), p->size, fp);
  n += fread((char*) p->ax, sizeof(Flt32), p->size, fp);
  n += fread((char*) p->ay, sizeof(Flt32), p->size, fp);
  n += fread((char*) p->az, sizeof(Flt32), p->size, fp);
  n += fread((char*) p->rx, sizeof(Flt32), p->size, fp);
  n += fread((char*) p->ry, sizeof(Flt32), p->size, fp);
  n += fread((char*) p->rz, sizeof(Flt32), p->size, fp);
  n += fread((char*) p->temp, sizeof(Flt32), p->size, fp);
  return n;
}


#endif

