# generate obspos for usnob directly on-disk, separated by survey # build using setup.py, then run dumpobspos.py import os import psycopg2 import sys import time import traceback def iterCorrections(survey, connection): curs = connection.cursor() curs.execute("SELECT field, da, dd, nTotal" " FROM usnob.platecorrs where survey=%(survey)s ORDER BY field", { "survey": survey}) res = curs.fetchall() curs.close() return res fieldNames = { "A": ("b1xi", "b1eta", "b1f", "b1s", "b1mag"), "B": ("r1xi", "r1eta", "r1f", "r1s", "r1mag"), "C": ("b2xi", "b2eta", "b2f", "b2s", "b2mag"), "D": ("r2xi", "r2eta", "r2f", "r2s", "r2mag"), "E": ("b2xi", "b2eta", "b2f", "b2s", "b2mag"), "F": ("r1xi", "r1eta", "r1f", "r1s", "r1mag"), "G": ("r2xi", "r2eta", "r2f", "r2s", "r2mag"), "I": ("ixi", "ieta", "if", "i_s", "imag"), "H": ("ixi", "ieta", "if", "i_s", "imag"), } def printCorrected(connection, survey, field, da, dd, outFile): cdef long ipix cdef double alphaEp, deltaEp, mag sys.stderr.write("Correcting %s%s..."%(survey, field)) sys.stderr.flush() startTime = time.time() xicol, etacol, fieldcol, surveycol, magcol = fieldNames[survey] curs = connection.cursor() curs.execute("BEGIN") curs.execute("SELECT usnob_createorigtable(%(survey)s, %(field)s," " %(xicol)s, %(etacol)s, %(magcol)s, %(surveycol)s, %(fieldcol)s, '')", {"survey": survey, "field": field, "xicol": xicol, "etacol": etacol, "magcol": magcol, "surveycol": surveycol, "fieldcol": fieldcol}) curs.execute("SELECT q3c_ang2ipix(raj2000, dej2000), alphaEp, deltaEp," " epoch, mag FROM origPos") sys.stderr.write("DB took %.2f seconds\n"%(time.time()-startTime)) fmtStr = ("%%023d %7s %7s %%011.7f %%+12.8f %%22s %%4.2f" " %5s %20s\n"%(None, None, "%s/%s"%(survey, field), None)) try: for ipix, alphaEp, deltaEp, epoch, mag in curs: try: outFile.write(fmtStr%(ipix, alphaEp+da, deltaEp+dd, epoch, mag)) except: sys.stderr.write("Bad record: %s\n", ipix) except: sys.stderr.write("Desaster struck while correcting %s %s\n"%( survey, field)) traceback.print_exc() else: sys.stderr.write("%d rows in %.2f seconds\n"%(curs.rowcount, time.time()-startTime)) sys.stderr.flush() curs.execute("ROLLBACK") curs.close() def getConnection(): conn = psycopg2.connect("dbname=gavo") curs = conn.cursor() curs.execute("SET work_mem=597151") curs.close() return conn def openOutFile(survey): return os.popen("sort -T /GavoInput/tmp | gzip" " > /var/svn/space_for_gavo/dumps/obspos%s.txt.gz"%survey, "w") def main(): connection = getConnection() for survey in fieldNames: outFile = openOutFile(survey) for field, da, dd, nTotal in iterCorrections(survey, connection): if nTotal<50: print "Skipping %s%s (n=%s)"%(survey, field, nTotal) continue printCorrected(connection, survey, field, da, dd, outFile) outFile.close()