# Reading HDF4 Files

**URL:** https://discourse.julialang.org/t/reading-hdf4-files/29470
**Category:** General Usage
**Created:** [October 4, 2019, 3:55am UTC](https://discourse.julialang.org/t/reading-hdf4-files/29470 "2019-10-04T03:55:08Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![natgeo-wong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natgeo-wong/32/10965_2.png) [@natgeo-wong](https://discourse.julialang.org/u/natgeo-wong)
#### Post date: [October 4, 2019, 3:55am UTC](https://discourse.julialang.org/t/reading-hdf4-files/29470/1 "2019-10-04T03:55:08Z")

</div>

Hi! As someone who needs to read in HDF4 files from TRMM and MODIS, I am trying to find a workaround in Julia using PyCall. However, I’m having some problems with loading the data.

fhdf = pyhdf.SDS(“3B42.20130909.00.7.HDF”,“/Grid/precipitation”);

returns  
PyObject \<bound method SDS.info of \<pyhdf.SD.SDS object at 0x1a45e318d0\>\>

i.e. a python object. Is there anyone who has the expertise and who has used pyhdf to read files to tell me how to access the data inside the python object?

---

<div class="post-metadata">

### Author: ![natgeo-wong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natgeo-wong/32/10965_2.png) [@natgeo-wong](https://discourse.julialang.org/u/natgeo-wong)
#### Post date: [October 4, 2019, 4:11am UTC](https://discourse.julialang.org/t/reading-hdf4-files/29470/2 "2019-10-04T04:11:06Z")

</div>

Update: I found the solution which is to use PyCall’s “get” function. But “get” is very irritating because it only allows me to retrieve one element at a time, so I have to do a for loop as follows:

dobj = h4read(file,variable); data = zeros(1440,400)  
for ii = 0 : 1439  
for jj = 0 : 399  
data[ii+1,jj+1] = get(dobj,(ii,jj));  
end  
end

Does anyone know if there is an effort to parallelize or optimize this so that I can read in the whole chunk instead of calling it one by one because I’m sure there’s significant overhead as a result haha.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [October 4, 2019, 10:32am UTC](https://discourse.julialang.org/t/reading-hdf4-files/29470/3 "2019-10-04T10:32:11Z")

</div>

You can read MODIS data via [GMT.jl](https://github.com/GenericMappingTools/GMT.jl). See first block in t[his example](https://github.com/GenericMappingTools/GMT.jl/blob/master/WL_Example_III.ipynb)

---

<div class="post-metadata">

### Author: ![natgeo-wong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natgeo-wong/32/10965_2.png) [@natgeo-wong](https://discourse.julialang.org/u/natgeo-wong)
#### Post date: [October 4, 2019, 1:30pm UTC](https://discourse.julialang.org/t/reading-hdf4-files/29470/4 "2019-10-04T13:30:12Z")

</div>

But this is HDF5. I need to read HDF4. Even if it is not MODIS, I have other datasets that store as HDF4 and not HDF5.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [October 4, 2019, 4:05pm UTC](https://discourse.julialang.org/t/reading-hdf4-files/29470/5 "2019-10-04T16:05:19Z")

</div>

HDF4 should work as well. The key is GDAL. GMT uses it to read these type of files. If GDAL was compiled with HDF4 support (the GMT windows installer comes with one GDAL linked with HDF4) then it should work.

---

<div class="post-metadata">

### Author: ![natgeo-wong](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/natgeo-wong/32/10965_2.png) [@natgeo-wong](https://discourse.julialang.org/u/natgeo-wong)
#### Post date: [October 4, 2019, 4:27pm UTC](https://discourse.julialang.org/t/reading-hdf4-files/29470/6 "2019-10-04T16:27:28Z")

</div>

Alright thanks.

Now to get GMT to work on my julia installation, lol.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [October 4, 2019, 4:41pm UTC](https://discourse.julialang.org/t/reading-hdf4-files/29470/7 "2019-10-04T16:41:14Z")

</div>

Ah, yes! (switch to Windows 🙂). Seriously, that homebrew link I posted should work flawlessly, but I’m not sure if uses a HDF4 aware GDAL
