# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# validator.py
# Created on: 2017-07-17 18:17:04.00000
#   (generated by ArcGIS/ModelBuilder)
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Local variables:
arcpy.env.workspace = arcpy.env.scratchFolder

points_statistics = "points_statistics.dbf"
pointsNew = "points_new.shp"

mean = 0
std = 0
threshold = 0

# overwriting output
arcpy.env.overwriteOutput = True

def validateShapefile(points, accuracyThreshold):
    arcpy.Copy_management(points, pointsNew)

    # generate statistics
    print "calculating statistics..."
    arcpy.Statistics_analysis(points, points_statistics, [["accuracy", "MEAN"], ["accuracy", "STD"]])

    # looping through statistics table and saving MEAN accuracy and STD (standard deviation) accuracy
    print "getting mean and std"
    rows = arcpy.SearchCursor(points_statistics)
    fields = arcpy.ListFields(points_statistics)
    for field in fields:
        cursor = arcpy.SearchCursor(points_statistics, "", "", field.name)
        row = cursor.next()
        field_value = row.getValue(field.name)
        del cursor
        if "MEAN" in field.name:
            mean = field_value
        if "STD" in field.name:
            std = field_value

    if accuracyThreshold == 0:
        threshold = mean + std
    else:
        threshold = accuracyThreshold

    print "using threshold: ", threshold

    # deleting rows depending on accuracy
    with arcpy.da.UpdateCursor(pointsNew, "accuracy") as cursor:
        for row in cursor:
            if row[0] >= threshold:
                cursor.deleteRow()

    return pointsNew

    
