Hi! I have a script with a set of environment properties. I'm having a problem to save my outputs due to Error 999998. After some attempts i have figured out that the problem occurs when I set a mask for the environment (a shapefile). If I remove the mask, then my outputs are saved.
Any idea how can I solve this issue?
The error (RuntimeError: Error 999998: Unexpected Error) happens in line 60 of the following script:
<SPAN class="keyword token">import</SPAN> os
<SPAN class="keyword token">import</SPAN> arcpy
<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="comment token"># Checking spatial analyst extension</SPAN>
<SPAN class="keyword token">if</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CheckExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Spatial'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="string token">'Available'</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>
<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">'Spatial Analyst extension is not available'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Setting paths </SPAN>
inp_ws <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"D:\\"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"master"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"data"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"cb"</SPAN><SPAN class="punctuation token">)</SPAN>
out_ws <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>inp_ws<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"results"</SPAN><SPAN class="punctuation token">)</SPAN>
temp_ws <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>inp_ws<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"temp"</SPAN><SPAN class="punctuation token">)</SPAN>
lu_folder <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>inp_ws<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"lu"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Setting geoprocessing environments</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> inp_ws
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>scratchWorkspace <SPAN class="operator token">=</SPAN> temp_ws
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>rasterStatistics <SPAN class="operator token">=</SPAN> <SPAN class="string token">"STATISTICS 1 1"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN> env<SPAN class="punctuation token">.</SPAN>snapRaster <SPAN class="operator token">=</SPAN> <SPAN class="string token">"soc_default"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>cartographicCoordinateSystem <SPAN class="operator token">=</SPAN> <SPAN class="string token">"PROJCS['South_America_Albers_Equal_Area_Conic',GEOGCS['GCS_South_American_1969',DATUM['D_South_American_1969',SPHEROID['GRS_1967_Truncated',6378160.0,298.25]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-60.0],PARAMETER['Standard_Parallel_1',-5.0],PARAMETER['Standard_Parallel_2',-42.0],PARAMETER['Latitude_Of_Origin',-32.0],UNIT['Meter',1.0]]"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>mask <SPAN class="operator token">=</SPAN> <SPAN class="string token">"BRA_adm0"</SPAN>
<SPAN class="comment token"># Describing rasters file extension</SPAN>
<SPAN class="keyword token">for</SPAN> dirpath<SPAN class="punctuation token">,</SPAN> dirnames<SPAN class="punctuation token">,</SPAN> filenames <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>Walk<SPAN class="punctuation token">(</SPAN>inp_ws<SPAN class="punctuation token">,</SPAN> datatype<SPAN class="operator token">=</SPAN><SPAN class="string token">"RasterDataset"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
dir_lu <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>d <SPAN class="keyword token">for</SPAN> d <SPAN class="keyword token">in</SPAN> dirnames <SPAN class="keyword token">if</SPAN> d <SPAN class="operator token">==</SPAN> <SPAN class="string token">'lu'</SPAN><SPAN class="punctuation token">]</SPAN>
dirnames <SPAN class="operator token">=</SPAN> dir_lu
<SPAN class="keyword token">if</SPAN> dirpath <SPAN class="operator token">==</SPAN> inp_ws <SPAN class="operator token">or</SPAN> dirpath <SPAN class="operator token">==</SPAN> lu_folder<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="string token">"\nThe formats of input rasters in folder {} are:"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>dirpath<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> f <SPAN class="keyword token">in</SPAN> filenames<SPAN class="punctuation token">:</SPAN>
path <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>dirpath<SPAN class="punctuation token">,</SPAN> f<SPAN class="punctuation token">)</SPAN>
ext <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>format
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"< {0} > for {1}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>ext<SPAN class="punctuation token">,</SPAN> f<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Cataloging LU maps in a list</SPAN>
lu_scenarios <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> dirpath<SPAN class="punctuation token">,</SPAN> dirnames<SPAN class="punctuation token">,</SPAN> filenames <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>Walk<SPAN class="punctuation token">(</SPAN>lu_folder<SPAN class="punctuation token">,</SPAN> datatype<SPAN class="operator token">=</SPAN><SPAN class="string token">'RasterDataset'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> f <SPAN class="keyword token">in</SPAN> filenames<SPAN class="punctuation token">:</SPAN>
lu_scenarios<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>dirpath<SPAN class="punctuation token">,</SPAN>f<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Setting input data</SPAN>
climate <SPAN class="operator token">=</SPAN> Raster<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"climate"</SPAN><SPAN class="punctuation token">)</SPAN>
soc_default <SPAN class="operator token">=</SPAN> Raster<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"soc_default"</SPAN><SPAN class="punctuation token">)</SPAN>
codes_soc <SPAN class="operator token">=</SPAN> <SPAN class="string token">"soc_random_test.txt"</SPAN>
codes_bc <SPAN class="operator token">=</SPAN> <SPAN class="string token">"bc_stock.txt"</SPAN>
<SPAN class="keyword token">for</SPAN> <SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">,</SPAN> lu_scenario<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>lu_scenarios<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
lu <SPAN class="operator token">=</SPAN> Raster<SPAN class="punctuation token">(</SPAN>lu_scenario<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># map algebra: combine climate + lu files</SPAN>
clim_lu <SPAN class="operator token">=</SPAN> climate <SPAN class="operator token">+</SPAN> lu
<SPAN class="comment token"># reclass by soc factor </SPAN>
soc_factor <SPAN class="operator token">=</SPAN> ReclassByTable<SPAN class="punctuation token">(</SPAN>clim_lu<SPAN class="punctuation token">,</SPAN> codes_soc<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Code"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Code"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Value"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># map algebra: create soc stock</SPAN>
soc <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>soc_factor<SPAN class="operator token">*</SPAN>soc_default<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">/</SPAN><SPAN class="number token">1000</SPAN> <SPAN class="comment token">#tonne C/ha</SPAN>
<SPAN class="comment token"># reclass by bc stock</SPAN>
bc <SPAN class="operator token">=</SPAN> ReclassByTable<SPAN class="punctuation token">(</SPAN>clim_lu<SPAN class="punctuation token">,</SPAN> codes_bc<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Code"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Code"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Value"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># map algebra: soc stock + bc stock</SPAN>
tc <SPAN class="operator token">=</SPAN> soc <SPAN class="operator token">+</SPAN> bc
<SPAN class="keyword token">if</SPAN> str<SPAN class="punctuation token">(</SPAN>tc<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">!=</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">:</SPAN>
tc_output <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>temp_ws <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\"</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN><SPAN class="string token">"-tc-test"</SPAN><SPAN class="punctuation token">)</SPAN>
tc<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN>tc_output<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> tc<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>Thank you very much!!