HDF文件包括科学数据和VData部分。读取HDF格式的calipso数据,用GDAL可以方便的读取其中的子数据集,但是没有发现GDAL中提供读取Vdata的方法。所以只好考虑借助hdf-4.2.6类库本身。毕竟GDAL也是采用的hdf-4.2.6类库进行了一次封装。
首先从HdfGroup网站下载一个编译好的安装包。解压后目录如下:
新建一个Win32控制台项目。
添加包含和引用的lib,注意安装目录文件夹下的lib文件夹中的.lib文件应该是relese版本的,我这里引用了dll文件夹下的.lib文件。
从Hdf4.2.6源码中找到VD_create_onefield_vdatas.c文件。我这里修改为VD_create_onefield_vdatas.cpp,添加到项目中,把项目中的HdfFirst.cpp直接排除掉,因为里面有个Main函数。
同时在VD_create_onefield_vdatas.cpp添加
#include "stdafx.h"
完整代码:
1 #include "stdafx.h"
2 #include "hdf.h"
3
4 #define FILE_NAME "General_Vdatas.hdf"
5 #define CLASS1_NAME "5x1 Array"
6 #define CLASS2_NAME "6x4 Array"
7 #define VDATA1_NAME "First Vdata"
8 #define VDATA2_NAME "Second Vdata"
9 #define FIELD1_NAME "Single-component Field"
10 #define FIELD2_NAME "Multi-component Field"
11 #define N_RECORDS_1 5 /* number of records the first vdata contains */
12 #define N_RECORDS_2 6 /* number of records the second vdata contains */
13 #define ORDER_2 4 /* order of the field in the second vdata */
14 /* Note that the order of the field in the first vdata is 1 */
15
16 int main( ) 17 { 18 /************************* Variable declaration **************************/
19
20 intn status_n; /* returned status for functions returning an intn */
21 int32 status_32; /* returned status for functions returning an int32 */
22 int32 file_id, vdata1_ref, vdata2_ref; 23
24 /*
25 * Define an array to buffer the data of the first vdata. 26 */
27 char8 vdata1_buf [N_RECORDS_1] = {'V', 'D', 'A', 'T', 'A'}; 28
29 /*
30 * Define an array to buffer the data of the second vdata. 31 */
32 int32 vdata2_buf [N_RECORDS_2][ORDER_2] = {{1, 2, 3, 4}, {2, 4, 6, 8}, 33 {3, 6, 9, 12}, {4, 8, 12, 16}, 34 {5, 10, 15, 20}, {6, 12, 18, 24}}; 35
36 /********************** End of variable declaration **********************/
37
38 /*
39 * Open the HDF file for writing. 40 */
41 file_id = Hopen (FILE_NAME, DFACC_WRITE, 0); 42
43 /*
44 * Initialize the VS interface. 45 */
46 status_n = Vstart (file_id); 47
48 /*
49 * Create the first vdata and populate it with data from the vdata1_buf 50 * array. Note that the buffer vdata1_buf is cast to (uint8 *) for the 51 * benefit of generic data type. 52 */
53 vdata1_ref = VHstoredata (file_id, FIELD1_NAME, (uint8 *)vdata1_buf, 54 N_RECORDS_1, DFNT_CHAR8, VDATA1_NAME, CLASS1_NAME); 55
56 /*
57 * Create the second vdata and populate it with data from the vdata2_buf 58 * array. 59 */
60 vdata2_ref = VHstoredatam (file_id, FIELD2_NAME, (uint8 *)vdata2_buf, 61 N_RECORDS_2, DFNT_INT32, VDATA2_NAME, CLASS2_NAME, ORDER_2); 62
63 /*
64 * Terminate access to the VS interface and close the HDF file. 65 */
66 status_n = Vend (file_id); 67 status_32 = Hclose (file_id); 68 return 0; 69 }
将Dll文件拷贝到Debug目录下,运行,发现目录下生成了General_Vdatas.hdf文件。在Matlab中查看: