#!/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 1D marginal distributions')
    parser.add_argument('input', metavar='HDF5FILE', type=str, help='Name of the HDF5 input file')
    parser.add_argument('variable', metavar='VAR', type=str, help='Name of the variable to be plotted')
    parser.add_argument('output', metavar='PDFFILE', type=str, help='Name of the PDF output file')
    parser.add_argument('--bins', type=int, default=100, help='Number of bins to use for the histogram')
    parser.add_argument('--kde', type=bool, default=False, help='Whether to use Kerndel Density Estimation (KDE) in the plot')
    parser.add_argument('--kde-bandwidth', type=float, default=1.0, help='Scaling factor for the automatically determined KDE bandwidth')
    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')

    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
    instructions = {
        'plot': {
            'size': [25, 25], # 25cm x 25cm
            'x': {
                'label': eos.plot.variable_to_latex(variable),
                'range': [args.xmin, args.xmax],
            },
            'y': {
                'label': r'$\textsf{probability}$',
            },
        },
        'contents' : [
            {
                'name': 'histogram',
                'type': 'histogram',
                'variable': args.variable,
                'bins': args.bins,
                'color': 'C0',
                'opacity': 0.75,
                'hdf5-file': args.input,
            },
            {
                'type': 'watermark'
            }
        ]
    }
    if args.kde:
        kde_item = {
            'name': 'kde',
            'type': 'kde',
            'variable': args.variable,
            'color': 'C0',
            'opacity': 1.0,
            'hdf5-file': args.input,
        }
        if args.kde_bandwidth:
            kde_item.update({'bandwidth': args.kde_bandwidth})

        instructions['contents'].append(kde_item)

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

    exit(0);

if __name__ == '__main__':
    main()
