# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Script Erosion.py
# Created on: 2014-04-14 16:56:37.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: Script Erosion <Zone_d_étude> <Modèle_d_élévation> <Matière_Organique> <Pédologie> <Value> <Précipitation> <Facteur_C> <Dossier_de_sortie> 
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")
arcpy.ImportToolbox("O:/PERMANENT/Cartes du centre/SIG_CRDBLP/Outil/Modèle Érosion/Erosion.tbx")

# Script arguments
Zone_d_étude = arcpy.GetParameterAsText(0)

Modèle_d_élévation = arcpy.GetParameterAsText(1)

Matière_Organique = arcpy.GetParameterAsText(2)
if Matière_Organique == '#' or not Matière_Organique:
    Matière_Organique = "Sols\\Analyse des sols\\Phosphore Mehlich 3\\Analyse_2012" # provide a default value if unspecified

Pédologie = arcpy.GetParameterAsText(3)
if Pédologie == '#' or not Pédologie:
    Pédologie = "Sols\\Pedologie" # provide a default value if unspecified

Value = arcpy.GetParameterAsText(4)
if Value == '#' or not Value:
    Value = "1" # provide a default value if unspecified

Précipitation = arcpy.GetParameterAsText(5)
if Précipitation == '#' or not Précipitation:
    Précipitation = "36.3" # provide a default value if unspecified

Facteur_C = arcpy.GetParameterAsText(6)
if Facteur_C == '#' or not Facteur_C:
    Facteur_C = "0.51" # provide a default value if unspecified

Dossier_de_sortie = arcpy.GetParameterAsText(7)

# Local variables:
MO = Zone_d_étude
Pedologie_Clip = Zone_d_étude
Field_MO = Pedologie_Clip
Calculate_MO = Field_MO
Calculate_K_Value = Calculate_MO
Field_K_Value__2_ = Calculate_K_Value
Calculate_MO__3_ = Field_K_Value__2_
Poly_To_Ras = Calculate_MO__3_
Reclass_Raster__2_ = Poly_To_Ras
Facteur_K_tif = Reclass_Raster__2_
RUSLE_CAN = Facteur_K_tif
Field_K_Value = Pedologie_Clip
Extract = Zone_d_étude
Fill__2_ = Extract
FlowDir = Fill__2_
FlowLenght = FlowDir
Facteur_LS_tif = FlowLenght
Output_drop_raster = Fill__2_
Slope_Degree_tif = Fill__2_
Slope_Percent_tif = Fill__2_
Reclass = Slope_Percent_tif
ReclassM = Reclass
Facteur_R = Précipitation

# Process: Clip
arcpy.Clip_analysis(Pédologie, Zone_d_étude, Pedologie_Clip, "")

