2

I am a Python novice trying to figure out how to perform calculations on temporary rasters with variable names.

My attempt at a minimum working example is below, but I would eventually like to do this with thousands of larger grids, so I expect that I will need to keep everything temporary until the very end to increase efficiency.

The code below fails on the last line. The error message is:

RuntimeError: ERROR 000732: Input Raster: Dataset bin2 does not exist or is not supported.

If I replace the last line with the permanent filenames:

tmpout = Minus("D:/tmp/tmp" + `year`,"D:/tmp/tmp" + `yearold`)

then it runs fine. I am running IDLE in ArcGIS 10.0 with Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32.

import arcgisscripting
import arcpy
from arcpy import env
from arcpy.sa import *

arcpy.env.extent = arcpy.Extent(0,0,2,2)

arcpy.CreateRandomRaster_management("D:/tmp/","tmp1","","",1)
arcpy.CreateRandomRaster_management("D:/tmp/","tmp2","","",1)
bin1 = arcpy.Raster("D:/tmp/tmp1")
bin2 = arcpy.Raster("D:/tmp/tmp2")

for year in range(2, 3, 1) :
    yearold = year - 1
    binyear = "bin" + `year`
    binyearold = "bin" + `yearold`
    print "fails in next line"
    tmpout = Minus(binyear,binyearold)

I think the problem is that bin1 and bin2 are being interpreted as strings as opposed to pointer to temporary rasters, but I can't figure out how to make my intent understood. Replacing binyear with Raster(binyear), for example, did not help.

  • Correct is binyear=arcpy.Raster ("d:/tmp/tmp"+whatever_STRING) – FelixIP Aug 17 '14 at 10:08
  • Thank you for this. When I interpret your whatever_STRING in my context as year, I get the same error message. – Adam Aug 17 '14 at 10:37
1

in your first loop, you create rasters called tmpX. The second line is useless and the third line is a raster object that you overwrite at each loop. You should use the name tmpX in your second loop, instead of binX.

  • Thank you for this. I would like the second loop to work with temporary rasters to avoid writing too much writing to disk (ultimately I am writing this script to loop though thousands of grids). If I understand your response correctly, it would perform the calculations on the permanent grids. I have replaced the first loop with unlooped code (its purpose is just to generate dummy data to show the error in the second loop). The error message is the same. – Adam Aug 17 '14 at 10:52
0

i'm probably confused, but it seems writing a raster to 'd:/tmp' is still writing to disk? therefore, in the loop 'd:/tmp' is not defined and an error is encountered when the process attempts to find the raster in the same directory as the script.

you might try 'in_memory/bin1' and 'in_memory/bin2' instead of specifying a path as noted in the accepted answer here. it seems this may still write to the default gdb, but is able to speed up the process.

  • Thank you for this. It appears that in_memory does not support raster datasets, at least in ArcGIS 10.0. Also, it appears that my ignorance is more fundamental, and has to do with referring to objects by variable names. Having defined the (temporary raster) objects bin1 and bin2, I can't figure out how to refer to them in a loop in such a way that they are interpreted as raster objects, as opposed to strings. – Adam Aug 18 '14 at 10:42
  • true, sorry i didn't realize. seems like @radouxju gives a method for calling the correct raster name - which may also require the full path to the raster. – fluidmotion Aug 18 '14 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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