import arcpy
from arcpy import env
from arcpy.sa import *
import os
import time
import traceback
import sys
import tempfile
import math
import time

def getArcGISVer():
    d = arcpy.GetInstallInfo('desktop')
    ver = d['Version']
    return ver

def getPolyfromExt(ext):
    #d = arcpy.GetInstallInfo('desktop')
    #ver = d['Version']
    #if ver == "10.3":
    poly = ext.polygon
   # else:
    #    pts = arcpy.Array()
    #    pts.add(ext.lowerLeft)
    #    pts.add(ext.upperLeft)
    #    pts.add(ext.upperRight)
    #    pts.add(ext.lowerRight)
    #    pts.add(ext.lowerLeft)
    #    poly = arcpy.Polygon(pts,ext.spatialReference)
    #    pts.removeAll()
    
    return poly

def areaCalc(inRaster,prefixName,imgExt):
    #This calculates a raster with cells having area values tied to cells for ranking
    sizeRas = Lookup(inRaster,"Count")
    sizeRas.save("xxlg_"+prefixName+"_sizeRas"+imgExt)
    #makes it an integer for allocation with variation and sets sizee of one cell equal to 1
    sizeValue = Plus(Int(Times(Ln("xxlg_"+prefixName+"_sizeRas"+imgExt),10)),1)    
    return sizeValue


def SimpleDist(inRaster,prefixName,zi_dist,imgExt):
    distFrom = EucDistance(inRaster,zi_dist)
    distFrom.save("xxlg_"+prefixName+"_dist"+imgExt)
    distFromGTzero = Con("xxlg_"+prefixName+"_dist"+imgExt,"xxlg_"+prefixName+"_dist"+imgExt,"","Value > 0")
    distFromGTzero.save("xxlg_"+prefixName+"_distGTzero"+imgExt)
    invsDist = Power("xxlg_"+prefixName+"_distGTzero"+imgExt,-0.5)
    return invsDist

def SimpleDistAbrupt(inRaster,prefixName,zi_dist,imgExt): 
    #Output 0 to 1 with 1 being closest
    distFrom = EucDistance(inRaster,zi_dist)
    distFrom.save("xxlg_"+prefixName+"_dist"+imgExt)
    distFromGTzero = Con("xxlg_"+prefixName+"_dist"+imgExt,"xxlg_"+prefixName+"_dist"+imgExt,"","Value > 0")
    distFromGTzero.save("xxlg_"+prefixName+"_distGTzero"+imgExt)
    invsDist = Power("xxlg_"+prefixName+"_distGTzero"+imgExt,-0.5)
    invDist.save("xxlg_"+prefixName+"_invsDist"+imgExt)
    r = Raster("xxlg_"+prefixName+"_invsDist"+imgExt)
    max = r.maximum
    min = r.minimum
    range = max - min
    outRaster = Divide(Minus("xxlg_"+prefixName+"_invsDist"+imgExt,min),range)
    return outRaster
   
def SimpleDistLinear(inRaster,prefixName,zi_dist,imgExt):
    #Output 0 to 1 with 1 being closest
    distFrom = EucDistance(inRaster,zi_dist)
    distFrom.save("xxlg_"+prefixName+"_dist"+imgExt)
    distFromGTzero = Con("xxlg_"+prefixName+"_dist"+imgExt,"xxlg_"+prefixName+"_dist"+imgExt,"","Value > 0")
    distFromGTzero.save("xxlg_"+prefixName+"_distGTzero"+imgExt)
    invsDist = Minus(1,Divide("xxlg_"+prefixName+"_distGTzero"+imgExt,zi_dist))
    invDist.save("xxlg_"+prefixName+"_invsDist"+imgExt)
    r = Raster("xxlg_"+prefixName+"_invsDist"+imgExt)
    max = r.maximum
    min = r.minimum
    range = max - min
    outRaster = Divide(Minus("xxlg_"+prefixName+"_invsDist"+imgExt,min),range)
    return outRaster  

