
# coding: utf-8

import sys
import numpy
import matplotlib.pyplot


def analyse (filename, outfile=None):
    """ Analyse a set of data, calculate the mean max and min and create three graphs side by sid within one plot
    """
    data = numpy.loadtxt(fname=filename, delimiter=',')
    
    # create a wide figure to hold the sublots
    fig = matplotlib.pyplot.figure (figsize=(10.0, 3.0))

    # create placeholder for plots
    subplot1 = fig.add_subplot(1,3,1)
    subplot2 = fig.add_subplot(1,3,2)
    subplot3 = fig.add_subplot(1,3,3)

    subplot1.set_ylabel('average')
    subplot1.plot(numpy.mean(data, axis=0))

    subplot2.set_ylabel('max')
    subplot2.plot(numpy.max(data, axis=0))

    subplot3.set_ylabel('min')
    subplot3.plot(numpy.min(data, axis=0))

    fig.tight_layout()
    if outfile is None:
        matplotlib.pyplot.show()
    else:
        matplotlib.pyplot.savefig(outfile)


def detect_problems (filename):
    """Some of our temperatures have problems check for these
    Function reads file (filename) and reports on odd looking maxima and minima. the function does not return data it adds
    a label to the graph
    """
    data = numpy.loadtxt(fname=filename, delimiter=',')
    
    if numpy.max (data, axis=0)[0] == 0 and numpy.max (data, axis=0)[20] == 20:
        print ('suspicious looking maxima')
    elif numpy.sum(numpy.min(data, axis=0)) ==0:
        print ('minimum add to zero')
    else:
        print ('data looks ok')

if __name__ == "__main__":
        
    print('Running', sys.argv[0])
    print (sys.argv[1])
    analyse (sys.argv[1],outfile=sys.argv[2])
    detect_problems (sys.argv[1])

    