//  TT8 specific includes
#include        <TT8.h>                 /* Tattletale Model 8 Definitions */
#include        <tt8lib.h>              /* definitions and prototypes for Model 8 library */

//  C standard includes
#include		<stdio.h>
#include		<stdlib.h>
#include		<math.h>
#include		<string.h>

//  Data logger program includes
#include		"ASSERT.h"
#include		"MacTrimp.h"
#include		<pcdisk.h>

//  MacTrimpi_print_file (int fd)
//
//	Takes a file descriptor, which is assumed to point at the
//  first char of the file, interprets the data in the file
//  as a MacTrimpi format file, and prints it out to the standard
//  output as ASCII
//
//	This subroutine uses a little over 1024 bytes of stack space
//
int MacTrimpi_print_file (int fd)  {
	struct MacTrimpi header;
	byte buffer[512];	
	byte* pdata;
	long n, nread, ndata, long_value;
	short wordsize;
	double data_min, data_max, binary_max, value;
	double period;
	
	ASSERT (sizeof(struct MacTrimpi) == 512);
	ASSERT ((sizeof(buffer)%4) == 0);
	//  start by reading and printing the header
	nread = po_read(fd, (byte*)(&header), sizeof(struct MacTrimpi));
	if (nread != sizeof (struct MacTrimpi))  {
		printf ("Could not read enough data to file header from file.\n");
		return -1;
	}	
	if (MacTrimpi_print_header (&header) != 0)
		return -1;

	wordsize = atoi (header.wordsize);
	if ((wordsize != 2) && (wordsize != 4))  {
		printf ("File format not recognized: bad word length.\n");
		return -1;
	}	

	//  the following code assumes that we are using signed data, and that
	//  we do not need to swap any bytes
	ASSERT (header.signing == 'S');
	binary_max = pow (2, atoi (header.samplebits) - 1);
	data_max = atof (header.calmax);
	period = atof (header.sampling_period);	
	
	//  now read and print the data
	while ((nread = po_read(fd,(byte*)buffer, sizeof(buffer))) > 0)  {
		if ((nread%wordsize) != 0)
			printf ("File format error: Number of bytes read not multiple of word length!\n"); 

		ndata = nread/wordsize;
		for (n = 0;  n < ndata;  n++)  {
			if (wordsize == 2)
				long_value = *((short*)(&buffer[wordsize*n]));
			else
				long_value = *((long*)(&buffer[wordsize*n]));
				
			//  this conversion assumes that we are converting signed data
			value = data_max*long_value/binary_max;
			printf ("%f  (%ld)\n", (float)(value), long_value);
		}
		if (SerByteAvail ())  {
			printf ("\n\nPrint command interrupted.\n");	
			break;
		}	
	}
	printf ("\n");
	return 0;
}





int MacTrimpi_print_header (struct MacTrimpi* header)  {
	//  this array is declared in the logtime.c file
	extern const char month[12][4];
	int n_month;

	if (strcmp (header->magicstring, "DATA") != 0)  {
		printf ("This file is not in the standard MacTrimpi format.\n");
		return -1;
	}

	printf ("Header version number is %s\n", header->sourcevers);
	printf ("Title: %s\n", header->title);
	printf ("Sampling period is %s seconds.\n", header->sampling_period);
	printf ("Number of bits/sample is %s\n", header->samplebits);
	printf ("Maximum input signal is %s\n", header->calmax);
	printf ("Minimum input signal is %s\n", header->calmin);
	printf ("Date and time first point was recorded was:\n  ");
	n_month = atoi (header->month);
	if ((n_month <= 0) || (n_month > 12))
		printf ("Illegal month!");
	else
		printf ("%s", month[n_month - 1]);
	printf (" %s, %s @ %s:%s:%s.%s\n",
					header->day, header->year,
					header->hours, header->minutes,
						header->seconds, header->msec);
	return 0;						
}	
