import numpy as npy

def checkplotoptions(md,options):
	'''
	CHECKPLOTOPTIONS - build a structure that holds all plot options

		Usage:
			options=checkplotoptions(md,options)

		See also: PLOTMODEL

		NOTE: not fully implemented yet
	'''


	#units
	if options.exist('unit'):
		if 'km' in options.getfieldvalue('unit','km'):
			options.changefieldvalue('unit',10**-3)
		elif '100km' in options.getfieldvalue('unit','100km'):
			options.changefieldvalue('unit',10**-5)
	
	#density
	if options.exist('density'):
		density=options.getfieldvalue('density')
		options.changefieldvalue('density',abs(ceil(density)))
	
	#show section
	if options.exist('showsection'):
		if 'on' in options.getfieldvalue('showsection','on'):
			options.changefieldvalue('showsection',4)
	
	#smooth values
	if options.exist('smooth'):
		if 'on' in options.getfieldvalue('smooth','on'):
			options.changefieldvalue('smooth',0)

	#contouronly values
	if options.exist('contouronly'):
		if 'on' in options.getfieldvalue('contouronly','on'):
			options.changefieldvalue('contouronly',1)

	#colorbar
	if options.exist('colorbar'):
		if 'on' in options.getfieldvalue('colorbar','on'):
			options.changefieldvalue('colorbar',1)
		elif 'off' in options.getfieldvalue('colorbar','off'):
			options.changefieldvalue('colorbar',0)

	#text
	if options.exist('text'):

		# text values (coerce to list for consistent functionality)
		textlist=[]
		text=options.getfieldvalue('text','default text')
		textlist.extend([text] if isinstance(text,str) else text)
		numtext=len(textlist)

		# text position	
		textpos=options.getfieldvalue('textposition',[0.5,0.5])
		if not isinstance(textpos,list):
			raise Exception('textposition should be passed as a list')
		textx=[item[0] for item in textpos]
		texty=[item[1] for item in textpos]
		if len(textx)!=numtext or len(texty)!=numtext:
			raise Exception('textposition should contain one list of x,y vertices for every text instance')

		# font size
		if options.exist('textsize'):
			textsize=options.getfieldvalue('textsize',12)
			sizelist=[]
			sizelist.extend(textsize if isinstance(textsize,list) else [textsize])
		else:
			sizelist=[12]
		if len(sizelist)==1:
			sizelist=npy.tile(sizelist,numtext)

		# font color
		if options.exist('textcolor'):
			textcolor=options.getfieldvalue('textcolor','k')
			colorlist=[]
			colorlist.extend(textcolor if isinstance(textcolor,list) else [textcolor])
		else:
			colorlist=['k']
		if len(colorlist)==1:
			colorlist=npy.tile(colorlist,numtext)

		# textweight
		if options.exist('textweight'):
			textweightvalues=options.getfieldvalue('textweight')
			weightlist=[]
			weightlist.extend(textweight if isinstance(textwieght,list) else [textweight])
		else:
			weightlist=['normal']
		if len(weightlist)==1:
			weightlist=npy.tile(weightlist,numtext)

		# text rotation
		if options.exist('textrotation'):
			textrotation=options.getfieldvalue('textrotation',0)
			rotationlist=[]
			rotationlist.extend(textrotation if isinstance(textrotation,list) else [textrotation])
		else:
			rotationlist=[0]
		if len(rotationlist)==1:
				rotationlist=npy.tile(rotationlist,numtext)

		options.changefieldvalue('text',textlist)
		options.addfield('textx',textx)
		options.addfield('texty',texty)
		options.changefieldvalue('textsize',sizelist)
		options.changefieldvalue('textcolor',colorlist)
		options.changefieldvalue('textweight',weightlist)
		options.changefieldvalue('textrotation',rotationlist)

	#expdisp
	expdispvaluesarray=[0,0]
	expstylevaluesarray=[0,0]
	expstylevalues=[0,0]
	if options.exist('expstyle'):
		expstylevalues=options.getfieldvalue('expstyle')
	if options.exist('expdisp'):
		expdispvalues=options.getfieldvalue('expdisp')
		for i in npy.arange(len(expdispvalues)):
			expdispvaluesarray.append(expdispvalues[i])
			if len(expstylevalues)>i+1:
				expstylevaluesarray.append(expstylevalues[i])
			else:
				expstylevaluesarray.append('-k')

	options.changefieldvalue('expstyle',expstylevaluesarray)
	options.changefieldvalue('expdisp',expdispvaluesarray)

	#latlonnumbering
	if options.exist('latlonclick'):
		if 'on' in options.getfieldvalue('latlonclick','on'):
			options.changefieldvalue('latlonclick',1)

	#northarrow
	if options.exist('northarrow'):
		if 'on' in options.getfieldvalue('northarrow','on'):
			#default values
			Lx=max(md.mesh.x)-min(md.mesh.x)
			Ly=max(md.mesh.y)-min(md.mesh.y)
			options.changefieldvalue('northarrow',[min(md.mesh.x)+1./6.*Lx, min(md.mesh.y)+5./6.*Ly, 1./15.*Ly, 0.25, 1./250.*Ly])

	#scale ruler
	if options.exist('scaleruler'):
		if 'on' in options.exist('scaleruler','on'):
			Lx=max(md.mesh.x)-min(md.mesh.x)
			Ly=max(md.mesh.y)-min(md.mesh.y)
			options.changefieldvalue('scaleruler',[min(md.mesh.x)+6./8.*Lx, min(md.mesh.y)+1./10.*Ly, 10**(ceil(log10(Lx)))/5, floor(Lx/100), 5])

	#log scale
	if options.exist('log'):
		if options.exist('caxis'):
			options.changefieldvalue('caxis',log(options.getfieldvalue('caxis'))/log(options.getfieldvalue('log')))
		options.changefieldvalue('cutoff',log(options.getfieldvalue('cutoff',1.5))/log(options.getfieldvalue('log')))

	return options
