curtvprice

Scratch Folder, Scratch GDB for ArcGIS 9.x and 10.0

Discussion created by curtvprice Champion on Mar 29, 2013
I ran into an issue that was solved by the new arcpy.env.scratchFolder functionality to force working in a folder.

Unfortunately I was using 10.0, not 10.1. So, I created some Python functions that support this functionality in 9.x and 10.0. I ran into an issue when the scratch workspace is an invalid path -- so this is a fix for 10.1 as well.

Any comments, fixes, or suggestions are welcomed.

ModelBuilder users could use these functions in a Calculate Value code block to generate a "safe" scratch folder in their model tools.

def ScratchFolder():
    """env.scratchFolder for all versions of ArcGIS geoprocessing
    1) If supported, return arcpy.env.scratchFolder (ArcGIS 10.1 or later)
    2) Return a value based on ScratchWorkspace (ArcGIS 9.3 - 10.0)
        a) Folder: scratchWorkspace
        b) GDB: scratchWorkspace/../scratch
        c) Not set or invalid: TEMP/scratch

    Curtis Price - U.S. Geological Survey - cprice@usgs.gov
    http://www.usgs.gov - science for a changing world
    """
    import os
    import arcgisscripting
    gp = arcgisscripting.create()
    try:
        sw = gp.scratchWorkspace
        if sw:
            # check for invalid scratchWorkspace path
            if not gp.Exists(sw):
                gp.AddIDMessage("Warning", 873, "Invalid Scratch Workspace", sw)
                raise
        sw = gp.scratchFolder
    except:
        try:
            sw = gp.scratchWorkspace
            try:
                swType = gp.Describe(sw).dataType
                if swType == "Folder":
                    pass
                elif swType == "Workspace":
                    pth = os.path.dirname(sw)
                    sw = os.path.join(pth, "scratch")
                else:
                    raise
            except:
                sw = os.path.join(os.environ["TEMP"], "scratch")
        finally:
            sw = os.path.realpath(sw)
            if not gp.Exists(sw): os.mkdir(sw)
    return sw


def ScratchGDB():
    """env.scratchGDB for all versions of ArcGIS geoprocessing

    1) If supported, return arcpy.env.scratchGDB (ArcGIS 10.1 or later)
    2) Return a value based on ScratchWorkspace (ArcGIS 9.3 - 10.0)
        a) GDB: scratchWorkspace
        b) Folder: scratchWorkspace/scratch.gdb
        c) Not set or invalid: TEMP\scratch.gdb

    Curtis Price - U.S. Geological Survey - cprice@usgs.gov
    http://www.usgs.gov - science for a changing world
    """
    import os
    import arcgisscripting
    gp = arcgisscripting.create()
    try:
        sw = gp.scratchWorkspace
        if sw:
            # check for invalid scratchWorkspace path
            if not gp.Exists(sw):
                gp.AddIDMessage("Warning", 873, "Invalid Scratch Workspace", sw)
               raise
        sw = gp.scratchFolder
    except:
        try:
            sw = gp.scratchWorkspace
            swType = gp.Describe(sw).dataType
            if swType == "Workspace":
                pass
            elif swType == "Folder":
                sw = os.path.join(sw, "scratch.gdb")
        except:
            sw = os.path.join(os.environ["TEMP"], "scratch.gdb")
        finally:
            sw = os.path.realpath(sw)
            if not gp.Exists(sw):
                gp.CreateFileGDB_management(os.path.dirname(sw),
                                               os.path.basename(sw))
    return sw

Outcomes