很多的遥感数据源(特别是NASA家的)都是用hdf格式承载的,比如著名的modis啊viirs,用Python也可以直接处理,从此和ENVI说拜拜!
Requestpython2.7(貌似这个pyhdf包在Python3下不能用)
conda环境
然后conda安装pyhdf(一定要按照下面的代码安装,直接conda install会装不上)
conda install -c conda-forge pyhdf
使用
下面以viirs文件为例说明其使用方法
读取hdf数据
from pyhdf.SD import SD, SDC
file_name = 'VNP09_NRT.A2018347.0324.001.hdf'
file = SD(file_name, SDC.READ) #加载数据
print file.info()
会返回(19,55), 19代表着VIIRS包含的19个波段的数据集。
获得每个波段的名称
datasets_dic = file.datasets()
for idx,sds in enumerate(datasets_dic.keys()):
print idx,sds
会得到:
0 QF2 Surface Reflectance
1 QF1 Surface Reflectance
2 375m Surface Reflectance Band I3
3 375m Surface Reflectance Band I2
4 375m Surface Reflectance Band I1
5 QF3 Surface Reflectance
6 QF7 Surface Reflectance
7 QF5 Surface Reflectance
8 750m Surface Reflectance Band M10
9 750m Surface Reflectance Band M11
10 QF6 Surface Reflectance
11 750m Surface Reflectance Band M8
12 QF4 Surface Reflectance
13 750m Surface Reflectance Band M7
14 750m Surface Reflectance Band M4
15 750m Surface Reflectance Band M5
16 750m Surface Reflectance Band M2
17 750m Surface Reflectance Band M3
18 750m Surface Reflectance Band M1
获得各个波段储存的数据:
sds_obj = file.select('750m Surface Reflectance Band M1') # select sds
data = sds_obj.get() # get sds data
print data
可获得
[[-28672 -28672 -28672 ..., -28672 -28672 -28672]
[-28672 -28672 -28672 ..., -28672 -28672 -28672]
[ 3664 3701 3710 ..., 10797 10804 10894]
...,
[ 6044 6913 6981 ..., 8519 8532 8562]
[-28672 -28672 -28672 ..., -28672 -28672 -28672]
[-28672 -28672 -28672 ..., -28672 -28672 -28672]]