#include "mfhdf.h" #define FILE_NAME "SDS_Skphuff.hdf" #define SDS_NAME "SDS skphuff" #define Z_LENGTH 100 #define X_LENGTH 1000 #define Y_LENGTH 1000 #define RANK 3 /* Macro to check for failure on function returned value */ #define CHECK(status, fail_value, name) {if(status == fail_value) { \ fprintf(stderr, "*** Routine %s FAILED at line %d ***\n", name, __LINE__); num_errs++;}} /* Macro to verify that a value is as expected */ #define VERIFY(item, value, test_name) {if(item != value) { \ fprintf(stderr, "*** UNEXPECTED VALUE from %s is %ld at line %4d in %s\n", test_name, (long)item,(int)__LINE__,__FILE__); num_errs++;}} /**************************************************************************** * tmodpartcomp.c.c - tests the function sdmodcompslab * Structure of the file: * test_modpartcompSDS - test driver * test_modslab - tests modifying compressed SDS in slabs * -BMR, Dec 2013 ****************************************************************************/ /**************************************************************************** * Function: create_file * Purpose: Creates an hdf file containing a compressed SDS having 100x1000x1000 * values of type int32. ****************************************************************************/ intn create_file() { /************************* Variable declaration **************************/ int32 sd_id, sds_id, sds_index; int32 start[RANK], edges[RANK], dim_sizes[RANK], *orig_data, /* points to data to be written to the SDS */ *ptr; /* traverses orig_data */ intn status; /* status returned by functions */ comp_coder_t comp_type; /* Compression flag */ comp_info c_info; /* Compression structure */ int i, j, k; int num_errs = 0; /* number of errors so far */ /********************* End of variable declaration ***********************/ /* Set dimension sizes */ dim_sizes[0] = Z_LENGTH; dim_sizes[1] = Y_LENGTH; dim_sizes[2] = X_LENGTH; /* Create the file and initialize the SD interface */ sd_id = SDstart (FILE_NAME, DFACC_CREATE); CHECK(sd_id, FAIL, "SDstart"); /* Create the data set with the name defined in SDS_NAME */ sds_id = SDcreate (sd_id, SDS_NAME, DFNT_INT32, RANK, dim_sizes); CHECK(sds_id, FAIL, "SDcreate"); /* Set compression for the data set */ comp_type = COMP_CODE_SKPHUFF; c_info.skphuff.skp_size = 18; status = SDsetcompress (sds_id, comp_type, &c_info); CHECK(status, FAIL, "SDsetcompress"); /* Define the location and size of the SDS to be written */ start[0] = 0; start[1] = 0; start[2] = 0; edges[0] = Z_LENGTH; edges[1] = Y_LENGTH; edges[2] = X_LENGTH; /* Allocate and initialize data buffer */ orig_data = (int32 *) HDmalloc(Z_LENGTH*Y_LENGTH*X_LENGTH * DFKNTsize(DFNT_INT32)); CHECK(orig_data, NULL, "HDmalloc"); ptr = orig_data; for (k = 0; k < Z_LENGTH; k++) for (j = 0; j < Y_LENGTH; j++) { for (i = 0; i < X_LENGTH; i++) { *ptr = k + j + i; ptr++; } } /* * Write the stored data to the data set. The last argument * must be explicitly cast to a generic pointer since SDwritedata * is designed to write generic data. */ status = SDwritedata (sds_id, start, NULL, edges, (VOIDP)orig_data); CHECK(status, FAIL, "SDwritedata"); /* Terminate access to the data set */ status = SDendaccess (sds_id); CHECK(status, FAIL, "SDendaccess"); /* Terminate access to the SD interface and close the file */ status = SDend (sd_id); CHECK(status, FAIL, "SDend"); return 0; } /* create_file */ /**************************************************************************** * Function: test_modpartcompSDS * Purpose: This test shows that the function sdmodcompslab can be used to * modify a compressed SDS partially. * Description: * The program calls create_file to create an hdf file containing * a compressed SDS then calls sdmodcompslab to modify some values only. * It then reads back the modified values and verifies them. * -BMR, Jan 2, 2013 ****************************************************************************/ int main( ) { /************************* Variable declaration **************************/ int32 sd_id, sds_id, sds_index; int32 start[3], edges[3], dim_sizes[3], stride[3]; intn status; comp_coder_t comp_type; /* Compression flag */ comp_info c_info; /* Compression structure */ int32 partial_data[10][Y_LENGTH][5]; int32 i, j, k; int num_errs = 0; /* number of errors so far */ /********************* End of variable declaration ***********************/ /* Create a file containing a data set of 100x1000x1000 values */ status = create_file(); CHECK(status, FAIL, "create_file"); /* Buffer array data and define array dimensions */ for (k = 0; k < 10; k++) for (j = 0; j < Y_LENGTH; j++) { for (i = 0; i < 5; i++) partial_data[k][j][i] = -44; } /* Open the file and initialize the SD interface */ sd_id = SDstart (FILE_NAME, DFACC_RDWR); CHECK(status, FAIL, "SDstart"); /* Open the dataset */ sds_id = SDselect (sd_id, 0); CHECK(status, FAIL, "SDselect"); /* Verify compression type */ status = SDgetcompinfo(sds_id, &comp_type, &c_info); CHECK(status, FAIL, "SDgetcompinfo"); VERIFY(comp_type, COMP_CODE_SKPHUFF, "SDgetcompinfo"); /* Attempt to modify some values */ start[0] = 0; start[1] = 0; start[2] = 20; edges[0] = 10; edges[1] = Y_LENGTH; edges[2] = 5; /* Write the modified data to the data set */ status = sdmodcompslab (sds_id, start, NULL, edges, (VOIDP)partial_data); CHECK(status, FAIL, "sdmodcompslab"); /* Verify updated values */ { int32 read_buffer[10][Y_LENGTH][5]; /* same as partial_data above */ HDmemset(&read_buffer, 0, sizeof(read_buffer)); status = SDreaddata(sds_id, start, NULL, edges, (VOIDP)read_buffer); CHECK(status, FAIL, "SDreaddata"); /* Buffer array data and define array dimensions */ for (k = 0; k < 10; k++) for (j = 0; j < Y_LENGTH; j++) { for (i = 0; i < 5; i++) if (read_buffer[k][j][i] != partial_data[k][j][i]) fprintf(stderr, " read_buffer[%d][%d][%d] = %d but partial_data[%d][%d][%d] = %d\n", k, j, i, read_buffer[k][j][i], k, j, i, partial_data[k][j][i]); } } /* Terminate access to the data set */ status = SDendaccess (sds_id); CHECK(status, FAIL, "SDendaccess"); /* Terminate access to the SD interface and close the file */ status = SDend (sd_id); CHECK(status, FAIL, "SDend"); return 0; }