Saturday, July 8, 2017

Python - Frequency interval for columns

This script is useful to determine the frequency of interval for several columns.In my case, I have created this script to determine the water surface elevation difference for various scenarios for particular intervals.

import arcpy
import re
#Modify the below paths.
InputTable=r"W:\Projects\NJ_Transit\FloodAnalysis\RBDAnalysis\XLS\Deltas.gdb\STVpts"
fields = ['F1Diff100y','F2Diff100y','F3Diff100y','F4Diff50yr','F5Diff50yr','F6Diff50yr','F7Diff10yr','F8Diff10yr','F9Diff10yr']
OutputCSV = r"W:\Projects\NJ_Transit\FloodAnalysis\RBDAnalysis\XLS\WDD.txt"

f = open(OutputCSV,'w')
f.write("Fields\Range (includes the lower limit and exclutes the upper limit),")
f.write("Less than -3.0,")
f.write("-3.0 to -2.5,")
f.write("-2.5 than -2.0,")
f.write("-2.0 than -1.5,")
f.write("-1.5 to -1.0,")
f.write("-1.0 to -0.83,")
f.write("-0.83 to -0.67,")
f.write("-0.67 to -0.5,")
f.write("-0.5 to 0,")
f.write("Equals to 0,")
f.write("0 to 0.5,")
f.write("0.5 to 0.67,")
f.write("0.67 to 0.83,")
f.write("0.83 to 1.0,")
f.write("1.0 to 1.5,")
f.write("1.5 to 2.0,")
f.write("2.0 to 2.5,")
f.write("2.5 to 3.0,")
f.write("Greater Than or Equal to 3.0")
#range value depends upon the number of fields.
for i in range(9):
  count1 = 0
  count2 = 0
  count3 = 0
  count4 = 0
  count5 = 0
  count6 = 0
  count7 = 0
  count8 = 0
  count9 = 0
  count10 = 0
  count11 = 0
  count12 = 0
  count13 = 0
  count14 = 0
  count15 = 0
  count16 = 0
  count17 = 0
  count18 = 0
  count19 = 0
  with arcpy.da.SearchCursor(InputTable,fields) as cursor:
    for row in cursor:
      if row[i] < -3.0:
        count1 += 1
      elif row[i] >= -3.0 and row[i] < -2.5:
        count2 += 1
      elif row[i] >= -2.5 and row[i] < -2.0:
        count3 += 1
      elif row[i] >= -2.0 and row[i] < -1.5:
        count4 += 1
      elif row[i] >= -1.5 and row[i] < -1.0:
        count5 += 1
      elif row[i] >= -1.0 and row[i] < -0.83:
        count6 += 1
      elif row[i] >= -0.83 and row[i] < -0.67:
        count7 += 1
      elif row[i] >= -0.67 and row[i] < -0.5:
        count8 += 1
      elif row[i] >= -0.5 and row[i] < 0:
        count9 += 1
      elif row[i] == 0:
        count10 += 1
      elif row[i] > 0 and row[i] <= 0.5:
        count11 += 1
      elif row[i] > 0.5 and row[i] <= 0.67:
        count12 += 1
      elif row[i] > 0.67 and row[i] <= 0.83:
        count13 += 1
      elif row[i] > 0.83 and row[i] <= 1.0:
        count14 += 1
      elif row[i] > 1.0 and row[i] <= 1.5:
        count15 += 1
      elif row[i] > 1.5 and row[i] <= 2.0:
        count16 += 1
      elif row[i] > 2.0 and row[i] <= 2.5:
        count17 += 1
      elif row[i] > 2.5 and row[i] <= 3.0:
        count18 += 1
      elif row[i] > 3.0:
        count19 += 1
    f.write("\n" + str(fields[i])+ ",")
    f.write(str(count1)+ ",")
    f.write(str(count2)+ ",")
    f.write(str(count3)+ ",")
    f.write(str(count4)+ ",")
    f.write(str(count5)+ ",")
    f.write(str(count6)+ ",")
    f.write(str(count7)+ ",")
    f.write(str(count8)+ ",")
    f.write(str(count9)+ ",")
    f.write(str(count10)+ ",")
    f.write(str(count11)+ ",")
    f.write(str(count12)+ ",")
    f.write(str(count13)+ ",")
    f.write(str(count14)+ ",")
    f.write(str(count15)+ ",")
    f.write(str(count16)+ ",")
    f.write(str(count17)+ ",")
    f.write(str(count18)+ ",")
    f.write(str(count19))
f.close()
print "CSV Created"

No comments:

Post a Comment