Search code examples
pythonurldownloadpyhdf

Downloading hdf file, but does not read using pyhdf


I have downloaded the hdf files from a website, but when I want to read them out using pyhdf I get the error: 'HDF4Error: SD (15): File is supported, must be either hdf, cdf, netcdf'

When I just click on the link on the website, and download the file that way, it does work..

import requests
from pyhdf.SD import SD, SDC

r = requests.get(url)
url = 'https://data.seaice.uni-bremen.de/amsr2/asi_daygrid_swath/s3125/2015/jan/McMurdo/'

with open('asi-AMSR2-s3125-20150101-v5.hdf', 'wb') as f:
    f.write(r.content)


path = 'path/to/file/on/computer/'
hdffile = 'asi-AMSR2-s3125-20150101-v5.hdf'

file = SD(path + hdffile,SDC.READ)

When I download the file manually from the website, the SD(...) command works, but when I have downloaded it through the python script, it gives the error 'HDF4Error: SD (15): File is supported, must be either hdf, cdf, netcdf'

I've tried a lot of different ways to download the file, but they all end up giving the same error when I want to read it.


Solution

  • The problem is that you are not downloading the file, but the directory listing. The following modified script should work:

    import requests
    from pyhdf.SD import SD, SDC
    
    url = 'https://data.seaice.uni-bremen.de/amsr2/asi_daygrid_swath/s3125/2015/jan/McMurdo/'
    hdffile = 'asi-AMSR2-s3125-20150101-v5.hdf'
    path = 'path/to/file/on/computer/'
    
    r = requests.get(url+hdffile)
    
    with open(path + hdffile, 'wb') as f:
        f.write(r.content)
    
    file = SD(path + hdffile,SDC.READ)