1

My question is similar to the Cell Statistics gives Wrong Answer question. In Python, I'm using the MINIMUM parameter to execute the cell statistics tool on several reclassified rasters ('0's and '1's) and I'm not getting accurate results. However, when I use the tool in ArcMap I get what I'm looking for. It seems as if it's only using two rasters (don't know which ones). Here's the code I'm using.

arcpy.env.workspace = ("F:\\hdf\\hdf_test\\reclass")
outh10v03 = ("F:\\hdf\\hdf_test\\h10v03\\")

h10v03 = arcpy.ListRasters("*2000*h10v03*", "tif")

for reclass in h10v03:
    print reclass

arcpy.gp.CellStatistics_sa(h10v03, outh10v03 + "h10v03_2000" + ".tif", "MINIMUM", "DATA")

Any ideas as to why it's not using all rasters (I believe that's the case)?

8
  • Can you format your code using the {} button? At the moment, there is no indentation in your for loop. I don't know if that is due to formatting or your code.
    – djq
    Commented Dec 13, 2011 at 1:17
  • Is the Cell Statistics function on the full list? i.e h10v03? If that's the case, changing the Cell Stats argument might help. Also, when I formatted the code I assumed the call was outside the loop - just cofirming.
    – om_henners
    Commented Dec 13, 2011 at 1:35
  • Thanks for editing the code, I'm still learning how format code on this site. By the full list do you mean are all the rasters that have the strings "2000" and "h10v03" in the file name being used in the tool? I believe so as the print reclass statement prints out all the files that need to be used in the tool. Also, I'm not indenting the for loop (using the ESRI format) and I tried calling the tool inside and outside of the loop, but I get the same results.
    – dchaboya
    Commented Dec 13, 2011 at 4:43
  • 3
    No worries. In this case I think it may be a case of putting the wrong variable inside the Cell Stats argument. Try using arcpy.gp.CellStatistics_sa(h10v03, outh10v03 + "h10v03_2000" + ".tif", "MINIMUM", "DATA") outside the for loop instead. If it's stil not working then we can try a different tack.
    – om_henners
    Commented Dec 13, 2011 at 4:52
  • 1
    As @om_henners notes above in his bit of code, it looks like your code is using the variable reclass which, by the time it loops through your raster list, will be only the last raster. Make sure you're using the full list variable h10v03. Commented Dec 13, 2011 at 14:29

1 Answer 1

1

I'm not certain if the problem is due to indentation in your code, or whether it is due to formatting on the site:

for reclass in h10v03:
    print reclass

arcpy.gp.CellStatistics_sa(reclass, outh10v03 + "h10v03_2000" + ".tif", "MINIMUM", "DATA") 
# only the last line is run once

In this case it should loop through all of the tifs in the folder:

for reclass in h10v03:
    print reclass    
    arcpy.gp.CellStatistics_sa(reclass, outh10v03 + "h10v03_2000" + ".tif", "MINIMUM", "DATA")
    # here the line arcpy.gp.CellStatistics_sa ... is run for every raster in h10v03
1
  • You are correct in that arcpy.gp.Cellstatistics_sa(reclass, outh10v03 + "h10v03_2000" + ".tif", "MINIMUM", "DATA") should be within the for` loop. However, I was using the wrong list variable. the appropriate variable (the use of all rasters in the list) was h10v03 and not reclass. Thanks for your response.
    – dchaboya
    Commented Dec 13, 2011 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.