Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a CF-conform NetCDF-file which can be loaded into Quantum GIS (Version 1.8) by using the "Add Raster Layer"-button and subsequently choosing the file and the raster layers of interest. This works without problems. However, when trying to load the same file by following these steps from the python cookbook, the layer remains invalid. I tried it using the console:

>>> fileName = "/path/to/file/"
>>> fileInfo = QtCore.QFileInfo(fileName)
>>> baseName = fileInfo.baseName()
>>> rlayer = QgsRasterLayer(fileName, baseName)
>>> rlayer.isValid()

When trying to add the layer to the map registry, it doesn't show up:

>>> QgsMapLayerRegistry.instance().addMapLayer(rlayer)
<qgis.core.QgsRasterLayer object at 0x3715f80>

I tried the same procedure with a jpeg with world file and it works.

The cookbook mentions that GDAL might not support some formats. I don't know how to check this in QGIS, but since it works manually, I don't think that this is the problem.

Maybe, I have to specify the raster layer of interest somewhere in the code?

Any help is highly appreciated.

share|improve this question
The problem is that netcdf can have several subdataset.See the scripts of the netcdfbrowser plugin. – gene Sep 10 '13 at 16:44
up vote 3 down vote accepted

Thanks to gene mentioning the netcdfbrowser plugin (see the addLayer()-method in I managed to get it working. In case of NetCDF-files the file name is not sufficient to load a raster. We also need to include the variable of interest. The following works:

>>> uri='NETCDF:"/path/to/file/":myvariable'
>>> rlayer = QgsRasterLayer(uri,'mylayername')
>>> rlayer.isValid()
>>> QgsMapLayerRegistry.instance().addMapLayer(rlayer)
<qgis.core.QgsRasterLayer object at 0x38f5560>

The layer shows up.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.