C语言对HDF文件数据集和属性的读写
以三维数据读写为例打开,读取空间属性、维度,数据集属性,文件属性,数据集的数据;//写入txt文件(动态定义数组——用指针),写入DAT文件(可以用ENVI打开查看图像),写入HDF文件写入HDF文件属性、数据属性、空间属性,数据压缩。hid_t file_id,space_id,dset_id;hsize_t dims[3]={0,0,0};int ndims=0;int status;int col = 0;int row =0;int high=0;char FileName[200];char OutFileName[200];int N=0;int i,j,k; memset(FileName,0,sizeof(FileName));memset(OutFileName,0,sizeof(OutFileName));strcpy(FileName,"C:\\Users\\hanss\\Desktop\\FY3B_MERSI_GBAL_L10000_1000M_MS.HDF");strcpy(OutFileName,"C:\\Users\\hanss\\Desktop\\FY3B.HDF");unsignedint* dset=newunsignedint[row * col*high];memset(dset,0,sizeof(unsignedint)* row * col*high);打开HDF文件并读取相关数据信息1、打开HDF文件file_id = H5Fopen(FileName, H5F_ACC_RDONLY, H5P_DEFAULT);2、打开数据集dset_id = H5Dopen(file_id,"EV_1KM_RefSB",H5P_DEFAULT);3、获取空间地址space_id = H5Dget_space(dset_id);4、获取空间维度信息ndims=H5Sget_simple_extent_dims(space_id,dims,NULL);5、读取数据集status=H5LTread_dataset(file_id,"EV_1KM_RefSB",H5T_NATIVE_UINT,dset);6、读取数据属性float Slope[30];int _FillValue[1];char band_name[41];H5LTget_attribute_float(file_id,"EV_1KM_RefSB","Slope",Slope);for(int i=0;i<15;i++) printf("Slope:%f\n",Slope[i]);H5LTget_attribute_int(file_id,"EV_1KM_RefSB","_FillValue",_FillValue); printf("_FillValue:%d\n",_FillValue[0]);H5LTget_attribute_string(file_id,"EV_1KM_RefSB","band_name",band_name); printf("%s\n",band_name);或者用H5LTget_attribute_float(dset_id,"/EV_1KM_RefSB","Slope",Slope);(推荐)H5LTget_attribute_float(file_id,"/EV_1KM_RefSB","Slope",Slope);或者char dset_name[20]="EV_1KM_RefSB";H5LTget_attribute_float(file_id,dset_name,"Intercept",Intercept);【注释】如果是file_id的话,不加斜杠也是可以的,但是如果时dse_id则一定要加斜杠如果是file_id,加斜杠也可以输出数组时遍历每个元素才行,直接输出数组名,其实是输出了数组地址,需要加*,那就是输出数组第一个元素值;也可以定义指针,首先分配空间float *Intercept;Intercept=(float*)malloc(sizeof(float)*15);H5LTget_attribute_float(dset_id,"/EV_1KM_RefSB","Intercept",Intercept); printf("Intercept:\n");for(int i=0;i<15;i++)printf("%f\n",*(Intercept+i));不要把IDL语法放到C中C++中的unsigned char就是byte型,0-255值unsigned char DQ[1];H5LTget_attribute_uchar(file_id,"/",