python读取hdf4文件

记录一下使用xarray读取hdf4(not hdf5)过程中遇到的问题.

  • 目的: 读取hdf4 file的matadata
  • 遇到的问题:使用xarray.open_dataset()失败
  • 解决方法:使用pyhdf.SD代替
import os
from pyhdf.SD import SD, SDC
import xarray as xr
import numpy as np
import pandas as pd
import datetime as dt
import hvplot.xarray 
import scipy
import pprint
import warnings
warnings.filterwarnings('ignore')


# 输入文件
file ='xxx.hdf' (ps:这里是hdf文件,not hdf5,没有试过hdf5是否成功)


# --------方法1: 失败-------
# --------方法1: 失败-------
ds       = xr.open_dataset(file,engine='pynio')
metadata = ds.attrs[attr_name]//e.g., attr_name = 'StructMetadata.0'

主要的原因:
for reading GRIB and other geoscience specific file formats. 
Note that PyNIO is not available for Windows and 
that the PyNIO backend may be moved outside of xarray in the future.


# --------方法2: 成功-------
# --------方法2: 成功-------
ds = SD(file, SDC.READ) 
all_metadata = ds.attributes()
specific_metadata = getattr(ds, attr_name) //attr_name为想要读取变量的name,比如’temperature'

参考

[1] 使用rioxarray读取hdf: link

[2] 使用rioxarray读取hdf: link

[3] 使用pyhdf读取hdf: link

[4] 使用pyhdf读取hdf metadata: link

[5] pyhio: link

[6] pyhio: link

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值