I am trying to calculate mean of selected raster chosen from multiple raster. I have multiple raster file in different month folder, i am trying to select only two raster (e.g For JANUARY : 001_Max_Temper.tif, 001_Min_Temper.tif) from different month folder (JANUARY, FEBRUARY....DECEMBER) and calculate the mean of those selected raster and save it as on same month folder (e.g output name, JANUARY: 001_Mean_Temp).
I have written a code to do this task but i am getting error massage while i am running this code. Below i have attached my code.
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os<SPAN class="punctuation token">,</SPAN> calendar
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env
<SPAN class="keyword token">from</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>sa <SPAN class="keyword token">import</SPAN> <SPAN class="operator token">*</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CheckOutExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Spatial"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>parallelProcessingFactor <SPAN class="operator token">=</SPAN> <SPAN class="string token">"100%"</SPAN>
topWorkspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'D:\SWAT-WEATHER-DATA2'</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> topWorkspace
<SPAN class="comment token"># Get dict of months and month-number (i.e. January = 001, March = 003 etc.)</SPAN>
months <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN>calendar<SPAN class="punctuation token">.</SPAN>month_name<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>upper<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> str<SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>zfill<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> i <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">13</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="comment token"># Get dict of months and month number (i.e. January = 001, March = 003 etc.)</SPAN>
<SPAN class="comment token"># Step through list of all folders</SPAN>
<SPAN class="keyword token">for</SPAN> folderPath <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListWorkspaces<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
baseName <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>basename<SPAN class="punctuation token">(</SPAN>folderPath<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>upper<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> baseName <SPAN class="keyword token">in</SPAN> months<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># Test that subfolder is a month name</SPAN>
monthNumber <SPAN class="operator token">=</SPAN> months<SPAN class="punctuation token">[</SPAN>baseName<SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># Get month-number for use in output filename</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> folderPath
rasterList <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListRasters<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'*Max_Temper.tif'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'*Min_Temper.tif'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Execute CellStatistics</SPAN>
outCellStatistics <SPAN class="operator token">=</SPAN> CellStatistics<SPAN class="punctuation token">(</SPAN>rasterList<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"MEAN"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NODATA"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Save the output</SPAN>
outRasterName <SPAN class="operator token">=</SPAN> outCellStatistics<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Mean_Temp_{}.tif"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>monthNumber<SPAN class="punctuation token">)</SPAN>
outCellStatistics<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN>outRasterName<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#print done</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">'done'</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>