Hi,
I want to loop the below process for multiple years so tested with one year. I am getting an error when it tries to change the (YEAR) as calculate field as a number variable with the corresponding year. Cannot understand what I am doing wrong here. Everything else up to line 29 is working. Problem is inside the code block section.
Any help is greatly appreciated.
Thanks in advance.
ExecuteError<SPAN class="punctuation token">:</SPAN> ERROR <SPAN class="number token">000539</SPAN><SPAN class="punctuation token">:</SPAN> Error running expression<SPAN class="punctuation token">:</SPAN> getClass<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1998.0</SPAN><SPAN class="punctuation token">)</SPAN>
Traceback <SPAN class="punctuation token">(</SPAN>most recent call last<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
File <SPAN class="string token">"<expression>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> <SPAN class="operator token"><</SPAN>module<SPAN class="operator token">></SPAN>
File <SPAN class="string token">"<string>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> getClass
NameError<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">global</SPAN> name <SPAN class="string token">'yr'</SPAN> <SPAN class="keyword token">is</SPAN> <SPAN class="operator token">not</SPAN> defined
Failed to execute <SPAN class="punctuation token">(</SPAN>CalculateField<SPAN class="punctuation 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>
years <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">1997</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># Write the selected features to a new featureclass</SPAN>
<SPAN class="keyword token">for</SPAN> year <SPAN class="keyword token">in</SPAN> years<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CopyFeatures_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"PLSS_KS_All_GCS"</SPAN><SPAN class="punctuation token">,</SPAN> r<SPAN class="string token">"C:/Users/Desktop/Flood/KS_All.gdb/PLSS_KS_All_WklyPrSum_PRISM_800m_"</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">"_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_weekly_sum_tif"</SPAN>
<SPAN class="comment token"># Set local variables</SPAN>
inPointFeatures <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:/Users/Desktop/Flood/KS_All.gdb/PLSS_KS_All_WklyPrSum_PRISM_800m_"</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">"_new.shp"</SPAN>
<SPAN class="comment token"># Set local variables</SPAN>
inRasterList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Week_1_Sum"</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">"_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_Sum"</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">"_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_Sum"</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">"_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_Sum"</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">"_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_Sum"</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">"_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_Sum"</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">"_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_Sum"</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">"_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>
<SPAN class="comment token"># Check out the ArcGIS Spatial Analyst extension license</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CheckOutExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Spatial"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Execute ExtractValuesToPoints </SPAN>
ExtractMultiValuesToPoints<SPAN class="punctuation token">(</SPAN>inPointFeatures<SPAN class="punctuation token">,</SPAN> inRasterList<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"BILINEAR"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#NONE</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):
global yr
yr=1997
if YEAR >= 1998:
return yr
else:
return -9999"""</SPAN>
<SPAN class="comment token"># Execute AddField</SPAN>
<SPAN class="comment token">#arcpy.AddField_management(inTable, fieldName, "SHORT")</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>