I have an ArcMap document open with LandSat images from one day in different bands as rasters. They're named:


and so on, B1 through to B9 (bands 1 to 9).

I want to do a calculation on each band and the result of the calculation should be a new raster for each band, with the name of it indicating which band it is. This is what I'm trying, but the string in the output name disallows the process. How do I achieve the same result, get output of TOA_B1, TOA_B2 etc.

import arcpy
from arcpy import env
from arcpy import mapping
from arcpy.sa import *
i = list(range(1,10))
while i<10:
        TOAB_ + str(i) = Raster('LC80541162014003LGN00_B' + str(i)+ '.TIF')*2

To be clear, the calculation within it works on its own

Raster('LC80541162014003LGN00_B' + str(i)+ '.TIF')*2

I have simplified the script here for clarity, I know I could do each calculation separately but for various reasons it will be much more efficient to do it in a loop of some kind.

  • looks like a typo to me in your output rater name, you have an extra i. Try: toad_ + str(i) = ..... – Dowlers Jul 13 '16 at 18:01
  • I'd use arcpy. Raster() – FelixIP Jul 14 '16 at 4:37
  • You can't assign a value to an operation - stackoverflow.com/a/8956876/737471 – user2856 Jul 14 '16 at 5:59

You can use enumerate with for loop to count the number of rasters inside a workspace and in the same time get the raster bands as objects. enumerate by default starts with 0, but you need to start with 1, so you have to write enumerate(rasterlist, start=1). Then you can use Spatial Analysts -> Times function to multiply each band with 2. The variable assigned to multiplication can be saved to the target workspace. Here you need to pass (i) when you save to a new file with a new name.

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

# Set the current workspace
env.workspace = r"C:\PathTO\Landsat\Bands1_9"

# Check out Spatial extension 

# Get a list of raster images of Landsat Bands from the workspace
rasterlist = arcpy.ListRasters()

# Get a count and the raster obtained from rsterlist
for i, raster in enumerate(rasterlist, start=1):
    print i,raster

# Multiply each Landsat band by 2
    toa = Times(raster,2)

# Save the output Landsat bands to the target workspace
    toa.save(r"C:\PathTO\Landsat\Bands1_9\TOA\TOA_B"+ str(i)+".tif")

The out put will be as follows:


'TOAB_' need to be in quotes since it is a string.

I suspect that you need to separate the TOAB_ + stri(i) part. Maybe toa = 'TOAB_' + stri(i) on the first line and then toa = Raster('LC80541162014003LGN00_B' + str(i)+ '.TIF')*2 on the second line.

  • 1
    Thanks but this doesn't work. There's no error, but no output. (If it did run, would this not just assign a string to toa then replace it by assigning a raster to a toa. i.e. I would just have a raster called toa, not TOAB_1. – Beardsley Jul 13 '16 at 18:55
  • @Beardsley you are right about it assigning then re-assigning a value. – Midavalo Jul 13 '16 at 19:16

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.