import arcpy
import sys

path = arcpy.GetParameterAsText(0)
table=arcpy.GetParameterAsText(1)

arcpy.env.overwriteOutput = True

arcpy.MakeFeatureLayer_management(path, "layer")

arcpy.AddIndex_management ("layer", "HydroID")

arcpy.AddJoin_management("layer", "HydroID", table, "Riverid")

fieldList = arcpy.ListFields(path, 'category')

fieldCount = len(fieldList)

if (fieldCount != 1):
    
    arcpy.AddField_management(path, 'category', 'SHORT')
    

rows= arcpy.SearchCursor("layer")

stack=[]
for row in rows:
    value=row.getValue('projectriversediment$.Sediment output river')
    stack.append(value)

del rows, row

rows = arcpy.UpdateCursor(path)
i = 0
k_1 = 0
k_2 = 0
k_3 = 0
k_4 = 0
k_5 = 0
for row in rows:
    if stack[i] <= 500:
        row.category = 1
        k_1 += 1
    elif stack[i] > 500 and stack[i] <= 1000:
        row.category = 2
        k_2 += 1
    elif stack[i] > 1000 and stack[i] <= 2000:
        row.category = 3
        k_3 += 1
    elif stack[i] > 2000 and stack[i] <= 3000:
        row.category = 4
        k_4 += 1
    else:
        row.category = 5
        k_5 += 1
    i+=1
    rows.updateRow(row)

del rows, row

arcpy.AddMessage('Category created')
arcpy.AddMessage('Total count of river parts: '+str(i))
arcpy.AddMessage('    Count of category 1: '+str(k_1))
arcpy.AddMessage('    Count of category 2: '+str(k_2))
arcpy.AddMessage('    Count of category 3: '+str(k_3))
arcpy.AddMessage('    Count of category 4: '+str(k_4))
arcpy.AddMessage('    Count of category 5: '+str(k_5))




