
/****************************************************************************
 * NCSA HDF                                                                 *
 * Scientific Data Technologies                                             *
 * National Center for Supercomputing Applications                          *
 * University of Illinois at Urbana-Champaign                               *
 * 605 E. Springfield, Champaign IL 61820                                   *
 *                                                                          *
 * For conditions of distribution and use, see the accompanying             *
 * hdf/COPYING f.                                                           *
 *                                                                          *
 ****************************************************************************/

#include "H5IM.h"
#include "pal_rgb.h"
#include <stdlib.h>


 /*
 The following C program provides an examples of how to generate HDF5 image data from 
 floating point data.  In the example we use real life topographic data 
 (from the North American hemisphere). In the dataset sea values are represented 
 as negative numbers and land values are represented as positive numbers. 
 The example generates 3 HDF5 images, one that generates an image from all the values, 
 another that generates an image from the land values and another that generates an 
 image from the sea values.
 
   For the example we used data from MODB, the Mediterranean Oceanic Data Base
   http://modb.oce.ulg.ac.be/
   
*/


#define DATA_FILE  "usa.wri"

int main( void )
{
 hid_t         file_id;
 hsize_t       pal_dims[2] = { 256, 3 };
 float         *data; 
 unsigned char *image_data;         
 int           imax, jmax, kmax;
 float         valex, xmin, xmax, value;
 FILE          *f;
 int           i;
 herr_t        status; 
	char          *srcdir = getenv("srcdir"); /* The source directory */
	char          data_file[512]="";          /* Buffer to hold name of existing data file */

	EXAMPLE("read and process data and make indexed images");
 
 /* Create a new HDF5 file using default properties. */
 file_id = H5Fcreate( "ex_image3.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );
 
/*-------------------------------------------------------------------------
 * read data; the file data format is described below
 *-------------------------------------------------------------------------
 */

	/* Compose the name of the file to open, using the srcdir, if appropriate */
	if ( srcdir )
	{
	 strcpy(data_file, srcdir);
	 strcat(data_file, "/");
 }
	strcat( data_file, DATA_FILE );
 
	/* Read  data file */   
 f  = fopen( data_file, "r" ) ;
 
 if ( f == NULL )
 {
  printf( "Could not find file %s. Try set $srcdir \n", DATA_FILE );
  H5Fclose( file_id );
		return 0;
 }
 
 
 fscanf( f, "%d %d %d", &imax, &jmax, &kmax ); 
 fscanf( f, "%f %f %f", &valex, &xmin, &xmax ); 
 
 data = (float*) malloc ( imax * jmax * kmax * sizeof( float ));
 image_data = (unsigned char*) malloc ( imax * jmax * kmax * sizeof( unsigned char ));
 
 for ( i = 0; i < imax * jmax * kmax; i++ ) 
 {
  fscanf( f, "%f ", &value ); 
  data[i] = value;
 }
 fclose( f );
 
 
 /*-------------------------------------------------------------------------
 * transform the data from floating point to unsigend char
 * we are processing all the  data here
 *-------------------------------------------------------------------------
 */
 
 for ( i = 0; i < imax * jmax * kmax; i++ ) 
 {
  image_data[i] = (unsigned char)(( 255 * (data[i] - xmin ) ) / (xmax - xmin ));
 }
 
 /* Make the image */
 status=H5IMmake_image_8bit( file_id, "All data", (hsize_t)imax,(hsize_t) jmax, image_data );
 
 /*-------------------------------------------------------------------------
 * transform the data from floating point to unsigend char
 * here we just process the land data
 *-------------------------------------------------------------------------
 */
 
 for ( i = 0; i < imax * jmax * kmax; i++ ) 
 {
  if ( data[i] < 0 )
   image_data[i] = 0;
  else
   image_data[i] = (unsigned char)(( 255 * (data[i] ) ) / xmax );
 }
 
 /* Make the image */
 status=H5IMmake_image_8bit( file_id, "Land data",(hsize_t) imax,(hsize_t) jmax, image_data );
 
 /*-------------------------------------------------------------------------
 * transform the data from floating point to unsigend char
 * here we just process the sea data
 *-------------------------------------------------------------------------
 */
 
 for ( i = 0; i < imax * jmax * kmax; i++ ) 
 {
  if ( data[i] > 0 )
   image_data[i] = 0;
  else
   image_data[i] = (unsigned char)(( 255 * (data[i] - xmin ) ) / xmin );
 }
 
 /* Make the image */
 status=H5IMmake_image_8bit( file_id, "Sea data",(hsize_t) imax, (hsize_t)jmax, image_data );
 
 /*-------------------------------------------------------------------------
 * make a palette and attach it to the datasets
 *-------------------------------------------------------------------------
 */
 
 /* Make a palette */
 status=H5IMmake_palette( file_id, "Rainbow pallete", pal_dims, pal_rgb );
 
 /* Attach the palette to the image datasets */
 status=H5IMlink_palette( file_id, "All data", "Rainbow pallete" );
 status=H5IMlink_palette( file_id, "Land data", "Rainbow pallete" );
 status=H5IMlink_palette( file_id, "Sea data", "Rainbow pallete" );
 
 
 /*-------------------------------------------------------------------------
 * end 
 *-------------------------------------------------------------------------
 */
 
 /* Close the file. */
 H5Fclose( file_id );
 
 free( data );
 free( image_data );

	PASSED();

 return 0;
 
 
}

/*
!The first line of the ASCII file contains the dimension of the array : 
! IMAX, JMAX, KMAX. The integers are stored with the FORTRAN format I5. 
!The second line contains the exclusion value, the minimum and the maximum value of this 
! file. These numbers are stored with the FORTRAN format E12.5. 
! The remaining lines contains the data of the array, with 5 numbers per line 
! (except the last line for each I-line). 
! The array is stored in horizontal slices from sea surface to sea bottom and from 
! north to south. So the indexes go from : 
!
!   DO K = KMAX to 1
!       DO J = JMAX to 1
!           DO I = 1 to IMAX
!              read
!           OD
!       OD
!   OD
! 
!              ____________________________
!             /                           /| (imax,jmax,kmax)
!            /        sea surface        / |
!           /                           /  |
!          /__________________________ /   |
!          |                          |    |
!          |                          |    | (imax,jmax,1)        n
!          |                          |   /                      /
!          |                          |  /                      /
!     ^  j |                          | /             w  <-----o-----> e
!  k  |  / |__________________________|/                      /
!     | /                           (imax,1,1)               /
!     |---------->                                          s
!     i
!
!
!
!
*/

