def dih_event_crop_select(filename1):
	#get list of related start times
	shared_times = dih_shared_groups(filename1)
	meta_file = open(filename1, 'r')
	meta_lines = meta_file.readlines()
	all_meta_datalist = []
	for member in meta_lines:
		member = ast.literal_eval(member)
		all_meta_datalist.append(member)
	event_list = []
	#getting full metadata for each event with time in shared_times
	for member in shared_times:
		event = []
		for guy in member:
			for part in all_meta_datalist:
				if part[0] == guy:
					event.append(part)
					break	
		event_list.append(event)
	uber_shared_list = []
	for idx,member in enumerate(event_list):
		print idx
		#setting cut_off so program knows to ignore extra long data sets
		cut_off = timedelta(seconds = dih_event_range(member))
		print cut_off
		shared_list = []
		for guy in member:
			cut_off_time = datetime.strptime(guy[0],'%Y-%m-%dT%H:%M:%S.%f')+cut_off
			#find peak that corresponds to peak from original event file
			peak_target = dih_ev_peak_compare(guy[3]+guy[4],guy[10][0])
			channel = guy[1]
			ivo = guy[5]
			if peak_target < cut_off_time:
				shared_list.append((datetime.strftime(peak_target,'%Y/%m/%d %H:%M:%S.%f'),channel,ivo))
		channel_list = [131,171,211,304,335,193]
		shared_no_copy_list = []
		for idx,channel in enumerate(channel_list):
			channel_group = [j for j, j in enumerate(shared_list) if j[1] == channel]
			if len(channel_group)>0:
				shared_no_copy_list.append(channel_group[0])
			else:
				continue
		uber_shared_list.append(shared_no_copy_list)
	return uber_shared_list
def dih_event_select(filename1):
	#get list of related start times
	shared_times = dih_shared_groups(filename1)
	meta_file = open(filename1, 'r')
	meta_lines = meta_file.readlines()
	all_meta_datalist = []
	for member in meta_lines:
		member = ast.literal_eval(member)
		all_meta_datalist.append(member)
	event_list = []
	#getting full metadata for each event with time in shared_times
	for member in shared_times:
		event = []
		for guy in member:
			for part in all_meta_datalist:
				if part[0] == guy:
					event.append(part)
					break	
		event_list.append(event)
	uber_shared_list = []
	for idx,member in enumerate(event_list):
		print idx
		primary_peaks = []
		secondary_peaks = []
		#setting cut_off so program knows to ignore extra long data sets
		cut_off = timedelta(seconds = dih_event_range(member))
		print cut_off
		for guy in member:
			cut_off_time = datetime.strptime(guy[0],'%Y-%m-%dT%H:%M:%S.%f')+cut_off
			if guy [7] == 'flag':
				print 'flag'
				continue
			#getting relative peaks
			for tim in guy[3]:
				timeval = datetime.strptime(tim,'%Y/%m/%d %H:%M:%S.%f')
				channel = guy[1]
				ivo = guy[5]
				if timeval < cut_off_time:
					secondary_peaks.append((timeval,channel,ivo))
			#getting primary peaks
			for tim in guy[4]:
				timeval = datetime.strptime(tim,'%Y/%m/%d %H:%M:%S.%f')
				channel = guy[1]
				ivo = guy[5]
				if timeval < cut_off_time:
					primary_peaks.append((timeval,channel,ivo))
		shared_peak_list = []
		#Attempt at removing extra events in every channel (needs to be refined most likely)
		channel_list = [171,131,211,304,335,193]
		primary_no_copy_peaks = []
		for idx,channel in enumerate(channel_list):
			channel_group = [j for j, j in enumerate(primary_peaks) if j[1] == channel]
			if len(channel_group) > 0:
				primary_no_copy_peaks.append(channel_group[0])
			else:
				continue
		all_peaks = primary_no_copy_peaks + secondary_peaks
		#comparing primary peaks to all other peaks in other channels
		for peak in primary_no_copy_peaks:
			print peak	
			peak_time = datetime.strftime(peak[0],'%Y/%m/%d %H:%M:%S.%f')
			sub_peak_list = [(peak_time,peak[1],peak[2])]
			compare_peaks = [j for j, j in enumerate(all_peaks) if j[1] != peak[1]]
			compare_list = []
			for channel in channel_list:
				compare_peaks_by_channel = list(set([j for j, j in enumerate(compare_peaks) if j[1] == channel]))
				compare_list.append(compare_peaks_by_channel)
			for group in compare_list:
				compare_times = []
				for thing in group:
					time_diff = peak[0]-thing[0]
					num_diff = abs(time_diff.total_seconds())
					compare_times.append(num_diff)
				compare_min = [i for i, j in enumerate(compare_times) if j == min(compare_times)]
				if len(compare_min) == 1:
					interest = list(group[compare_min[0]])
					interest[0] = datetime.strftime(interest[0],'%Y/%m/%d %H:%M:%S.%f')
					print interest
					sub_peak_list.append(tuple(interest))		
			shared_peak_list.append(list(set(tuple(sub_peak_list))))
		uber_shared_list.append(shared_peak_list)			
	return uber_shared_list