Read Specific Z Component slice of 3D HDF from Python

跟風遠走 提交于 2019-12-25 06:59:50

问题


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)


回答1:


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).

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]

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]

Hope that helps!



来源:https://stackoverflow.com/questions/27255869/read-specific-z-component-slice-of-3d-hdf-from-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!