/****************************************************************************/
/* Copyright 2004 MBARI.                                                    */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  : MOOS Power System Data Aquistion Software                     */
/* Author   : Mike Risi                                                     */
/* Created  : 02/10/2004                                                    */
/****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "log_hdr.h"


#define VERSION "0.2"

void byteSwap32(char* bytes);
void byteSwap16(char* bytes);
int readLogFileHeader(LogFileHeader* hdr, FILE* file);

int main(int argc, char *argv[]) 
{
    int i;
    short raw_data;
    float cal_data;
    FILE* in_file = NULL;
    FILE* out_file = NULL;
    LogFileHeader log_header;
    char out_file_name[80];
    char time_str[80];


    printf("bin2ascii version " VERSION ", built on " \
           __DATE__  " at " __TIME__ "\n");
    
    /* only accept two args server and port number */
    if ( argc != 2 )
    {
        fprintf(stderr, "usage: log_parse [logfile]\n");
        goto ErrorExit;
    }

    /* open the log file for reading */
    in_file = fopen(argv[1], "rb");

    if ( in_file == NULL )
    {
        fprintf(stderr, "failed to open input file %s\n", argv[1]);
        goto ErrorExit;
    }

    /* open an output file for the parsed data */
    
    /* create output file name */
    strcpy(out_file_name, argv[1]);
/*    out_file_name[strlen(argv[1]) - 4] = '\0'; */
    strcat(out_file_name, ".txt");
    
    out_file = fopen(out_file_name, "w");

    if ( out_file == NULL )
    {
        fprintf(stderr, "failed to open output file %s\n", out_file_name);
        goto ErrorExit;
    }
    
    /* read out ints until they are all gone */
    while ( readLogFileHeader(&log_header, in_file) ) 
    {
        /* fix ctime format */
        strcpy(time_str, ctime(&log_header.start_time));
        if (strlen(time_str) > 1)
            time_str[strlen(time_str) - 1] = '\0';

        printf("ADC CHANNEL         : %ld\n", log_header.adc_chan);
        printf("SAMPLE PERIOD       : %lu ms\n", log_header.sample_period);
        printf("FIRST SAMPLE LOGGED : %s\n", time_str);
        printf("TOTAL SAMPLES       : %ld\n", log_header.total_samples);
        printf("CALIBRATION OFFSET  : %f\n", log_header.cal_gain);
        printf("CALIBRATION GAIN    : %f\n", log_header.cal_offset);

        /* write log file header to the output file */
	
        fprintf(out_file, "ADC CHANNEL         : %d\r\n", 
                (int)log_header.adc_chan);
        fprintf(out_file, "SAMPLE PERIOD       : %lu ms\r\n", 
                log_header.sample_period);
        fprintf(out_file, "FIRST SAMPLE LOGGED : %s\r\n", time_str); 
        fprintf(out_file, "TOTAL SAMPLES       : %ld\r\n", 
                log_header.total_samples);
        fprintf(out_file, "CALIBRATION OFFSET  : %f\r\n", log_header.cal_gain);
        fprintf(out_file, "CALIBRATION GAIN    : %f\r\n", log_header.cal_offset);
	
        for (i = 0; i < log_header.total_samples; i++)
        {
            /* grab a sample */
            fread(&raw_data, sizeof(short), 1, in_file);
            /* the raw_data is in big endian order, swap it */
            byteSwap16((char*)&raw_data);
            /* apply the cal factors */
            cal_data = (raw_data * log_header.cal_gain) + log_header.cal_offset;
            fprintf(out_file, "%f\r\n", cal_data);
        }
    }

    /* close the log file */
    fclose(in_file);
    fclose(out_file);

    return 0;

ErrorExit:

    fclose(in_file);
    fclose(out_file);

    return -1;

}

int readLogFileHeader(LogFileHeader* hdr, FILE* file)
{
    /* read the header and display */
    int cnt = fread(hdr, sizeof(LogFileHeader), 1, file);

    if ( cnt != 1 ) 
        return 0;

    /* this is from a big endian processor */
    byteSwap32((char*)&hdr->adc_chan);
    byteSwap32((char*)&hdr->sample_period);
    byteSwap32((char*)&hdr->start_time);
    byteSwap32((char*)&hdr->total_samples);    
    byteSwap32((char*)&hdr->cal_gain);
    byteSwap32((char*)&hdr->cal_offset);

    return cnt;
}


void byteSwap32(char* bytes)
{
    char swap_buff[4];
    
    swap_buff[0] = bytes[0];
    swap_buff[1] = bytes[1];
    swap_buff[2] = bytes[2];
    swap_buff[3] = bytes[3];
    
    bytes[0] = swap_buff[3];
    bytes[1] = swap_buff[2];
    bytes[2] = swap_buff[1];
    bytes[3] = swap_buff[0];
}

void byteSwap16(char* bytes)
{
    char swap_buff[2];
    
    swap_buff[0] = bytes[0];
    swap_buff[1] = bytes[1];
    
    bytes[0] = swap_buff[1];
    bytes[1] = swap_buff[0];
}

