Extracting List of Rasters By name

1156
8
Jump to solution
02-25-2023 05:32 AM
AliTurabHani
New Contributor II

Hi everyone. I am using Spyder and I have downloaded MODIS data from Earth Explorer and separated the daytime rasters using ListRasters(). Now I want to separate data from the different seasons and create a mean for 3 months, and for that I will have to use the image numbers( for e,g from 001 to 089 for Jan to March). I am not able to do so using ListRasters(), and can not find a solution online. I would appreciate any help in this matter. Please find below my code for reference: 


import arcpy
import os
##Extract Daytime Temp from Terra Folder
arcpy.env.workspace = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra/'
os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_D/')
rootPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra/'
outputPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_D/'

hdfList = arcpy.ListRasters()

for filename in hdfList:
arcpy.ExtractSubDataset_management(in_raster= rootPath + filename, out_raster= outputPath + filename[8:-29] + ".tif", subdataset_index= "0" )

##Extract Nightime Rasters from Terra
os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_N/')
outputPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_N/'

for filename in hdfList:
arcpy.ExtractSubDataset_management(in_raster= rootPath + filename, out_raster= outputPath + filename[8:-29] + ".tif", subdataset_index= "4" )

##Extract Daytime Temp from Aqua Folder

arcpy.env.workspace = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua/'
os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_D/')
rootPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua/'
outputPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_D/'

hdfList = arcpy.ListRasters()

for filename in hdfList:
arcpy.ExtractSubDataset_management(in_raster= rootPath + filename, out_raster= outputPath + filename[8:-29] + ".tif", subdataset_index= "0" )

##Extract Nightime Rasters from Aqua
os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_N/')
outputPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_N/'

for filename in hdfList:
arcpy.ExtractSubDataset_management(in_raster= rootPath + filename, out_raster= outputPath + filename[8:-29] + ".tif", subdataset_index= "4" )


##Create Mean for Day and Nighttime temps for Terra

arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_D/'
Terra_D = arcpy.ListRasters("*", "TIF")
arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_N/'
Terra_N = arcpy.ListRasters("*", "TIF")
os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_mean/')

from arcpy.sa import *
for (i,j) in zip(Terra_D,Terra_N):
outCellStats =CellStatistics([f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_D/{i}',f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_N/{j}'], "MEAN", "NODATA")
outCellStats.save(f"C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_mean/{i}")


##Create Mean for Day and Nighttime temps for AQUA

arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_D/'
Aqua_D = arcpy.ListRasters("*", "TIF")
arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_N/'
Aqua_N = arcpy.ListRasters("*", "TIF")
os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_mean/')

from arcpy.sa import *
for (i,j) in zip(Aqua_D, Aqua_N):
outCellStats =CellStatistics([f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_D/{i}',f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_N/{j}'], "MEAN", "NODATA")
outCellStats.save(f"C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_mean/{i}")

##Create Mean for Daily Land Surface Temperature Of all four Rasters

arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_D/'
Terra_D = arcpy.ListRasters("*", "TIF")
arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_N/'
Terra_N = arcpy.ListRasters("*", "TIF")

arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_D/'
Aqua_D = arcpy.ListRasters("*", "TIF")
arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_N/'
Aqua_N = arcpy.ListRasters("*", "TIF")

os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Daily_Mean/')


from arcpy.sa import *
for (i,j,k,l) in zip(Terra_D,Terra_N,Aqua_D, Aqua_N):
outCellStats =CellStatistics([f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_D/{i}',f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_N/{j}', f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_D/{j}', f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_N/{k}' ], "MEAN", "NODATA")
outCellStats.save(f"C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Daily_Mean/{i}")

#Apply Scale Factor

arcpy.env.workspace = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Daily_Mean/'

rootPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Daily_Mean/'

