#ifndef RADAR_HDF5_H
#define RADAR_HDF5_H

#include <hdf5.h>
#include <hdf5_hl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "radar_4g.h"
#include "lcmtypes_wec_ahrs_data_l.h"

//* File Memory Items
struct Scans {
    uint8_t *data; // 4 bit samples stored as 8-bit values.  
    double *times; //time array
    double *angles;//decimal angle array
    int *ranges;   // range array
    int n_samples; // array lengths
} scans_a, scans_b;

//* Reports
struct Reports {
    double *times; //time array
    uint8_t * command; // placeholder
    uint32_t * range;
    uint8_t * gain; // placeholder
    uint8_t * sea_auto; // 0 = off, 1 = harbour, 2 = offshore
    uint32_t * sea; // sea state
    uint8_t * rain; // rain clutter
    uint8_t * interference_rejection; 
    uint8_t * target_expansion;
    uint8_t * target_boost;
    int n_samples; // array lengths
} reports_a, reports_b;

//* AHRS Data
struct Ahrs {
    
    double * packet_time;
    double * timestamp;
    float * latitude;
    float * longitude;
    float * yawAngleTrue;
    float * pitchAngle;
    float * rollAngle;
    float * xAccel;
    float * yAccel;
    float * zAccel;
    float * nVelocity;
    float * eVelocity;
    float * dVelocity;
    float * altitude;
    float * xRateCorrected;
    float * yRateCorrected;
    float * zRateCorrected;
    float * xRateTemp;
    int n_samples; // array lengths
} ahrs;

void h5_open(const char *fname);
void h5_close();
void h5_free_all();
void h5_malloc_reports_a(int dim);
void h5_malloc_reports_b(int dim);
void h5_write_scans_a(int dim);
void h5_write_scans_b(int dim);
void h5_write_reports_a(int dim);
void h5_write_reports_b(int dim);
void h5_write_ahrs(); 

void h5_malloc_scans_a(int dim);
void h5_malloc_scans_b(int dim);

void h5_malloc_ahrs(int dim);
void h5_copy_ahrs(int pos, double * time, lcmtypes_wec_ahrs_data_l * message);

void h5_copy_frame_a(int pos, uint8_t data[SCANS_PER_FRAME][SPOKELEN], double times[SCANS_PER_FRAME], double angles[SCANS_PER_FRAME], int ranges[SCANS_PER_FRAME]);
void h5_copy_frame_b(int pos, uint8_t data[SCANS_PER_FRAME][SPOKELEN], double times[SCANS_PER_FRAME], double angles[SCANS_PER_FRAME], int ranges[SCANS_PER_FRAME]);
void h5_copy_report_a(int pos, double * time, struct RadarReport_02C4_99 * report);
void h5_copy_report_b(int pos, double * time, struct RadarReport_02C4_99 * report);

void h5_malloc_reports_a(int dim);
void h5_malloc_reports_b(int dim);

#endif //RADAR_HDF5_H