I'm trying to reproduce a specific file structure to store a complex dataset in HDF5. The example I was given reads as follows:
a <- rhdf5::H5Fopen(f)
a
HDF5 FILE
name /
filename
name otype dclass dim
0 additional_keys H5I_GROUP
2 geometry H5I_GROUP
3 materials H5I_GROUP
5 tmatrix H5I_DATASET COMPOUND 30 x 30
6 uuid H5I_DATASET OPAQUE ( 0 )
7 vacuum_wavelength H5I_DATASET FLOAT ( 0 )
where the "tmatrix" dataset is the most important part. It consists of two 30x30 matrices (r and i, since HDF5 does not support complex values):
tmat <- rhdf5::h5read(f, 'tmatrix', compoundAsDataFrame = FALSE)
str(tmat)
List of 2
$ r: num [1:30, 1:30] -6.01e-05 9.17e-08 -7.26e-09 1.74e-05 1.27e-07 ...
$ i: num [1:30, 1:30] -4.27e-04 -1.15e-08 -2.75e-09 1.45e-05 7.81e-08 ...
I am unable to re-create this kind of dataset in a new file. I tried two strategies:
Creating sub-groups (fails -- I clearly don't understand what "COMPOUND" means for DATASET):
h5File <- 'test.h5'
rhdf5::h5createFile(h5File)
m <- matrix(1:9,3,3)
rhdf5::h5createDataset(h5File, 'tmatrix', dims = dim(m))
h5createGroup(h5File, 'tmatrix/r') # fails
h5write(Re(m), file=h5File, name="tmatrix")
or wrapping both matrices in a data.frame and using that method (which seems to produce COMPOUND datasets):
h5File <- 'test.h5'
rhdf5::h5createFile(h5File)
m <- matrix(1:9,3,3)
h5write(data.frame(r=I(Re(m)),i=I(Im(m))), file=h5File, name="tmatrix")
but then the matrices have been flattened to dimension 9:
HDF5 FILE
name /
filename
name otype dclass dim
0 tmatrix H5I_DATASET COMPOUND 9
Many thanks for any advice!
sessionInfo( )
R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.5
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Pacific/Auckland
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rhdf5_2.46.1
loaded via a namespace (and not attached):
[1] compiler_4.3.1 tools_4.3.1 fs_1.6.3 rstudioapi_0.15.0 rhdf5filters_1.15.1
[6] Rhdf5lib_1.24.0
PS: first post on this forum, and I had no idea why my message was rejected (all red). Posting a smaller version and then editing it revealed that a en-dash was the culprit! Why aren't UTF8 characters allowed?