/*
 *	File	: cpic.c
 *	Contents: This file is a sample 3D HDF SDS data generation program.
 *	You need to have compiled HDF libraries in order to make this program.
 *	Use this program only if you want to generate simple datasets for use
 *	by X DataSlice.
 */

#include "../hdf/df.h"
#include <stdio.h>
#include <math.h>

/* Specify the size of the 3D array */
#define MX	11
#define MY	11
#define MZ  11

/* Change the following string data as required */
char *formatStr="E5.2";			/* Formating string of data */
char *dataName="sphere";		/* Name of data */
char *dataUnitStr="Radians";	/* Units */

/* Axes information */
struct {
	char	*units;
	char	*label;
	char	*format;
} dimStrs[3] = { {"Radians","Alpha","E5.2"},	/* X - axis */
				 {"Radians","Beta","E5.2"},		/* Y - axis */
				 {"Radians","Gamma","E5.2"},	/* Z - axis */
			   };

static float32 xfactor=(float32)1.0;	/* x scale factor */
static float32 yfactor=(float32)1.0;	/* y scale factor */
static float32 zfactor=(float32)1.0;	/* z scale factor */

/* Do not change the following variables */
int32  	dims[3] ={MX, MY, MZ};
float32 xdimscale[MX],ydimscale[MY],zdimscale[MZ];
float32 array[MX][MY][MZ];
float32 min,max;
char fname[100];

/*
 *	Data generation dependency function.
 *	Change the following line to your own function.
 */
float32
fnc(x,y,z)
int32 x,y,z;
{
	return(
	   (float32)((x-5)*(x-5)+(y-5)*(y-5)+(z-5)*(z-5))
	   );
}


/*
 *	Data generation procedure
 *	If you plan on converting a 3D array from a file to a 3D SDS HDF
 *	file, then instead of generating the data using the fnc(x,y,z)
 *	function, you should read your data from your file into another
 *	array (eg. arr) that you may declare with the same dimensions as
 *	array and then use fnc(x,y,z) to return the element arr[x][y][z].
 */
void
generate()
{
	int i,j,k;

	min = max = array[0][0][0];
	for (i=0;i<MZ;i++)
	{
	 printf("\n\tDoing Z-plane = %d\n",i);
	 for (j=0;j<MY;j++)
	 {
	  for (k=0;k<MX;k++)
	  {
	   array[k][j][i] = fnc(k,j,i);
	   if (array[k][j][i] < min)
		min = array[k][j][i];
	   else
		if (array[k][j][i] > max)
			max = array[k][j][i];
	  }
	 }
	 printf("\tDone Z-plane = %d\n",i);
	}
}

main()
{
	int		ret,rank;
	int32	i,j,k;
	int32  	inDims[3];
	float32 inmin,inmax;

	printf("Generating data...\n");
	generate();
	printf("Finished producing data...\n");

	/* Set up X-, Y- and Z- scales */
	for (i=0;i<MX;i++)
		xdimscale[i] = (float32)xfactor*i;
	for (i=0;i<MY;i++)
		ydimscale[i] = (float32)yfactor*i;
	for (i=0;i<MZ;i++)
		zdimscale[i] = (float32)zfactor*i;

	sprintf(fname,"%s%dx%dx%d.hdf",dataName,dims[0],dims[1],dims[2]);

	/* Set up HDF SDS file data information */

	ret = DFSDsetdims(3, dims);
	printf("setdims ret = %d\n",ret);
	ret = DFSDsetdatastrs(dataName,dataUnitStr,formatStr,"");
	printf("setdatastrs ret = %d\n",ret);

	for (i=0;i<3;i++)
	{
		ret = DFSDsetdimstrs(i+1, dimStrs[i].label,
							 	dimStrs[i].units,
								dimStrs[i].format);
		printf("setdimstrs %d ret = %d\n",i,ret);
	}
	ret = DFSDsetdimscale(1, dims[0], xdimscale);
	printf("setdimscalex ret = %d\n",ret);
	ret = DFSDsetdimscale(2, dims[1], ydimscale);
	printf("setdimscaley ret = %d\n",ret);
	ret = DFSDsetdimscale(3, dims[2], zdimscale);
	printf("setdimscalez ret = %d\n",ret);

	ret = DFSDsetmaxmin(max,min);
	printf("setmaxmin ret = %d, min=%5.2f, max=%5.2f\n",ret,min,max);

	/* Write array out to HDF file */
	ret = DFSDputdata(fname,3,dims,array);
	printf("putdata ret = %d\n",ret);

/* Uncomment the following only if you want to check your dataset */
/*
	printf("Getting data...\n");
	ret = DFSDgetdims(fname,&rank,inDims,3);
	printf("getdims ret = %d rank=%d, [0]=%ld [1]=%ld [2]=%ld\n",ret,
		rank,inDims[0],inDims[1],inDims[2]);
	ret = DFSDgetmaxmin(&inmax,&inmin);
	printf("getmaxmin ret = %d, min=%5.2f, max=%5.2f\n",ret,inmin,inmax);

	ret = DFSDgetdata(fname, 3,inDims,array);
	printf("getdata ret = %d\n",ret);

	for (i=0;i<inDims[2];i++)
	{
	 printf("\n\tPrinting Z-plane = %d\n",i);
	 for (j=0;j<inDims[1];j++)
	 {
	  for (k=0;k<inDims[0];k++)
	   printf("%5.2f ", array[(k*inDims[1]*inDims[2])+(j*inDims[2])+i]);
	  printf("\n");
	 }
	 printf("\tFinished Printing Z-plane = %d\n",i);
	}
*/
}