def SimpleDistExp(inRaster,prefixName,zi_dist,imgExt):
    #Gradual curve inbetween Abrupt and Linear
    #Output 0 to 1 with 1 being closest
    distFrom = EucDistance(inRaster,zi_dist)
    distFrom.save("xxlg_"+prefixName+"_dist"+imgExt)
    distFromGTzero = Con("xxlg_"+prefixName+"_dist"+imgExt,"xxlg_"+prefixName+"_dist"+imgExt,"","Value > 0")
    distFromGTzero.save("xxlg_"+prefixName+"_distGTzero"+imgExt)
    invsDist = Exp(Divide(Times(-2.99,"xxlg_"+prefixName+"_distGTzero"+imgExt),zi_dist))
    invDist.save("xxlg_"+prefixName+"_invsDist"+imgExt)
    r = Raster("xxlg_"+prefixName+"_invsDist"+imgExt)
    max = r.maximum
    min = r.minimum
    range = max = min
    outRaster = Divide(Minus("xxlg_"+prefixName+"_invsDist"+imgExt,min),range)
    return outRaster      

def PropRem(inRaster,rems,prefixName,imgExt):
    ZonalStats = ZonalStatistics(rems,"Value",inRaster,"SUM","DATA")
    ZonalStats.save("xxlg_"+prefixName+"ZonalStats"+imgExt)
    propRem = RoundUp(Times(Divide("xxlg_"+prefixName+"ZonalStats"+imgExt,"xxlg_remSizeInCells"+imgExt),10))
    return propRem
    
def newFocalDist(inRaster,prefixName,imgExt):
     #For prioritizing close inlets
     maxList =[]
     binaryRas = Con(IsNull(inRaster),0,1)
     binaryRas.save("xxlg_"+prefixName+"_binary"+imgExt)
     neighborhood =  NbrCircle(100,"MAP")
     outFocalStats = FocalStatistics("xxlg_"+prefixName+"_binary"+imgExt,neighborhood, "MEAN")
     outFocalStats.save("xxlg_"+prefixName+"_outFocalStats"+imgExt)
     maxList.append("xxlg_"+prefixName+"_outFocalStats"+imgExt)
     
     outFS2 = FocalStatistics("xxlg_"+prefixName+"_outFocalStats"+imgExt,neighborhood, "MEAN")
     outFS2.save(prefixName+"fs2"+imgExt)
     maxList.append(prefixName+"fs2"+imgExt)

     outFS3 = FocalStatistics(prefixName+"fs2"+imgExt,neighborhood, "MEAN")
     outFS3.save(prefixName+"fs3"+imgExt)
     maxList.append(prefixName+"fs3"+imgExt)     
 
     outFS4 = FocalStatistics(prefixName+"fs3"+imgExt,neighborhood, "MEAN")
     outFS4.save(prefixName+"fs4"+imgExt)
     maxList.append(prefixName+"fs4"+imgExt)    
     
     outFS5 = FocalStatistics(prefixName+"fs4"+imgExt,neighborhood, "MEAN")
     outFS5.save(prefixName+"fs5"+imgExt)
     maxList.append(prefixName+"fs5"+imgExt)    
     
     allMax = CellStatistics(maxList,"MAXIMUM","DATA")
     allMax.save(prefixName+"MaxFS"+imgExt)
     
     #Select only non-remnant values
     conOnlyOutside = Con("xxlg_"+prefixName+"_binary"+imgExt,prefixName+"MaxFS"+imgExt,"","Value = 0")
     conOnlyOutside.save("xxlg_"+prefixName+"_onlyOut"+imgExt)
     #Remove 0
     fd = Con("xxlg_"+prefixName+"_onlyOut"+imgExt,"xxlg_"+prefixName+"_onlyOut"+imgExt,"","Value > 0")
     return fd  
    
