Saving rasters in Arcpy loop

08-09-2018 11:52 AM
New Contributor
I'm trying to do Map Algebra on a bunch of rasters by iterating through a for loop and saving them in an empty geodatabase. However, they aren't saving properly. Only the last raster is created and it is named "rescaledRaster" rather than the outName. Here's the code:
import arcpy, os
from import *
arcpy.env.workspace = r"[abspath]"
arcpy.env.scratchWorkspace = r"[abspath]\rescale.gdb"
aprx ="[abspath]\filename.aprx")

# Select correct interpolated rasters
for lyr in aprx.listMaps()[0].listLayers():
        if lyr.isRasterLayer:
                if 'Temp' in
                        if 'Int_Raster' in

                                # Set local variables
                                raster = Raster(lyr.dataSource)
                                thresholds = [14, 23]
                                bounds = [14, 23]
                                scale = [1, 0.496714]
                                transformation = TfLinear( bounds[0], bounds[1], thresholds[0], 1, thresholds[1], 0 )

                                outName = arcpy.env.scratchWorkspace + "\\" +'Int_Raster','Resc')

                                # Rescale the raster
                                rescaledRaster = RescaleByFunction( raster, transformation, scale[0], scale[1] )

                                # Save the rescaled raster
The print statements seem correct each time; .isTemporary comes out False and .name returns the outName. Despite this, the output rasters keep getting overwritten.
I also tried outName = os.path.join(arcpy.env.scratchWorkspace,'Int_Raster','Resc')) already and that didn't work either. 
0 Kudos
3 Replies
MVP Legendary Contributor

Rae... could you format your code using python syntax highlighting to make it readable


0 Kudos
MVP Legendary Contributor

a duplicate of 

just the OP name is different

0 Kudos
New Contributor II

Sorry no solution but I have the same problem:

i = 0
#loop through rasters in list
for ir in rrings:
    #sum rasters together
    if i == 0:
      ringSUM = arcpy.Raster(ir)
      i += 1
      ringSUM = ringSUM + arcpy.Raster(ir)
      i += 1

#save final output to the current dirctory'C:/SALR_0801/wksp/ringSUMall.tif')


Traceback (most recent call last):
File "C:\SALR_0801\", line 87, in <module>'C:/SALR_0801/wksp/ringSUMall.tif')
RuntimeError: ERROR 999998: Unexpected Error.

Sorry that the code is not in  python syntax did not copy optimally.   The thing for me and wondering if for others is that it seems the doesn't work no matter what I have tried.   (using variable with path&file, changing to GRID even though I want a TIF, etc).


I want to understand this as I have several other programs to modify and can't seem to get a generic solution.  I have lots of disk space and memory running ArcGIS 10.6


0 Kudos