#include #include #include #include #include "hdf5.h" #include "h5trav.h" /* Example 1 dset1 dset2 h5diff_test1.h5 h5diff_test2.h5 Example 2 dset1 dset2 -v h5diff_test1.h5 h5diff_test2.h5 Example 3 -v h5diff_test1.h5 h5diff_test2.h5 Example 4 -v -r h5diff_test1.h5 h5diff_test2.h5 Example 5 dset1 dset2 -n 2 h5diff_test1.h5 h5diff_test2.h5 Example 6 dset3 dset4 -m 0.01 h5diff_test1.h5 h5diff_test2.h5 Example 7 dset5 dset6 -p 0.05 h5diff_test1.h5 h5diff_test2.h5 */ #ifndef FALSE #define FALSE 0 #endif #ifndef TRUE #define TRUE 1 #endif typedef struct options_t { int d_; int g_; int t_; int r_; /* report only what objects differ */ int m_; double m_limit; int p_; double p_percent; int n_; int n_number_count; int a_; int v_; /* verbose */ } options_t; int do_test_files(); int diff_dataset( hid_t file1_id, hid_t file2_id, const char *obj1_name, const char *obj2_name, options_t options ); int array_diff( void *buf1, void *buf2, hsize_t tot_cnt, hid_t type_id, int rank, hsize_t *dims, options_t options ); void print_pos( hsize_t curr_pos, hsize_t *acc, hsize_t *pos, int rank ); /*------------------------------------------------------------------------- * Function: usage * * Purpose: print a usage message * * Return: void * * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu * * Date: October 8, 2002 * * Comments: * * Modifications: * *------------------------------------------------------------------------- */ void usage( const char *progname ) { #define USAGE "\ [-m ] Print a basic help message\n\ [-v ] Print a verbose output message\n\ [-r ] Print only what objects differ \n\ [-d ] Compare only datasets\n\ [-g ] Compare only groups\n\ [-n count] Print difference up to count number for each variable\n\ [-m delta] Print difference when it is greater than limit delta\n\ [-p percent] Print differences which are within a certain percent value\n\ file1 File name of the first HDF5 file\n\ file2 File name of the second HDF5 file\n" fprintf(stderr, "%s [-d delta] file1 file2\n%s", progname, USAGE); exit(EXIT_FAILURE); } /*------------------------------------------------------------------------- * Function: main * * Purpose: H5diff * * Return: Success: 0, Failure: 1 * * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu * * Date: October 8, 2002 * * Comments: * * Modifications: * *------------------------------------------------------------------------- */ int main(int argc, const char *argv[]) { const char *progname = argv[0]; int dset_only = FALSE; int have_group = FALSE; int argno, i, j; const char *s = NULL; const char *file1_name = NULL; const char *file2_name = NULL; char *obj1_name = NULL; char *obj2_name = NULL; hid_t file1_id, file2_id; herr_t status; int nobjects1, nobjects2; info_t *info1=NULL; info_t *info2=NULL; char *attr1_name = NULL; char *attr2_name = NULL; options_t options = {0,0,0,0,0,0,0,0,0,0,0,0}; /* string compare */ char *pdest; int result; int len; do_test_files(); /*------------------------------------------------------------------------- * parse command line options *------------------------------------------------------------------------- */ if (argc < 2) { usage( progname ); exit(EXIT_FAILURE); } /* last 2 items are the file names */ for (argno=1; argno tot_cnt2) tot_cnt = tot_cnt2; else tot_cnt = tot_cnt1; if ( array_diff(buf1,buf2,tot_cnt,type1_id,rank1,dims1,options) == 0 ) printf("No differences found\n" ); free((char *) buf1); free((char *) buf2); /*------------------------------------------------------------------------- * attributes *------------------------------------------------------------------------- */ if ( options.a_==1 ) { } /*------------------------------------------------------------------------- * close *------------------------------------------------------------------------- */ out: /* Close */ status = H5Dclose(dset1_id); status = H5Dclose(dset2_id); status = H5Sclose(space1_id); status = H5Sclose(space2_id); status = H5Tclose(type1_id); status = H5Tclose(type2_id); return 0; } /*------------------------------------------------------------------------- * Function: array_diff * * Purpose: * * Return: Success: 0, Failure: -11 * * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu * * Date: October 8, 2002 * * Comments: * * Modifications: * *------------------------------------------------------------------------- */ int array_diff( void *buf1, void *buf2, hsize_t tot_cnt, hid_t type_id, int rank, hsize_t *dims, options_t options ) { char *i1ptr1, *i1ptr2; short *i2ptr1, *i2ptr2; int *i4ptr1, *i4ptr2; float *fptr1, *fptr2; double *dptr1, *dptr2; int found = 0; /* accumulator and matrix position */ hsize_t acc[32]; hsize_t pos[32]; int i; H5T_class_t type_class; size_t type_size; acc[rank-1]=1; for(i=(rank-2); i>=0; i--) { acc[i]=acc[i+1]*dims[i+1]; } /* Get the class. */ type_class = H5Tget_class( type_id ); /* Get the size. */ type_size = H5Tget_size( type_id ); switch(type_class) { case H5T_INTEGER: switch(type_size) { /*------------------------------------------------------------------------- * H5T_INTEGER 1 *------------------------------------------------------------------------- */ case 1: i1ptr1 = (char *) buf1; i1ptr2 = (char *) buf2; break; /*------------------------------------------------------------------------- * H5T_INTEGER 2 *------------------------------------------------------------------------- */ case 2: i2ptr1 = (short *) buf1; i2ptr2 = (short *) buf2; break; /*------------------------------------------------------------------------- * H5T_INTEGER 4 *------------------------------------------------------------------------- */ case 4: i4ptr1 = (int *) buf1; i4ptr2 = (int *) buf2; for ( i = 0; i < tot_cnt; i++) { if (*i4ptr1 != *i4ptr2) { print_pos( i, acc, pos, rank ); printf(" %d, %d\n", *i4ptr1, *i4ptr2); found++; if ( options.n_ && found>options.n_number_count-1) return found; } i4ptr1++; i4ptr2++; } break; } /*switch*/ break; /* H5T_INTEGER */ case H5T_FLOAT: switch(type_size) { /*------------------------------------------------------------------------- * H5T_FLOAT 4 *------------------------------------------------------------------------- */ case 4: fptr1 = (float *) buf1; fptr2 = (float *) buf2; for ( i = 0; i < tot_cnt; i++) { if ( options.n_ && i>options.n_number_count-1) return found; /* delta but not percentage */ if ( options.m_ && !options.p_ ) { if ( fabs(*fptr1 - *fptr2) > options.m_limit ) { print_pos( i, acc, pos, rank ); printf(" %f, %f\n", *fptr1, *fptr2); found=1; } } /* percentage but not delta */ else if ( !options.m_ && options.p_ ) { if ( 1 - *fptr1 / *fptr2 > options.p_percent ) { print_pos( i, acc, pos, rank ); printf(" %f, %f\n", *fptr1, *fptr2); found=1; } } /* percentage and delta */ else if ( options.m_ && options.p_ ) { if ( 1 - *fptr1 / *fptr2 > options.p_percent && fabs(*fptr1 - *fptr2) > options.m_limit ) { print_pos( i, acc, pos, rank ); printf(" %f, %f\n", *fptr1, *fptr2); found=1; } } else if (*fptr1 != *fptr2) { print_pos( i, acc, pos, rank ); printf(" %f, %f\n", *fptr1, *fptr2); found=1; } fptr1++; fptr2++; } break; /*------------------------------------------------------------------------- * H5T_FLOAT 8 *------------------------------------------------------------------------- */ case 8: dptr1 = (double *) buf1; dptr2 = (double *) buf2; break; } /*switch*/ break; /* H5T_FLOAT */ } /*switch*/ return found; } /*------------------------------------------------------------------------- * Function: print_pos * * Purpose: conver an array index position to matrix notation * * Return: pos matrix array * * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu * * Date: December 19, 2002 * * Comments: * * Modifications: * *------------------------------------------------------------------------- */ void print_pos( hsize_t curr_pos, hsize_t *acc, hsize_t *pos, int rank ) { int i; for ( i = 0; i < rank; i++) pos[i]=0; for ( i = 0; i < rank; i++) { pos[i] = curr_pos/acc[i]; curr_pos -= acc[i]*pos[i]; } assert( curr_pos == 0 ); printf("[ " ); for ( i = 0; i < rank; i++) { printf("%d ", pos[i]+1 ); } printf("] :" ); } /*------------------------------------------------------------------------- * do some test files *------------------------------------------------------------------------- */ int do_test_files() { hid_t file1_id, file2_id; hid_t dataset_id; hid_t space_id; hid_t group_id, group2_id; hsize_t dims [1] = { 7 }; hsize_t dims2 [2] = { 3,2 }; int data1[7] = {1,1,1,1,1,1,1}; int data2[7] = {1,1,1,4,5,6,7}; float data3[7] = {1,1,3,4,5,6,7}; float data4[7] = {1,1,3.02f,4.002f,5.00002f,6,7}; float data5[3][2] = {1,1,3,4,5,6}; float data6[3][2] = {1,1.1f,3.02f,4.002f,5.00002f,6}; /* attribute */ size_t size_attr = 5; float attr_data1[5] = {1,2,3,4,5}; float attr_data2[5] = {1,2.1f,3.01f,4.001f,5.00001f}; herr_t status; /*------------------------------------------------------------------------- * Create two files *------------------------------------------------------------------------- */ /* Create a file */ file1_id = H5Fcreate ("h5diff_test1.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* Create a file */ file2_id = H5Fcreate ("h5diff_test2.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /*------------------------------------------------------------------------- * Make dataset "dset1" on file1 *------------------------------------------------------------------------- */ /* Create a data space */ space_id = H5Screate_simple(1,dims,NULL); /* Create a dataset "dset1" */ dataset_id = H5Dcreate(file1_id,"dset1",H5T_NATIVE_INT,space_id,H5P_DEFAULT); /* Write the data */ status = H5Dwrite(dataset_id,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,data1); /* Close */ status = H5Dclose(dataset_id); status = H5Sclose(space_id); /*------------------------------------------------------------------------- * Make dataset "dset3" on file1 *------------------------------------------------------------------------- */ /* Create a data space */ space_id = H5Screate_simple(1,dims,NULL); /* Create a dataset "dset3" */ dataset_id = H5Dcreate(file1_id,"dset3",H5T_NATIVE_FLOAT,space_id,H5P_DEFAULT); /* Write the data */ status = H5Dwrite(dataset_id,H5T_NATIVE_FLOAT,H5S_ALL,H5S_ALL,H5P_DEFAULT,data3); /* Close */ status = H5Dclose(dataset_id); status = H5Sclose(space_id); /*------------------------------------------------------------------------- * Make group "g1" on file1 *------------------------------------------------------------------------- */ /* Create a group. */ group_id = H5Gcreate(file1_id, "g1", 0); /* Close */ status = H5Gclose(group_id); /*------------------------------------------------------------------------- * Make dataset "dset1" on file2 *------------------------------------------------------------------------- */ /* Create a data space */ space_id = H5Screate_simple(1,dims,NULL); /* Create a dataset "dset1" */ dataset_id = H5Dcreate(file2_id,"dset1",H5T_NATIVE_INT,space_id,H5P_DEFAULT); /* Write the data */ status = H5Dwrite(dataset_id,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,data2); /* Close */ status = H5Dclose(dataset_id); status = H5Sclose(space_id); /*------------------------------------------------------------------------- * Make dataset "dset2" on file2 *------------------------------------------------------------------------- */ /* Create a data space */ space_id = H5Screate_simple(1,dims,NULL); /* Create a dataset "dset1" */ dataset_id = H5Dcreate(file2_id,"dset2",H5T_NATIVE_INT,space_id,H5P_DEFAULT); /* Write the data */ status = H5Dwrite(dataset_id,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,data2); /* Close */ status = H5Dclose(dataset_id); status = H5Sclose(space_id); /*------------------------------------------------------------------------- * Make dataset "g1/dset1" on file2 *------------------------------------------------------------------------- */ /* Create a data space */ space_id = H5Screate_simple(1,dims,NULL); /* Create a group. */ group_id = H5Gcreate(file2_id, "g1", 0); /* Create a dataset "g1/dset1" */ dataset_id = H5Dcreate(group_id,"dset1",H5T_NATIVE_INT,space_id,H5P_DEFAULT); /* Write the data */ status = H5Dwrite(dataset_id,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,data2); /* Close */ status = H5Dclose(dataset_id); status = H5Gclose(group_id); status = H5Sclose(space_id); /*------------------------------------------------------------------------- * Make group "g2/g1" on file2 *------------------------------------------------------------------------- */ /* Create a group. */ group_id = H5Gcreate(file2_id, "g2", 0); group2_id = H5Gcreate(group_id, "g1", 0); /* Close */ status = H5Gclose(group_id); status = H5Gclose(group2_id); /*------------------------------------------------------------------------- * Make dataset "dset4" on file2 *------------------------------------------------------------------------- */ /* Create a data space */ space_id = H5Screate_simple(1,dims,NULL); /* Create a dataset "dset" */ dataset_id = H5Dcreate(file2_id,"dset4",H5T_NATIVE_FLOAT,space_id,H5P_DEFAULT); /* Write the data */ status = H5Dwrite(dataset_id,H5T_NATIVE_FLOAT,H5S_ALL,H5S_ALL,H5P_DEFAULT,data4); /* Close */ status = H5Dclose(dataset_id); status = H5Sclose(space_id); /*------------------------------------------------------------------------- * Make dataset "dset5" on file1 *------------------------------------------------------------------------- */ /* Create a data space */ space_id = H5Screate_simple(2,dims2,NULL); /* Create a dataset "dset" */ dataset_id = H5Dcreate(file1_id,"dset5",H5T_NATIVE_FLOAT,space_id,H5P_DEFAULT); /* Write the data */ status = H5Dwrite(dataset_id,H5T_NATIVE_FLOAT,H5S_ALL,H5S_ALL,H5P_DEFAULT,data5); /* Close */ status = H5Dclose(dataset_id); status = H5Sclose(space_id); /*------------------------------------------------------------------------- * Make dataset "dset6" on file2 *------------------------------------------------------------------------- */ /* Create a data space */ space_id = H5Screate_simple(2,dims2,NULL); /* Create a dataset "dset" */ dataset_id = H5Dcreate(file2_id,"dset6",H5T_NATIVE_FLOAT,space_id,H5P_DEFAULT); /* Write the data */ status = H5Dwrite(dataset_id,H5T_NATIVE_FLOAT,H5S_ALL,H5S_ALL,H5P_DEFAULT,data6); /* Close */ status = H5Dclose(dataset_id); status = H5Sclose(space_id); /*------------------------------------------------------------------------- * Close files *------------------------------------------------------------------------- */ status = H5Fclose(file1_id); status = H5Fclose(file2_id); return 0; }