# import required modules...
import arcpy
from arcpy import env
from arcpy.sa import *
import os
import time
import traceback
import sys
import math
import LegalGeo_Routines_Module

def ThreatRank(inPath,outPath,imgExt,crs):
    try:

        lgAccess = inPath+"lgThreat_Access"+imgExt
        lgRemOnes = inPath +"lg_REM_Ones"+imgExt
        lgThreatOther = inPath+"lgThreat_Other"+imgExt
        lgBndOnes = inPath+"lg_bnd_ones"+imgExt
        

        
        threatSum = []
        
        if arcpy.Exists(lgAccess):
            arcpy.AddMessage("-->Running Accessibility analysis")
            intAccess = LegalGeo_Routines_Module.createInt(lgAccess)
            intAccess.save("intAcces"+imgExt)
            accessLandscapeRank = Slice("intAcces"+imgExt,100,"NATURAL_BREAKS","1")
            accessLandscapeRank.save(outPath+"lgThreat_AccessRank"+imgExt)
            crs.write("Accessibility: lgThreat_AccessRank"+imgExt+"\n")
            threatSum.append(outPath+"lgThreat_AccessRank"+imgExt)
            
        if arcpy.Exists(lgThreatOther):
            arcpy.AddMessage("-->Running Other Threat analysis")
              
            intOtherThreat = LegalGeo_Routines_Module.createInt(lgThreatOther)
            intOtherThreat.save("intOtherThreat"+imgExt)
            #Inveting values  
            rasterObj = Raster("intOtherThreat"+imgExt)
            maxVal = rasterObj.maximum              
            minVal = rasterObj.minimum 
            outRaster = Plus(Times(Minus("intOtherThreat"+imgExt,maxVal),-1),minVal)
            outRaster.save("xxlg_invsOtherThreat"+imgExt)
            
            otLandscapeRank = Slice("xxlg_invsOtherThreat"+imgExt,100,"NATURAL_BREAKS",1)
            otLandscapeRank.save("xxlgThreat_otRank"+imgExt)    
            otRankScaled =  LegalGeo_Routines_Module.scaleUp("xxlgThreat_otRank"+imgExt,100)
            otRankScaled.save(outPath+"lgThreat_OtherRank"+imgExt)
            threatSum.append(outPath+"lgThreat_OtherRank"+imgExt) 
            crs.write("Other threat: lgThreat_OtherRank"+imgExt+"\n")

        
        
        lgRemsPrjOnly = inPath+"lg_remPrjOnly"+imgExt
        lgConvPrjOnly = inPath+"lg_ConvProjOnly"+imgExt            

        threatLayerSum = CellStatistics(threatSum,"SUM","DATA")
        threatLayerSum.save("xxlg_threatSum"+imgExt)
        #Rems
        FinalThreatSumRankSave = LegalGeo_Routines_Module.rankVals("xxlg_threatSum"+imgExt,"remThreat",lgRemsPrjOnly,imgExt)
        FinalThreatSumRankSave.save(outPath+"lgThreatAll_RemRank"+imgExt)
        crs.write("Final Remnant Threat Layer:  lgThreatAll_RemRank"+imgExt)
        crs.write("\n")        
        #Conv
        FinalConvSumRankSave = LegalGeo_Routines_Module.rankVals("xxlg_threatSum"+imgExt,"remThreat",lgConvPrjOnly,imgExt)
        FinalConvSumRankSave.save(outPath+"lgThreatAll_ConvRank"+imgExt)   
        crs.write("Final Converted Lands Threat Layer:  lgThreatAll_ConvRank"+imgExt)
        crs.write("\n")        
        
        
    
    except:
        #arcpy.AddMessage("exiting")
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        #arcpy.AddMessage("exiting2")
        pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value)

        arcpy.AddError(pymsg)

        print pymsg     