Skip to content

Instantly share code, notes, and snippets.

@blippy
Last active November 15, 2015 21:52
Show Gist options
  • Save blippy/82c23d1d0cdb932a4c29 to your computer and use it in GitHub Desktop.
Save blippy/82c23d1d0cdb932a4c29 to your computer and use it in GitHub Desktop.
Using C to print out an HDF5 file of float64's
begin 764 foo.h5
MB4A$1@T*&@H```````@(``0`$`````````````````#__________YX5````
M````__________\``````````&```````````0````````"(`````````*@"
M`````````0`!``$````8`````````!$`$```````B`````````"H`@``````
M`%12144```$`_____________________P``````````5A0````````(````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M``````!(14%0`````%@`````````$`````````#(`@``````````````````
M4$52```````!`````````$@`````````````````````````````````````
M``````````````````````````````````````````````!44D5%`0`!`/__
M__________________]."`````````````````````````````!0"P``````
M````````````JP(````````(````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M`````````````````````'A>59A]B%1E%,:W((S%R,VM=4W7W?F\\WWOS)W/
MW5GOS)J;FMGV)=)JF2""%$9%906;MD@2+($1%D1B251(M!0B2!>CC"C""!9A
M"98H(BN$6(D@:/8^O_=&\\\P<]_W/><\YSG/.>^]^\S`].57&EZS>^[D@7,E
M;^%@[^GQW07OL:4_KB3Y'?%*2Y\_A_5[J.FE)F?.+[Y;\CJ[FMU]3>^WB<Y!
M&=L[/=Y9D<]Z>Y<^5_=[7<$GZGVV:$W.M&V^&W[P?:BD]:?KWI+YYL`+7N=4
M:W);4>=;>6_)O8FG;]?Z\VGY=<#R7C_>^;ST('XGM*X6]X)MDV7O]L#1O-;;
M^'7:#?;M6/V1]\W7G<\=#>T[D])YQVW_ZI*9Q>4A#F]WPIO;T];Z#S9XSW;^
M7CC;\)9V#4SG%?<#]WO!OB>SX&)I7S2'OROQ/Z[]!]=ZZX(#5OD!KK.V\$K4
M%$^S0!QIUN?T?*^+W9SLON^`MRW[Y1%P`)_^]5JWWY*]-\?EU\EUBF\AQ?,X
M?J?Q8UAV&V-^8.\ZV[L[.'B+;_PQN,DO2WD]WZ=S]KG*^U1%>>W.B">OC9*O
M@N*XKDA>HJRWB<^!9U7Q[O$J_D>)R]%ZSR9O1?%F)B_[7[:4_PL1K>LRO(O`
MBPSYM>37=K.NP7==^?[:4=Q/O4@<4=DY?$#G'7+`+8E_>?E;VDH>T_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EWNNDPSD[H/\I>'MVF#F4/MN9
MI]5OJ8>A-GI`77SWA/K]RD=TCSSETF^'I/.[S#R1T_.-8]CG?4QG/E.\272(
M/C9CTW=3\)AY>='$%U'>WMI$7+?YPB<+_YAG:^C7+O->:AA_XM3_(/L2\F<W
M>.P=\?7;S)'<K];0KR>?@0=-ZK;F_0L2V</Q`0`&``$```"H``````````$`
M&````````0$!``````"K`@```````*L"`````````P`8``$````1(#\`"```
M````0``T"P`T_P,````````%``@``0````(#``$`````"P`@``$````!`0``
M``````$`"``!``$`9&5F;&%T90`$``````````@`&``!`````P("(`,`````
M``"K`@``"```````````$@`(```````!````8&E&5E-.3T0!``$`"```````
M``">$P``````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
L````````````````````````````````````````````````````````````
`
end
/*
Example of reading data in HDF
Compile using:
gcc h5print.c -lhdf5
Dataset is available in UUENCODED format at:
https://gist.github.com/blippy/82c23d1d0cdb932a4c29
*/
#include "hdf5.h"
#include <stdio.h>
#include <stdlib.h>
#define FILE "foo.h5"
#define DATASET "PER"
#define DIM0 32
#define DIM1 64
#define CHUNK0 4
#define CHUNK1 8
int
main (void)
{
hid_t file, space, dset, dcpl; /* Handles */
herr_t status;
htri_t avail;
H5Z_filter_t filter_type;
hsize_t dims[2] = {DIM0, DIM1},
chunk[2] = {CHUNK0, CHUNK1};
size_t nelmts;
unsigned int flags,
filter_info;
int i, j;
float rdata[DIM0][DIM1];
/*
* Open file and dataset using the default properties.
*/
file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
dset = H5Dopen (file, DATASET, H5P_DEFAULT);
/*
* Retrieve dataset creation property list.
*/
dcpl = H5Dget_create_plist (dset);
space = H5Dget_space(dset);
printf("Sizeof float is %d\n", sizeof(float));
nelmts = H5Sget_simple_extent_npoints(space);
printf("Number of elements: %ld\n", nelmts);
/*
* Read the data using the default properties.
*/
status = H5Dread (dset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, // me added FLOAT
rdata[0]);
// print the data -- not very elegent
for (i=0; i<DIM0; i++)
for (j=0; j<DIM1; j++)
{
printf("%f\n", rdata[i][j]);
if (--nelmts == 0) {goto bored;}
}
bored:
status = H5Pclose (dcpl);
status = H5Dclose (dset);
status = H5Fclose (file);
return 0;
}
@blippy
Copy link
Author

blippy commented Nov 15, 2015

The sample dataset is in UU-ENCODED form, and needs to be decoded first before use.

@blippy
Copy link
Author

blippy commented Nov 15, 2015

I think I may have to
status = H5Sclose (space);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment