Hi,
I'm new to python. I need to generate a new raster C from two rasters A and B by first multiplying a constant value with all elements of raster A and then adding A and B. I need to generate raster C for multiple years so I have it looping over the years I need. However, line 41 below is not giving the correct value after multiplication except on the first iteration through the loop at line 27. Can anyone explain why that might be? Why does this code only work on the first iteration?
EDIT: the code also works without using the Decimal module but still only gives the correct raster on the first iteration
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> numpy
<SPAN class="keyword token">import</SPAN> decimal
<SPAN class="comment token">#inRas = arcpy.Raster("C:/VegModel/CCSSRawData.gdb/vegnorm")</SPAN>
<SPAN class="comment token">#lowerLeft = arcpy.Point(inRas.extent.XMin,inRas.extent.YMin)</SPAN>
<SPAN class="comment token">#cellSize = inRas.meanCellWidth</SPAN>
inRasPETcgcm <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Raster<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"C:/VegModel/CCSSRawData.gdb/petCgcmRS100Clip"</SPAN><SPAN class="punctuation token">)</SPAN>
inRasPETnorm <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Raster<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"C:/VegModel/CCSSRawData.gdb/petNormRS100Clip"</SPAN><SPAN class="punctuation token">)</SPAN>
lowerLeft <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Point<SPAN class="punctuation token">(</SPAN>inRasPETnorm<SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">.</SPAN>XMin<SPAN class="punctuation token">,</SPAN>inRasPETnorm<SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">.</SPAN>YMin<SPAN class="punctuation token">)</SPAN>
cellSize <SPAN class="operator token">=</SPAN> inRasPETnorm<SPAN class="punctuation token">.</SPAN>meanCellWidth
inArrayPETcgcm <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>RasterToNumPyArray<SPAN class="punctuation token">(</SPAN>inRasPETcgcm<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#inArrayPETcgcm = inArrayPETcgcm * 30</SPAN>
inArrayPETnorm <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>RasterToNumPyArray<SPAN class="punctuation token">(</SPAN>inRasPETnorm<SPAN class="punctuation token">)</SPAN>
rasHeight<SPAN class="punctuation token">,</SPAN> rasWidth <SPAN class="operator token">=</SPAN> inArrayPETnorm<SPAN class="punctuation token">.</SPAN>shape
<SPAN class="comment token">#outArrayPET = inArrayPETnorm + inArrayPETcgcm</SPAN>
outArrayPET <SPAN class="operator token">=</SPAN> inArrayPETnorm
decimal<SPAN class="punctuation token">.</SPAN>getcontext<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>prec <SPAN class="operator token">=</SPAN> <SPAN class="number token">20</SPAN>
<SPAN class="keyword token">for</SPAN> x <SPAN class="keyword token">in</SPAN> range <SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</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">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> j <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
value1 <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>inArrayPETcgcm<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">,</SPAN> j<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
value2 <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>inArrayPETnorm<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">,</SPAN> j<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
loopValue <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#print "value1 is: "</SPAN>
<SPAN class="comment token">#print value1</SPAN>
<SPAN class="comment token">#print "value2 is: "</SPAN>
<SPAN class="comment token">#print value2</SPAN>
value1 <SPAN class="operator token">=</SPAN> decimal<SPAN class="punctuation token">.</SPAN>Decimal<SPAN class="punctuation token">(</SPAN>value1<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> decimal<SPAN class="punctuation token">.</SPAN>Decimal<SPAN class="punctuation token">(</SPAN>loopValue<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#print "value1 after multiplication is: "</SPAN>
<SPAN class="comment token">#print value1</SPAN>
value3 <SPAN class="operator token">=</SPAN> value1 <SPAN class="operator token">+</SPAN> decimal<SPAN class="punctuation token">.</SPAN>Decimal<SPAN class="punctuation token">(</SPAN>value2<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#print "value3 is: "</SPAN>
<SPAN class="comment token">#print value3</SPAN>
outArrayPET<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">,</SPAN> j<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> value3
newRasPET <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>NumPyArrayToRaster<SPAN class="punctuation token">(</SPAN>outArrayPET<SPAN class="punctuation token">,</SPAN> lowerLeft<SPAN class="punctuation token">,</SPAN> cellSize<SPAN class="punctuation token">)</SPAN>
newRasPET<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"C:/VegModel/Revise.gdb/pet100ReviseYear%dB"</SPAN> <SPAN class="operator token">%</SPAN>x<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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>