Search code examples
python-3.xmatplotlibgisrasterrasterio

How to resolve : "IndexError: band index 1 out of range (not in ())". Raster. Rasterio


I am trying to plot MODIS data product MOD09GQ. The following is my code and console output:

import rasterio
from rasterio.plot import show
import numpy as np
import matplotlib.pyplot as plt

filepath1 = '/Users/sayantanmandal/Projects/MODIS/MOD09GQ.A2010200.h26v06.061.2021166023144.hdf'
with rasterio.open(filepath1) as modis:
    print(modis.profile)
    print(modis.crs)
    show(modis)

Console output:

{'driver': 'HDF4', 'dtype': 'float_', 'nodata': None, 'width': 512, 'height': 512, 'count': 0, 'crs': None, 'transform': Affine(1.0, 0.0, 0.0,
       0.0, 1.0, 0.0), 'tiled': False}
None
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/opt/miniconda3/lib/python3.9/site-packages/rasterio/plot.py in show(source, with_bounds, contour, contour_label_kws, ax, title, transform, adjust, **kwargs)
    101                 # Gather the indexes of the RGB channels in that order
--> 102                 rgb_indexes = [source_colorinterp[ci] for ci in
    103                                (colorinterp.red, colorinterp.green, colorinterp.blue)]

~/opt/miniconda3/lib/python3.9/site-packages/rasterio/plot.py in <listcomp>(.0)
    101                 # Gather the indexes of the RGB channels in that order
--> 102                 rgb_indexes = [source_colorinterp[ci] for ci in
    103                                (colorinterp.red, colorinterp.green, colorinterp.blue)]

KeyError: <ColorInterp.red: 3>

During handling of the above exception, another exception occurred:

IndexError                                Traceback (most recent call last)
/var/folders/bt/kqf88mw53h55m9mj35rkwt6h0000gn/T/ipykernel_3220/591497476.py in <module>
      3     print(modis.profile)
      4     print(modis.crs)
----> 5     show(modis)

~/opt/miniconda3/lib/python3.9/site-packages/rasterio/plot.py in show(source, with_bounds, contour, contour_label_kws, ax, title, transform, adjust, **kwargs)
    109 
    110             except KeyError:
--> 111                 arr = source.read(1, masked=True)
    112     else:
    113         # The source is a numpy array reshape it to image if it has 3+ bands

rasterio/_io.pyx in rasterio._io.DatasetReaderBase.read()

IndexError: band index 1 out of range (not in ())

At first I thought that may be the image does not have any values for the selected area. But when I open this file in QGIS I do get a multiband image. I may be wrong in assuming few things and may be throwing out wrong jargons, as this subject is pretty much new to me. Any idea what may be causing this error? Thanks.


Solution

  • Resolved this.

    import rioxarray as rxr
    
    modis = rxr.open_rasterio('/Users/sayantanmandal/Projects/MODIS/MOD09GQ. 
            A2010200.h26v06.061.2021166023144.hdf', masked = True)
    
    type(modis)
    

    Console output:

    xarray.core.dataset.Dataset