#!/usr/bin/env python
"""Tool to create convergence plots for Amp."""

import matplotlib
# The 'Agg' command must be *before* all other matplotlib imports for
# headless operation.
matplotlib.use('Agg')

from optparse import OptionParser

from amp.analysis import plot_convergence


parser = OptionParser(
    usage='usage: %prog logfile [plotfile]\n Create convergence plot'
          ' logfile is amp log file; plotfile is an optional filename '
          ' for the output that takes any allowable matplotlib format.')
options, args = parser.parse_args()

if len(args) not in [1, 2]:
    raise RuntimeError('Bad number of arguments.')
plotfile = 'convergence.pdf'
if len(args) == 2:
    plotfile = args.pop(-1)
logfile = args.pop(0)

plot_convergence(logfile=logfile, plotfile=plotfile)
