Hi,
I need to find the file index from a list of rasters that matches year, month and week. My code below work when I give the index manually. But, I need to loop through many raster files.
My fmaskrasters list has 180 rasters and as follows:
fmaskrasters = [
Week_1_Sum1981_10_Crawford.tif
Week_1_Sum1982_10_Crawford.tif
.
Week_1_Sum2016_10_Crawford.tif
Week_2_Sum1981_10_Crawford.tif
.
Week_2_Sum2016_10_Crawford.tif
.
Week_5_Sum2016_10_Crawford.tif]
normal raster list has 5 rasters).
normal = [Crawford_Normal_Week_1_1981_2016_10.tif,
Crawford_Normal_Week_2_1981_2016_10.tif,
.
Crawford_Normal_Week_5_1981_2016_10.tif]
Can somebody help me to find the index from each raster list that matches, year, month and week so that I calculate the percent difference. My code below works fine but I need to pass the index that matches year, month and week criteria to find the index of raster from "fmaskrasters" and "normal".
Thanks in advance,
months <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">]</SPAN>
weeks <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
years <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">1981</SPAN><SPAN class="punctuation token">]</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'H:\PRISM_800m_weekly_sum\Crawford\All'</SPAN>
out <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'H:\PRISM_800m_weekly_sum\Crawford\All\output'</SPAN>
<SPAN class="keyword token">for</SPAN> year <SPAN class="keyword token">in</SPAN> years<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> month <SPAN class="keyword token">in</SPAN> months<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> week <SPAN class="keyword token">in</SPAN> weeks<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'H:\PRISM_800m_weekly_sum\Crawford\All'</SPAN>
fmaskrasters <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListRasters<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
fmaskrasters<SPAN class="punctuation token">.</SPAN>sort<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#"Week_" + str(week) + "_Sum" + str(year) + "_" + str(month) + "_Crawford.tif"</SPAN>
<SPAN class="comment token">#for file in fmaskrasters:</SPAN>
<SPAN class="comment token"># if fnmatch.fnmatch(file, 'Week_' + str(week) + '_Sum' + str(year) + '_' + str(month) + '_Crawford.tif'):</SPAN>
<SPAN class="comment token"># print (file)</SPAN>
fmaskrasters1<SPAN class="operator token">=</SPAN>fmaskrasters<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
weekras1 <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Raster<SPAN class="punctuation token">(</SPAN>fmaskrasters1<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'H:\PRISM_800m_weekly_sum\Crawford\All\output'</SPAN>
normal <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListRasters<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
normal<SPAN class="punctuation token">.</SPAN>sort<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#normalras = arcpy.Raster(normal["Crawford_Normal_Week_" + str(week) + "_1981_2016_" + str(month) + ".tif"])</SPAN>
normalras1<SPAN class="operator token">=</SPAN>normal<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
outcalc1 <SPAN class="operator token">=</SPAN> Minus<SPAN class="punctuation token">(</SPAN>normalras1<SPAN class="punctuation token">,</SPAN> weekras1<SPAN class="punctuation token">)</SPAN>
Final <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>outcalc1<SPAN class="operator token">/</SPAN>normalras1<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">*</SPAN><SPAN class="number token">100</SPAN>
finras1 <SPAN class="operator token">=</SPAN> out <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\"</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"Crawford_Normal_Week_"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>week<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"_Difference"</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"_"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>year<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"_"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>month<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">".tif"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CopyRaster_management<SPAN class="punctuation token">(</SPAN>Final<SPAN class="punctuation token">,</SPAN>finras1 <SPAN class="operator token">+</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><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>