def zv_subroutine(path, file_name):
	log = 'z(V) spectra "{}":\n'.format(file_name)
	log += '  folder: {}\n'.format(path)
	
	allZs = spec.ZVSTSBundle(path+file_name)
	
	rr = allZs.has_ramp_reversal
	if rr:
		log += '  ramp reversal detected\n'
		allZs_f, allZs_r = allZs.ramp_reversal_fix(opt='split')
		log += '  keeping track of ramp directions\n'
		
		N_spec = allZs_f.N
		mZ_f = allZs_f.coavg()
		mZ_r = allZs_r.coavg()
		V = allZs_f.X
	else:
		N_spec = allZs.N
		mZ = allZs.coavg()
		V = allZs.X
	# END if
	
	log += '  {} spectra in sample\n'.format(N_spec)
	
	# make density plot for all the z(V) spectra
	fig = plt.figure()
	ax = fig.add_subplot(111)
	if rr:
		spec.plot_density(allZs_f, ax)
		spec.plot_density(allZs_r, ax, mkrcolor='red')
		ax.plot(V, mZ_f, color=(0.5,0.6,1.0), linewidth=2)
		ax.plot(V, mZ_r, color=(1.0,0.5,0.5), linewidth=2)
	else:
		spec.plot_density(allZs, ax)
		ax.plot(V, mZ, color=(0.5,0.6,1.0), linewidth=2)
	# END if
	ax.set_title( '{}, N={}'.format(file_name, N_spec) )
	spec.pltformat_basic(ax)
	save_name = re.sub(r'\.[zi]vms(?:\.asc)?$', '-zv.png', file_name)
	ax.figure.savefig(path+save_name, dpi=150)
	plt.close(fig)
	log += '  saved "{}"\n'.format(save_name)
	
	# create a dz/dV(V) spectra bundle
	x_window = 0.1 #V
	poly_order = 1
	deriv_order = 1
	if rr:
		alldZs_f = allZs_f.deriv_sg(x_window, poly_order, deriv_order)
		alldZs_r = allZs_r.deriv_sg(x_window, poly_order, deriv_order)
		mdZ_f = alldZs_f.coavg()
		mdZ_r = alldZs_r.coavg()
	else:
		alldZs = allZs.deriv_sg(x_window, poly_order, deriv_order)
		mdZ = alldZs.coavg()
	# END if
	
	# make density plot for all the dz/dV(V) spectra
	fig = plt.figure()
	ax = fig.add_subplot(111)
	if rr:
		spec.plot_density(alldZs_f, ax)
		spec.plot_density(alldZs_r, ax, mkrcolor='red')
		ax.plot(V, mdZ_f, color=(0.5,0.6,1.0), linewidth=2)
		ax.plot(V, mdZ_r, color=(1.0,0.5,0.5), linewidth=2)
	else:
		spec.plot_density(alldZs, ax)
		ax.plot(V, mdZ, color=(0.5,0.6,1.0), linewidth=2)
	# END if
	ax.set_title( '{}, N={}'.format(file_name, N_spec) )
	spec.pltformat_basic(ax)
	save_name = re.sub(r'\.[zi]vms(?:\.asc)?$', '-dzdv.png', file_name)
	ax.figure.savefig(path+save_name, dpi=150)
	plt.close(fig)
	log += '  saved "{}"\n'.format(save_name)
	
	print log
