From 97516866c7cde5fccce3da33c4bc40193d4c747c Mon Sep 17 00:00:00 2001 From: Achim Gsell Date: Fri, 15 Apr 2016 19:01:00 +0200 Subject: [PATCH] examples/H5Block: attach_field_attribs and dump_field_attribs added --- .gitignore | 2 + examples/H5Block/Makefile.am | 4 + examples/H5Block/attach_field_attributes.c | 66 +++++++ examples/H5Block/dump_field_attributes.c | 194 +++++++++++++++++++++ 4 files changed, 266 insertions(+) create mode 100644 examples/H5Block/attach_field_attributes.c create mode 100644 examples/H5Block/dump_field_attributes.c diff --git a/.gitignore b/.gitignore index d767a03..39f18d4 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ Makefile Makefile.in aclocal.m4 +attach_field_attributes +dump_field_attributes autom4te.cache/ compile config.guess diff --git a/examples/H5Block/Makefile.am b/examples/H5Block/Makefile.am index 4291150..47f5be2 100644 --- a/examples/H5Block/Makefile.am +++ b/examples/H5Block/Makefile.am @@ -25,6 +25,8 @@ noinst_PROGRAMS = if ENABLE_C noinst_PROGRAMS += \ + attach_field_attributes \ + dump_field_attributes \ fields \ has_field \ read_write_scalar_field \ @@ -37,6 +39,8 @@ noinst_PROGRAMS += read_write_scalar_fieldf endif endif +attach_field_attributes_SOURCES = attach_field_attributes.c +dump_field_attributes_SOURCES = dump_field_attributes.c fields_SOURCES = fields.c has_field_SOURCES = has_field.c read_write_scalar_field_SOURCES = read_write_scalar_field.c diff --git a/examples/H5Block/attach_field_attributes.c b/examples/H5Block/attach_field_attributes.c new file mode 100644 index 0000000..6da8cbe --- /dev/null +++ b/examples/H5Block/attach_field_attributes.c @@ -0,0 +1,66 @@ +/* + Copyright (c) 2006-2015, The Regents of the University of California, + 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" + +// name of output file +const char* fname = "example_field.h5"; + +// H5hut verbosity level +const h5_int64_t h5_verbosity = H5_VERBOSE_DEFAULT; + +int +main ( + int argc, + char* argv[] + ){ + + // initialize MPI & H5hut + MPI_Init (&argc, &argv); + MPI_Comm comm = MPI_COMM_WORLD; + int comm_size = 1; + MPI_Comm_size (comm, &comm_size); + int comm_rank = 0; + MPI_Comm_rank (comm, &comm_rank); + H5AbortOnError (); + H5SetVerbosityLevel (h5_verbosity); + //H5SetDebugMask (-1); + + // open file and create first step + h5_file_t file = H5OpenFile (fname, H5_O_RDWR, H5_PROP_DEFAULT); + H5SetStep (file, 0); + + if (!H5BlockHasField (file, "data")) { + printf ("Doesn't have field data with name 'data' in step#0\n"); + goto done; + } + + h5_int32_t attrib[1] = { 42 }; + H5BlockWriteFieldAttribInt32 ( + file, + "data", + "The answer", + attrib, + sizeof (attrib) / sizeof (*attrib)); + h5_float64_t origin[3] = { 0.0, 0.0, 1.0 }; + H5Block3dSetFieldOrigin ( + file, + "data", + origin[0], origin[1], origin[2]); + h5_float64_t spacing[3] = { 1.0, 2.0, 3.0 }; + H5Block3dSetFieldSpacing ( + file, + "data", + spacing[0], spacing[1], spacing[2]); +done: + // done + H5CloseFile(file); + MPI_Finalize (); + return 0; +} diff --git a/examples/H5Block/dump_field_attributes.c b/examples/H5Block/dump_field_attributes.c new file mode 100644 index 0000000..79af791 --- /dev/null +++ b/examples/H5Block/dump_field_attributes.c @@ -0,0 +1,194 @@ +/* + Copyright (c) 2006-2015, The Regents of the University of California, + 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" + +// name of output file +const char* fname = "example_field.h5"; + +// H5hut verbosity level +const h5_int64_t h5_verbosity = H5_VERBOSE_DEFAULT; + +static inline void +dump_int64_attrib ( + h5_file_t file, + const char* const field_name, + const char* const attrib_name, + h5_size_t attrib_nelems + ) { + h5_int64_t attrib_data[attrib_nelems]; + H5BlockReadFieldAttribInt64 ( + file, + field_name, + attrib_name, + attrib_data); + printf ("Attribute: '%s'\n", attrib_name); + printf (" Type: H5_INT64_T\n"); + printf (" Data: %lld", attrib_data[0]); + for (size_t i = 1; i < attrib_nelems; i++) { + printf (", %lld", attrib_data[i]); + } + printf ("\n"); +} + +static inline void +dump_int32_attrib ( + h5_file_t file, + const char* const field_name, + const char* const attrib_name, + h5_size_t attrib_nelems + ) { + h5_int32_t attrib_data[attrib_nelems]; + H5BlockReadFieldAttribInt32 ( + file, + field_name, + attrib_name, + attrib_data); + printf ("Attribute: '%s'\n", attrib_name); + printf (" Type: H5_INT32_T\n"); + printf (" Data: %ld", (long)attrib_data[0]); + for (size_t i = 1; i < attrib_nelems; i++) { + printf (", %ld", (long)attrib_data[i]); + } + printf ("\n"); +} + +static inline void +dump_float64_attrib ( + h5_file_t file, + const char* const field_name, + const char* const attrib_name, + h5_size_t attrib_nelems + ) { + h5_float64_t attrib_data[attrib_nelems]; + H5BlockReadFieldAttribFloat64 ( + file, + field_name, + attrib_name, + attrib_data); + printf ("Attribute: '%s'\n", attrib_name); + printf (" Type: H5_FLOAT64_T\n"); + printf (" Data: %2f", attrib_data[0]); + for (size_t i = 1; i < attrib_nelems; i++) { + printf (", %2f", attrib_data[i]); + } + printf ("\n"); +} + +static inline void +dump_float32_attrib ( + h5_file_t file, + const char* const field_name, + const char* const attrib_name, + h5_size_t attrib_nelems + ) { + h5_float32_t attrib_data[attrib_nelems]; + H5BlockReadFieldAttribFloat32 ( + file, + field_name, + attrib_name, + attrib_data); + printf ("Attribute: '%s'\n", attrib_name); + printf (" Type: H5_FLOAT32_T\n"); + printf (" Data: %2f", attrib_data[0]); + for (size_t i = 1; i < attrib_nelems; i++) { + printf (", %2f", attrib_data[i]); + } + printf ("\n"); +} + +inline void +dump_string_attrib ( + h5_file_t file, + const char* const field_name, + const char* const attrib_name, + h5_size_t attrib_nelems + ) { +} + +void +dump_attrib ( + h5_file_t file, + const char* const field_name, + const char* const attrib_name, + h5_int64_t attrib_type, + h5_size_t attrib_nelems + ) { + if (attrib_type == H5_INT64_T) { + dump_int64_attrib (file, field_name, attrib_name, attrib_nelems); + } else if (attrib_type == H5_INT32_T) { + dump_int32_attrib (file, field_name, attrib_name, attrib_nelems); + } else if (attrib_type == H5_FLOAT64_T) { + dump_float64_attrib (file, field_name, attrib_name, attrib_nelems); + } else if (attrib_type == H5_FLOAT32_T) { + dump_float32_attrib (file, field_name, attrib_name, attrib_nelems); + } else if (attrib_type == H5_STRING_T) { + dump_string_attrib (file, field_name, attrib_name, attrib_nelems); + } +} + +int +main ( + int argc, + char* argv[] + ){ + + // initialize MPI & H5hut + MPI_Init (&argc, &argv); + MPI_Comm comm = MPI_COMM_WORLD; + int comm_size = 1; + MPI_Comm_size (comm, &comm_size); + int comm_rank = 0; + MPI_Comm_rank (comm, &comm_rank); + H5AbortOnError (); + H5SetVerbosityLevel (h5_verbosity); + //H5SetDebugMask (-1); + + // open file and create first step + h5_file_t file = H5OpenFile (fname, H5_O_RDWR, H5_PROP_DEFAULT); + H5SetStep (file, 0); + + // test wheter field exists + const char field_name[] = "data"; + if (!H5BlockHasField (file, field_name)) { + printf ("Doesn't have field data with name 'data' in step#0\n"); + goto done; + } + + // get number of attributes attached to field + h5_ssize_t n_attribs = H5BlockGetNumFieldAttribs ( + file, + field_name); + printf ("Field has %lld attributes attached.\n", + n_attribs); + + // dump all attached attributes + for (h5_size_t i = 0; i < n_attribs; i++) { + char attrib_name[128]; + h5_size_t sizeof_attrib_name = sizeof (attrib_name); + h5_int64_t attrib_type; + h5_size_t attrib_nelems; + H5BlockGetFieldAttribInfo ( + file, + field_name, + i, + attrib_name, sizeof_attrib_name, + &attrib_type, + &attrib_nelems); + + dump_attrib (file, + field_name, + attrib_name, attrib_type, attrib_nelems); + } +done: + // done + H5CloseFile(file); + MPI_Finalize (); + return 0; +}