Reputation: 817
I try to mount different files that all have the same structure to a single new one (in C++). Therefore I create a new File "new.h5" and in there a new Group "/G". The existing two files ("file1.h5" and "file2.h5") have some Attributes in the root-Group "/" and then a path "dataset1/data1/data", where "dataset1/data1" contains some Attributes and the "data"-Path with numerical Data in it.
/dataset1
/attribute1
/attribute2
/data1
/data
/attribute1
/attribute2
In the new file I want a structure:
/G
/dataset1
/attribute1
/attribute2
/data1
/data
/attribute1
/attribute2
/dataset2
/attribute1
/attribute2
/data1
/data
/attribute1
/attribute2
I tried it this way:
#include "hdf5.h"
#ifdef OLD_HEADER_FILENAME
#include <iostream.h>
#else
#include <iostream>
#endif
#include <string>
#ifndef H5_NO_NAMESPACE
#ifndef H5_NO_STD
using std::cout;
using std::endl;
#endif // H5_NO_STD
#endif
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
#define FILE_NAME_1 "file1.h5"
#define FILE_NAME_2 "file2.h5"
#define FILE_NAME_ALL "new.h5"
int main(void)
{
hid_t fid1, fid2, fid3, gid; /* Files and group identifiers */
fid1 = H5Fopen(FILE_NAME_1, H5F_ACC_RDWR, H5P_DEFAULT);
fid2 = H5Fopen(FILE_NAME_2, H5F_ACC_RDWR, H5P_DEFAULT);
fid3 = H5Fcreate(FILE_NAME_ALL, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
gid = H5Gcreate2(fid3, "/G", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Gclose(gid);
/*
* Mount second file under G in the first file.
*/
H5Fmount(fid3, "/G", fid1, H5P_DEFAULT);
H5Fmount(fid3, "/G", fid2, H5P_DEFAULT);
H5Fclose(fid1);
H5Fclose(fid2);
H5Fclose(fid3);
return 0;
}
When I take a look to new.h5 with h5dump
I get:
h5dump new.h5
HDF5 "new.h5" {
GROUP "/" {
GROUP "G" {
}
}
}
so I created the Group G but there are no files in it?! What went wrong?
And is there a possibility to rename the "dataset1" from the second file to "dataset2" while mounting the files or do I have to do it before?
Thank you!
EDIT:
I might have found a problem: I want to mount files from the root-group. What I mean: I want to have a common file that contains everything from the other two files. That makes it difficult to find a mounting point. It can't be the root-directory but there are no groups called "/G" in the existing files. How can I solve this?
Upvotes: 1
Views: 558
Reputation: 2280
This is the correct behavior. Using H5Fmount
only mounts the file at that location in memory. So when you close the files and exit your program the newfile (new.h5
) does not (and will not) show the contents of the mounted files.
If you want this kind of behavior, use H5Lcreate_external
to create external links.
I've changed your code to be C (not C++ - sorry, but the idea is the same):
#include <stdlib.h>
#include <stdio.h>
#include <hdf5.h>
#define FILE_NAME_1 "file1.h5"
#define FILE_NAME_2 "file2.h5"
#define FILE_NAME_ALL "new.h5"
int main(void)
{
hid_t fid1; /* Files identifier */
fid1 = H5Fcreate(FILE_NAME_ALL, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/*
* Link "file1.h5" to group F1
*/
H5Lcreate_external(FILE_NAME_1, "/", fid1, "/F1", H5P_DEFAULT, H5P_DEFAULT);
/*
* Link "file2.h5" to group F2
*/
H5Lcreate_external(FILE_NAME_2, "/", fid1, "/F2", H5P_DEFAULT, H5P_DEFAULT);
H5Fclose(fid1);
return 0;
}
Where the contents of file1.h5
and file2.h5
are the same for the test case:
h5dump file1.h5
HDF5 "file1.h5" {
GROUP "/" {
DATASET "D" {
DATATYPE H5T_STD_I32LE
DATASPACE SIMPLE { ( 4, 5 ) / ( 4, 5 ) }
DATA {
(0,0): 0, 1, 2, 3, 4,
(1,0): 1, 2, 3, 4, 5,
(2,0): 2, 3, 4, 5, 6,
(3,0): 3, 4, 5, 6, 7
}
}
}
}
Then if we compile and run the test program:
h5cc -o test test.c && ./test
Finally dump the new file:
h5dump new.h5
HDF5 "new.h5" {
GROUP "/" {
EXTERNAL_LINK "F1" {
TARGETFILE "file1.h5"
TARGETPATH "/"
GROUP "/" {
DATASET "D" {
DATATYPE H5T_STD_I32LE
DATASPACE SIMPLE { ( 4, 5 ) / ( 4, 5 ) }
DATA {
(0,0): 0, 1, 2, 3, 4,
(1,0): 1, 2, 3, 4, 5,
(2,0): 2, 3, 4, 5, 6,
(3,0): 3, 4, 5, 6, 7
}
}
}
}
EXTERNAL_LINK "F2" {
TARGETFILE "file2.h5"
TARGETPATH "/"
GROUP "/" {
DATASET "D" {
DATATYPE H5T_STD_I32LE
DATASPACE SIMPLE { ( 4, 5 ) / ( 4, 5 ) }
DATA {
(0,0): 0, 1, 2, 3, 4,
(1,0): 1, 2, 3, 4, 5,
(2,0): 2, 3, 4, 5, 6,
(3,0): 3, 4, 5, 6, 7
}
}
}
}
}
}
Upvotes: 2