read_file_attribs.c 2.77 KB
Newer Older
1
/*
gsell's avatar
gsell committed
2
  Copyright (c) 2006-2015, The Regents of the University of California,
3 4 5 6 7 8 9 10 11 12
  through Lawrence Berkeley National Laboratory (subject to receipt of any
  required approvals from the U.S. Dept. of Energy) and the Paul Scherrer
  Institut (Switzerland).  All rights reserved.

  License: see file COPYING in top level of source distribution.
*/

#include "H5hut.h"

#define FNAME           "example_file_attribs.h5"
gsell's avatar
gsell committed
13
#define VERBOSITY       H5_VERBOSE_ERROR
14 15 16 17 18 19 20 21 22 23 24 25 26

#define ATTR_STRING     "FileAttrString"
#define ATTR_INT32      "FileAttrInt32"
#define ATTR_INT64      "FileAttrInt64"
#define ATTR_FLOAT32    "FileAttrFloat32"
#define ATTR_FLOAT64    "FileAttrFloat64"

int
main (
	int argc,
	char** argv
	) {
        H5AbortOnError ();
gsell's avatar
gsell committed
27 28 29 30
        H5SetVerbosityLevel (VERBOSITY);

	MPI_Init (&argc, &argv);

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        // if file properties is set to default, MPI_COMM_WORLD will be used
        h5_file_t f = H5OpenFile (FNAME, H5_O_RDONLY, H5_PROP_DEFAULT);
        h5_size_t len;

        H5GetFileAttribInfoByName (f, ATTR_STRING, NULL, &len);
        char* attr_string = malloc (len+1);
        H5ReadFileAttribString (f, ATTR_STRING, attr_string);
        printf ("%s: %s\n", ATTR_STRING, attr_string);
        free (attr_string);

        H5GetFileAttribInfoByName (f, ATTR_INT32, NULL, &len);
        int32_t* attr_int32 = malloc (sizeof(*attr_int32)*len);
        H5ReadFileAttribInt32 (f, ATTR_INT32, attr_int32);
        printf ("%s:", ATTR_INT32);
        for (int i = 0; i < len; i++) {
                printf (" %d", attr_int32[i]);
        }
        printf ("\n");
        free (attr_int32);

        H5GetFileAttribInfoByName (f, ATTR_INT64, NULL, &len);
        int64_t* attr_int64 = malloc (sizeof(*attr_int64)*len);
        H5ReadFileAttribInt64 (f, ATTR_INT64, attr_int64);
        printf ("%s:", ATTR_INT64);
        for (int i = 0; i < len; i++) {
                printf (" %lld", (long long int)attr_int64[i]);
        }
        printf ("\n");
        free (attr_int64);

        H5GetFileAttribInfoByName (f, ATTR_FLOAT32, NULL, &len);
        h5_float32_t* attr_float32 = malloc (sizeof(*attr_float32)*len);
        H5ReadFileAttribFloat32 (f, ATTR_FLOAT32, attr_float32);
        printf ("%s:", ATTR_FLOAT32);
        for (int i = 0; i < len; i++) {
                printf (" %f", attr_float32[i]);
        }
        printf ("\n");
        free (attr_float32);

        H5GetFileAttribInfoByName (f, ATTR_FLOAT64, NULL, &len);
        h5_float64_t* attr_float64 = malloc (sizeof(*attr_float64)*len);
        H5ReadFileAttribFloat64 (f, ATTR_FLOAT64, attr_float64);
        printf ("%s:", ATTR_FLOAT64);
        for (int i = 0; i < len; i++) {
                printf (" %f", attr_float64[i]);
        }
        printf ("\n");
        free (attr_float64);

gsell's avatar
gsell committed
81
        // cleanup
82 83 84 85
	H5CloseFile (f);
	MPI_Finalize ();
	return 0;
}