import arcpy
from arcpy import env
from arcpy.sa import *
import os
import traceback
import sys


def isArcMap():
	try:
		arcpy.mapping.MapDocument("CURRENT")
		return True
	except:
		return False

try:

    #Check for spatial analyst license
    if arcpy.CheckExtension("Spatial") == "Available":
        arcpy.CheckOutExtension("Spatial")
    else:
        raise LicenseError

		project_folder = arcpy.GetParameterAsText(0)
    if arcpy.Exists(project_folder+"\\lg_input.gdb"):
        inPath = project_folder+"\\lg_input.gdb\\"
        inWS = project_folder+"\\lg_input.gdb"
        arcpy.CreateFileGDB_management(project_folder,"lg_Wtscratch.gdb")
        env.workspace = project_folder+"\\lg_Wtscratch.gdb"
        outPath = project_folder+"\\lg_output.gdb\\"
        outWS = project_folder+"\\lg_output.gdb" #For adding to map view if user in ArcMap
        imgExt = ""
    else:
        inPath = project_folder+"\\lg_input\\"
        inWS = project_folder+"\\lg_input"
        arcpy.CreateFolder_management(project_folder,"lg_Wtscratch")
        env.workspace = project_folder+"\\lg_Wtscratch"
        outPath = project_folder+"\\lg_output\\"
        outWS = project_folder+"\\lg_output" #For adding to map view if user in ArcMap
				imgExt = ".img"

    strCatVals = []
    intCatVals = []		
    strIndVals = []
    intIndVals = []
    for x in range (3,30):
        val = arcpy.GetParameterAsText(x)
        intVal = int(val)
        if x < 8:
            strCatVals.append(val)
            intCatVals.append(intVal)
      else:
            strIndVals.append(val)
            intIndVals.append(intVal)
    env.scratchWorkspace = env.workspace
    remRankName4Arcmap = arcpy.GetParameterAsText(1)
    regRankName4Arcmap = arcpy.GetParameterAsText(2)
    
    nonChangeCnt = intIndVals.count(1)
    if nonChangeCnt <> 22:
        #Run all indv first
        arcpy.AddMessage(str(nonChangeCnt))
    else:
        #Just run categorys
        arcpy.AddMessage(str(nonChangeCnt))
    
    
    
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)
		crs.write("*********Ranking Failed************\n")
		crs.write(pymsg)
		crs.write("\n")
		crs.close()

		arcpy.AddError(pymsg)


finally:

		arcpy.AddMessage("Cleaning up temporary files")
		env.workspace=outWS
		#Will test this later..
		#arcpy.Delete_mangement(project_folder+"\\lg_scratch.gdb")
		#LegalGeo_Routines_Module.deleteTempFiles()
		

				