#!/usr/bin/env python
# vim:ft=python

# Average over files specified in argument
# Assumes first 'n' (default=1) columns are keys

import sys
import numpy as np

def read_from_files(files):
    # Store for the files

    values = []
    for f in files:
        if f == "-":
            f = sys.stdin
        else:
            f = open( f )
        for line in f.xreadlines():
            values.append( np.array( map( float, line.split() ) ) )

    return np.array( values )

def plot_scatter( values, bins = 10, out = None ):
    import matplotlib.pyplot as plt

    for (label, vs) in values:
        plt.scatter( vs.T[0], vs.T[1], label=label )
    plt.legend()
    plt.show()
    if out is not None:
        plt.savefig( out )

def main(*args):
    if len(args) == 0 or args[0] == "-h":
        print "Usage scatter [-h] <list-of-files>" 
        sys.exit(1)

    if args[0] == "-bins" or args[0] == "-b" :
        bins = int( args[1] )
        files = args[2:]
    else:
        bins = 10
        files = args

    values = [ (f, read_from_files( [f] ) ) for f in files ]
    plot_scatter( values, bins, files[0] + ".png" )


if __name__ == "__main__":
    main(*sys.argv[1:])

