AnsweredAssumed Answered

Calculate monthly average AOD based on the Julian day wise data.

Question asked by HANIMANI1 on Sep 30, 2018
Latest reply on Oct 8, 2018 by rvburton
import datetime
start = datetime.datetime.now()
print 'start run: %s\n' % (start)
import arcpy, os
from arcpy import sa
from arcpy import env
import glob
arcpy.env.overwriteOutput = True
#outdir=r"G:\\ANFIS_FINAL\\AOD\\3KM_AOD\\"
di = r"G:\\ANFIS_FINAL\\AOD\\3KM_AOD\\201-18"
lstrasters = glob.glob(di + os.sep + "*Corrected_Optical_Depth_Land_2-Corrected_Optical_Depth_Land.tif")
lstrasters.sort()


M01 =[]
M02 =[]
M03 =[]
M04 =[]
M05 =[]
M06 =[]
M07 =[]
M08 =[]
M09 =[]
M10 =[]
M11 =[]
M12 =[]

yer=2017
for ras in lstrasters:
    yrr=str(ras[49:(len(ras)-116)])
    year=int(yrr)
    #print year
    if year == yer:
     jdate=str(ras[53:(len(ras)-113)])
     day=int(jdate)
     #print day
     sa.Raster(ras)
     if day in range     (1,32):
        M01.append(ras)
     elif day in range  (32,60):
        M02.append(ras)
     elif day in range  (60,91):
        M03.append(ras)
     elif day in range (91,121):
        M04.append(ras)
     elif day in range(121,152):
        M05.append(ras)
     elif day in range(152,182):
        M06.append(ras)
     elif day in range(182,213):
        M07.append(ras)
     elif day in range(213,244):
        M08.append(ras)
     elif day in range(244,274):
        M09.append(ras)
     elif day in range(274,305):
        M10.append(ras)
     elif day in range(305,335):
        M11.append(ras)
     elif day in range (335,366):
        M12.append(ras)

     else:
        print "Jdate out of range"
    else:
        print ("No available Data in this year:"+ str(yer))
        #yer+=0001



print M01
print M02
print M03
print M04
print M05
print M06
print M07
print M08
print M09
print M10
print M11
print M12



def Null_sc_mean(input,output):
SCM00_000=[]
if len(input)!= 0:
             for ds in input:
               print ds
               dat=str(ds[59:(len(ds)-113)])
               setnull =arcpy.gp.SetNull_sa(ds,ds, "in_memory/dat", "\"Value\" < 0")
               ras=arcpy.Raster(setnull)
               scalefactor=float(0.001)
               scale=scalefactor*ras
               SCM00_000.append(scale)
               if len(SCM00_000)!=0:
                   arcpy.gp.CellStatistics_sa(SCM00_000,output,"MEAN", "DATA")

extra=1
m3 ="G:\\ANFIS_FINAL\\AOD\\3KM_AOD\\01_18\\"
for ls in M01,M02,M03,M04,M05,M06,M07,M08,M09,M10,M11,M12:
  out1= m3+'{0}.img'.format("Month"+ str(extra)+str(yer))
  mon1= Null_sc_mean(ls,out1)
  extra += 1
  if extra==13:
    yer+=0001




print arcpy.GetMessages()
print 'finished run: %s\n\n' % (datetime.datetime.now() - start)





I have Julian day wise AOD data 2017/06 to 2018/05. Every day(...2017152...) have three files(...Land1, Land2, Land3), here we'll take only the land2 data after that I want to calculate the monthly average value from month wise data and save it in raster format with month wise by the loop. Before going to the average function we have some additional calculation is that every reading raster file first removes or deleting specific value ( less than 0 to -9999) and multiplied with 0.001 value, and then going to the Average calculation function. 

When  I am running this code then there is some anomaly. like when I am processing the data specifying subsequent months of a year or years then it saves the output files haphazardly. For example, my desired years are 2017 and 2018. It picks all the file for the specified years, but it saves the output file for 2018 in 2017 file and along with its own output file.

kindly give me a resolution or any other alternative way, and also I want to set this code leap year also.

 

I have attached some sample data.

Attachments

Outcomes