<SPAN class="keyword token">import</SPAN> datetime
start <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">'start run: %s\n'</SPAN> <SPAN class="operator token">%</SPAN> <SPAN class="punctuation token">(</SPAN>start<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> sa
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env
<SPAN class="keyword token">import</SPAN> glob
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token">#outdir=r"G:\\ANFIS_FINAL\\AOD\\3KM_AOD\\"</SPAN>
di <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"G:\\ANFIS_FINAL\\AOD\\3KM_AOD\\201-18"</SPAN>
lstrasters <SPAN class="operator token">=</SPAN> glob<SPAN class="punctuation token">.</SPAN>glob<SPAN class="punctuation token">(</SPAN>di <SPAN class="operator token">+</SPAN> os<SPAN class="punctuation token">.</SPAN>sep <SPAN class="operator token">+</SPAN> <SPAN class="string token">"*Corrected_Optical_Depth_Land_2-Corrected_Optical_Depth_Land.tif"</SPAN><SPAN class="punctuation token">)</SPAN>
lstrasters<SPAN class="punctuation token">.</SPAN>sort<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
M01 <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
M02 <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
M03 <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
M04 <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
M05 <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
M06 <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
M07 <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
M08 <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
M09 <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
M10 <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
M11 <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
M12 <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
yer<SPAN class="operator token">=</SPAN><SPAN class="number token">2017</SPAN>
<SPAN class="keyword token">for</SPAN> ras <SPAN class="keyword token">in</SPAN> lstrasters<SPAN class="punctuation token">:</SPAN>
yrr<SPAN class="operator token">=</SPAN>str<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">[</SPAN><SPAN class="number token">49</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">116</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
year<SPAN class="operator token">=</SPAN>int<SPAN class="punctuation token">(</SPAN>yrr<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#print year</SPAN>
<SPAN class="keyword token">if</SPAN> year <SPAN class="operator token">==</SPAN> yer<SPAN class="punctuation token">:</SPAN>
jdate<SPAN class="operator token">=</SPAN>str<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">[</SPAN><SPAN class="number token">53</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">113</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
day<SPAN class="operator token">=</SPAN>int<SPAN class="punctuation token">(</SPAN>jdate<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#print day</SPAN>
sa<SPAN class="punctuation token">.</SPAN>Raster<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> day <SPAN class="keyword token">in</SPAN> range <SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">32</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
M01<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> day <SPAN class="keyword token">in</SPAN> range <SPAN class="punctuation token">(</SPAN><SPAN class="number token">32</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">60</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
M02<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> day <SPAN class="keyword token">in</SPAN> range <SPAN class="punctuation token">(</SPAN><SPAN class="number token">60</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">91</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
M03<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> day <SPAN class="keyword token">in</SPAN> range <SPAN class="punctuation token">(</SPAN><SPAN class="number token">91</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">121</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
M04<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> day <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">121</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">152</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
M05<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> day <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">152</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">182</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
M06<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> day <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">182</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">213</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
M07<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> day <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">213</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">244</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
M08<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> day <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">244</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">274</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
M09<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> day <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">274</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">305</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
M10<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> day <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">305</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">335</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
M11<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">elif</SPAN> day <SPAN class="keyword token">in</SPAN> range <SPAN class="punctuation token">(</SPAN><SPAN class="number token">335</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">366</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
M12<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>ras<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Jdate out of range"</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"No available Data in this year:"</SPAN><SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>yer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#yer+=0001</SPAN>
<SPAN class="keyword token">print</SPAN> M01
<SPAN class="keyword token">print</SPAN> M02
<SPAN class="keyword token">print</SPAN> M03
<SPAN class="keyword token">print</SPAN> M04
<SPAN class="keyword token">print</SPAN> M05
<SPAN class="keyword token">print</SPAN> M06
<SPAN class="keyword token">print</SPAN> M07
<SPAN class="keyword token">print</SPAN> M08
<SPAN class="keyword token">print</SPAN> M09
<SPAN class="keyword token">print</SPAN> M10
<SPAN class="keyword token">print</SPAN> M11
<SPAN class="keyword token">print</SPAN> M12
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">Null_sc_mean</SPAN><SPAN class="punctuation token">(</SPAN>input<SPAN class="punctuation token">,</SPAN>output<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
SCM00_000<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>input<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">!=</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> ds <SPAN class="keyword token">in</SPAN> input<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> ds
dat<SPAN class="operator token">=</SPAN>str<SPAN class="punctuation token">(</SPAN>ds<SPAN class="punctuation token">[</SPAN><SPAN class="number token">59</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>ds<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">113</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
setnull <SPAN class="operator token">=</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>gp<SPAN class="punctuation token">.</SPAN>SetNull_sa<SPAN class="punctuation token">(</SPAN>ds<SPAN class="punctuation token">,</SPAN>ds<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"in_memory/dat"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"\"Value\" < 0"</SPAN><SPAN class="punctuation token">)</SPAN>
ras<SPAN class="operator token">=</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>Raster<SPAN class="punctuation token">(</SPAN>setnull<SPAN class="punctuation token">)</SPAN>
scalefactor<SPAN class="operator token">=</SPAN>float<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0.001</SPAN><SPAN class="punctuation token">)</SPAN>
scale<SPAN class="operator token">=</SPAN>scalefactor<SPAN class="operator token">*</SPAN>ras
SCM00_000<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>scale<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>SCM00_000<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">!=</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>gp<SPAN class="punctuation token">.</SPAN>CellStatistics_sa<SPAN class="punctuation token">(</SPAN>SCM00_000<SPAN class="punctuation token">,</SPAN>output<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"MEAN"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"DATA"</SPAN><SPAN class="punctuation token">)</SPAN>
extra<SPAN class="operator token">=</SPAN><SPAN class="number token">1</SPAN>
m3 <SPAN class="operator token">=</SPAN><SPAN class="string token">"G:\\ANFIS_FINAL\\AOD\\3KM_AOD\\01_18\\"</SPAN>
<SPAN class="keyword token">for</SPAN> ls <SPAN class="keyword token">in</SPAN> M01<SPAN class="punctuation token">,</SPAN>M02<SPAN class="punctuation token">,</SPAN>M03<SPAN class="punctuation token">,</SPAN>M04<SPAN class="punctuation token">,</SPAN>M05<SPAN class="punctuation token">,</SPAN>M06<SPAN class="punctuation token">,</SPAN>M07<SPAN class="punctuation token">,</SPAN>M08<SPAN class="punctuation token">,</SPAN>M09<SPAN class="punctuation token">,</SPAN>M10<SPAN class="punctuation token">,</SPAN>M11<SPAN class="punctuation token">,</SPAN>M12<SPAN class="punctuation token">:</SPAN>
out1<SPAN class="operator token">=</SPAN> m3<SPAN class="operator token">+</SPAN><SPAN class="string token">'{0}.img'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Month"</SPAN><SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>extra<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN>str<SPAN class="punctuation token">(</SPAN>yer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
mon1<SPAN class="operator token">=</SPAN> Null_sc_mean<SPAN class="punctuation token">(</SPAN>ls<SPAN class="punctuation token">,</SPAN>out1<SPAN class="punctuation token">)</SPAN>
extra <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">if</SPAN> extra<SPAN class="operator token">==</SPAN><SPAN class="number token">13</SPAN><SPAN class="punctuation token">:</SPAN>
yer<SPAN class="operator token">+=</SPAN><SPAN class="number token">0001</SPAN>
<SPAN class="keyword token">print</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">'finished run: %s\n\n'</SPAN> <SPAN class="operator token">%</SPAN> <SPAN class="punctuation token">(</SPAN>datetime<SPAN class="punctuation token">.</SPAN>datetime<SPAN class="punctuation token">.</SPAN>now<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">-</SPAN> start<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>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.