def dih_target_tester(filename,num1,num2):
	list = pickle.load(open(filename+'.txt','rb'))
	target = list[num1][1]
	domain = list[num1][0]
	target = dd.dih_spike_picker(target)
	target = dd.dih_dip_picker(target)
	window = 11
	recursions = 11
	endrange = dih_boxavg_recurs(target,window,recursions)
	targetlist = []
	targetlist.append(domain)
	targetlist.append(endrange)
	peak = max(endrange[(window-1)/2:len(target)-(window-1)/2])
	subpeaklist = [i for i, j in enumerate(endrange) if j== peak]
	peaklist = argrelextrema(endrange,np.greater)
	print peaklist 
	plt.figure()
	plt.plot(domain,endrange)
	plt.plot(domain,list[num1][1])
	crestlist = []
	cresttimelist = []
	extrapeaks = []
	for member in peaklist[0]:
		if member < window or member > (len(endrange)-window):
			extrapeaks.append(member)
			
			continue
		else:
			crestlist.append(endrange[member])
			cresttimelist.append(domain[member])
			plt.plot(domain[member],endrange[member],'yD')
			continue	
	for member in subpeaklist:
		plt.plot(domain[member],endrange[member],'rD')
	print crestlist
	observed = np.array(list[num1][1])
	expected = np.array(endrange)*np.sum(observed)
	chi = chisquare(observed,expected)
	plt.xlabel('Seconds since first data point')
	plt.ylabel('Arbitrary Flux Units')
	plt.title('Lightcurve for '+filename+str(num1)+' '+'chi = '+str(chi[0])+'\n r='+str(recursions)+' w='+str(window) ,y = 1.03)
	plt.savefig('test'+str(num2)+'.ps')
	return targetlist
def dih_target_tester(filename,num1,num2):
	list = pickle.load(open(filename+'.txt','rb'))
	target = list[num1][1]
	domain = list[num1][0]
	target = dd.dih_spike_picker(target)
	target = dd.dih_dip_picker(target)
	window = 5
	endrange = dih_boxavg_recurs(target,window,50)
	targetlist = []
	targetlist.append(domain)
	targetlist.append(endrange)
	diplist = argrelextrema(endrange[window:len(target)-window],np.less)
	print diplist
	peaklist = argrelextrema(endrange[window:len(target)-window], np.greater)
	x_short = domain[window:len(target)-window]
	y_short = target[window:len(target)-window]
	sub_y_list = np.split(y_short,diplist[0])
	sub_x_list = np.split(x_short,diplist[0])
	plt.figure()
	plt.plot(domain,target,color = 'b')
	plt.plot(domain,endrange,color = 'g')
	diff = max(target)-min(target)
	for idx,member in enumerate(sub_y_list):
		member = dd.dih_spike_picker3(member)
		subpeak = max(member)
		if (subpeak-min(target))>.1*diff:
			subpeaklist = [i for i, j in enumerate(member) if j== subpeak]
			for num in subpeaklist:
				plt.plot(sub_x_list[idx][num],member[num],'rD')
		else:
			print 'Bad Peak!'
	plt.xlabel('Seconds since first data point')
	plt.ylabel('Arbitrary Flux Units')
	plt.title('Lightcurve for '+filename+str(num1))
	plt.savefig('test'+str(num2)+'.ps')
	return targetlist
