def filter_pat_temp(fn, fnout, tempth):
    n = 0
    with open(fn, 'r') as fin,  open(fnout, 'w') as fout:
        for lin in fin:
            linL = lin.strip().split(',')
            #print(linL)
            tempL = filter_tempL(linL[2:], tempth)
            if len(tempL) > 0:
                avg = round(sum(tempL)/len(tempL), 1)
                newlinL = [str(avg), linL[1]] + convert_tempL(tempL)
                fout.write(','.join(newlinL) + '\n')
                n += 1
    return n

def filter_tempL(tempL, tempth):
    solL = []
    for ts in tempL:
        t = float(ts)
        if t >= tempth:
            solL.append(t)
    return solL

def convert_tempL(tempL):
    solL = []
    for t in tempL:
        solL.append(str(t))
    return solL
