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)