Unable to save a 3 dimensional array to stacked raster .img file.
I have this array:
arr.shape (10, 148, 160)
Now when I try to save this into raster .img format:
out_ras = arcpy.NumPyArrayToRaster(arr)
out_ras.save(r"C:\Users\Gurminder\Documents\study\IWMI\HomeTools\Dump\Converted_arr\stack.img")
I get the following error:
Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> RuntimeError:
ERROR 010240: Could not save raster dataset to C:\Users\Gurminder\Documents\study\IWMI\HomeTools\Dump\
Converted_arr\stack.img with output format IMAGINE Image.
Although a file named stack.img is created in the given directory but all it contains is garbage values.
I have been facing this issue since I have upgraded from ArcMap 10.3 to 10.4, before upgrade everything was working just fine.
That error message is a generic one normally associated with memory issues or file path issues when working with raster data. (search on error 010240 to see what I mean) But your array is only 10 bands with 148x160 in size, so that shouldn't be an issue...
I would suggest saving the file to a shorter path as a first step.
Secondly, try saving one of the bands to a new shorter path.
Third, you could definitely have something wrong with one of the bands that is producing garbage in the final array, so you might want to throw in some print statements
Finally create this 10 band raster and see if you can save it
>>> import numpy as np
>>> np.set_printoptions(edgeitems=2,linewidth=80,precision=2,
... suppress=True,threshold=20,
... formatter={'float': '{: 0.3f}'.format})
>>>
>>> a = np.arange(10*5*6).reshape(10,5,6)
>>> a
array([[[ 0, 1, ..., 4, 5],
[ 6, 7, ..., 10, 11],
...,
[ 18, 19, ..., 22, 23],
[ 24, 25, ..., 28, 29]],
[[ 30, 31, ..., 34, 35],
[ 36, 37, ..., 40, 41],
...,
[ 48, 49, ..., 52, 53],
[ 54, 55, ..., 58, 59]],
...,
[[240, 241, ..., 244, 245],
[246, 247, ..., 250, 251],
...,
[258, 259, ..., 262, 263],
[264, 265, ..., 268, 269]],
[[270, 271, ..., 274, 275],
[276, 277, ..., 280, 281],
...,
[288, 289, ..., 292, 293],
[294, 295, ..., 298, 299]]])
Some test results that you can also run on yours
>>> a = np.arange(10*5*6).reshape(10,5,6)
>>> a[0]
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29]])
>>> a[-1]
array([[270, 271, 272, 273, 274, 275],
[276, 277, 278, 279, 280, 281],
[282, 283, 284, 285, 286, 287],
[288, 289, 290, 291, 292, 293],
[294, 295, 296, 297, 298, 299]])
>>> np.mean(a,axis=0)
array([[ 135., 136., 137., 138., 139., 140.],
[ 141., 142., 143., 144., 145., 146.],
[ 147., 148., 149., 150., 151., 152.],
[ 153., 154., 155., 156., 157., 158.],
[ 159., 160., 161., 162., 163., 164.]])
>>> np.ptp(a,axis=0)
array([[270, 270, 270, 270, 270, 270],
[270, 270, 270, 270, 270, 270],
[270, 270, 270, 270, 270, 270],
[270, 270, 270, 270, 270, 270],
[270, 270, 270, 270, 270, 270]])
Hello Dan,
Thank you for the prompt reply. I have made the array suggested by you and still I am getting the same error while saving it.
>>> import numpy as np np.set_printoptions(edgeitems=2,linewidth=80,precision=2, suppress=True,threshold=20, formatter={'float': '{: 0.3f}'.format}) a = np.arange(10*5*6).reshape(10,5,6) out_ras = arcpy.NumPyArrayToRaster(a) out_ras.save(r"C:\Users\Gurminder\Documents\study\IWMI\HomeTools\Dump\Converted_arr\stack_geonet.img") Runtime error Traceback (most recent call last): File "<string>", line 8, in <module> RuntimeError: ERROR 010240: Could not save raster dataset to C:\Users\Gurminder\Documents\study\IWMI\HomeTools\Dump\Converted_arr\stack_geonet.img with output format IMAGINE Image.
what about my other suggestions? Use the array I gave you to try...also, try something other than *.img
I would suggest saving the file to a shorter path as a first step.
Secondly, try saving one of the bands to a new shorter path.
Third, you could definitely have something wrong with one of the bands that is producing garbage in the final array, so you might want to throw in some print statements
I tried shorter path with img as well as tif but still I get the same error
>>> import numpy as np
np.set_printoptions(edgeitems=2,linewidth=80,precision=2,
suppress=True,threshold=20,
formatter={'float': '{: 0.3f}'.format})
a = np.arange(10*5*6).reshape(10,5,6)
out_ras = arcpy.NumPyArrayToRaster(a)
out_ras.save("C:\\Users\\Gurminder\\Desktop\\stack_geonet.img")
Runtime error
Traceback (most recent call last):
File "<string>", line 8, in <module>
RuntimeError: ERROR 010240: Could not save raster dataset to C:\Users\Gurminder\Desktop\stack_geonet.img with output format IMAGINE Image.
>>> import numpy as np
np.set_printoptions(edgeitems=2,linewidth=80,precision=2,
suppress=True,threshold=20,
formatter={'float': '{: 0.3f}'.format})
a = np.arange(10*5*6).reshape(10,5,6)
out_ras = arcpy.NumPyArrayToRaster(a)
out_ras.save("C:\\Users\\Gurminder\\Desktop\\stack_geonet.tif")
Runtime error
Traceback (most recent call last):
File "<string>", line 8, in <module>
RuntimeError: ERROR 010240: Could not save raster dataset to C:\Users\Gurminder\Desktop\stack_geonet.tif with output format TIFF.
I have tried converting single band raster to array and back to raster. Everything works fine when it comes to single band.
Taking the array as you have mentioned cannot contain garbage values still it gives the same error so garbage values does not exist in the array "a".
try saving to
r"c:\test\x.img"
r"c:\test\x.tif"
shorter and not in the same path and your file may be barking at the length of its name or the underscore or anything else... let's stretch a little in the things to test
In short, it doesn't like your filename or the saving location, or you don't have permissions OR are you saving to the DESKTOP????
If so .... no ... Bill Gates may say it is ok, but it isn't
Still having the same issue.
>>> import numpy as np
... np.set_printoptions(edgeitems=2,linewidth=80,precision=2,
... suppress=True,threshold=20,
... formatter={'float': '{: 0.3f}'.format})
...
... a = np.arange(10*5*6).reshape(10,5,6)
... out_ras = arcpy.NumPyArrayToRaster(a)
... out_ras.save(r"C:\test\x.img")
...
Runtime error
Traceback (most recent call last):
File "<string>", line 8, in <module>
RuntimeError: ERROR 010240: Could not save raster dataset to C:\test\x.img with output format IMAGINE Image.
>>> import numpy as np
... np.set_printoptions(edgeitems=2,linewidth=80,precision=2,
... suppress=True,threshold=20,
... formatter={'float': '{: 0.3f}'.format})
...
... a = np.arange(10*5*6).reshape(10,5,6)
... out_ras = arcpy.NumPyArrayToRaster(a)
... out_ras.save(r"C:\test\x.tif")
...
Runtime error
Traceback (most recent call last):
File "<string>", line 8, in <module>
RuntimeError: ERROR 010240: Could not save raster dataset to C:\test\x.tif with output format TIFF.
Ahhhh
I will let you figure out what line you left out
Hello Dan,
Are you suggesting that import arcpy was missing.
I have tried with import statement still its giving me the same error.
One question Dan, what version of ArcMap are you using?
>>> import numpy as np
... import arcpy
... np.set_printoptions(edgeitems=2,linewidth=80,precision=2,
... suppress=True,threshold=20,
... formatter={'float': '{: 0.3f}'.format})
...
... a = np.arange(10*5*6).reshape(10,5,6)
... out_ras = arcpy.NumPyArrayToRaster(a)
... out_ras.save(r"C:\test\x.img")
...
Runtime error
Traceback (most recent call last):
File "<string>", line 9, in <module>
RuntimeError: ERROR 010240: Could not save raster dataset to C:\test\x.img with output format IMAGINE Image.
>>> import numpy as np
... import arcpy
... a = np.arange(10*5*6).reshape(10,5,6)
... out_ras = arcpy.NumPyArrayToRaster(a)
... out_ras.save(r"C:\test\x.tif")
...
Runtime error
Traceback (most recent call last):
File "<string>", line 5, in <module>
RuntimeError: ERROR 010240: Could not save raster dataset to C:\test\x.tif with output format TIFF.
>>>
If you are doing it inside of arcmap's python IDE, you shouldn't need it... but you didn't specify where you were running it.
So... it should work without the import arcpy.
I am using ArcMap 10.4.1 and I did the calculation in pythonwin running python 3.4, but it worked in 2.7 as well
I basically think your setup is ....
So the only thing I can suggest is go try it on someone else's machine. The code works, an image was produced and you will just keep spinning your wheels until you can prove that you can save that basic stacked array.
Good luck