I have ndarray with shape of (12, 773, 1231) ((12 bands)). I want to save it as array in order to reproject it. I have image with the same shape and image but reprojected so I baiscally want my ndarray "to take" the coordinates from the original image.
I am trying to use the folliwing script from this post:
img=img=rasterio.open('TOA_22_10_2020_ (1).tif')
##doing processes on the image as numpy ndarray....
#now let's save it as tiff with the original image coordinates
with rasterio.open('ff_correction.tif',
'w',
driver='GTiff',
height=ff_correction.shape[1],
width=ff_correction.shape[2],
count=12,
dtype=ff_correction.dtype,
crs=img.crs,
nodata=None, # change if data has nodata value
transform=img.transform) as dst:
dst.write(ff_correction, 12)
But I recieve the next error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-74-786bb4d9518d> in <module>
9 nodata=None, # change if data has nodata value
10 transform=img.transform) as dst:
---> 11 dst.write(ff_correction, 1)
rasterio\_io.pyx in rasterio._io.DatasetWriterBase.write()
ValueError: Source shape (1, 12, 773, 1231) is inconsistent with given indexes 1
I don't know why the shape changes in the error. what is the mistake?
My end goal us to reproject the ndarray to have the same coordinates as the original image, but other solutions for how to give the ndarray the original image coordinates back would work as well.
Edit: Adding what I have done based on John answer, still getting error:
ff_correction.shape
>>>(12, 773, 1231)
np.squeeze(ff_correction).shape
>>>(12, 773, 1231)
>>> with rasterio.open('ff_correction.tif',
'w',
driver='GTiff',
height=ff_correction.shape[1],
width=ff_correction.shape[2],
count=12,
dtype=ff_correction.dtype,
crs=img.crs,
nodata=None, # change if data has nodata value
transform=img.transform) as dst:
dst.write(np.squeeze(ff_correction), 12)
>>>ValueError: Source shape (1, 12, 773, 1231) is inconsistent with given indexes 1
EDIT2: When I save only one band it works. but I need all the 12...