00001 #include "h5File.h"
00002 #include "clH5Handler.h"
00003 #include <assert.h>
00004 #include <stdio.h>
00005 #include <string.h>
00006 #include <time.h>
00007
00008 #define PRINT_ALL 1
00009 #define TEST_ATTRI 1
00010 #define TEST_PALETTE 1
00011 #define TEST_SUBSET 1
00012
00013 #if 0
00014 #define HDF5_LOCAL 1
00015 #endif
00016
00017 int print_group(rcComm_t *conn, const H5Group *pg);
00018 int print_dataset(const H5Dataset *d);
00019 int print_attribute(const H5Attribute *a);
00020
00021 int main(int argc, char* argv[])
00022 {
00023 int ret_value=0, i=0;
00024 char fname[1024];
00025 H5File *f=0;
00026 H5Dataset *d=0;
00027 #ifndef HDF5_LOCAL
00028 int status;
00029 rodsEnv myEnv;
00030 rErrMsg_t errMsg;
00031 #endif
00032 rcComm_t *conn = NULL;
00033
00034
00035
00036
00037
00038
00039
00040 if (argc <=1 )
00041 {
00042 printf("Enter file name: ");
00043 scanf("%s", fname);
00044 } else
00045 strncpy(fname, argv[1], strlen( argv[1] ) ); fname[ strlen( argv[1] )-1 ] = '\0';
00046
00047 if (strlen(fname)<=1)
00048 strcpy (fname, "/tempZone/home/rods/hdf5_test.h5");
00049
00050 printf("\n..... test file: %s\n", fname);
00051 fflush(stdout);
00052
00053 #ifndef HDF5_LOCAL
00054 status = getRodsEnv (&myEnv);
00055
00056 if (status < 0) {
00057 rodsLogError (LOG_ERROR, status, "main: getRodsEnv error. ");
00058 exit (1);
00059 }
00060 conn = rcConnect (myEnv.rodsHost, myEnv.rodsPort, myEnv.rodsUserName,
00061 myEnv.rodsZone, 1, &errMsg);
00062
00063 if (conn == NULL) {
00064 rodsLogError (LOG_ERROR, errMsg.status, "rcConnect failure %s",
00065 errMsg.msg);
00066 exit (2);
00067 }
00068
00069 status = clientLogin(conn);
00070 if (status != 0) {
00071 rcDisconnect(conn);
00072 exit (7);
00073 }
00074 #endif
00075
00076 f = (H5File*)malloc(sizeof(H5File));
00077 assert(f);
00078 H5File_ctor(f);
00079
00080 f->filename = (char*)malloc(strlen(fname)+1);
00081 strcpy(f->filename, fname);
00082
00083
00084
00085
00086
00087
00088
00089 f->opID = H5FILE_OP_OPEN;
00090 ret_value = h5ObjRequest(conn, f, H5OBJECT_FILE);
00091 if (ret_value < 0) {
00092 fprintf (stderr, "H5FILE_OP_OPEN failed, status = %d\n", ret_value);
00093 exit (1);
00094 }
00095
00096 #ifdef foo
00097
00098
00099
00100
00101
00102
00103
00104
00105 h5ObjProcess(f, H5OBJECT_FILE);
00106 #endif
00107
00108
00109
00110 if (f->fid < 0)
00111 goto exit;
00112
00113 assert(f->root);
00114
00115
00116
00117
00118 for (i=0; i<f->root->ndatasets; i++)
00119 {
00120 d = (H5Dataset *) &f->root->datasets[i];
00121 d->type.order = (H5Datatype_order_t)get_machine_endian();
00122 }
00123
00124 #ifdef PRINT_ALL
00125 ret_value = print_group(conn, f->root);
00126 #endif
00127
00128
00129
00130
00131
00132 d = NULL;
00133
00134 if (f->root->ndatasets > 0) {
00135 d = (H5Dataset *) &f->root->datasets[0];
00136 }
00137
00138
00139 if (d)
00140 {
00141 time_t t1=time(NULL);
00142
00143
00144
00145
00146
00147
00148
00149 d->opID = H5DATASET_OP_READ;
00150 ret_value = h5ObjRequest(conn, d, H5OBJECT_DATASET);
00151 if (ret_value < 0) {
00152 fprintf (stderr, "H5DATASET_OP_READ failed, status = %d\n",
00153 ret_value);
00154 exit (1);
00155 }
00156
00157
00158 #ifdef foo
00159
00160
00161
00162
00163
00164
00165
00166
00167 h5ObjProcess(d, H5OBJECT_DATASET);
00168 #endif
00169
00170 time_t t2=time(NULL);
00171 printf("HDF5 time on read: %d seconds\n", d->time);
00172 printf("Total time on read: %ld seconds\n", t2-t1);
00173
00174
00175 print_dataset(d);
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 f->opID = H5FILE_OP_CLOSE;
00187 ret_value = h5ObjRequest(conn, f, H5OBJECT_FILE);
00188 if (ret_value < 0) {
00189 fprintf (stderr, "H5FILE_OP_CLOSE failed, status = %d\n", ret_value);
00190 exit (1);
00191 }
00192
00193 #ifdef foo
00194
00195
00196
00197
00198
00199
00200
00201
00202 h5ObjProcess(f, H5OBJECT_FILE);
00203 #endif
00204
00205 exit:
00206 H5File_dtor(f);
00207 if (f) free(f);
00208
00209 rcDisconnect (conn);
00210 return ret_value;
00211 }
00212
00213 int print_group(rcComm_t *conn, const H5Group *pg)
00214 {
00215 int ret_value=0, i=0, j=0;
00216 H5Group *g=0;
00217 H5Dataset *d=0;
00218
00219 assert(pg);
00220
00221 printf("\n====== HDF5 Group: %s\n", pg->fullpath);
00222 printf("\tNumber of datasets: %d\n", pg->ndatasets);
00223 printf("\tNubmer of groups: %d\n", pg->ngroups);
00224 printf("\tNumber of attributes: %d\n", pg->nattributes);
00225
00226 for (i=0; i<pg->nattributes; i++)
00227 print_attribute(&(pg->attributes[i]));
00228
00229 for (i=0; i<pg->ngroups; i++)
00230 {
00231 g = (H5Group *) &pg->groups[i];
00232
00233 #ifdef TEST_ATTRI
00234 g->opID = H5GROUP_OP_READ_ATTRIBUTE;
00235 ret_value = h5ObjRequest(conn, g, H5OBJECT_GROUP);
00236 if (ret_value < 0) {
00237 fprintf (stderr,
00238 "H5GROUP_OP_READ_ATTRIBUTE failed, status = %d\n", ret_value);
00239 exit (1);
00240 }
00241 #endif
00242
00243 ret_value = print_group(conn, g);
00244 }
00245
00246 for (i=0; i<pg->ndatasets; i++)
00247 {
00248 d = (H5Dataset *) &pg->datasets[i];
00249
00250 #ifdef TEST_PALETTE
00251 if (d->nattributes > 0)
00252 {
00253 int i=0;
00254 unsigned char *pv;
00255 H5Attribute a = (d->attributes)[0];
00256 if ( strcmp(a.name, PALETTE_VALUE) == 0)
00257 {
00258 pv = (unsigned char *)a.value;
00259 for (i=0; i<20; i++) printf("%u\t", *((unsigned char*)(pv+i)));
00260 }
00261 }
00262 #endif
00263
00264 d->opID = H5DATASET_OP_READ;
00265 d->nattributes = 0;
00266
00267 #ifdef TEST_SUBSET
00268 for (j=0; j<d->space.rank; j++)
00269 {
00270 d->space.count[j] = d->space.dims[j]/10;
00271 if (d->space.count[j] == 0) d->space.count[j] =1;
00272 }
00273 #endif
00274
00275 ret_value = h5ObjRequest(conn, d, H5OBJECT_DATASET);
00276 if (ret_value < 0) {
00277 fprintf (stderr, "H5DATASET_OP_READ failed, status = %d\n",
00278 ret_value);
00279 exit (1);
00280 }
00281
00282 #ifdef TEST_ATTRI
00283 d->opID = H5DATASET_OP_READ_ATTRIBUTE;
00284 ret_value = h5ObjRequest(conn, d, H5OBJECT_DATASET);
00285 if (ret_value < 0) {
00286 fprintf (stderr,
00287 "H5DATASET_OP_READ_ATTRIBUTE failed, status = %d\n", ret_value);
00288 exit (1);
00289 }
00290 #endif
00291
00292 ret_value = print_dataset(d);
00293 }
00294
00295 return ret_value;
00296 }
00297
00298 int print_dataset(const H5Dataset *d)
00299 {
00300 int ret_value=0, size=0, i=0;
00301 char* pv;
00302 char **strs;
00303
00304 assert(d);
00305
00306 printf("\n====== HDF5 Dataset: %s\n", d->fullpath);
00307 printf("\tSize of data buffer (bytes) = %d\n", d->nvalue);
00308 printf("\tThe first 10 values:\t");
00309 if (d->value)
00310 {
00311 size = 1;
00312 pv = (char*)d->value;
00313 for (i=0; i<d->space.rank; i++) size *= d->space.count[i];
00314 if (size > 10 ) size = 10;
00315 if (d->type.tclass == H5DATATYPE_VLEN
00316 || d->type.tclass == H5DATATYPE_COMPOUND
00317 || d->type.tclass == H5DATATYPE_STRING)
00318 strs = (char **)d->value;
00319
00320 for (i=0; i<size; i++)
00321 {
00322 if (d->type.tclass == H5DATATYPE_INTEGER)
00323 {
00324 if (d->type.sign == H5DATATYPE_SGN_NONE)
00325 {
00326 if (d->type.size == 1)
00327 printf("%u\t", *((unsigned char*)(pv+i)));
00328 else if (d->type.size == 2)
00329 printf("%u\t", *((unsigned short*)(pv+i*2)));
00330 else if (d->type.size == 4)
00331 printf("%u\t", *((unsigned int*)(pv+i*4)));
00332 else if (d->type.size == 8)
00333 printf("%llu\t", *((unsigned long long*)(pv+i*8)));
00334 }
00335 else
00336 {
00337 if (d->type.size == 1)
00338 printf("%d\t", *((char*)(pv+i)));
00339 else if (d->type.size == 2)
00340 printf("%d\t", *((short*)(pv+i*2)));
00341 else if (d->type.size == 4)
00342 printf("%d\t", *((int*)(pv+i*4)));
00343 else if (d->type.size == 8)
00344 printf("%lld\t", *((long long*)(pv+i*8)));
00345 }
00346 } else if (d->type.tclass == H5DATATYPE_FLOAT)
00347 {
00348 if (d->type.size == 4)
00349 printf("%f\t", *((float *)(pv+i*4)));
00350 else if (d->type.size == 8)
00351 printf("%f\t", *((double *)(pv+i*8)));
00352 } else if (d->type.tclass == H5DATATYPE_VLEN
00353 || d->type.tclass == H5DATATYPE_COMPOUND)
00354 {
00355 if (strs[i])
00356 printf("%s\t", strs[i]);
00357 } else if (d->type.tclass == H5DATATYPE_STRING)
00358 {
00359 if (strs[i])
00360 printf("%s\t", strs[i]);
00361 }
00362 }
00363 printf("\n");
00364 }
00365
00366
00367 printf("\tNumber of attributes: %d\n", d->nattributes);
00368 for (i=0; i<d->nattributes; i++)
00369 print_attribute(&(d->attributes[i]));
00370
00371 return ret_value;
00372 }
00373
00374 int print_attribute(const H5Attribute *a)
00375 {
00376 int ret_value=0, size=0, i=0;
00377 char* pv;
00378 char **strs;
00379
00380 assert(a);
00381
00382 printf("\tName of the attribute: %s", a->name);
00383 printf("\tSize of data buffer (bytes) = %d\n", a->nvalue);
00384 printf("\tThe first 10 values:\t");
00385 if (a->value)
00386 {
00387 size = 1;
00388 pv = (char*)a->value;
00389
00390 for (i=0; i<a->space.rank; i++) size *= a->space.count[i];
00391 if (size > 10 ) size = 10;
00392 if (a->type.tclass == H5DATATYPE_VLEN
00393 || a->type.tclass == H5DATATYPE_COMPOUND
00394 || a->type.tclass == H5DATATYPE_STRING)
00395 strs = (char **)a->value;
00396
00397 for (i=0; i<size; i++)
00398 {
00399 if (a->type.tclass == H5DATATYPE_INTEGER)
00400 {
00401 if (a->type.sign == H5DATATYPE_SGN_NONE)
00402 {
00403 if (a->type.size == 1)
00404 printf("%u\t", *((unsigned char*)(pv+i)));
00405 else if (a->type.size == 2)
00406 printf("%u\t", *((unsigned short*)(pv+i*2)));
00407 else if (a->type.size == 4)
00408 printf("%u\t", *((unsigned int*)(pv+i*4)));
00409 else if (a->type.size == 8)
00410 printf("%llu\t", *((unsigned long long*)(pv+i*8)));
00411 }
00412 else
00413 {
00414 if (a->type.size == 1)
00415 printf("%d\t", *((char*)(pv+i)));
00416 else if (a->type.size == 2)
00417 printf("%d\t", *((short*)(pv+i*2)));
00418 else if (a->type.size == 4)
00419 printf("%d\t", *((int*)(pv+i*4)));
00420 else if (a->type.size == 8)
00421 printf("%lld\t", *((long long*)(pv+i*8)));
00422 }
00423 } else if (a->type.tclass == H5DATATYPE_FLOAT)
00424 {
00425 if (a->type.size == 4)
00426 printf("%f\t", *((float *)(pv+i*4)));
00427 else if (a->type.size == 8)
00428 printf("%f\t", *((double *)(pv+i*8)));
00429 } else if (a->type.tclass == H5DATATYPE_VLEN
00430 || a->type.tclass == H5DATATYPE_COMPOUND)
00431 {
00432 if (strs[i])
00433 printf("%s\t", strs[i]);
00434 } else if (a->type.tclass == H5DATATYPE_STRING)
00435 {
00436 if (strs[i]) printf("%s\t", strs[i]);
00437 }
00438 }
00439 printf("\n\n");
00440 }
00441
00442
00443 return ret_value;
00444 }
00445