无法使用pyhdf读取HDF4数据集(pyhdf.error.HDF4Error:select:不存在的数据集) [英] Unable to read HDF4 dataset using pyhdf (pyhdf.error.HDF4Error: select: non-existent dataset )

查看:1352
本文介绍了无法使用pyhdf读取HDF4数据集(pyhdf.error.HDF4Error:select:不存在的数据集)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取HDF4文件(

I am trying to read a HDF4 file (https://www.dropbox.com/s/5d40ukfsu0yupwl/MOD13A2.A2016001.h23v05.006.2016029070140.hdf?dl=0).

import os
import numpy as np
from pyhdf.SD import SD, SDC

# Open file.
FILE_NAME = 'MOD13A2.A2016001.h23v05.006.2016029070140.hdf'
hdf = SD(FILE_NAME, SDC.READ)

# List available SDS datasets.
print (hdf.datasets())

# Read dataset.
DATAFIELD_NAME="1_km_16_days_NDVI"
data2D = hdf.select(DATAFIELD_NAME)
data = data2D[:,:] 

执行此脚本时,出现以下错误: 追溯(最近一次通话): 在第15行的文件"Test.py"中 data2D = hdf.select(DATAFIELD_NAME) 选择文件"C:\ Python35 \ lib \ site-packages \ pyhdf \ SD.py",行1599 引发HDF4Error(选择:不存在的数据集") pyhdf.error.HDF4Error:选择:不存在的数据集

When I executed this script, I am getting following error: Traceback (most recent call last): File "Test.py", line 15, in data2D = hdf.select(DATAFIELD_NAME) File "C:\Python35\lib\site-packages\pyhdf\SD.py", line 1599, in select raise HDF4Error("select: non-existent dataset") pyhdf.error.HDF4Error: select: non-existent dataset

我已经使用类似的python代码读取其他HDF4文件,并且效果很好.但是在这种情况下,我无法理解问题.

I have used similar python code for reading other HDF4 files and it works well. But I am unable to understand problem in this case.

推荐答案

如果仔细查看print(hdf.datasets())的输出,您会注意到 以下行:

If you look carefully at the output of print(hdf.datasets()) you would notice the following line:

 ...
 '1 km 16 days NDVI': (('YDim:MODIS_Grid_16DAY_1km_VI',
                        'XDim:MODIS_Grid_16DAY_1km_VI'),
                       (1200, 1200),
                       22,
                       0),
 ...

键是数据集的名称.请注意,该名称使用空格分隔单词,而不是示例中的下划线.

The key is the name of the dataset. Note that the name uses spaces to separate words, not the underscores as in your example.

如果将DATAFIELD_NAME替换为DATAFIELD_NAME='1 km 16 days NDVI'.那就是用数据集名称中的空格替换下划线,您的代码将正常工作而不会出现错误.

If you replace the DATAFIELD_NAME with DATAFIELD_NAME='1 km 16 days NDVI'. That is replace underscores with spaces in the dataset name, your code would work without the error.

这足以访问图块中的数据,但是地理定位需要更多的工作.目前,您已经可以使用来可视化数据

This is enough to access the data within the tile, but geolocation requires more work. At the moment you already can visualize the data with

from matplotlib import pyplot as plt
plt.imshow(data)
plt.colorbar()
plt.show()

剩下要做的是

  • 扩展到科学单位
  • 地理位置

顺便说一句,请使用pprint模块输出结构复杂的大型词典,即将print(hdf.datasets())替换为

As an aside, do use the pprint module to output large dictionaries with complex structure, i.e. replace print(hdf.datasets()) with

import pprint
...
pprint.pprint(hdf.datasets())

这篇关于无法使用pyhdf读取HDF4数据集(pyhdf.error.HDF4Error:select:不存在的数据集)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