# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# extract_basin_gSSURGO_data.py
# Created on: 2014-07-25 14:20:22.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: extract_basin_gSSURGO_data <Folder_with_Basin__shp_Files> <State_gSSURGO_Raster> <Output_Excel_Sheet> 
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

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

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")

# Set Geoprocessing environments
arcpy.env.scratchWorkspace = "P:\\NWS\\GIS\\Models\\Model_Output.gdb"
arcpy.env.outputCoordinateSystem = "PROJCS['USA_Contiguous_Albers_Equal_Area_Conic_USGS_version',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-96.0],PARAMETER['Standard_Parallel_1',29.5],PARAMETER['Standard_Parallel_2',45.5],PARAMETER['Latitude_Of_Origin',23.0],UNIT['Meter',1.0]]"
arcpy.env.parallelProcessingFactor = "50%"

# Script arguments
Folder_with_Basin__shp_Files = arcpy.GetParameterAsText(0)
if Folder_with_Basin__shp_Files == '#' or not Folder_with_Basin__shp_Files:
    Folder_with_Basin__shp_Files = "P:\\NWS\\GIS\\MBRFC\\Shapefiles\\Basins" # provide a default value if unspecified

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

Output_Excel_Sheet = arcpy.GetParameterAsText(2)
if Output_Excel_Sheet == '#' or not Output_Excel_Sheet:
    Output_Excel_Sheet = "P:\\NWS\\GIS\\MBRFC\\SSURGO\\data_files\\%Value%_ssurgo.xls" # provide a default value if unspecified

# Local variables:
Basin_gSSURGO_Raster = State_gSSURGO_Raster
Joined_Raster = Basin_gSSURGO_Raster
v_Value__gssurgo = Joined_Raster
Model_Output_gdb = "P:\\NWS\\GIS\\Models\\Model_Output.gdb"
Basin_Boundary__shp_File = Folder_with_Basin__shp_Files
Value = Basin_Boundary__shp_File
Name = Folder_with_Basin__shp_Files

# Process: Iterate Feature Classes
arcpy.IterateFeatureClasses_mb(Folder_with_Basin__shp_Files, "", "POLYGON", "NOT_RECURSIVE")

# Process: Parse Path
arcpy.ParsePath_mb(Basin_Boundary__shp_File, "NAME")

# Process: Extract by Mask
arcpy.gp.ExtractByMask_sa(State_gSSURGO_Raster, Basin_Boundary__shp_File, Basin_gSSURGO_Raster)

# Process: Join Field
arcpy.JoinField_management(Basin_gSSURGO_Raster, "MUKEY", State_gSSURGO_Raster, "hydgrpdcd", "")

# Process: Table to Table
arcpy.TableToTable_conversion(Joined_Raster, Model_Output_gdb, "%Value%_gssurgo", "", "VALUE \"VALUE\" false true true 0 Long 0 0 ,First,#,,VALUE,-1,-1,,Value,-1,-1;COUNT \"COUNT\" false true true 0 Long 0 0 ,First,#,,COUNT,-1,-1,,Count,-1,-1;MUKEY \"MUKEY\" true true false 30 Text 0 0 ,First,#,,MUKEY,-1,-1;hydgrpdcd \"Hydrologic Group - Dominant Conditions\" true true false 254 Text 0 0 ,First,#,,hydgrpdcd,-1,-1", "")

# Process: Table To Excel
arcpy.TableToExcel_conversion(v_Value__gssurgo, Output_Excel_Sheet, "NAME", "CODE")