def iv_subroutine(path, file_name):
	log = 'I(V) spectra "{}":\n'.format(file_name)
	log += '  folder: {}\n'.format(path)
	
	allIs = spec.IVSTSBundle(path+file_name)
	
	rr = allIs.has_ramp_reversal
	if rr:
		log += '  ramp reversal detected\n'
		allIs_f, allIs_r = allIs.ramp_reversal_fix(opt='split')
		log += '  keeping track of ramp directions\n'
		
		N_spec = allIs_f.N
		mI_f = allIs_f.coavg()
		mI_r = allIs_r.coavg()
		V = allIs_f.X
	else:
		N_spec = allIs.N
		mI = allIs.coavg()
		V = allIs.X
	# END if
	log += '  {} spectra in sample\n'.format(N_spec)
	
	# make density plot for all the I(V) spectra
	fig = plt.figure()
	ax = fig.add_subplot(111)
	if rr:
		spec.plot_density(allIs_f, ax)
		spec.plot_density(allIs_r, ax, mkrcolor='red')
		ax.plot(V, mI_f, color=(0.5,0.6,1.0), linewidth=2)
		ax.plot(V, mI_r, color=(1.0,0.5,0.5), linewidth=2)
	else:
		spec.plot_density(allIs, ax)
		ax.plot(V, mI, color=(0.5,0.6,1.0), linewidth=2)
	# END if
	ax.set_title( '{}, N={}'.format(file_name, N_spec) )
	spec.pltformat_basic(ax)
	save_name = re.sub(r'\.[zi]vms(?:\.asc)?$', '-iv.png', file_name)
	ax.figure.savefig(path+save_name, dpi=150)
	plt.close(fig)
	log += '  saved "{}"\n'.format(save_name)
	
	# create a dz/dV(V) spectra bundle
	x_window = 0.1 #V
	poly_order = 1
	if rr:
		allndIs_f = allIs_f.norm_deriv(x_window, poly_order)
		allndIs_r = allIs_r.norm_deriv(x_window, poly_order)
		ndmI_f = spec.norm_deriv(V, allIs_f.coavg(), x_window, poly_order)
		ndmI_r = spec.norm_deriv(V, allIs_r.coavg(), x_window, poly_order)
	else:
		allndIs = allIs.norm_deriv(x_window, poly_order)
	# END if
	
	# make density plot for all the norm. dI/dV(V) spectra
	fig = plt.figure()
	ax = fig.add_subplot(111)
	if rr:
		spec.plot_density(allndIs_f, ax)
		spec.plot_density(allndIs_r, ax, mkrcolor='red')
		ax.plot(V, ndmI_f, color=(0.5,0.6,1.0), linewidth=2)
		ax.plot(V, ndmI_r, color=(1.0,0.5,0.5), linewidth=2)
		ax.plot(V, allndIs_f.coavg(), color=(0.5,0.5,0.6), linewidth=1)
		ax.plot(V, allndIs_r.coavg(), color=(0.6,0.5,0.5), linewidth=1)
	else:
		spec.plot_density(allndIs, ax)
		ax.plot(V, allndIs.coavg(), color=(0.5,0.6,1.0), linewidth=2)
	# END if
	ax.set_title( '{}, N={}'.format(file_name, N_spec) )
	spec.pltformat_basic(ax)
	spec.tight_scale(ax)
	
	save_name = re.sub(r'\.[zi]vms(?:\.asc)?$', '-ndidv.png', file_name)
	ax.figure.savefig(path+save_name, dpi=150)
	plt.close(fig)
	log += '  saved "{}"\n'.format(save_name)
	
	print log
