1

I'm new to arcpy and am having trouble running CopyRaster. Do I need a separate for loop for this process like I have below?

import os,arcpy, glob  


filepath = r"C:\VMshared\small_example_valley2\snowdepthtest"   

outFolder = r"C:\VMshared\small_example_valley2\snowdepthout"  


ascList = glob.glob(filepath + "/*.asc")  

print ascList  

rasterpath = r"C:\VMshared\small_example_valley2\snowdepthout"

LastOutFolder = r"C:\VMshared\small_example_valley2\snowrast"

rasterList = glob.glob(rasterpath + "/*.tif")
print rasterList


for ascFile in ascList:  

    outRaster = outFolder + "/" + os.path.split(ascFile)[1][:-3] + "tif"  
    print outRaster  

    arcpy.ASCIIToRaster_conversion(ascFile, outRaster, "FLOAT")



for rasterFile in rasterList:   

    LastOutRaster = LastOutFolder + "/" + os.path.split(rasterFile)[1][:-3] + "tif"
    arcpy.CopyRaster_management(r"C:\VMshared\small_example_valley2\snowdepthout",r"C:\VMshared\small_example_valley2\snowrast","DEFAULTS","","","NONE","NONE","16_BIT_UNSIGNED")

Edit:

After modifying the script, I get the following error:

ERROR 000876: Output raster: C:\VMshared\small_example_valley2\snowdepthtest\snowdepthN0001.asc's extension is invalid for the output raster format.

import os,arcpy, glob

filepath = r"C:\VMshared\small_example_valley2\snowdepthtest"
outFolder = r"C:\VMshared\small_example_valley2\snowdepthout"
ascList = glob.glob(filepath + "/*.asc")

rasterpath = r"C:\VMshared\small_example_valley2\snowdepthout"

OutFolderTwo = r"C:\VMshared\small_example_valley2\snowrast"

rasterList = glob.glob(rasterpath + "/*.asc")
print rasterList

for ascFile in ascList:
    outRaster = os.path.join(outFolder, ascFile)
    arcpy.ASCIIToRaster_conversion(ascFile, outRaster, "FLOAT")

rasterList = glob.glob(outFolder + "/*.tif")

for rasterFile in rasterList:

    OutRasterTwo = os.path.join(OutFolderTwo, rasterFile)
    arcpy.CopyRaster_management(rasterFile, OutRasterTwo, "DEFAULTS","","","NONE","NONE","16_BIT_UNSIGNED")
6
  • What error are are you receiving?
    – Aaron
    Jan 12, 2015 at 20:47
  • I don't get an error, but the process stops after batch ascIItoRaster, and does not execute CopyRaster. Also, I'm not sure what the lines beginning with 'outRaster' and LastOutRaster' are doing being new to arcpy.
    – AF2k15
    Jan 12, 2015 at 20:49
  • CopyRaster syntax is : arcpy.CopyRaster_management(INPUT, OUTPUT). Don't you want to make the INPUT = LastOutRaster? Otherwise, you're just copying "snowdepthout" x times. You don't make use of LastOutRaster as it is.
    – phloem
    Jan 12, 2015 at 20:50
  • @phloem I replaced the directory paths in CopyRaster_management with 'rasterpath' and 'LastOutFolder' respectively, still I cannot execute copyraster.
    – AF2k15
    Jan 12, 2015 at 21:01
  • The answer to your main question is, no, you do not need a separate loop, in fact it would be easier to reuse the variable outRaster in your call to CopyRaster (e.g. CopyRaster(outRaster, newRaster)).
    – phloem
    Jan 12, 2015 at 21:08

1 Answer 1

1

This is bordering on a shot-in-the-dark, but try the following changes. Essentially, your parameters were off and you need to generate a list of TIFFs after they are created, not before. Finally, I assume you are using Copy Raster to convert to 16 bit unsigned, otherwise, you can drop this command entirely.

import os,arcpy, glob

filepath = r"C:\VMshared\small_example_valley2\snowdepthtest"
outFolder = r"C:\VMshared\small_example_valley2\snowdepthout"
ascList = glob.glob(filepath + "/*.asc")

rasterpath = r"C:\VMshared\small_example_valley2\snowdepthout"

LastOutFolder = r"C:\VMshared\small_example_valley2\snowrast"

for ascFile in ascList:
    outRaster = os.path.join(outFolder, os.path.basename(ascFile).split(".")[0] + "tif")
    arcpy.ASCIIToRaster_conversion(ascFile, outRaster, "FLOAT")

rasterList = glob.glob(outFolder + "/*.tif")

for rasterFile in rasterList:
    LastOutRaster = os.path.join(LastOutFolder, rasterFile + "tif")
    arcpy.CopyRaster_management(rasterFile, LastOutRaster, "DEFAULTS","","","NONE","NONE","16_BIT_UNSIGNED")
2
  • Thanks Aaron, yes I need to convert to 16 bit unsigned to I can go on to create color maps. I follow your code and it seems to make sense, however, I get: ERROR 000876: Output raster: C:\VMshared\small_example_valley2\snowdepthtest\snowdepthN0001.asctif's extension is invalid for the output raster format.
    – AF2k15
    Jan 12, 2015 at 21:42
  • I think glob.glob() generates the entire path and file name with the extension, so try extracting just the basename and change the extension (note. script updated to reflect this).
    – Aaron
    Jan 12, 2015 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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