/*
savematuss.c                                         Steve Mann 1991 March 2

SAVES A SMALLER VERSION OF MATRIX ..................UNSIGNED 16 BIT*

based on savemat.c that comes with Matlab
*/

#include <stdio.h>

typedef struct {
     long type;   /* type */
     long mrows;  /* row dimension */
     long ncols;  /* column dimension */
     long imagf;  /* flag indicating imag part */
     long namlen; /* name length (including NULL) */
} Fmatrix;

savematuss(fp, type, pname, mrows, ncols, imagf, preal, pimag)
FILE *fp;       /* File pointer */
int type;       /* Type flag: Normally 0 for PC, 1000 for Sun, Mac, and	 */
		/* Apollo, 2000 for VAX D-float, 3000 for VAX G-float    */
		/* Add 1 for text variables.	 */
		/* See LOAD in reference section of guide for more info. */
int mrows;      /* row dimension */
int ncols;      /* column dimension */
int imagf;	/* imaginary flag */
char *pname;    /* pointer to matrix name */
/***double *preal; ***/ /* pointer to real data */
unsigned short *preal;
/***double *pimag; ***/ /* pointer to imag data */
unsigned short *pimag;
{
	Fmatrix x;
	int mn;
	
	x.type = type;
	x.mrows = mrows;
	x.ncols = ncols;
	x.imagf = imagf;
	x.namlen = strlen(pname) + 1;
	mn = x.mrows * x.ncols;

	fwrite(&x, sizeof(Fmatrix), 1, fp);
	fwrite(pname, sizeof(char), (int)x.namlen, fp);
/***	fwrite(preal, sizeof(double), mn, fp);   ***/
	fwrite(preal, sizeof(unsigned short), mn, fp);
/*      fprintf(stderr,"sizeof(unsigned short)= %d\n",sizeof(unsigned short));*/
	if (imagf) {
/***	     fwrite(pimag, sizeof(double), mn, fp);   ***/
	     fwrite(pimag, sizeof(unsigned short), mn, fp);
	}
}


