#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "hdf5.h"
#include "DataSignals.h"

/* 
void *chkmallocB(size_t l){
	void *p = malloc(l);
	printf("malloc(%i) = %p\n", (int)l, p);
	return p;		
}

void chkfreeB(void *p){
	printf("free(%p)\n", p);
	free(p);
}
#define malloc(l) chkmallocB(l)
#define free(p) chkfreeB(p)
*/
int strncatAndFixup(char *dest, const char *src, int maxLen);

int main(int argc, char *argv[]){

	float *d = NULL, *x = NULL, *t = NULL;
	int nx, nt, uflag = -1, sflag = -1, ret;
	char *comment = NULL;
	char *units = NULL;
	int i,j;
	int pulse;
	char *dda,*dtype,*cacheRoot;
	int seq = 0;
	char *uid = "";


	if(argc < 5){
		printf("Usage: %s cacheRoot pulse dda dtype [uid [seq]]\n", argv[0]);
		return -EINVAL;
	}

	cacheRoot = argv[1];
	pulse = atoi(argv[2]);
	dda = argv[3];
	dtype = argv[4];
	if(argc > 5)
		uid = argv[5];
	if(argc > 6)
		seq = atoi(argv[6]);

	ret = getPPFFromCache(cacheRoot, uid, pulse, dda, dtype, seq, (void**)&t, &nt, (void**)&x, &nx, (void**)&d, &comment, &units, &uflag, &sflag);

	printf("\nPPF:\n");
	printf("ret = %i\n", ret);
	printf("nx = %i\n", nx);
	printf("nt = %i\n", nt);
	printf("comment = |%s|\n", comment);
	printf("units = |%s|\n", units);
	printf("uflag = %i\n", uflag);
	printf("sflag = %i\n", sflag);



/*	FILE *f = fopen("data","wb");
	fwrite(d, sizeof(float), nt*nx, f);
	fclose(f);

	f = fopen("t","wb");
	fwrite(t, sizeof(float), nt, f);
	fclose(f);

	f = fopen("x","wb");
	fwrite(x, sizeof(float), nx, f);
	fclose(f);

	printf("t: ");
	for(i=0;i<nt;i++)
		printf("%g ",t[i]);

	printf("\n\nx: ");
	for(i=0;i<nx;i++)
		printf("%g ",x[i]);

	printf("\n\nd: ");
	for(i=0;i<(nx*nt);i++)
		printf("%g ",d[i]);
*/
	if(nx==0){
		printf("1D Data:\n");
		for(i=0;i<nt;i++){
			printf("%g\n", d[i]);
		}
	}else{
		printf("2D Data:\n");
		for(i=0;i<nt;i++){
			for(j=0;j<nx;j++)
				printf("%g ", d[j*nt+i]);
			printf("\n");
		}
	}

	if(x)free(x);
	if(t)free(t);
	if(d)free(d);
	if(comment)free(comment);
	if(units)free(units);

	ret = getJPFFromCache(cacheRoot, 70545, "df", "g4-hwp3<pos", (void**)&t, &nt, (void**)&d);

	printf("\nJPF:\n");
	printf("ret = %i\n", ret);
	printf("nt = %i\n", nt);

	if(t)free(t);
	if(d)free(d);

	return 0;
}