def dih_sun_scratch_plot(dirname,savename,num,newname):
	#directory_lists = finder.dih_dir_finder(dirname)#gets fits files and ivo files
	#fits_list = directory_lists[0]
	#ivo_list = directory_lists[1]
	datalist = pickle.load(open('/Volumes/Scratch/Users/dherman/sundata/rawdata' + savename + str(num) + '.txt','rb'))
	meta_datalist = pickle.load(open('/Volumes/Scratch/Users/dherman/sundata/metadata' + savename + '_meta' + str(num) + '.txt','rb'))
	fits_date = meta_datalist[0]#datetime for first fits file in dirpath
	fits_channel = meta_datalist[1]#channel for first fits
	ivo = meta_datalist[5]
	fits_ev_peak = meta_datalist[10][0]
	fits_ev_peak = datetime.strptime(fits_ev_peak,'%Y-%m-%dT%H:%M:%S')
	print str(fits_channel)
	fits_center = meta_datalist[2]#center of first fits
	fits_box = meta_datalist[-4]
	colors = iter(cm.rainbow(np.linspace(0,1,len(datalist[0])))) #creates color table
	#x,y coordinate data sorting
	testsort = zip(*[datalist[0],datalist[1]])
	sorted_data = sorted(testsort, key=getKey)
	x = np.array(zip(*sorted_data)[0])
	y = np.array(zip(*sorted_data)[1])
	xcopy = x
	x = x - x[0]
	ycopy = y
	yspikeless = spike.dih_spike_picker(y)#removes ultra noisy peaks
	yspikeless = spike.dih_dip_picker(yspikeless)#removes ultra noisy dips
	#channel-selective smoothing
	if fits_channel == 131:
		ysmooth = box.dih_boxavg_recurs(yspikeless,11,2)
		window = 11
	elif fits_channel == 171:
		ysmooth = box.dih_boxavg_recurs(yspikeless,7,2)
		window = 7
	elif fits_channel == 211:
		ysmooth = box.dih_boxavg_recurs(yspikeless,7,3)
		window = 7
	elif fits_channel == 193:
		ysmooth = box.dih_boxavg_recurs(yspikeless,7,2)
		window = 7
	elif fits_channel == 304:
		ysmooth = box.dih_boxavg_recurs(yspikeless,7,2)	
		window = 7
	elif fits_channel == 335:
		ysmooth = box.dih_boxavg_recurs(yspikeless,7,2)
		window = 7
	elif fits_channel == 94:
		ysmooth = box.dih_boxavg_recurs(yspikeless,7,2)
		window = 7
	else:
		print "Bad Channel!"	
	fits_time = datetime.strptime(fits_date,'%Y-%m-%dT%H:%M:%S.%f')
	fits_first = timedelta(seconds = xcopy[0])
	fits_last = timedelta(seconds = xcopy[-1])
	fits_begin = fits_time+fits_first
	fits_end = fits_time+fits_last
	fits_begin = datetime.strftime(fits_begin,'%Y-%m-%dT%H:%M:%S.%f')
	fits_end = datetime.strftime(fits_end,'%Y-%m-%dT%H:%M:%S.%f')
	peaklist = argrelextrema(ysmooth,np.greater)#relative max
	peak = max(ysmooth[(window-1)/2:len(ysmooth)-(window-1)/2])#absolute max ignoring the very ends of the data set
	maxpeaklist = [i for i, j in enumerate(ysmooth) if j == peak]
	plt.figure()
	relpeaktimelist = []
	for member in peaklist[0]:
		if member < window or member > (len(ysmooth)-window):
			continue
		else:
			plt.plot(x[member],ysmooth[member],'yD',markersize =8)
			#recreating peak times from time difference data
			first_time = datetime.strptime(fits_begin,'%Y-%m-%dT%H:%M:%S.%f')
			timediff = timedelta(seconds = x[member])
			peaktime = first_time+timediff
			relpeaktimelist.append(peaktime.strftime('%Y/%m/%d %H:%M:%S.%f'))
			continue	
	maxpeaktimelist = []
	flagged_peaktimelist=[]
	for member in maxpeaklist:
		first_time = datetime.strptime(fits_begin,'%Y-%m-%dT%H:%M:%S.%f')
		timediff = timedelta(seconds = x[member])
		print 'timediff'
		print timediff
		peaktime = first_time+timediff
		if x[member] > 250 and x[member] < x[-1]-250:
			maxpeaktimelist.append(peaktime.strftime('%Y/%m/%d %H:%M:%S.%f'))
		else:
			flagged_peaktimelist.append(peaktime.strftime('%Y/%m/%d %H:%M:%S.%f'))
	real_peaklist = [j for j, j in enumerate(maxpeaklist) if x[j] > 250 and x[j] < x[-1]-250]
	print maxpeaklist
	print real_peaklist
	flagged_peaklist = [j for j, j in enumerate(maxpeaklist) if x[j] < 250 or x[j] > x[-1]-250]
	print flagged_peaklist
	for member in real_peaklist:
		plt.plot(x[member],ysmooth[member],'gD',markersize = 8)
	for member in flagged_peaklist:
		plt.plot(x[member],ysmooth[member],'rD',markersize = 8)
	#creating chi-squared value
	observed = np.array(ycopy)
	expected = np.array(ysmooth)*np.sum(observed)
	chi = chisquare(observed,expected)
	metadatalist = []
	metadatalist.append(fits_begin)
	metadatalist.append(fits_channel)
	metadatalist.append(fits_center)
	metadatalist.append(relpeaktimelist)
	if len(maxpeaktimelist) == 1:
		max_peak_dt = datetime.strptime(maxpeaktimelist[0],'%Y/%m/%d %H:%M:%S.%f')
		peakdiff = fits_ev_peak-max_peak_dt
		peakdiff = peakdiff.total_seconds()
		if abs(peakdiff) < 900:
			metadatalist.append(maxpeaktimelist)
		else:
			metadatalist.append([])
	else:
		metadatalist.append([])	
	metadatalist.append(ivo)
	metadatalist.append(flagged_peaktimelist)
	smooth_range = max(ysmooth)-min(ysmooth)
	#consider raising .75 to .9
	if smooth_range > max(ysmooth)*1.0:
		metadatalist.append('flag')
	else:
		metadatalist.append('clear')
	if len(flagged_peaktimelist) > 0:
		metadatalist.append('peakflag')
		#file_peakflag = open('/data/george/dherman/metadata/' + 'all_peakflag_meta.txt','a')
		#simplejson.dump(metadatalist,file_peakflag)
		#file_peakflag.write('\n')
		#file_peakflag.close()
	else:
		metadatalist.append('no_peakflag')
	metadatalist.append(fits_box)
	metadatalist.append(list(x))
	metadatalist.append(list(ysmooth))
	metadatalist.append(fits_date)
	metadatalist.append(fits_end)	
	#pickling of metadata
	#with open(savename+'_'+newname+'_meta'+str(num)+'.txt','wb') as fff:
		#pickle.dump(metadatalist,fff)
	#shutil.move(savename+'_'+newname+'_meta'+str(num)+'.txt','/data/george/dherman/metadata')
	#with open(savename+'_'+newname+'_chi'+str(num)+'.txt','wb') as fff:
		#pickle.dump(chi,fff)
	#shutil.move(savename+'_'+newname+'_chi'+str(num)+'.txt','/data/george/dherman/metadata')
	#Saving all relavant metadata/peakdata to human readable text file
	#file = open(savename+'_human_'+newname+'_meta'+str(num)+'.txt','wb')
	#simplejson.dump(metadatalist,file)
	#file.close()
	#shutil.move(savename+'_human_'+newname+'_meta'+str(num)+'.txt','/data/george/dherman/metadata')
	#finish up plot characteristics
	plt.plot(x,y,'b',linewidth = 1.0)
	plt.plot(x,ysmooth,'r',linewidth = 1.5)
	plt.title('Lightcurve at'+' '+fits_begin+ ' '+ str(fits_channel)+'$\AA$',y=1.07)
	plt.xlabel('Seconds Since'+' '+fits_begin)
	plt.ylabel('Arbitrary Flux Units')
	plt.savefig('/Volumes/Scratch/Users/dherman/sundata/sun_plots/'+newname+'_'+fits_begin+'_'+savename+str(num)+'.ps')#saves postscript file
	return metadatalist