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


import LegalGeo_Routines_Module


def esRank(inPath,outPath,crs):

    try:

        # temporary workspace location...
        wsDesc = arcpy.Describe(arcpy.env.workspace)
        imgExt =""
        if wsDesc.workspaceType == "FileSystem":
            imgExt = ".img"

        lgRems = inPath+"lg_REMs"+imgExt
        lgAra = inPath+"lgES_ara"+imgExt
        lgFA = inPath+"lgES_fa"+imgExt
        lgSD = inPath+"lgES_spring_density"+imgExt
        lgOther = inPath+"lgES_other"+imgExt
        lgBndOnes = inPath+"lg_bnd_ones"+imgExt

        lgRemsPrjOnly = inPath+"lg_remPrjOnly"+imgExt
        lgConvPrjOnly = inPath+"lg_ConvProjOnly"+imgExt 
        
        desc = arcpy.Describe(lgRems)    # describe object
        cellSize = desc.MeanCellHeight      # cell size
        spatRef = desc.SpatialReference
        units = spatRef.LinearUnitName      # projection units
        
        esSum =[]
        waterDataUsed = []
        
        if arcpy.Exists(lgAra):
            arcpy.AddMessage("-->Runnning Active River Area analysis")
            #If user ranks by 100 vals kept as is
            rasterObj = Raster(lgAra)
            max = rasterObj.maximum
            if max == 100:
                #Custom Ranking
                rasterObj.save(outPath+"lgES_ARA_Rank"+imgExt)
            else:
                #Reclassify based on assigned values by Marcelo
                remap = "2 100; 5 70; 7 40; 20 90; 22 90; 50 60; 55 60; 70 30; 77 30; 200 80; 500 50;700 20" 
                araRank = Reclassify(lgAra,"Value",remap,"NODATA")
                araRank.save(outPath+"lgES_ARA_Rank"+imgExt)
            esSum.append(outPath+"lgES_ARA_Rank"+imgExt)
            crs.write("Active River Area Layer:  lgES_ARA_Rank"+imgExt)
            crs.write("\n")
            
        if arcpy.Exists(lgFA):
            arcpy.AddMessage("-->Runnning Fragile Area analysis")
            #If user ranks by 100 vals kept as is
            rasterObj = Raster(lgFA)
            max = rasterObj.maximum
            if max == 100:
               rasterObj.save(outPath+"lgES_FA_Rank"+imgExt)
            else:
                #Pre-ranked by user why using equal_interval
                faSlice = Slice(lgFA,100,"EQUAL_INTERVAL",1)
                faSlice.save(outPath+"lgES_FA_Rank"+imgExt)            
            esSum.append(outPath+"lgES_FA_Rank"+imgExt)
            crs.write("Fragile Area Layer:  lgES_ARA_Rank"+imgExt)
            crs.write("\n")
            
        if arcpy.Exists(lgSD):
            arcpy.AddMessage("-->Runnning Springs analysis")
            sdSlice = Slice(lgSD,100,"NATURAL_BREAKS",1)
            sdSlice.save("xxlg_sdSlice"+imgExt)
            sdScaleUp = LegalGeo_Routines_Module.scaleUp("xxlg_sdSlice"+imgExt,100)
            sdScaleUp.save(outPath+"lgES_SD_Rank"+imgExt)
            esSum.append(outPath+"lgES_SD_Rank"+imgExt)
            crs.write("Spring Density Layer:  lgES_SD_Rank"+imgExt)
            crs.write("\n")
        
        if arcpy.Exists(lgOther):
            arcpy.AddMessage("-->Runnning Other Environmental Services analysis")
            #If user ranks by 100 vals kept as is
            rasterObj = Raster(lgOther)
            max = rasterObj.maximum
            if max == 100:
                rasterObj.save(outPath+"lgES_Other_Rank"+imgExt)
            else:
                intOtherES = LegalGeo_Routines_Module.createInt(lgOther)
                intOtherES.save("intOtherES"+imgExt)
                otherSlice = Slice("intOtherES"+imgExt,100,"NATURAL_BREAKS",1)
                otherSlice.save("xxlg_OtherSlice"+imgExt)           
                otherScaleUp = LegalGeo_Routines_Module.scaleUp("xxlg_OtherSlice"+imgExt,100)
                otherScaleUp.save(outPath+"lgES_Other_Rank"+imgExt)
            esSum.append(outPath+"lgES_Other_Rank"+imgExt)  
            crs.write("Environmental Services Other Layer:  lgES_Other_Rank"+imgExt)
            crs.write("\n")
            

        esLayerSum = CellStatistics(esSum,"SUM","DATA")
        esLayerSum.save("xxlg_esLayerSum"+imgExt)
        esSumRemsOnly = Times(lgRemsPrjOnly,"xxlg_esLayerSum"+imgExt)
        esSumRemsOnly.save("xxlg_esSumRemsOnly"+imgExt)
        esRemSumRank = Slice("xxlg_esSumRemsOnly"+imgExt,100,"NATURAL_BREAKS",1)
        esRemSumRank.save("xxlg_esRemSumRank"+imgExt)
        FinalRemESRank  = LegalGeo_Routines_Module.scaleUp("xxlg_esRemSumRank"+imgExt,100)
#        waterTest20.save("xxlg_saTest20"+imgExt)
#        FinalRemWaterRank = Times("xxlg_saTest20"+imgExt,5)
        FinalRemESRank.save(outPath+"lgESAll_RemRank"+imgExt)
        crs.write ("Final Remnant Environment Services Layer:  lgESAll_RemRank"+imgExt)
        crs.write("\n")
        #Conv
        ThreatSumConvOnly = Times(lgConvPrjOnly,"xxlg_esLayerSum"+imgExt)
        ThreatSumConvOnly.save("xxlg_esSumConvOnly"+imgExt)
        esConvSumRank = Slice("xxlg_esSumConvOnly"+imgExt,100,"NATURAL_BREAKS",1)
        esConvSumRank.save("xxlg_esConvSumRank"+imgExt)
        FinalEsConvSumRank = LegalGeo_Routines_Module.scaleUp("xxlg_esConvSumRank"+imgExt,100)
        FinalEsConvSumRank.save(outPath+"lgESAll_ConvRank"+imgExt)  
        crs.write ("Final Convertd Lands Environmental Services Layer:  lgESAll_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   