[GIS] Building python script to compute monthly average from 8 days MODIS data

arcgis-10.1arcpymodisremote sensing

Goal- Building python script to compute monthly average from 8 days MODIS data

I have MODIS data which is contain 8 days product. Some of month has 3 images and some of month has four imgaes. So how i can build a Arc GIS python scrip to calculate monthly average from this data

I have listed below number of images in each month.

Jan – 4, Feb- 4, Mar- 4, Apr- 3, May- 3, June-4, July- 4, Aug – 4, Sept – 4, Oct – 3, Nov – 4, Dec – 4,

Total 45 images are in same foldar with 8 days interval name

e.g. MOD15A2.MRTWEB.A2005337.005.Fpar_1km, MOD15A2.MRTWEB.A2005345.005.Fpar_1km

After calculation i just i want to save it in month wise name.

I have Build one code but i can take only defined interval e.g 3 days or 4 days. not 3 days and 4 days at a same time.

Below i have shown my python code

import arcpy, os, sys  
from arcpy import env  
from arcpy.sa import *  
arcpy.env.overwriteOutput = True  
arcpy.CheckOutExtension("Spatial")  
arcpy.env.extent = "MAXOF"    
env.workspace = 'D:\MODIS-NDVI\FPAR-2005\MASKED-FPAR-05B'  
rasters = arcpy.ListRasters()  
out_ws = 'D:\MODIS-NDVI\FPAR-2005\AVG-FPAR-05'  

rastersCount = len(rasters)  
counter = 0  
dek = 1  
while counter < rastersCount:  
    dekad = []  
    for i in range(counter,(counter+2)):  
        print i  
        dekad.append(rasters[i])  
        outCellStatistics = CellStatistics(dekad, "MEAN", "NODATA")  
        outRasterName = out_ws + 'tmax_dek_{:03d}.tif'.format (dek)  
        #outRasterName = "00" + str(dek) + ".tif"  
        outCellStatistics.save(outRasterName)  
    counter += 2  
    dek += 1  

Above can take only defined interval e.g 3 days or 4 days. not 3 days and 4 days at a same time.

Best Answer

It sounds like you just need to iterate over each month's folder(?), add each month's tif files to the list "dekad", perform the CellStatistics command and save result as the monthly average raster, correct?

Instead of the "for i in range... dekad.append[]..." method, you could do something like below using "glob"...

import arcpy, os, glob
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")

rootdir = 'D:/MODIS-NDVI/FPAR-2005/MASKED-FPAR-05B'
dekad = []


for month in os.listdir(rootdir):
    if os.path.isdir(os.path.join(rootdir, item)):
        print month  
        work_dir = os.path.join(rootdir, month)
        final_raster = os.path.join(work_dir, month) + '_mean.tif'
        for raster in glob.glob(work_dir + "*.tif"):
            dekad.append(raster)
        print dekad
        print "Creating a MEAN raster!"
        outfile = CellStatistics(dekad, "MEAN", "NODATA")
        outfile.save(final_raster)
    dekad = []    
Related Question