Hi,
I need to calculate the zonal mean (areal precipitation) for grids in a shape file for multiple rasters using a Python script. I have created a script but it gives me an error.
Any help is appreciated to fix the issue. Please see the code below.
<SPAN class="keyword token">import</SPAN> arcpy
<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>
<SPAN class="keyword token">import</SPAN> os
<SPAN class="keyword token">import</SPAN> arcgisscripting<SPAN class="punctuation token">,</SPAN> sys
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"># Set environment settings</SPAN>
env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:/Users/Desktop/KS_All.gdb"</SPAN>
<SPAN class="comment token"># Make a layer from the feature class </SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Public_Land_Survey_System"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"lyr"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Write the selected features to a new featureclass</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CopyFeatures_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Public_Land_Survey_System"</SPAN><SPAN class="punctuation token">,</SPAN> r<SPAN class="string token">"C:/Users/Desktop/KS_All.gdb/PLSS_KS_All_WeeklyPr_PRISM_800m_2016_new"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Set environment settings</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> <SPAN class="string token">"H:/PRISM_800m_Daily"</SPAN>
<SPAN class="comment token"># Set local variables</SPAN>
inPointFeatures <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:/Users/Desktop/KS_All.gdb/PLSS_KS_All_WeeklyPr_PRISM_800m_2016_new.shp"</SPAN>
inRasterList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Week_1_Avg2016_10.tif"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Oct_PCPwk1"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Week_2_Avg2016_10.tif"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Oct_PCPwk2"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Week_3_Avg2016_10.tif"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Oct_PCPwk3"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Week_4_Avg2016_10.tif"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Oct_PCPwk4"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Week_5_Avg2016_10.tif"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Oct_PCPwk5"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Week_1_Avg2016_11.tif"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Nov_PCPwk1"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Week_2_Avg2016_11.tif"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Nov_PCPwk2"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation 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>
zoneField <SPAN class="operator token">=</SPAN> <SPAN class="string token">"S_R_T"</SPAN>
outZonalStatistics <SPAN class="operator token">=</SPAN> ZonalStatistics<SPAN class="punctuation token">(</SPAN>inPointFeatures<SPAN class="punctuation token">,</SPAN> zoneField<SPAN class="punctuation token">,</SPAN> inRasterList<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>
<SPAN class="comment token"># Save the output </SPAN>
outZonalStatistics<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"C:/Users/Desktop/KS_All.gdb/zonalstattblout"</SPAN><SPAN class="punctuation token">)</SPAN>
fieldName <SPAN class="operator token">=</SPAN> <SPAN class="string token">"YEAR"</SPAN>
expression <SPAN class="operator token">=</SPAN> <SPAN class="string token">"getClass(!YEAR!)"</SPAN>
codeblock <SPAN class="operator token">=</SPAN> <SPAN class="string token">"""def getClass(YEAR):
if YEAR >= 1998:
return 2016
else:
return -9999"""</SPAN>
<SPAN class="comment token"># Execute CalculateField </SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CalculateField_management<SPAN class="punctuation token">(</SPAN>inPointFeatures<SPAN class="punctuation token">,</SPAN> fieldName<SPAN class="punctuation token">,</SPAN> expression<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"PYTHON_9.3"</SPAN><SPAN class="punctuation token">,</SPAN> codeblock<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>