def _iv_subroutine(path, file_name):
	log = 'I(V) spectra "{}":\n'.format(file_name)
	log += '  folder: {}\n'.format(path)
	
	allIs = spec.IVSTSBundle(path+file_name)
	
	rr = allIs.has_ramp_reversal
	if rr:
		log += '  ramp reversal detected\n'
		allIs_f, allIs_r = allIs.ramp_reversal_fix(opt='split')
		log += '  keeping track of ramp directions\n'
		
		N_spec = allIs_f.N
		mI_f = allIs_f.coavg()
		mI_r = allIs_r.coavg()
		V = allIs_f.X
	else:
		N_spec = allIs.N
		mI = allIs.coavg()
		V = allIs.X
	# END if
	log += '  {} spectra in sample\n'.format(N_spec)
	
	# make density plot for all the I(V) spectra
	fig = plt.figure()
	ax = fig.add_subplot(111)
	if rr:
		spec.plot_density(allIs_f, ax)
		spec.plot_density(allIs_r, ax, mkrcolor='red')
		ax.plot(V, mI_f, color=(0.5,0.6,1.0), linewidth=2)
		ax.plot(V, mI_r, color=(1.0,0.5,0.5), linewidth=2)
	else:
		spec.plot_density(allIs, ax)
		ax.plot(V, mI, color=(0.5,0.6,1.0), linewidth=2)
	# END if
	ax.set_title( '{}, N={}'.format(file_name, N_spec) )
	spec.pltformat_basic(ax)
	save_name = re.sub(r'\.[zi]vms(?:\.asc)?$', '-iv.png', file_name)
	ax.figure.savefig(path+save_name, dpi=150)
	plt.close(fig)
	log += '  saved "{}"\n'.format(save_name)
	
	# create a dz/dV(V) spectra bundle
	x_window = 0.1 #V
	poly_order = 1
	if rr:
		allndIs_f = allIs_f.norm_deriv(x_window, poly_order)
		allndIs_r = allIs_r.norm_deriv(x_window, poly_order)
		dv = V[1] - V[0]
		ndmI_f = spec.norm_deriv(V, allIs_f.coavg(), x_window, poly_order)
		ndmI_r = spec.norm_deriv(V, allIs_r.coavg(), x_window, poly_order)
	else:
		allndIs = allIs.norm_deriv(x_window, poly_order)
	# END if
	
	# make density plot for all the norm. dI/dV(V) spectra
	fig = plt.figure()
	ax = fig.add_subplot(111)
	if rr:
		spec.plot_density(allndIs_f, ax)
		spec.plot_density(allndIs_r, ax, mkrcolor='red')
		ax.plot(V, ndmI_f, color=(0.5,0.6,1.0), linewidth=2)
		ax.plot(V, ndmI_r, color=(1.0,0.5,0.5), linewidth=2)
		ax.plot(V, allndIs_f.coavg(), color=(0.5,0.5,0.6), linewidth=1)
		ax.plot(V, allndIs_r.coavg(), color=(0.6,0.5,0.5), linewidth=1)
	else:
		spec.plot_density(allndIs, ax)
		ax.plot(V, allndIs.coavg(), color=(0.5,0.6,1.0), linewidth=2)
	# END if
	ax.set_title( '{}, N={}'.format(file_name, N_spec) )
	spec.pltformat_basic(ax)
	spec.tight_scale(ax)
	
	save_name = re.sub(r'\.[zi]vms(?:\.asc)?$', '-ndidv.png', file_name)
	ax.figure.savefig(path+save_name, dpi=150)
	plt.close(fig)
	log += '  saved "{}"\n'.format(save_name)
	
	print log
