arcpy.AddMessage(
            str(
                time.strftime("Reclassify: %m/%d/%Y  %H:%M:%S",
                              time.localtime())))
    arcpy.AddMessage("Reclassifying slope...")
    reclass = sa.Reclassify(scratchSlope, "VALUE", remap, missing_values)
    reclass.save(reclassSlope)
    deleteme.append(reclassSlope)
    #clean edges
    boundaryClean = os.path.join(scratch, "boundaryClean")
    if debug == True:
        arcpy.AddMessage(
            str(
                time.strftime("BoundaryClean: %m/%d/%Y  %H:%M:%S",
                              time.localtime())))
    clean = sa.BoundaryClean(reclassSlope, "NO_SORT", "TWO_WAY")
    clean.save(boundaryClean)
    deleteme.append(boundaryClean)

    # Convert reclassified slope ranges to polygon features
    slopePoly = os.path.join(scratch, "slopePoly")
    if debug == True:
        arcpy.AddMessage(
            str(
                time.strftime("RasterToPolygon: %m/%d/%Y  %H:%M:%S",
                              time.localtime())))
    arcpy.RasterToPolygon_conversion(boundaryClean, slopePoly, "NO_SIMPLIFY",
                                     "VALUE")
    arcpy.AddField_management(slopePoly, "SlopeCat", "SHORT")
    arcpy.CalculateField_management(slopePoly, "SlopeCat", "!gridcode!",
                                    "PYTHON_9.3")
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True

# Input variables
HabRaster = sys.argv[1]
CleanBoundary = sys.argv[2]
MinPatchSize = sys.argv[3]

# Output variables
PatchRaster = sys.argv[4]

# Boundary clean the patches, if asked
if CleanBoundary == 'true':
    arcpy.AddMessage("Cleaning patch boundaries")
    HabRaster2 = sa.BoundaryClean(HabRaster, "NO_SORT", "TWO_WAY")
else:
    HabRaster2 = HabRaster

# RegionGroup the HabSource into Patches
arcpy.AddMessage("Finding habitat clusters")
AllPatches = sa.RegionGroup(HabRaster2, "EIGHT", "WITHIN", "NO_LINK")

# Convert minimum patch size HA to cells (based on cell size of HabRaster)
arcpy.AddMessage("Converting minimum size to cells")
cellSizeResult = arcpy.GetRasterProperties_management(HabRaster, "CELLSIZEX")
cellSize = float(cellSizeResult.getOutput(0))
minCellSize = round(10000.0 * float(MinPatchSize) / (cellSize**2))

# Remove patches below the cell size
arcpy.AddMessage("Removing patches smaller than %s HA (%d cells)" %