PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/getavg.py

https://github.com/Gim6626/casconvection
Python | 58 lines | 48 code | 8 blank | 2 comment | 15 complexity | 6ccaefe31d49e2bd3baa35dac4df5001 MD5 | raw file
Possible License(s): GPL-2.0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import sys
  5. from optparse import OptionParser
  6. usage = "usage: %prog [options]"
  7. parser = OptionParser(usage = usage)
  8. parser.add_option("-i", "--input", dest="inputfile",
  9. help="input file, two tab separated columns -- x and y",
  10. metavar="INPUT")
  11. parser.add_option("-b", "--bounds", dest="bounds",
  12. help="bounds on x for extremum finding, comma separated list"
  13. + " of two values 'a,b' ('x' values are taken only"
  14. + " from [a, b]) or one number 'a' ('x' values are "
  15. + " taken if they are more than 'a')",
  16. metavar="INPUT")
  17. (options, args) = parser.parse_args()
  18. if options.inputfile == None:
  19. inputfile = sys.stdin
  20. else:
  21. inputfile = open(options.inputfile, 'r')
  22. lower = None
  23. upper = None
  24. if options.bounds != None:
  25. bounds = options.bounds.split(',')
  26. if len(bounds) == 2:
  27. lower = float(bounds[0])
  28. upper = float(bounds[1])
  29. elif len(bounds) == 1:
  30. lower = float(bounds[0])
  31. input = []
  32. for line in inputfile:
  33. input.append([float(i) for i in line.split()])
  34. size = len(input)
  35. if size == 0:
  36. print "Error, input is empty"
  37. os._exit(1)
  38. sum = 0.0
  39. count = 0
  40. for i in input:
  41. x = i[0]
  42. y = i[1]
  43. if lower != None and upper != None:
  44. if x < lower or x > upper:
  45. continue
  46. elif lower != None and x < lower:
  47. continue
  48. sum += y
  49. count += 1
  50. print sum / count