# Process: Add Field (2)
arcpy.AddField_management(Pedologie_Clip, "Kvalue", "FLOAT", "5", "3", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Add Field
arcpy.AddField_management(Pedologie_Clip, "MO", "FLOAT", "7", "2", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(Matière_Organique, "INTERSECT", Zone_d_étude, "", "NEW_SELECTION")

# Process: Get Field Value
arcpy.GetFieldValue_mb(MO, "M_O", "String", "0")

# Process: Calculate Field
arcpy.CalculateField_management(Field_MO, "MO", Value, "VB", "")

# Process: Calculate Field (2)
arcpy.CalculateField_management(Field_K_Value, "Kvalue", "Kvalue(!MO!,!Analyse!)", "PYTHON_9.3", "def Kvalue(mo,type):\\n	if (mo >= 2):\\n		if (type == \"Argile\"):\\n			return 0.028\\n		if (type == \"Loam argileux\"):\\n			return 0.037\\n		if (type == \"Loam sableux grossier\"):\\n			return 0.009\\n		if (type == \"Sable fin\"):\\n			return 0.008\\n		if (type == \"Loam sableux fin\"):\\n			return 0.022\\n		if (type == \"Argile lourde\"):\\n			return 0.020\\n		if (type == \"Loam\"):\\n			return 0.038\\n		if (type == \"Sable fin loameux\"):\\n			return 0.012\\n		if (type == \"Sable loameux\"):\\n			return 0.005\\n		if (type == \"Sable très fin loameux\"):\\n			return 0.033\\n		if (type == \"Sable\"):\\n			return 0.003\\n		if (type == \"Loam sablo-argileux\"):\\n			return 0.026\\n		if (type == \"Loam sableux\"):\\n			return 0.016\\n		if (type == \"Loam limoneux\"):\\n			return 0.049\\n		if (type == \"Argile limoneuse\"):\\n			return 0.034\\n		if (type == \"Loam limono-argileux\"):\\n			return 0.040\\n		if (type == \"Sable tres fin\"):\\n			return 0.049\\n		if (type == \"Loam sableux tres fin\"):\\n			return 0.044\\n		else:\\n			return 0\\n			\\n	if (mo < 2):\\n		if (type == \"Argile\"):\\n			return 0.032\\n		if (type == \"Loam argileux\"):\\n			return 0.044\\n		if (type == \"Loam sableux grossier\"):\\n			return 0.009\\n		if (type == \"Sable fin\"):\\n			return 0.012\\n		if (type == \"Loam sableux fin\"):\\n			return 0.029\\n		if (type == \"Argile lourde\"):\\n			return 0.025\\n		if (type == \"Loam\"):\\n			return 0.045\\n		if (type == \"Sable fin loameux\"):\\n			return 0.020\\n		if (type == \"Sable loameux\"):\\n			return 0.007\\n		if (type == \"Sable très fin loameux\"):\\n			return 0.058\\n		if (type == \"Sable\"):\\n			return 0.001\\n		if (type == \"Loam sablo-argileux\"):\\n			return 0.026\\n		if (type == \"Loam sableux\"):\\n			return 0.018\\n		if (type == \"Loam limoneux\"):\\n			return 0.054\\n		if (type == \"Argile limoneuse\"):\\n			return 0.036\\n		if (type == \"Loam limono-argileux\"):\\n			return 0.046\\n		if (type == \"Sable tres fin\"):\\n			return 0.061\\n		if (type == \"Loam sableux tres fin\"):\\n			return 0.054\\n		else:\\n			return 0\\n	else:\\n		return 0")

# Process: Add Field (3)
arcpy.AddField_management(Calculate_K_Value, "K", "FLOAT", "5", "2", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field (3)
arcpy.CalculateField_management(Field_K_Value__2_, "K", "mul(!K_Value!)", "PYTHON_9.3", "def mul(test):\\n	return test*1000")

# Process: Polygon to Raster (2)
tempEnvironment0 = arcpy.env.cellSize
arcpy.env.cellSize = "1"
tempEnvironment1 = arcpy.env.mask
arcpy.env.mask = ""
arcpy.PolygonToRaster_conversion(Calculate_MO__3_, "K", Poly_To_Ras, "CELL_CENTER", "NONE", "1")
arcpy.env.cellSize = tempEnvironment0
arcpy.env.mask = tempEnvironment1

# Process: Copy Raster (2)
arcpy.CopyRaster_management(Poly_To_Ras, Reclass_Raster__2_, "", "", "NoData", "NONE", "NONE", "32_BIT_FLOAT", "NONE", "NONE")

# Process: Raster Calculator (2)
arcpy.gp.RasterCalculator_sa("\"%Reclass_Raster (2)%\" / 1000", Facteur_K_tif)

# Process: Extract by Mask
arcpy.gp.ExtractByMask_sa(Modèle_d_élévation, Zone_d_étude, Extract)

# Process: Fill
arcpy.gp.Fill_sa(Extract, Fill__2_, "")

# Process: Slope (2)
arcpy.gp.Slope_sa(Fill__2_, Slope_Percent_tif, "PERCENT_RISE", "1")

# Process: Flow Direction
arcpy.gp.FlowDirection_sa(Fill__2_, FlowDir, "NORMAL", Output_drop_raster)

# Process: Flow Length
arcpy.gp.FlowLength_sa(FlowDir, FlowLenght, "UPSTREAM", "")

# Process: Slope
arcpy.gp.Slope_sa(Fill__2_, Slope_Degree_tif, "DEGREE", "1")

# Process: Reclassify
arcpy.gp.Reclassify_sa(Slope_Percent_tif, "Value", "0 1 2;1 3 3;3 5 4;5 100 5", Reclass, "DATA")

# Process: Copy Raster
arcpy.CopyRaster_management(Reclass, ReclassM, "", "", "NoData", "NONE", "NONE", "32_BIT_FLOAT", "NONE", "NONE")

# Process: Con FacteurLS
arcpy.gp.toolbox = "O:/PERMANENT/Cartes du centre/SIG_CRDBLP/Outil/Modèle Érosion/Erosion.tbx";
# Warning: the toolbox O:/PERMANENT/Cartes du centre/SIG_CRDBLP/Outil/Modèle Érosion/Erosion.tbx DOES NOT have an alias. 
# Please assign this toolbox an alias to avoid tool name collisions
# And replace arcpy.gp.Script2(...) with arcpy.Script2_ALIAS(...)
arcpy.gp.Script2(Slope_Percent_tif, FlowLenght, Slope_Degree_tif, ReclassM, Dossier_de_sortie, Facteur_LS_tif)

# Process: Calculate Value
arcpy.CalculateValue_management("0.417*(%Précipitation%**2.17)", "", "Variant")

# Process: Raster Calculator (3)
arcpy.gp.RasterCalculator_sa("\"%Facteur_K.tif%\"*\"%Facteur_LS.tif%\"*float(%Facteur_R%)*float(%Facteur_C%)", RUSLE_CAN)

