我只是撿起HDF5,對於爲內存創建數據和爲文件創建數據之間的區別,我有點困惑。有什麼不同?HDF5複合類型本機與IEEE
在this例如,在創建複合型數據,需要在存儲器中創建並放置在文件中的數據:
/*
* Create the memory data type.
*/
s1_tid = H5Tcreate (H5T_COMPOUND, sizeof(s1_t));
H5Tinsert(s1_tid, "a_name", HOFFSET(s1_t, a), H5T_NATIVE_INT);
H5Tinsert(s1_tid, "c_name", HOFFSET(s1_t, c), H5T_NATIVE_DOUBLE);
H5Tinsert(s1_tid, "b_name", HOFFSET(s1_t, b), H5T_NATIVE_FLOAT);
/*
* Create the dataset.
*/
dataset = H5Dcreate(file, DATASETNAME, s1_tid, space, H5P_DEFAULT);
/*
* Wtite data to the dataset;
*/
status = H5Dwrite(dataset, s1_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s1);
然而,在另一示例here,筆者還創建用於一個組合數據文件,它指定了不同的數據類型。例如,在創建內存數據類型時,serial_no使用的類型爲H5T_NATIVE_INT,但是在爲該文件創建數據類型時,使用了serial_no H5T_STD_I64BE。他爲什麼這樣做?
/*
* Create the compound datatype for memory.
*/
memtype = H5Tcreate (H5T_COMPOUND, sizeof (sensor_t));
status = H5Tinsert (memtype, "Serial number",
HOFFSET (sensor_t, serial_no), H5T_NATIVE_INT);
status = H5Tinsert (memtype, "Location", HOFFSET (sensor_t, location),
strtype);
status = H5Tinsert (memtype, "Temperature (F)",
HOFFSET (sensor_t, temperature), H5T_NATIVE_DOUBLE);
status = H5Tinsert (memtype, "Pressure (inHg)",
HOFFSET (sensor_t, pressure), H5T_NATIVE_DOUBLE);
/*
* Create the compound datatype for the file. Because the standard
* types we are using for the file may have different sizes than
* the corresponding native types, we must manually calculate the
* offset of each member.
*/
filetype = H5Tcreate (H5T_COMPOUND, 8 + sizeof (hvl_t) + 8 + 8);
status = H5Tinsert (filetype, "Serial number", 0, H5T_STD_I64BE);
status = H5Tinsert (filetype, "Location", 8, strtype);
status = H5Tinsert (filetype, "Temperature (F)", 8 + sizeof (hvl_t),
H5T_IEEE_F64BE);
status = H5Tinsert (filetype, "Pressure (inHg)", 8 + sizeof (hvl_t) + 8,
H5T_IEEE_F64BE);
/*
* Create dataspace. Setting maximum size to NULL sets the maximum
* size to be the current size.
*/
space = H5Screate_simple (1, dims, NULL);
/*
* Create the dataset and write the compound data to it.
*/
dset = H5Dcreate (file, DATASET, filetype, space, H5P_DEFAULT, H5P_DEFAULT,
H5P_DEFAULT);
status = H5Dwrite (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata);
這兩種方法有什麼區別?
謝謝你,這是非常清楚的 – foboi1122 2013-03-01 23:36:42
從我的解釋:它可以與NATIVE_some_type文件類型;在回讀內容時進行映射是客戶的責任。換句話說:「當這個文件被MIPS系統使用時,它會將該數字讀作H5T_STD_I32BE,這是不期望的。」不是真的;客戶端應該讀取文件類型,並相應地創建內存佈局。客戶端應該期望字節順序不匹配,並通過適當的映射緩解它。該示例演示文件字節順序/佈局與主機(內存)字節順序/佈局解耦。 – 2014-08-22 19:16:36
@StevenVarga如果使用H5T_NATIVE_INT,你不知道它是大端還是小端,除非你在某處放置了一個標誌,告訴字節順序是什麼。即使您知道實際的字節順序,加載後轉換所有數據也不是一件容易的事情。代碼維護也很困難。何必?保存到文件時爲什麼不使用顯式字節順序? – Chen 2014-08-26 15:54:05