1

I have tried and run this script from Rutger Kassies.

import gdal
import matplotlib.pyplot as plt

ds = gdal.Open('HDF4_SDS:sample:"A2002037045000.L2_LAC.SAMPLE.hdf":01')
data = ds.ReadAsArray()
ds = None

fig, ax = plt.subplots(figsize=(6,6))

ax.imshow(data[0,:,:], cmap=plt.cm.Greys, vmin=1000, vmax=6000)

But then an error always occured:

Traceback (most recent call last):
File "D:\path\to\python\stackoverflow.py", line 5, in <module>
data = ds.ReadAsArray()
AttributeError: 'NoneType' object has no attribute 'ReadAsArray'

What's wrong with the script? Am I missing something? In installing GDAL I have followed this instruction http://pythongisandstuff.wordpress.com/2011/07/07/installing-gdal-and-ogr-for-python-on-windows/

Am using windows 7/32 bit/Python 2.7.

Thanks!

lovelyvm
  • 127
  • 2
  • 16
doo
  • 31
  • 2
  • 6

2 Answers2

3

gdal.Open() is failing and returning 'None'. This produces the sometimes counterintuitive message "NoneType' object has no attribute ...". Quoting from Python: Attribute Error - 'NoneType' object has no attribute 'something', "NoneType means that instead of an instance of whatever Class or Object you think you're working with, you've actually got None. That usually means that an assignment or function call up above failed or returned an unexpected result."

Apparently GDAL is correctly installed. It could be that the file is not readable or that there is an issue with the HDF driver. Are you getting any error message like:

`HDF4_SDS:sample:"A2002037045000.L2_LAC.SAMPLE.hdf":01' does not exist in the file system, and is not recognised as a supported dataset name.

To get additional information you can try something like this instead of the gdal.Open() line in your script:

gdal.UseExceptions()
ds=None
try:
    ds = gdal.Open('HDF4_SDS:sample:"A2002037045000.L2_LAC.SAMPLE.hdf":01')
except RuntimeError, err:
    print "Exception: ", err
    exit(1)

Also, there's an extra '}' at the end of the script.

Community
  • 1
  • 1
fedemp
  • 210
  • 1
  • 4
  • An error message always appears 'Exception:`HDF4_SDS:UNKNOWN:"A2014037040500.L2_LAC.SeAHABS.hdf":37' does not exist in the file system, and is not recognised as a supported dataset name.' – doo May 27 '14 at 03:44
  • It could be that there is some issue with HDF support in your GDAL installation. You can see the list of formats that are in principle compiled in your GDAL with the following command: `gdalinfo --formats`. I'd also try to open a more simple/basic GIS file, like an ascii or GeoTIFF, and see what happens. – fedemp May 27 '14 at 20:48
  • I have an UPDATE above. That's what appears every time I run --formats. – doo May 29 '14 at 07:50
1

By default, osgeo.gdal returns None on error, and does not normally raise informative exceptions. You can change this with gdal.UseExceptions().

Try something like this:

from osgeo import gdal
gdal.UseExceptions()

source_path = r'HDF4_SDS:sample:"D:\path\to\file\A2002037045000.L2_LAC.SAMPLE.hdf":01'
try:
    ds = gdal.Open(source_path)
except RuntimeError as ex:
    raise IOError(ex)

The last bit just re-raises the exception as an IOError rather than a RuntimeException.

The solution is to modify source_path to a working path to your data source, e.g., I see

IOError: `HDF4_SDS:sample:"A2002037045000.L2_LAC.SAMPLE.hdf":01' does not exist in the file system, and is not recognised as a supported dataset name.

lovelyvm
  • 127
  • 2
  • 16
Mike T
  • 41,085
  • 18
  • 152
  • 203
  • I have tried to modify the source_path from where the hdf file is located but an error always appears, **IOError: `D:\local directory where hdf file is located\A2014037040500.L2_LAC.SeAHABS.hdf' does not exist in the file system, and is not recognised as a supported dataset name.** – doo May 27 '14 at 03:49
  • Assuming you have one GDAL library, are you able to use `gdalinfo` with this dataset? Is the path string valid, i.e. `os.path.isfile(path)`? (Note: it's good practice to use raw escaping with Windows paths, e.g. `r'D:\local directory\file.hdf'`) – Mike T May 27 '14 at 07:28
  • I have an Update above. That's what appears every time I run the hdf file in gdalinfo. What do you mean that it's good practice to use raw escaping with Windows paths? – doo May 29 '14 at 07:41
  • Use strings like `r'some\text'` treats the \ as a raw character, rather than escaping a tab from `\t`. Notably important for Windows paths. – Mike T May 29 '14 at 11:28
  • I've tried your sample (r'D:\localdirectory\file.hdf'), here's the error ERROR 4: `r'D:\local directory\A2014037040500.L2_LAC.SeAHABS.hdf ' does not exist in the file system, and is not recognised as a supported dataset name. gdalinfo failed - unable to open 'r'D:\local directory\A2014037040500.L2_LAC.SeAHABS.hdf'. – doo May 30 '14 at 03:00
  • `r'D:\localdirectory\file.hdf'` is how you capture a whole string in Python, but that's not what the error message has! What did you *actually* use? Take note of my example for `source_path`. – Mike T May 30 '14 at 03:35
  • Sorry I'm really not good at this. Here is the directory where you can find the hdf file. D:\samoa.gsfc.nasa.gov\subscriptions\MODISA\XM\occo.habtech\2421\. The source_path in your example is actually where my hdf file is located. – doo Jun 02 '14 at 02:45
  • It's not clear if your Python environment has a driver to read it. Does `gdal.GetDriverByName('HDF4Image').GetDescription()` show "HDF4Image"? – Mike T Jun 03 '14 at 02:34
  • I've tried this: 'from osgeo import gdal import ogr gdal.GetDriverByName('HDF4Image').GetDescription()' But there's an error: **Traceback (most recent call last): File "C:/Users/Oogway/Desktop/getdriver.py", line 4, in gdal.GetDriverByName('HDF4Image').GetDescription() AttributeError: 'NoneType' object has no attribute 'GetDescription'** – doo Jun 04 '14 at 08:51
  • Ok, that's why. You don't have an HDF4Image driver for your Python installation. This may seem confusing since you have this from a command-line (`gdalinfo --formats`), but that's probably a separate installation of GDAL. You should either find out how to install GDAL Python bindings with this driver, or ask a new question to use this driver. A normal result of `GetDescription()` should be `'HDF4Image'`. – Mike T Jun 04 '14 at 21:27