# A script that checks if any catalogue entries cannot be assigned to rows in the
# plate tables.
# None of the queries should yield any results.

import sys

from gavo import api
from gavo import base

def checkBand(title, survCol, fieldCol):
	with base.getTableConn() as conn:
		if False:
			print "Checking for misassigned %s..."%title,
			sys.stdout.flush()
			res = list(conn.query("SELECT d.raj2000, d.dej2000, d.%s, d.%s"
				" FROM usnob.data as d, usnob.plates as p"
				" WHERE d.%s=p.survey AND d.%s=p.field AND"
				" NOT q3c_radial_query(d.raj2000, d.dej2000, p.alpha2000,"
					" p.delta2000, 7) LIMIT 10"%(
				survCol, fieldCol, survCol, fieldCol)))
			if not res:
				print "Ok"
			else:
				print "Not Ok"
				print "Example for failed entry:"
				print res[0]
		print "Checking for unassigned %s..."%title,
		sys.stdout.flush()
		res = list(conn.query("SELECT d.raj2000, d.dej2000, d.%s, d.%s"
			" FROM usnob.data AS d, usnob.plates AS p"
			" WHERE (d.%s, d.%s) NOT IN (SELECT survey, field FROM usnob.plates)"
			" LIMIT 10"%(survCol, fieldCol, survCol, fieldCol)))
		if not res:
			print "Ok"
		else:
			print "Not Ok"
			print "Examples for failed entry:"
			print res


if __name__=="__main__":
	base.setDBProfile("trustedquery")
#	checkBand("Blue 1", "b1s", "b1f")
#	checkBand("Blue 2", "b2s", "b2f")
#	checkBand("Red 1", "r1s", "r1f")
#	checkBand("Red 2", "r2s", "r2f")
	checkBand("Infrared", "i_s", "if")