os.mkdir( 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Mean_K/')


outputPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Mean_K/'

rasterList = arcpy.ListRasters("*", "TIF")

for filename in rasterList:
output_raster = arcpy.sa.Raster(filename) * 0.02
output_raster.save(outputPath + filename)

0 Kudos
2 Solutions

Accepted Solutions
ChrisRingo
Occasional Contributor

Can't you just build the list in python rather than using arcpy.ListRasters()? I.e., if your rasters were named as you suggested above, it would go something like this:

 

lstRasters = [] 
i = 1

while i < 90:
	if i < 10:
		rnum = "00" + str(i)
	else:
		rnum = "0" + str(i)
	lstRasters.append(os.path.join(Terra_D, "A2021" + rnum))
	i+=1

 

Then use lstRasters in CellStatistics. If you're working with 8-day MODIS averages rather than daily, then of course you'd modify the list index.

Unless I'm not understanding your question...

View solution in original post

AliTurabHani
New Contributor II

Yes that would also work as I just want to separate rasters 1 to 90, 90 to 180, and so on (only from the daytime rasters). Would you be kind enough to explain the above code ? How would I specify the output path ? 

 

Yes I am working with 8 day MODIS averages. The first image is A2021001 and the next is A2021009 and so on. What do you mean by modifying the list index ? 

 

 

View solution in original post

0 Kudos
8 Replies
DanPatterson
MVP Esteemed Contributor

Please format your code and provide error messages

Code formatting ... the Community Version - Esri Community


... sort of retired...
AliTurabHani
New Contributor II

Dear Sir, 

I am not getting any error message in this code. This is just the previous code showing what I have done till now. Please find below the formatted code. I have mentioned each step in the code, and now I want to extract Daytime Values for the Months Jan to March. The filenames in my folder are in the format of "A2021001 ..... A2021002 ...... " and so on. I would appreciate any help in this issue. Thanks 

 

 

import arcpy
import os
##Extract Daytime Temp from Terra Folder
arcpy.env.workspace = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra/'
os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_D/')
rootPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra/'
outputPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_D/'

hdfList = arcpy.ListRasters()

for filename in hdfList:
arcpy.ExtractSubDataset_management(in_raster= rootPath + filename, out_raster= outputPath + filename[8:-29] + ".tif", subdataset_index= "0" )

##Extract Nightime Rasters from Terra
os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_N/')
outputPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_N/'

for filename in hdfList:
arcpy.ExtractSubDataset_management(in_raster= rootPath + filename, out_raster= outputPath + filename[8:-29] + ".tif", subdataset_index= "4" )

##Extract Daytime Temp from Aqua Folder

arcpy.env.workspace = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua/'
os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_D/')
rootPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua/'
outputPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_D/'

hdfList = arcpy.ListRasters()

for filename in hdfList:
arcpy.ExtractSubDataset_management(in_raster= rootPath + filename, out_raster= outputPath + filename[8:-29] + ".tif", subdataset_index= "0" )

##Extract Nightime Rasters from Aqua
os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_N/')
outputPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_N/'

for filename in hdfList:
arcpy.ExtractSubDataset_management(in_raster= rootPath + filename, out_raster= outputPath + filename[8:-29] + ".tif", subdataset_index= "4" )


##Create Mean for Day and Nighttime temps for Terra

arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_D/'
Terra_D = arcpy.ListRasters("*", "TIF")
arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_N/'
Terra_N = arcpy.ListRasters("*", "TIF")
os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_mean/')

from arcpy.sa import *
for (i,j) in zip(Terra_D,Terra_N):
outCellStats =CellStatistics([f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_D/{i}',f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_N/{j}'], "MEAN", "NODATA")
outCellStats.save(f"C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_mean/{i}")


##Create Mean for Day and Nighttime temps for AQUA

arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_D/'
Aqua_D = arcpy.ListRasters("*", "TIF")
arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_N/'
Aqua_N = arcpy.ListRasters("*", "TIF")
os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_mean/')

from arcpy.sa import *
for (i,j) in zip(Aqua_D, Aqua_N):
outCellStats =CellStatistics([f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_D/{i}',f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_N/{j}'], "MEAN", "NODATA")
outCellStats.save(f"C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_mean/{i}")

##Create Mean for Daily Land Surface Temperature Of all four Rasters

arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_D/'
Terra_D = arcpy.ListRasters("*", "TIF")
arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_N/'
Terra_N = arcpy.ListRasters("*", "TIF")

arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_D/'
Aqua_D = arcpy.ListRasters("*", "TIF")
arcpy.env.workspace ='C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_N/'
Aqua_N = arcpy.ListRasters("*", "TIF")

os.mkdir('C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Daily_Mean/')


from arcpy.sa import *
for (i,j,k,l) in zip(Terra_D,Terra_N,Aqua_D, Aqua_N):
outCellStats =CellStatistics([f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_D/{i}',f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Terra_N/{j}', f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_D/{j}', f'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Aqua_N/{k}' ], "MEAN", "NODATA")
outCellStats.save(f"C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Daily_Mean/{i}")

#Apply Scale Factor

arcpy.env.workspace = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Daily_Mean/'

rootPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Daily_Mean/'

os.mkdir( 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Mean_K/')


outputPath = 'C:/Users/DELL/OneDrive/Desktop/ABD1/Map_Algebra/Ass1/DATA/Mean_K/'

rasterList = arcpy.ListRasters("*", "TIF")

for filename in rasterList:
output_raster = arcpy.sa.Raster(filename) * 0.02
output_raster.save(outputPath + filename)

 

0 Kudos
DavidPike
MVP Frequent Contributor

you sure about your filepaths? linux or windows?

0 Kudos
AliTurabHani
New Contributor II

Hi David, 

Yes my filepaths are correct, and I am working on windows. This code is working, and I am now trying to get the mean for the daytime rasters for the first 3 months of the year. The names of the files in the Terra_D folder are in the format "A2021001, A2021009" and so forth. I am trying to apply ListRasters() for  raster 001 to raster 089 only and get the mean and apply other statistics. Any help in this regard would be appreciated. Thanks. 

 

0 Kudos
ChrisRingo
Occasional Contributor

Can't you just build the list in python rather than using arcpy.ListRasters()? I.e., if your rasters were named as you suggested above, it would go something like this:

 

lstRasters = [] 
i = 1

while i < 90:
	if i < 10:
		rnum = "00" + str(i)
	else:
		rnum = "0" + str(i)
	lstRasters.append(os.path.join(Terra_D, "A2021" + rnum))
	i+=1

 

Then use lstRasters in CellStatistics. If you're working with 8-day MODIS averages rather than daily, then of course you'd modify the list index.

Unless I'm not understanding your question...

AliTurabHani
New Contributor II

Yes that would also work as I just want to separate rasters 1 to 90, 90 to 180, and so on (only from the daytime rasters). Would you be kind enough to explain the above code ? How would I specify the output path ? 

 

Yes I am working with 8 day MODIS averages. The first image is A2021001 and the next is A2021009 and so on. What do you mean by modifying the list index ? 

 

 

0 Kudos
ChrisRingo
Occasional Contributor

Sorry, I didn't phrase that correctly - I meant the loop index, not list index - if you are using the 8-day MODIS, then you'd need to replace the last line with "i+=8" instead of "i+=1"

0 Kudos
AliTurabHani
New Contributor II

Got it. Thanks very much. 

 

0 Kudos