def _giv_subroutine(path, file_name):
	log = 'I(V) spectra "{}":\n'.format(file_name)
	gspec_file_name = re.sub(r'\.ivms', '.gvms', file_name)
	log += '  dI/dV(V) spectra: "{}"'.format(gspec_file_name)
	log += '  folder: {}\n'.format(path)
	
	allIs = spec.IVSTSBundle(path+file_name)
	alldIs = spec.IVSTSBundle(path+gspec_file_name)
	
	rr = allIs.has_ramp_reversal
	if rr:
		log += '  ramp reversal detected\n'
		allIs_f, allIs_r = allIs.ramp_reversal_fix(opt='split')
		alldIs_f, alldIs_r = alldIs.ramp_reversal_fix(opt='split')
		log += '  keeping track of ramp directions\n'
		
		N_spec = allIs_f.N
		mI_f = allIs_f.coavg()
		mI_r = allIs_r.coavg()
		mdI_f = alldIs_f.coavg()
		mdI_r = alldIs_r.coavg()
		V = allIs_f.X
	else:
		N_spec = allIs.N
		mI = allIs.coavg()
		mdI = alldIs.coavg()
		V = allIs.X
	# END if
	log += '  {} spectra in sample\n'.format(N_spec)
	
	# make density plot for all the I(V) spectra
	fig = plt.figure()
	ax = fig.add_subplot(111)
	if rr:
		spec.plot_density(allIs_f, ax)
		spec.plot_density(allIs_r, ax, mkrcolor='red')
		ax.plot(V, mI_f, color=(0.5,0.6,1.0), linewidth=2)
		ax.plot(V, mI_r, color=(1.0,0.5,0.5), linewidth=2)
	else:
		spec.plot_density(allIs, ax)
		ax.plot(V, mI, color=(0.5,0.6,1.0), linewidth=2)
	# END if
	ax.set_title( '{}, N={}'.format(file_name, N_spec) )
	ax.set_xlabel('Gap Bias ({})'.format(allIs.units[0]))
	ax.set_ylabel('Tunneling Current ({})'.format(allIs.units[1]))
	spec.pltformat_basic(ax)
	save_name = re.sub(r'\.[zi]vms(?:\.asc)?$', '-iv.png', file_name)
	ax.figure.savefig(path+save_name, dpi=150)
	plt.close(fig)
	log += '  saved "{}"\n'.format(save_name)
	
	# make density plot for all the dI/dV(V) spectra
	fig = plt.figure()
	ax = fig.add_subplot(111)
	if rr:
		spec.plot_density(alldIs_f, ax)
		spec.plot_density(alldIs_r, ax, mkrcolor='red')
		ax.plot(V, mdI_f, color=(0.5,0.6,1.0), linewidth=2)
		ax.plot(V, mdI_r, color=(1.0,0.5,0.5), linewidth=2)
	else:
		spec.plot_density(alldIs, ax)
		ax.plot(V, mdI, color=(0.5,0.6,1.0), linewidth=2)
	# END if
	ax.set_title( '{}, N={}'.format(file_name, N_spec) )
	ax.set_xlabel('Gap Bias ({})'.format(alldIs.units[0]))
	ax.set_ylabel('Lock-in Signal ({})'.format(alldIs.units[1]))
	spec.pltformat_basic(ax)
	save_name = re.sub(r'\.[zi]vms(?:\.asc)?$', '-didv.png', file_name)
	ax.figure.savefig(path+save_name, dpi=150)
	plt.close(fig)
	log += '  saved "{}"\n'.format(save_name)
	
	# create a dz/dV(V) spectra bundle
	x_window = 0.1 #V
	poly_order = 1
	if rr:
		sIs_f = allIs_f.deriv_sg(x_window, poly_order, 0)
		sIs_f.zero_y0()
		sIs_r = allIs_r.deriv_sg(x_window, poly_order, 0)
		sIs_r.zero_y0()
		sdIs_f = alldIs_f.deriv_sg(x_window, poly_order, 0)
		sdIs_r = alldIs_r.deriv_sg(x_window, poly_order, 0)
		allndIs_f = alldIs_f.copy()
		allndIs_r = alldIs_r.copy()
		for i in range(allndIs_f.N):
			allndIs_f[i] = allIs_f.X * sdIs_f[i] / sIs_f[i]
			allndIs_r[i] = allIs_f.X * sdIs_r[i] / sIs_r[i]
		# END for
		
		ndmI_f = spec.norm_deriv(V, allIs_f.coavg(), x_window, poly_order)
		ndmI_r = spec.norm_deriv(V, allIs_r.coavg(), x_window, poly_order)
	else:
		sIs = allIs.deriv_sg(x_window, poly_order, 0)
		sIs.zero_y0()
		sdIs = alldIs.deriv_sg(x_window, poly_order, 0)
		allndIs = alldIs.copy()
		for i in range(allndIs.N):
			allndIs[i] = allIs.X * sdIs[i] / sIs[i]
		# END for
	# END if
	
	# make density plot for all the norm. dI/dV(V) spectra
	fig = plt.figure()
	ax = fig.add_subplot(111)
	if rr:
		spec.plot_density(allndIs_f, ax)
		spec.plot_density(allndIs_r, ax, mkrcolor='red')
		ax.plot(V, ndmI_f, color=(0.5,0.6,1.0), linewidth=2)
		ax.plot(V, ndmI_r, color=(1.0,0.5,0.5), linewidth=2)
		ax.plot(V, allndIs_f.coavg(), color=(0.5,0.5,0.6), linewidth=1)
		ax.plot(V, allndIs_r.coavg(), color=(0.6,0.5,0.5), linewidth=1)
	else:
		spec.plot_density(allndIs, ax)
		ax.plot(V, allndIs.coavg(), color=(0.5,0.6,1.0), linewidth=2)
	# END if
	ax.set_title( '{}, N={}'.format(file_name, N_spec) )
	ax.set_xlabel('Gap Bias ({})'.format(alldIs.units[0]))
	ax.set_ylabel('(V/I)*dI/dV')
	spec.pltformat_basic(ax)
	spec.tight_scale(ax)
	
	save_name = re.sub(r'\.[zi]vms(?:\.asc)?$', '-ndidv.png', file_name)
	ax.figure.savefig(path+save_name, dpi=150)
	plt.close(fig)
	log += '  saved "{}"\n'.format(save_name)
	
	print log