def FocalDist(inRaster,prefixName,imgExt):
     #For prioritizing close inlets
     binaryRas = Con(IsNull(inRaster),0,1)
     binaryRas.save("xxlg_"+prefixName+"_binary"+imgExt)
     neighborhood =  NbrCircle(500,"MAP")
     outFocalStats = FocalStatistics("xxlg_"+prefixName+"_binary"+imgExt,neighborhood, "MEAN")
     outFocalStats.save("xxlg_"+prefixName+"_outFocalStats"+imgExt)
     conOnlyOutside = Con("xxlg_"+prefixName+"_binary"+imgExt,"xxlg_"+prefixName+"_outFocalStats"+imgExt,"","Value = 0")
     conOnlyOutside.save("xxlg_"+prefixName+"_onlyOut"+imgExt)
     fd = Con("xxlg_"+prefixName+"_onlyOut"+imgExt,"xxlg_"+prefixName+"_onlyOut"+imgExt,"","Value > 0")
     #fd.save("xxlg_"+prefixName+"_fDist"+imgExt)
     #r = Raster("xxlg_"+prefixName+"_fDist"+imgExt)
     #max = r.maximum
     #min = r.minimum
     #range = max - min
     #outRaster = Divide(Minus("xxlg_"+prefixName+"_fDist"+imgExt,min),range)
     return fd     

def SimpleDistLegal(inRaster,prefixName,zi_dist,imgExt):
    #Gradual curve inbetween Abrupt and Linear
    #Output 0 to 1 with 1 being closest
    distFrom = EucDistance(inRaster,zi_dist)
    distFrom.save("xxlg_"+prefixName+"_dist"+imgExt)
    distFromGTzero = Con("xxlg_"+prefixName+"_dist"+imgExt,"xxlg_"+prefixName+"_dist"+imgExt,"","Value > 0")
    distFromGTzero.save("xxlg_"+prefixName+"_distGTzero"+imgExt)
    invsDist = Exp(Divide(Times(-2.99,"xxlg_"+prefixName+"_distGTzero"+imgExt),zi_dist))
    invsDist.save("xxlg_"+prefixName+"_invsDist"+imgExt)
    allVals = CellStatistics(["xxlg_"+prefixName+"_invsDist"+imgExt,inRaster],"MAXIMUM","DATA")
    allVals.save("xxlg_"+prefixName+"_sdAllVals"+imgExt)   
    allValsInt = Int(Times("xxlg_"+prefixName+"_sdAllVals"+imgExt,100))
    allValsInt.save("xxlg_"+prefixName+"_sdAllValsInt"+imgExt)
    SoothingNbr = NbrCircle(500,"MAP")
    fStatMean = FocalStatistics("xxlg_"+prefixName+"_sdAllValsInt"+imgExt,SoothingNbr,"MEAN")
    fStatMean.save("xxlg_"+prefixName+"_sdAllFs"+imgExt)
    finalVals = Times(Con("xxlg_"+prefixName+"_distGTzero"+imgExt,1,"","Value > 0"),"xxlg_"+prefixName+"_sdAllFs"+imgExt)
    finalVals.save("xxlg_"+prefixName+"_finalVals"+imgExt)    
    return finalVals 
     
