从Python读取3D HDF的特定Z组件切片 [英] Read Specific Z Component slice of 3D HDF from Python
问题描述
有人知道如何修改以下代码,以便我可以在Python中读取3D hdf数据的特定z分量切片吗?从所附图像中可以看到,z值范围从0到160,我只想绘制'80'.尺寸为400x160x160.这是我的代码.
Does anyone know how to make the modification of the following code so that I can read the specific z component slice of 3D hdf data in Python? As you can see from the attached image, z value spans from 0 to 160 and I want to plot the '80' only. And the dimension is 400x160x160. Here is my code.
import h5handler as h5h
h5h.manager.setPath('E:\data\Data5', False)
for i in np.arange(0,1,5000):
cycleFile = h5h.CycleFile(h5h.manager.cycleFiles['cycle_'+str(i)+'.hdf'], 'r')
fig = plt.figure()
fig.suptitle('Cycle_'+str(i)+' at t=14.4s', fontsize=20)
ax1 = plt.subplot(311)
ax2 = plt.subplot(312)
ax3 = plt.subplot(313)
Bx = np.reshape(cycleFile.get('/fields/Bx').value.T, (160,400))*6.872130320978866e-06*(1e9)
推荐答案
我的名字可能是h5handler是用于处理HDF5(.h5)文件的工具,而您似乎正在使用的文件是HDF4或HDF-EOS(.hdf)文件.用来处理HDF4文件的两个库是pyhdf( http://pysclint.sourceforge.net/pyhdf/documentation.html )和pyNIO( https://www.pyngl.ucar.edu/Nio.shtml ).
I would guess from the name that h5handler is a tool meant for working with HDF5 (.h5) files, whereas the file you appear to be working with is an HDF4 or HDF-EOS (.hdf) file. The two libraries for working with HDF4 files are pyhdf (http://pysclint.sourceforge.net/pyhdf/documentation.html) and pyNIO (https://www.pyngl.ucar.edu/Nio.shtml).
如果选择使用pyhdf,则可以按以下方式读取数据片段:
If you choose to use pyhdf, you can read your slice of data as follows:
from pyhdf.SD import *
import numpy as np
file_handle = SD("your_input_file.hdf", SDC.READ)
data_handle = file_handle.select("Bx")
Bx_data = data_handle.get() #creates a numpy array filled with the data
Bx_subset = Bx_data[:,:,80]
如果您选择使用pyNIO,则可以按以下方式读取数据片段:
If you choose to use pyNIO, you can read your slice of data as follows:
import Nio
file_handle = nio.open_file("your_input_file.hdf", format='hdf')
Bx_data = file_handle.variables["Bx"][:] #creates a numpy array filled with the data
Bx_subset = Bx_data[:,:,80]
希望有帮助!
这篇关于从Python读取3D HDF的特定Z组件切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!