#!/usr/bin/env python
#
# Author: Mike McKerns (mmckerns @uqfoundation)
# Author: Daniel Stouffer (daniel @stoufferlab.org)
# Copyright (c) 2013 Daniel Stouffer.
# Copyright (c) 2023-2026 The Uncertainty Quantification Foundation.
# License: 3-clause BSD.  The full license text is available at:
#  - https://github.com/uqfoundation/pygrace/blob/master/LICENSE
#
from pygrace.parser import XYParser
from pygrace.project import Project
from pygrace.extensions.distribution import CDFGraph

class CDFParser(XYParser):
    def __init__(self, *args, **kwargs):
        XYParser.__init__(self, *args, **kwargs)

        # adding specific CDF options here
        self.add_option('-f', '--dots', dest='fancy_dots', action='store_true',
                          help='format cdf with fancy open dots [%default]')
        self.add_option('-w', '--width', dest='print_width', type='float',
                          help="load the input as this type ['%default']")
        self.add_option('-o', '--out_file', dest='out_file', type='str',
                          help="write output as this file ['%default']")
        self.add_option('-l', '--log_axes', dest='log_axes', type='str',
                          help="output with log axes ['%default']")

        self.set_defaults(
            fancy_dots = None,
            print_width = 6.0,
            out_file = None,
            log_axes = None,
            )

    def parse_log_axes(self, options):
        content = options.log_axes
        if content == 'x':
            return True, False
        elif content == 'y':
            return False, True
        elif content == 'xy':
            return True, True
        elif content:
            message = "'%s' is not a valid option for log_axes" % content
            self.error(message)
        else:
            return False, False

    def parse_args(self, *args, **kwargs):
        options, data = XYParser.parse_args(self, *args, **kwargs)

        # parse the string that is passed on command line
        options.log_xaxis, options.log_yaxis = self.parse_log_axes(options)

        return options, data
        
if __name__ == '__main__':

    parser = CDFParser('usage: %prog [options] filename')
    options, data = parser.parse_args()

    grace = Project()
    graph = grace.add_graph(CDFGraph, data, options.fancy_dots)

    if options.log_xaxis: graph.logx()
    if options.log_yaxis: graph.logy()

    grace.autoformat(options.print_width)

    if options.out_file: grace.write_file(options.out_file)
    else: print(grace)