def WeightedDistLegal(inRaster,prefixName,zi_dist,imgExt):
    dist = "xxlg_"+prefixName+"_dist"+imgExt
    allocat = EucAllocation(inRaster,zi_dist,"#","#","#",dist) #Currently only in meters
    allocat.save("xxlg_"+prefixName+"_allocat"+imgExt)
    halfDist = zi_dist/2

    neighborhood =  NbrCircle(halfDist, "MAP")
    focalStatMax = FocalStatistics(inRaster,neighborhood, "MAXIMUM")
    focalStatMax.save("xxlg_"+prefixName+"_fsMax"+imgExt)
    meanCS = CellStatistics(["xxlg_"+prefixName+"_fsMax"+imgExt,"xxlg_"+prefixName+"_allocat"+imgExt],"MEAN","DATA")
    meanCS.save("xxlg_"+prefixName+"_meanCS"+imgExt)
    SoothingNbr = NbrCircle(500,"MAP")

    focalStatMean = FocalStatistics("xxlg_"+prefixName+"_meanCS"+imgExt,SoothingNbr,"MEAN")
    focalStatMean.save("xxlg_"+prefixName+"_meanFS"+imgExt)
    distFromGTzero = Con("xxlg_"+prefixName+"_dist"+imgExt,"xxlg_"+prefixName+"_dist"+imgExt,"","Value > 0")
    distFromGTzero.save("xxlg_"+prefixName+"_distGTzero"+imgExt)
    #invDist = Minus(1,Divide("xxlg_"+prefixName+"_distGTzero"+imgExt,zi_dist))   
    invDist = Exp(Divide(Times(-2.99,"xxlg_"+prefixName+"_distGTzero"+imgExt),zi_dist))
    invDist.save("xxlg_"+prefixName+"_invDist"+imgExt)
    wdDist = Times("xxlg_"+prefixName+"_meanFS"+imgExt,"xxlg_"+prefixName+"_invDist"+imgExt)
    wdDist.save("xxlg_"+prefixName+"_wdDist"+imgExt)
    allVals = CellStatistics(["xxlg_"+prefixName+"_wdDist"+imgExt,inRaster],"MAXIMUM","DATA")
    allVals.save("xxlg_"+prefixName+"_wdAllVals"+imgExt)
    allValsInt = Int(Times("xxlg_"+prefixName+"_wdAllVals"+imgExt,100))
    allValsInt.save("xxlg_"+prefixName+"_wdAllValsInt"+imgExt)
    fStatMean = FocalStatistics("xxlg_"+prefixName+"_wdAllValsInt"+imgExt,SoothingNbr,"MEAN")
    fStatMean.save("xxlg_"+prefixName+"_wdAllFs"+imgExt)
    finalVals = Times(Con("xxlg_"+prefixName+"_distGTzero"+imgExt,1,"","Value > 0"),"xxlg_"+prefixName+"_wdAllFs"+imgExt)
    #finalVals.save("xxlg_"+prefixName+"_finalVals"+imgExt)
    #r = Raster("xxlg_"+prefixName+"_finalVals"+imgExt)
    #max = r.maximum
    #min = r.minimum
    #range = max - min
    #produces values from 0-1
    #outRaster = Divide(Minus("xxlg_"+prefixName+"_finalVals"+imgExt,min),range)
    return finalVals    

def WeightedDistVeg(inRaster,prefixName,zi_dist,imgExt):
    dist = "xxlg_"+prefixName+"_dist"+imgExt
    allocat = EucAllocation(inRaster,zi_dist,"#","#","#",dist) #Currently only in meters
    allocat.save("xxlg_"+prefixName+"_allocat"+imgExt)
    halfDist = zi_dist/2

    neighborhood =  NbrCircle(halfDist, "MAP")
    focalStatMax = FocalStatistics(inRaster,neighborhood, "MAXIMUM")
    focalStatMax.save("xxlg_"+prefixName+"_fsMax"+imgExt)
    meanCS = CellStatistics(["xxlg_"+prefixName+"_fsMax"+imgExt,"xxlg_"+prefixName+"_allocat"+imgExt],"MEAN","DATA")
    meanCS.save("xxlg_"+prefixName+"_meanCS"+imgExt)
    SoothingNbr = NbrCircle(500,"MAP")

    focalStatMean = FocalStatistics("xxlg_"+prefixName+"_meanCS"+imgExt,SoothingNbr,"MEAN")
    focalStatMean.save("xxlg_"+prefixName+"_meanFS"+imgExt)
    distFromGTzero = Con("xxlg_"+prefixName+"_dist"+imgExt,"xxlg_"+prefixName+"_dist"+imgExt,"","Value > 0")
    distFromGTzero.save("xxlg_"+prefixName+"_distGTzero"+imgExt)
    #invDist = Minus(1,Divide("xxlg_"+prefixName+"_distGTzero"+imgExt,zi_dist))   
    #invDist = Exp(Divide(Times(-2.99,"xxlg_"+prefixName+"_distGTzero"+imgExt),zi_dist))
    invDist = Power("xxlg_"+prefixName+"_distGTzero"+imgExt,-0.5)
    invDist.save("xxlg_"+prefixName+"_invDist"+imgExt)
    rDist = Raster("xxlg_"+prefixName+"_invDist"+imgExt)
    maxDist = rDist.maximum
    minDist = rDist.minimum
    rangeDist = maxDist - minDist 
    zeroToOne = Divide(Minus("xxlg_"+prefixName+"_invDist"+imgExt,minDist),rangeDist)
    zeroToOne.save("xxlg_"+prefixName+"_zeroToOne"+imgExt)
    wdDist = Times("xxlg_"+prefixName+"_meanFS"+imgExt,"xxlg_"+prefixName+"_zeroToOne"+imgExt)
    wdDist.save("xxlg_"+prefixName+"_wdDist"+imgExt)
    allVals = CellStatistics(["xxlg_"+prefixName+"_wdDist"+imgExt,inRaster],"MAXIMUM","DATA")
    allVals.save("xxlg_"+prefixName+"_wdAllVals"+imgExt)
    allValsInt = Int(Times("xxlg_"+prefixName+"_wdAllVals"+imgExt,100))
    allValsInt.save("xxlg_"+prefixName+"_wdAllValsInt"+imgExt)
    fStatMean = FocalStatistics("xxlg_"+prefixName+"_wdAllValsInt"+imgExt,SoothingNbr,"MEAN")
    fStatMean.save("xxlg_"+prefixName+"_wdAllFs"+imgExt)
    finalVals = Times(Con("xxlg_"+prefixName+"_distGTzero"+imgExt,1,"","Value > 0"),"xxlg_"+prefixName+"_wdAllFs"+imgExt)
    #finalVals.save("xxlg_"+prefixName+"_finalVals"+imgExt)
    #r = Raster("xxlg_"+prefixName+"_finalVals"+imgExt)
    #max = r.maximum
    #min = r.minimum
    #range = max - min
    #produces values from 0-1
    #outRaster = Divide(Minus("xxlg_"+prefixName+"_finalVals"+imgExt,min),range)
    return finalVals      
     
