## Read in existing file if it exists
	if (rerun == False) & (os.path.isfile(settingsFile) == True):
		oldSettings = ao.readSettingsFile(settingsFile)
		print "Reusing prior settings for "+obj
		for key in allKeys:
			if key not in oldSettings.keys():
				oldSettings[key]="XX"
	else:
		for key in allKeys:
			oldSettings[key]="XX"
		print "Recreating settings file from scratch for "+obj

	### Get object 2MASS magnitudes
	if obj in objects2MASS:
		print obj + " is in our 2MASS table with Ks mag:",magDict[obj,"Ks"]
		jMag = catalogs.getObjMag2MASS(obj,"J")
		hMag = catalogs.getObjMag2MASS(obj,"H")
		kMag = catalogs.getObjMag2MASS(obj,"Ks")
	else:
		print obj + " is not in any existing 2MASS catalog. Use catalogs.py to make a list that 2MASS likes; or edit the settings file by hand."
		kMag = "XX"
		jMag = "XX"
		hMag = "XX"

	### Initialize the settings dictionary
	settingsDict={}
	
	### Now set all of the dictionary values to either their defaults or what they were before
	for kk in allKeys:
		settingsDict[kk] = setDictValues(kk)
	
def tableForAllStars(distCutoffArcsec=closeDist, filters =["J","Ks"], extra=""):
	print "Making table of all stars within:",distCutoffArcsec," arcsec"
	g  = open(ao.compFileSorted(distCutoffArcsec, extra+".tsv"),"w")
	gTex = open(ao.compFileSorted(distCutoffArcsec, extra+".tex"),"w")
	## options for ARIES/Ks only
	delimiter = "\t&"
	print >>gTex, delimiter.join(["KNNNNN", "KeplerID", "KepMag", "Jmag", "Kmag","Filt","Star","Dist","PA","$\Delta_Mag$", "$Kep_{Dwarf}$", "\\\\"])
	delimiter = "\t" 
	print >>g, delimiter.join(["KNNNNN", "KeplerID", "KepMag", "Jmag", "Kmag", "Filt", "Star","Dist","PA","Delta_Mag", "Kep_Dwarf"])
	
	for obj in useObjects:
		firstLineForObj = True # switch this when the first line is output
		## read in the summary file
		ss = open(ao.starsFile(obj,instr=instrUsed),"r")
		lines = ss.readlines()
		ss.close()
		## find each filter
		filtersFound=[]
		for ii,line in enumerate(lines):
			elems = line.split("\t")
			if elems[0] in filters:
				filtersFound.append(elems[0])
		
		## read in the detected stars for each filter
		fwhm = lines[1].rstrip().split()[2]
		for line in lines:
			elems = line.rstrip().split()
			if elems[0] == 'J':
				filt = 'J'
			elif elems[0] == 'Ks':
				filt = 'Ks'
			## get rid of headers, also star 0
			if elems[0] not in ["0","J","Ks","Star","object"]:
			#	print line, "xx", elems,"xx"
				num = elems[0]
				dist = elems[1]
				deltaMag = elems[2]
				xCoord = elems[3]
				yCoord = elems[4]
				pa = elems[5]
				## estimate the kepler mag from K
				try:
					kMag = catalogs.getObjMag2MASS(obj,"Ks")[0]
					jMag = catalogs.getObjMag2MASS(obj,"J")[0]
				except:
					jMag = "--"
					kMag = "--"
			#	print "DIAG:",obj,kMag,jMag
				
				try:
					kepID = ksasDict[obj]["kepid"]
					kepMag = ksasDict[obj]["kpmag"]
					### There used to be a bug here (pre April 2013)! This should return the Kepler magnitude of the COMPANION, not the TARGET
					if filt == "Ks":
						kepMagDwarf = str(round(kepler.kepMagCiardi("Ks", eval(kMag)+eval(deltaMag)),1))
					elif filt == "J":
						kepMagDwarf = str(round(kepler.kepMagCiardi("J", eval(jMag)+eval(deltaMag)),1))
					else:
						kepMagDwarf = "--"
					### Should allow for J AND K, since the transform is better, but matching stars is tricksy
				except:
					kepID = "--"
					kepMag = "--"
					kepMagDwarf = "--"
				
				### Print info
				if eval(dist) <= distCutoffArcsec:
					if (firstLineForObj):
						output = [obj, kepID, kepMag, jMag, kMag, filt, num,dist, pa, deltaMag, kepMagDwarf]
						firstLineForObj = False ## for next star
					else:
						output = ["--", "--", "--","--", "--", filt, num, dist, pa, deltaMag, kepMagDwarf]
					delimiter = "\t&"
					print >>gTex, delimiter.join(output), "\\\\"
					delimiter = "\t"
					print >>g, delimiter.join(output)
	gTex.close()
	g.close()