I have 5 folders, each with 80 .tif files. These 80 files represent one landsat image taken on a different date. Each folder represents a vegetative indice (NDVI, NDII etc.)
Each file in each folder is listed in the same order, based on date. So the first file in all five folders correspond to each other. I want to add all 5 .tif files together (for all 80 dates) using map algebra. The code I am trying so far is this:
import arcpy
from arcpy import *
arcpy.CheckOutExtension('Spatial')
arcpy.env.overwriteOutput = True
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\NDVI'
NDVIrasters=arcpy.ListRasters('*.tif*')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\NDII'
NDIIrasters=arcpy.ListRasters('*.tif*')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\RGR'
RGRrasters=arcpy.ListRasters('*.tif*')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\SWIR32'
SWIR32rasters=arcpy.ListRasters('*.tif*')
arcpy.env.workspace=r'F:\Sheyenne\Normalized_Indices\SATVI'
SATVIrasters=arcpy.ListRasters('*.tif*')
outpath='F:\Sheyenne\Normalized_Indices\Composited\\'
for ndviraster in NDVIrasters:
ras1 = Raster(ndviraster)
for ndiiraster in NDIIrasters:
ras2=Raster(ndiiraster)
for rgrraster in RGRrasters:
ras3=Raster(rgrraster)
for swir32raster in SWIR32rasters:
ras4=Raster(swir32raster)
for satviraster in SATVIrasters:
ras5=Raster(satviraster)
outRaster = ras1 + ras2 +ras3 +ras4 + ras5
outpath=outpath + ras1
outRaster.save(outpath)
but I think there is something wrong with how I am doing the nested loop, first of all, when I simply print ras5
at the end, it lists more than the 80 files found in the pathway r'F:\Sheyenne\Normalized_Indices\SATVI'
The actual error returned is:
Traceback (most recent call last):
File "<ipython-input-61-567d86ebd99a>", line 1, in <module>
runfile('F:/python codes/normalize_indices.py', wdir='F:/python codes')
File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "F:/python codes/normalize_indices.py", line 98, in <module>
outpath=outpath + ras1
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\sa\Functions.py", line 4143, in Plus
in_raster_or_constant2)
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\sa\Utils.py", line 47, in swapper
result = wrapper(*args, **kwargs)
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\sa\Functions.py", line 4140, in Wrapper
["Plus", in_raster_or_constant1, in_raster_or_constant2])
RuntimeError: ERROR 000732: Input Raster: Dataset F:\Sheyenne\Normalized_Indices\Composited\ does not exist or is not supported
but that pathway definitely does exists for the outpath. Possibly an indenting issue in the last three lines?
EDIT:
I figured out the problem with the loop based on below answers and comments.
I am now using this:
outpath='F:\Sheyenne\Normalized_Indices\Composited\\'
for ndvi, ndii, rgr, swir32, satvi in zip(NDVIrasters, NDIIrasters,RGRrasters, SWIR32rasters, SATVIrasters):
outraster= ndvi +ndii + rgr + swir32 +satvi
outpath=outpath + ndvi
outraster.save(outpath)
print outpath
but the out path is using iteration to ad the raster names to the pathway. So the first outpath
is the first raster in my list. But the second one is the first and second combined, and the third one is the first through third combined etc.