在遥感应用中,会遇到一些HDF4格式的影像数据,HDF是一种自描述性的文件,不像之前介绍的SSMI / SSMIS数据和QuikScat数据那样,需要一个专门的数据描述文档,然后我们通过文档获取相关参数去读取文件。HDF的好处是完全跨平台,只要给定一个完整的HDF文件,我们就可以根据相关的规则去读取而不需要任何文档。关于HDF的文件的详细介绍,你可以看这篇文章:关于HDF文件的一点概述(HDF4,HDF5)
一些前提
言归正传,下面介绍一个完整的读取HDF文件的过程,本文中所用到的HDF文件,你可以从这里下载。链接:http://pan.baidu.com/s/1gdxhBv9 密码:hmcn
所有代码
;Author:Sailor
;2014-01-15
PRO Course_11
;定义文件路径
MyRootDir='D:\8\'
;遍历文件夹
filearr = file_search(MyRootDir+'day','*-sst.hdf',count=num);
FOR fileindex=0,num-1,1 DO BEGIN
;打开SDS模式的HDF文件&返回文件信息
hdfid=hdf_sd_start(filearr[fileindex])
hdf_sd_fileinfo,hdfid,datasets,attributes
;遍历每个属性
FOR attrindex=0,attributes-1 DO BEGIN
hdf_sd_attrinfo,hdfid,attrindex,name=a,data=b,count=c,hdf_type=d,type=e
print,'Name',':',a
print,'Data',':',b
print,'Count',':',c
print,'Hdf_type',':',d
print,'Type',':',e
print,'+++++++++++++++++++++++++++++++++++++'
ENDFOR
;遍历每个数据集
FOR dsindex=0,datasets-1 DO BEGIN
;读取数据集数据
varid=hdf_sd_select(hdfid,dsindex)
hdf_sd_getdata,varid,Data
hdf_sd_getinfo,varid,name=dstName,natts=dstNum
;如果数据集不为0
IF(dstNum NE 0) THEN BEGIN
print,'Name',':',dstName
help,Data
print,'Num',':',dstNum
FOR ind2=0,dstNum-1 DO BEGIN
hdf_sd_attrinfo,varid,ind2,name=disName,data=disData
print,disName
print,disData
ENDFOR
print,'******************************'
hdf_sd_endaccess,varid
ENDIF
ENDFOR
;显示数据
;读取sst数据集
ind=hdf_sd_nametoindex(hdfid,'sst');得到sst数据集的索引号
var_id=hdf_sd_select(hdfid,ind)
hdf_sd_getdata,var_id,sst
sst=congrid(sst,1440,720)
;对sst数组进行处理
sst=sst*0.075-3
sst=reverse(sst,2)
sst=bytscl(sst,min=-3,max=33,top=255)
Img=image(sst,rgb_table=39,title=filearr(fileindex),grid_units=1,POSITION=[0.1,0,0.85,1])
xaxis=axis('X',location=[0,0],range=[-180,180],minor=0, major=19,coord_transform=[-180,0.25],title='Longitude(°)')
yaxis=axis('Y',location=[0,0],range=[-90,90],minor=0, major=7,coord_transform=[-90,0.25],title='Latitude(°)')
;COLORBAR
c = COLORBAR(TARGET=Img, ORIENTATION=1,TITLE='SST(Deg)')
ENDFOR
END
效果图
代码解释
MyRootDir='D:\8\'
这句代码规定了HDF的数据路径,当然你要换成你自己的路径,测试数据可以从这里下载。
filearr = file_search(MyRootDir+'day','*-sst.hdf',count=num);
FOR fileindex=0,num-num,1 DO BEGIN
这句代码段是对上面的路径做一个遍历,找出所有的.hdf数据,之所以这么写是为了方便批处理,因为不可能只处理一个文件。不过如果仅仅是做测试,这里可以改为num-num只处理一个文件测试效果,效果满意再做批处理。关于file_search函数的详细使用,你可以看这篇文章:IDL使用file_search函数遍历某个文件夹下的所有文件
;打开SDS模式的HDF文件&返回文件信息
hdfid=hdf_sd_start(filearr[fileindex])
hdf_sd_fileinfo,hdfid,datasets,attributes
这段代码是读取某一个具体的hdf文件,并且获取这个文件的ID,数据集个数 datasets 和属性个数 attributes。
;遍历每个属性
FOR attrindex=0,attributes-1 DO BEGIN
hdf_sd_attrinfo,hdfid,attrindex,name=a,data=b,count=c,hdf_type=d,type=e
print,'Name',':',a
print,'Data',':',b
print,'Count',':',c
print,'Hdf_type',':',d
print,'Type',':',e
print,'+++++++++++++++++++++++++++++++++++++'
ENDFOR
这段代码遍历了文件的全局属性,并输出。
;遍历每个数据集
FOR dsindex=0,datasets-1 DO BEGIN
;读取数据集数据
varid=hdf_sd_select(hdfid,dsindex)
hdf_sd_getdata,varid,Data
hdf_sd_getinfo,varid,name=dstName,natts=dstNum
;如果数据集不为0
IF(dstNum NE 0) THEN BEGIN
print,'Name',':',dstName
help,Data
print,'Num',':',dstNum
FOR ind2=0,dstNum-1 DO BEGIN
hdf_sd_attrinfo,varid,ind2,name=disName,data=disData
print,disName
print,disData
ENDFOR
print,'******************************'
hdf_sd_endaccess,varid
ENDIF
ENDFOR
这段代码遍历了全局的数据集,并且对每个数据集的属性做了一个遍历。
;读取sst数据集
ind=hdf_sd_nametoindex(hdfid,'sst');得到sst数据集的索引号
var_id=hdf_sd_select(hdfid,ind)
hdf_sd_getdata,var_id,sst
sst=congrid(sst,1440,720)
这里通过上面两步遍历,找出我们要找的数据集sst,这里读取sst数据集,并且根据数据集信息做了一个采样。
;对sst数组进行处理
sst=sst*0.075-3
sst=reverse(sst,2)
sst=bytscl(sst,min=-3,max=33,top=255)
Img=image(sst,rgb_table=39,title=filearr(fileindex),grid_units=1,POSITION=[0.1,0,0.85,1])
xaxis=axis('X',location=[0,0],range=[-180,180],minor=0, major=19,coord_transform=[-180,0.25],title='Longitude(°)')
yaxis=axis('Y',location=[0,0],range=[-90,90],minor=0, major=7,coord_transform=[-90,0.25],title='Latitude(°)')
;COLORBAR
c = COLORBAR(TARGET=Img, ORIENTATION=1,TITLE='SST(Deg)')
最后对整个数据集做了一个处理,这里的sst=sst*0.075-3 也是通过之前的属性遍历得到的,如图:
这就是整个HDF4文件的读取过程,如果有什么问题,欢迎留言。