def _zv_subroutine(path, file_name):
	log = 'z(V) spectra "{}":\n'.format(file_name)
	log += '  folder: {}\n'.format(path)
	
	allZs = spec.ZVSTSBundle(path+file_name)
	
	rr = allZs.has_ramp_reversal
	if rr:
		log += '  ramp reversal detected\n'
		allZs_f, allZs_r = allZs.ramp_reversal_fix(opt='split')
		log += '  keeping track of ramp directions\n'
		
		N_spec = allZs_f.N
		mZ_f = allZs_f.coavg()
		mZ_r = allZs_r.coavg()
		V = allZs_f.X
	else:
		N_spec = allZs.N
		mZ = allZs.coavg()
		V = allZs.X
	# END if
	
	log += '  {} spectra in sample\n'.format(N_spec)
	
	# make density plot for all the z(V) spectra
	fig = plt.figure()
	ax = fig.add_subplot(111)
	if rr:
		spec.plot_density(allZs_f, ax)
		spec.plot_density(allZs_r, ax, mkrcolor='red')
		ax.plot(V, mZ_f, color=(0.5,0.6,1.0), linewidth=2)
		ax.plot(V, mZ_r, color=(1.0,0.5,0.5), linewidth=2)
	else:
		spec.plot_density(allZs, ax)
		ax.plot(V, mZ, color=(0.5,0.6,1.0), linewidth=2)
	# END if
	ax.set_title( '{}, N={}'.format(file_name, N_spec) )
	spec.pltformat_basic(ax)
	save_name = re.sub(r'\.[zi]vms(?:\.asc)?$', '-zv.png', file_name)
	ax.figure.savefig(path+save_name, dpi=150)
	plt.close(fig)
	log += '  saved "{}"\n'.format(save_name)
	
	# create a dz/dV(V) spectra bundle
	x_window = 0.1 #V
	poly_order = 1
	deriv_order = 1
	if rr:
		alldZs_f = allZs_f.deriv_sg(x_window, poly_order, deriv_order)
		alldZs_r = allZs_r.deriv_sg(x_window, poly_order, deriv_order)
		mdZ_f = alldZs_f.coavg()
		mdZ_r = alldZs_r.coavg()
	else:
		alldZs = allZs.deriv_sg(x_window, poly_order, deriv_order)
		mdZ = alldZs.coavg()
	# END if
	
	# make density plot for all the dz/dV(V) spectra
	fig = plt.figure()
	ax = fig.add_subplot(111)
	if rr:
		spec.plot_density(alldZs_f, ax)
		spec.plot_density(alldZs_r, ax, mkrcolor='red')
		ax.plot(V, mdZ_f, color=(0.5,0.6,1.0), linewidth=2)
		ax.plot(V, mdZ_r, color=(1.0,0.5,0.5), linewidth=2)
	else:
		spec.plot_density(alldZs, ax)
		ax.plot(V, mdZ, color=(0.5,0.6,1.0), linewidth=2)
	# END if
	ax.set_title( '{}, N={}'.format(file_name, N_spec) )
	spec.pltformat_basic(ax)
	save_name = re.sub(r'\.[zi]vms(?:\.asc)?$', '-dzdv.png', file_name)
	ax.figure.savefig(path+save_name, dpi=150)
	plt.close(fig)
	log += '  saved "{}"\n'.format(save_name)
	
	print log