#!/usr/bin/env python3

# Copyright (c) 2016, 2018 Danny van Dyk
#
# This file is part of the EOS project. EOS is free software;
# you can redistribute it and/or modify it under the terms of the GNU General
# Public License version 2, as published by the Free Software Foundation.
#
# EOS is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307  USA

import argparse
import eos
import os

def main():
    parser = argparse.ArgumentParser(description='Plot 2D marginal distributions')
    parser.add_argument('input', metavar='HDF5FILE', type=str, help='Name of the HDF5 input file')
    parser.add_argument('xvariable', metavar='XVAR', type=str, help='Name of the variable to be plotted on the x axis')
    parser.add_argument('yvariable', metavar='YVAR', type=str, help='Name of the variable to be plotted on the y axis')
    parser.add_argument('output', metavar='PDFFILE', type=str, help='Name of the PDF output file')
    parser.add_argument('--contours', dest='contours', default=False, action='store_true', help='Whether to draw 68%, 95% and 99% probability contours')
    parser.add_argument('--no-histogram', dest='histogram', default=True, action='store_false', help='Whether to histogram the samples')
    parser.add_argument('--xmin', type=float, help='Minimum value on the x axis')
    parser.add_argument('--xmax', type=float, help='Maximum value on the x axis')
    parser.add_argument('--ymin', type=float, help='Minimum value on the y axis')
    parser.add_argument('--ymax', type=float, help='Maximum value on the y axis')

    args = parser.parse_args()

    # ensure that the input file exists
    if not os.path.isfile(args.input):
        error('\'%s\' is not a file' % args.input)

    # instructions for eos.plot.Plotter
    contents = []
    if args.histogram == True:
        contents.append({
            'name': 'histogram2D',
            'type': 'histogram2D',
            'variables': [args.xvariable, args.yvariable],
            'hdf5-file': args.input,
        })
    if args.contours == True:
        contents.append({
            'name': 'contours2D',
            'type': 'contours2D',
            'variables': [args.xvariable, args.yvariable],
            'hdf5-file': args.input,
        })
    contents.append({'type': 'watermark'})

    instructions = {
        'plot': {
            'size': [25, 25], # 25cm x 25cm
            'x': {
                'label': eos.plot.variable_to_latex(args.xvariable),
                'range': [args.xmin, args.xmax],
            },
            'y': {
                'label': eos.plot.variable_to_latex(args.yvariable),
                'range': [args.ymin, args.ymax],
            },
        },
        'contents' : contents,
    }
    if args.xmin or args.xmax:
        instructions['plot']['x']['range'] = [args.xmin, args.xmax]
    if args.ymin or args.ymax:
        instructions['plot']['y']['range'] = [args.ymin, args.ymax]

    # plot data
    plotter = eos.plot.Plotter(instructions, args.output)
    plotter.plot()

    exit(0);

if __name__ == '__main__':
    main()
