# gmplot1.py
# Written by Todd Wareham for CS 2500
"""
Given a plot specification file as a command-line argument, plots the 
 specified function in the specified range. If an additional command-line 
 argument plot-file is also given, saves plot to that file in the format 
 specified by the file's extension.

Note that this is implemented using x-data/function plotting, where x-data is
 explicitly created as a NumPy-style array.
"""

from pylab import *


## Plot-line style constant dictionaries

lineColor = {"blue" : "b", "green" : "g", "red" : "r", "cyan" : "c", 
             "magenta" : "m", "yellow" : "y", "black" : "k"}

lineStyle = {"solid" : "-", "dashed" : "--", "dash-dot" : "-.", 
             "dotted" : ":", "none" : ""}

markerStyle = {"point" : ".", "circle" : "o", "triangle" : "^", 
               "square" : "s", "pentagon" : "p", "hexagon" : "h",
	       "plus" : "+", "cross" : "x", "diamond" : "D"}


if len(sys.argv) not in [2, 3]:
    print "usage: ", sys.argv[0], " nlim {plot-file}"
    sys.exit(1)

## Read plot-spefication file

f = open(sys.argv[1], "r")
nlim, flim = [int(x) for x in f.readline().split()]
funcStr, rlineColor, rlineStyle, rmarkerStyle = \
    [x for x in f.readline().split()]
f.close()

## Set up plot data

x = arange(1, (nlim + 1), 2)

## Describe plot characteristics

xlabel("x")
ylabel(funcStr)
title("specified function plot")

## Create plotted line

plotLineSpec = lineColor[rlineColor] + lineStyle[rlineStyle] + \
	       markerStyle[rmarkerStyle]

plot(x, eval(funcStr), plotLineSpec)

if flim != -1: ylim(0, flim)

## Display plotted figure

print ">>> Creating requested plot ..."

show()

## Save plotted figure

if len(sys.argv) == 3:
    print ">>> Saving requested plot ..."
    savefig(sys.argv[2])

print ">>> Plotting finished"

