from numpy import *
from pylab import *

class OneDimMap:
	
	def __init__(self, ind_var, param):	# ind_var = [theta_0, theta_f], or [a_0, a_f], etc.	param = [a,b,gamma,theta_0] regardless of ind_var 
		self.IC = param[3]		# 
		self.current = param[3]		# current y_value
		self.ind = ind_var
		self.param = param	# should be a list including [a,b,gamma,theta_0]
		self.a = param[0] 
		self.b = param[1]
		self.gamma = param[2]
		self.theta = ind_var[0] 
		self.ThetaMax = pi
		self.t = 0.0	# change this, used in some map tools
		self.nTransients = 50	# how many iterations to throw away to avoid plotting transient behavior
		self.nIterates = 50	#250  how many iterations to plot at each r
		self.nSteps = 100	#400 # how many r-values will be swept through
		self.theta_n = [ ]

	def iterate2(self,r):	# map from OneDMaps
		self.t = 0.0
		self.current,transienttheta,self.t,transient_tau = self.maptransients(r)
		self.theta_n = [ ]	# see if this helps
		#self.current,self.theta_n,self.t,tau_i_map = self.map(r)	#ignores transients!
		self.current,thetaN,self.t,tau_i_map = self.map(r)
		#for i in xrange(self.nTransients):	## fix this!***!***!***!
		#	self.current = self.map(r)		## Don't pass self!!	
		rsweep = [r,]*int(self.nIterates)
		#rsweep = [ ]   # The parameter value
		#theta = [ ]        # The iterates
		#theta = 
		#for i in xrange(self.nIterates):
		#	self.current = self.map(r)		## Don't pass self!!
		#	rsweep.append(r)			# --> [r,r,r,r,... ]
		#	theta.append( self.current )
		return thetaN, rsweep
	
	def show(self):		# plot map
		figure(1,(12,9))	# Setup the plot; It's numbered 1 and is 8 x 6 inches.
		# Stuff parameter range into a string via the string formating commands.
		TitleString  = self.name
		TitleString += ' from [%g,%g]' % (self.ind[0],self.ind[1])
		title(TitleString)
		# Label axes
		xlabel('Control parameter ' + self.independent_var)
		ylabel(self.ylabel)	#{Theta(n)}')
		# We tell plot()'s autoscaling the range in (r,x) we want by
		#	putting dots at the corners of the desired data window.
		#	Otherwise, the plot constantly rescales with new data.
		#plot([self.ind[1]], [1.0], 'k,')
		#plot([self.ind[1]], [0.0], 'k,')
		#plot([self.ind[0]], [0.0], 'k,')
		#plot([self.ind[0]], [1.0], 'k,')
		# Sweep the control parameter over the desired range
		rInc = (self.ind[1]-self.ind[0])/float(self.nSteps)
		for r in arange(self.ind[0],self.ind[1],rInc):
			# Set the initial condition to the reference value
			self.current = r	# since theta = theta_0 initially	#self.IC
			# Throw away the transient iterations
			x, rsweep = self.iterate2(r)				## Don't pass self!!
			plot(rsweep, x, 'ko',markersize=.5) # Plot the list of (r,x) pairs as pixels, ms = .5
		#savefig(self.name[0:8]+'.ps')
		# Display plot in window
		#axis([self.param[0],self.param[1],-10.,0.])
		show()
	
	#def SavePlot(self,filename):
	#	savefig(filename)

