# raster calculation using rasterio

I have a multi image tif with 7 bands and I try to use rastrio package to create a simple calculation. I follow the docs but that not work for me.

any idea ?

here the code:

``````with rasterio.open('L7.tif') as src:
#b1, b2, b3, b4, b5 = src.read()
profile = src.profile
profile.update(
dtype=rasterio.float64,
count=1,
compress='lzw')

MI = numpy.zeros(blue.shape)
MI = (blue *green*red *nir  )

with rasterio.open('ndvi_python.tif', 'w', **profile) as dst:
dst.write(NDMI.astype(rasterio.float64), 1)
``````

error :

``````    blue = src.read_band(1)
``````

if I try to :

``````with rasterio.open('L7.tif') as src:
b1, b2, b3, b4, b5 = src.read()
``````

then I take this error :

``````    b1, b2, b3, b4, b5 = src.read()
ValueError: need more than 1 value to unpack
``````

You should be using `read()` method when using Rasterio version 1.0a12 (Rasterio reference).

The raster array for a raster band can be accessed by calling dataset.read() with the band’s index number. Following the GDAL convention, bands are indexed from 1.

The following is now the preferred method to read raster bands (Rasterio reference):

``````import rasterio

with rasterio.open('tests/data/RGB.byte.tif') as src:
``````

This will return a Numpy N-D array of the form:

``````>>> band1
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint16)
``````
• thank you do much now working but I have one more question.i take this message `RuntimeWarning: divide by zero encountered in divide NDMI = (blue *green*red *nir )` I think is fro this line `numpy.zeros(blue.shape)` any idea what is this ? – Mar Feb 2 '18 at 15:25
• I would recommend opening a new question with the code that is causing the error. I do not think I would be able to address your question properly in the comments. – Aaron Feb 2 '18 at 15:39
{"ft_lang_label":"__label__en","ft_lang_prob":0.5659807,"math_prob":0.941608,"size":873,"snap":"2021-04-2021-17","text_gpt3_token_len":281,"char_repetition_ratio":0.16340621,"word_repetition_ratio":0.062015504,"special_character_ratio":0.32760596,"punctuation_ratio":0.24878049,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96284163,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-01-23T15:24:21Z\",\"WARC-Record-ID\":\"<urn:uuid:eaf7823e-c664-4a26-b600-a14c414f3a5e>\",\"Content-Length\":\"149545\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c871e584-da85-41e1-803c-f8db5e95a081>\",\"WARC-Concurrent-To\":\"<urn:uuid:f32aed86-e256-40be-a142-a3dbbe0f60eb>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://gis.stackexchange.com/questions/269610/raster-calculation-using-rasterio\",\"WARC-Payload-Digest\":\"sha1:KEJ73M7SBAATA5UWR7HYGNSPD2BSCRI3\",\"WARC-Block-Digest\":\"sha1:53XTDVUYZILAHOCH7X7J2U4FLSF2GXM2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-04/CC-MAIN-2021-04_segments_1610703538082.57_warc_CC-MAIN-20210123125715-20210123155715-00177.warc.gz\"}"}