def NewWeightedDist(inRaster,prefixName,zi_dist,imgExt):
    dist = "xxlg_"+prefixName+"_dist"+imgExt
    allocat = EucAllocation(inRaster,zi_dist,"#","#","#",dist) #Currently only in meters
    allocat.save("xxlg_"+prefixName+"_allocat"+imgExt)
    halfDist = zi_dist/2

    neighborhood =  NbrCircle(halfDist, "MAP")
    focalStatMax = FocalStatistics(inRaster,neighborhood, "MAXIMUM")
    focalStatMax.save("xxlg_"+prefixName+"_fsMax"+imgExt)
    meanCS = CellStatistics(["xxlg_"+prefixName+"_fsMax"+imgExt,"xxlg_"+prefixName+"_allocat"+imgExt],"MEAN","DATA")
    meanCS.save("xxlg_"+prefixName+"_meanCS"+imgExt)
    SoothingNbr = NbrCircle(500,"MAP")

    focalStatMean = FocalStatistics("xxlg_"+prefixName+"_meanCS"+imgExt,SoothingNbr,"MEAN")
    focalStatMean.save("xxlg_"+prefixName+"_meanFS"+imgExt)
    rankDistVals = Divide("xxlg_"+prefixName+"_meanFS"+imgExt,Ln(Plus("xxlg_"+prefixName+"_dist"+imgExt,1)))
    rankDistVals.save("xxlg_"+prefixName+"_rDVals"+imgExt)
    r = Raster(inRaster)
    cSize = r.meanCellWidth

    inRank = Divide(inRaster,Ln(cSize))
    inRank.save("xxlg_"+prefixName+"_inRank"+imgExt)
    allVals = CellStatistics(["xxlg_"+prefixName+"_inRank"+imgExt,"xxlg_"+prefixName+"_rDVals"+imgExt],"MAXIMUM","DATA")
    allVals.save("xxlg_"+prefixName+"_MaxCS"+imgExt)
    intTimes = Int(Times("xxlg_"+prefixName+"_MaxCS"+imgExt,100))
    intTimes.save("intTimes"+prefixName+imgExt)
    
    smoothData = FocalStatistics("intTimes"+prefixName+imgExt,SoothingNbr,"MEAN")
    smoothData.save("xxlg_"+prefixName+"_SM"+imgExt)
    finalWD = Con(dist,"xxlg_"+prefixName+"_SM"+imgExt,"","Value > 0")
    
    return finalWD
    
    
    
    
    
