Read Specific Z Component slice of 3D HDF from Python

557 views Asked by At

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)

enter image description here

1

There are 1 answers

0
Heather QC On

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!