/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
 *  This example demonstrates the use of SCALE-OFFSET filter for integer type.
 *      1. Prepare the datatype for the dataset
 *      2. Define the creation property list for the dataset
 *      3. Set the use of SCALE-OFFSET filter in the creation property list
 *      4. Write to and read data from the dataset, then compare the data
 *      5. Clean up
 *  See description of H5Pset_scaleoffset() in the HDF5 Reference Manual.
 */
#include "hdf5.h"
#include <stdlib.h>

#define 	H5FILE_NAME  	"scaleoffset_int.h5"
#define 	DATASET_NAME 	"scaleoffset_int"
#define 	NX 	200
#define 	NY 	300
#define 	CH_NX 	10
#define 	CH_NY 	15

int main(void)
{
    hid_t   file, dataspace, dataset, datatype, properties;
    hsize_t dims[2], chunk_size[2];
    int     orig_data[NX][NY];
    int     new_data[NX][NY];   
    int     i, j, fill_val;   

    /* Define dataset datatype */
    datatype = H5Tcopy(H5T_NATIVE_INT);   
   
    /* Initiliaze data buffer */
    for (i=0; i < NX; i++) 
       for (j=0; j < NY; j++)
           orig_data[i][j] = rand() % 10000;

    /* Create the data space for the dataset */
    dims[0] = NX;
    dims[1] = NY;
    if ((dataspace = H5Screate_simple(2, dims, NULL)) < 0) {
	printf("Error: fail to create data space\n");
	goto error;
    }

    /* Create the HDF5 file */
    if((file = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
	printf("Error: fail to create file\n");
	goto error;
    }

   /*
    * Set the dataset creation property list to specify that
    * the raw data is to be partitioned into 10x15 element chunks.
    * Note that application using the SCALE-OFFSET filter must store data with chunked storage.
    */
    chunk_size[0] = CH_NX;
    chunk_size[1] = CH_NY;
    if((properties = H5Pcreate(H5P_DATASET_CREATE))<0) {
	printf("Error: fail to create dataset property\n");
	goto error;
    }

    if(H5Pset_chunk(properties, 2, chunk_size)<0) {
	printf("Error: fail to set chunk\n");
	goto error;
    }

    /* Set the fill value of dataset */
    fill_val = 10000;
    if (H5Pset_fill_value(properties, H5T_NATIVE_INT, &fill_val)<0) {
	printf("Error: can not set fill value for dataset\n");
	goto error;
    }

    /*
     * Set the SCALE-OFFSET filter in the creation property list of the dataset:
     *  Set integer scale type and let library calculate MinBits
     */
    if(H5Pset_scaleoffset(properties, H5Z_SO_INT, H5Z_SO_INT_MINBITS_DEFAULT)<0) {
	printf("Error: fail to set scaleoffset filter\n");
	goto error;
    }

    /* Create a dataset with the defined property list */
    if ((dataset = H5Dcreate(file, DATASET_NAME, datatype, dataspace, H5P_DEFAULT, properties, H5P_DEFAULT)) < 0) {
	printf("Error: fail to create dataset\n");
	goto error;
    }

    /* Write the data to the dataset and then close it */
    if (H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, orig_data) <0 ) {
	printf("Error: fail to write to dataset\n");
	goto error;
    }
    H5Dclose (dataset);

    /* Reopen the dataset */
    if ((dataset = H5Dopen2(file, DATASET_NAME, H5P_DEFAULT)) < 0) {
	printf("Error: fail to open dataset\n");
	goto error;
    }   

    /* Read the data back from the dataset into a buffer. */
    if(H5Dread (dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, new_data)<0) {
	printf("Error: fail to read from dataset\n");
	goto error;
    }

    /*  
     * Check that the values read are the same as the values written.
     * Exit and return error on the first different value encountered.
     */
    for (i = 0; i < NX; i++) {
	for (j = 0; j < NY; j++) {
	    if (new_data[i][j] != orig_data[i][j]) {
                printf("Error:Read different values than written.\n");
                goto error;
	    }
	}
    }

    /*
     * Closing
     */
    H5Tclose (datatype);
    H5Dclose (dataset);
    H5Sclose (dataspace);
    H5Pclose (properties);
    H5Fclose (file);

    return 0;
error:
    return -1;
}