def OldWeightedDist(inRaster,prefixName,zi_dist,imgExt):
    #Allows for any raster to be ranked based on weighted dist and focalstats
    dist = "xxlg_"+prefixName+"_dist"+imgExt
    allocat = EucAllocation(inRaster,zi_dist,"#","#","#",dist) #Currently only in meters
    allocat.save("xxlg_"+prefixName+"_allocat"+imgExt)
    
    #For use with limiting ranking to only distances within range
    dist2bRanked = Con(dist,1,"","Value > 0")
    dist2bRanked.save("xxlg_"+prefixName+"_distOnly"+imgExt)
    
    distWOZero = Con(dist,dist,"","Value > 0")
    distWOZero.save("xxlg_"+prefixName+"_distWOzeros"+imgExt)
    
    distMultiplier = Power("xxlg_"+prefixName+"_distWOzeros"+imgExt,-0.5)
    distMultiplier.save("xxlg_"+prefixName+"_distX"+imgExt)
    DistXAllocat = Times("xxlg_"+prefixName+"_allocat"+imgExt,"xxlg_"+prefixName+"_distX"+imgExt)
    DistXAllocat.save("xxlg_"+prefixName+"_distXallocat"+imgExt)
    intDistXSize = Plus(Int(Times("xxlg_"+prefixName+"_distXallocat"+imgExt,100)),1)
    intDistXSize.save("xxlg_"+prefixName+"intDistXSize"+imgExt)
    FinalRank = Slice("xxlg_"+prefixName+"intDistXSize"+imgExt,100,"NATURAL_BREAKS",1)
    

    return FinalRank
     
def returnRequiredCells(areaUnits,rasterLayer):
    regenImgDesc = arcpy.Describe(rasterLayer)
    unitsName = regenImgDesc.spatialReference.linearUnitName
    cellWidth = Raster(regenRLyrName).meanCellWidth
    sqUnitsPerCell = cellWidth*cellWidth
    if "Meter" in unitsName:
        cnvUnits = 10000
    elif "Feet" in unitsName:
        cnvUnits =107639


def createInt(InRaster):
    dscR = arcpy.Describe(InRaster)
    rasterObj = Raster(InRaster)
    if dscR.isInteger:
        return rasterObj
    else:
        maxVal = rasterObj.maximum
        if maxVal > 1000:
            int = Int(RoundUp(InRaster))
        elif maxVal > 10:
            int = Int(RoundUp(Times(InRaster,100)))
        else:
            int = Int(RoundUp(Times(InRaster,1000)))
        return int

        
def returnLinUnits(linUnits,outUnit):
    dist_units = linUnits.split()
    dist = dist_units[0]
    units = dist_units[1]
    if units == outUnit:
        return dist
    else:
        if outUnit == "Feet":
            if units == "Miles":
                cvFact = 5280
            elif units == "Meters":
                cvFact = 3.28084
            else:
                cvFact = 3280.84
            returnVal = float(dist) * cvFact
        else:
            if units == "Miles":
                cvFact = 1609.34
            elif units == "Feet":
                cvFact = 0.3048
            else:
                cvFact = 1000
            returnVal = float(dist) * cvFact
        return returnVal

#not used in this app        
def MinMaxNormalize(raster,highVal4Scale):
    rasterObj = Raster(raster)
    min = rasterObj.minimum #should always be one but will leave
    max = rasterObj.maximum
    range = max - min
    minMaxNormalized = Int(Divide(Minus(raster,min),range))*highVal4Scale
    return minMaxNormalized

def rankVals(inRaster,outName,rankExtRaster,imgExt):
    onlyRank = Times(inRaster,rankExtRaster)
    #inRasterGets locked here seems like times is holding onto locks
    onlyRank.save(outName+"_onlyRank"+imgExt)
    sliceRank = Slice(outName+"_onlyRank"+imgExt,100,"NATURAL_BREAKS",1)
    sliceRank.save(outName+"_sliceRank"+imgExt)
    rasterObj = Raster(outName+"_sliceRank"+imgExt)  
    max =  rasterObj.maximum
    if max < 100:
        rankedVals = Slice(outName+"_sliceRank"+imgExt,100,"EQUAL_INTERVAL",1)
    else:
        rankedVals = rasterObj

    return rankedVals

