casconvection /scripts/getavg.py

Language Python Lines 59
MD5 Hash 6ccaefe31d49e2bd3baa35dac4df5001
Repository https://github.com/Gim6626/casconvection.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
from optparse import OptionParser

usage = "usage: %prog [options]"
parser = OptionParser(usage = usage)
parser.add_option("-i", "--input", dest="inputfile",
                  help="input file, two tab separated columns -- x and y",
                  metavar="INPUT")
parser.add_option("-b", "--bounds", dest="bounds",
                  help="bounds on x for extremum finding, comma separated list"
                        + " of two values 'a,b' ('x' values are taken only"
                        + " from [a, b]) or one number 'a' ('x' values are "
                        + " taken if they are more than 'a')",
                  metavar="INPUT")
(options, args) = parser.parse_args()

if options.inputfile == None:
    inputfile = sys.stdin
else:
    inputfile = open(options.inputfile, 'r')
lower = None
upper = None
if options.bounds != None:
    bounds = options.bounds.split(',')
    if len(bounds) == 2:
        lower = float(bounds[0])
        upper = float(bounds[1])
    elif len(bounds) == 1:
        lower = float(bounds[0])

input = []
for line in inputfile:
    input.append([float(i) for i in line.split()])

size = len(input)

if size == 0:
    print "Error, input is empty"
    os._exit(1)

sum = 0.0
count = 0
for i in input:
    x = i[0]
    y = i[1]
    if lower != None and upper != None:
        if x < lower or x > upper:
            continue
    elif lower != None and x < lower:
        continue
    sum += y
    count += 1

print sum / count
Back to Top