#!/usr/bin/python

import sys
import optparse

import numpy

from pylal import Fr

##############################################################################
usage = """usage: %prog [options] file1 (file2 file3)

Basic frame reading example.
"""
parser = optparse.OptionParser(usage)
parser.add_option("-c", "--channels",
  help="comma-delimited channels to read from the frame files")
parser.add_option("-p", "--plot-filename",
  help="the filename to which you wish to save the plot")
parser.add_option("-s", "--show-plot", action="store_true", default=False,
  help="draw plot to screen")

opts, args = parser.parse_args()

# check at least one file was specified
if len(args) == 0:
  print >>sys.stderr, "Error: a frame file must be specified"
  print >>sys.stderr, "Enter 'plotchannel --help' for usage"
  sys.exit(2)

if opts.channels is None:
  print >>sys.stderr, "Error: a channel name must be specified"
  print >>sys.stderr, "Enter 'plotchannel --help' for usage"
  sys.exit(2)

# switch to non-graphical backend to prevent X server dependence
if not opts.show_plot:
    import matplotlib
    matplotlib.use("Agg")
import pylab
from pylal import plotutils

# channel names have characters of significance to LaTeX
pylab.rc("text", usetex=False)


channels = opts.channels.split(",")
filenames = args

for channel in channels:
  plot = plotutils.SimplePlot()
  for filename in filenames:
    y_data, x0, x1, dx, x_unit, y_unit = Fr.frgetvect1d(filename, channel)
    n = len(y_data)

    x_data = numpy.arange(x0 + x1, x0 + x1 + n*dx, dx)

    print filename + ": " + channel
    print "len(y_data) =", n
    print "x_0 =", x0
    print "x_1 =", x1
    print "dx =", dx
    print "x_unit =", x_unit
    print "y_unit =", y_unit

    plot.add_content(x_data, y_data, label=filename + ": " + channel)

  plot.finalize()
  if opts.plot_filename is not None:
    plot.savefig(opts.plot_filename)
  if not opts.show_plot:
    plot.close()

if opts.show_plot:
    pylab.show()