def rankAllVals(inRaster,outName,imgExt):
    sliceRank = Slice(inRaster,100,"NATURAL_BREAKS",1)
    sliceRank.save(outName+"_sliceRank"+imgExt)
    rasterObj = Raster(outName+"_sliceRank"+imgExt)
    max =  rasterObj.maximum
    if max < 100:
        rankedVals = Slice(outName+"_sliceRank"+imgExt,100,"EQUAL_INTERVAL",1)
    else:
        rankedVals = rasterObj
    
    return rankedVals    
        
#Since Ranked by NBs but did not reach 100 will then use Equal Interval to scale to 100
#Same as streching data since output from NB ranks data in a series without skipping a rank
#-e.g. -- 1,2,3,4,5,6,7
def scaleUp(raster,highVal4Scale):
    rasterObj = Raster(raster)
    max = rasterObj.maximum
    if max < highVal4Scale: 
        #min = rasterObj.minimum
        #inRange = max-min
        #outRange = highVal4Scale-1#this case will always want one
        #scaledUp = RoundDown(Plus(Divide(Times(Minus(raster,min),outRange),inRange),1.5))
        #scaledUp = Int(Divide(Times(raster,highVal4Scale),max))
         scaledUp = Slice(raster,100,"EQUAL_INTERVAL",1)
    else:
        scaledUp = rasterObj
    return scaledUp

def testForExist(testWS,wildcard):
    currentWs = arcpy.env.workspace
    arcpy.env.workspace = testWS
    dList = arcpy.ListDatasets(wildcard)
    arcpy.env.workspace = currentWs
    if len(dList) > 0:
        return True
    else:
        return False
    
def testRaster(ras):
    r = Raster(ras)
    test = r.maximum
    if test:
        return True
    else:
        return False

def deleteScratchFiles(DirToRemove,outWS):
    
   #wsRemove = env.workspace
    env.workspace=outWS
    env.scratchWorkspace = outWS
    #arcpy.Compact_management(DirToRemove)

    try:
        #arcpy.AddMessage("Removing")
        cmd = "RMDIR " +DirToRemove+" /s /q"
        pipe = os.popen(cmd + ' 2>&1', 'r')
        text = pipe.read()
        sts = pipe.close()
        #arcpy.AddMessage(text)
    except:
        arcpy.AddWarning("Could not delete scratch workspace: "+DirToRemove)
        arcpy.AddWarning("Please remove if not needed.")

        
def deleteTempFiles():
    dataList = arcpy.ListDatasets("xxlg_*")
    for d in dataList:
        try:
            #Remove comment from next line and add comment to Delete to keep intermediary files
            #arcpy.AddMessage("Deleting "+d)
            arcpy.Delete_management(d)
        except:
            arcpy.AddWarning ("Could not delete "+d)
            continue

    fcList = arcpy.ListFeatureClasses("xxlg_*")
    for fc in fcList:
        try:
            #Remove comment from next line and add comment to Delete to keep intermediary files
            #arcpy.AddMessage("Deleting "+fc)
            arcpy.Delete_management(fc)
            
        except:
            arcpy.AddWarning ("Could not delete "+fc)
            continue   
    tableList = arcpy.ListTables("xxlg_*")
    for t in tableList:
        try:
            #Remove comment from next line and add comment to Delete to keep intermediary files
            #arcpy.AddMessage("Deleting "+t)
            arcpy.Delete_management(t)
            
        except:
            arcpy.AddWarning ("Could not delete "+t)
            continue 
def deleteLGScratchFiles():
    #datasetsList = arcpy.ListDatasets()
    fcList = arcpy.ListFeatureClasses()
    tList = arcpy.ListTables()
    rList = arcpy.ListRasters()
    for fc in fcList:
        try:
            arcpy.Delete_management(fc)
        except:
            continue
    for t in tList:
        try:
            arcpy.Delete_management(t)
        except:
            continue   
            
    for r in rList:
        try:
            arcpy.Delete_management(r)
        except:
            continue             
            
    