Index: /trunk/mops/mops_ringsv3/populate_ringsv3_database.py
===================================================================
--- /trunk/mops/mops_ringsv3/populate_ringsv3_database.py	(revision 37503)
+++ /trunk/mops/mops_ringsv3/populate_ringsv3_database.py	(revision 37503)
@@ -0,0 +1,75 @@
+#!/usr/bin/env python
+
+import MySQLdb
+import pyfits
+import numpy
+
+###################################################################
+# "Parameters"
+###################################################################
+# Name of the table which is going to be created and populated
+tableName = "skycells"
+# Name of the database host
+dbhost = 'localhost'
+dbuser = 'mops'
+dbpasswd = 'mops'
+dbname = 'ringsV3'
+# Name of the tesselation file
+tesselation = '/local/ipp/gpc1/tess/RINGS.V3/Images.dat'
+###################################################################
+
+def create_database(db):
+    cursor = db.cursor()
+    statement = "DROP TABLE IF EXISTS %s;" % tableName
+    cursor.execute(statement)
+
+    statement = """CREATE TABLE %s (
+  skycell CHAR(16) NOT NULL PRIMARY KEY COMMENT 'Skycell ID (aka Name in Images.dat file)',
+  raProjectionCenter DOUBLE NOT NULL COMMENT 'Right Ascension of the center of projection cell (in degrees, aka crval1)', 
+  craProjectionCenter DOUBLE NOT NULL COMMENT 'Cosine of ra of center of projection cell',
+  sraProjectionCenter DOUBLE NOT NULL COMMENT 'Sine of ra of center of projection cell',
+  decProjectionCenter DOUBLE NOT NULL COMMENT 'Declination of center of projection cell (in degrees, aka crval2)', 
+  cdecProjectionCenter DOUBLE NOT NULL COMMENT 'Cosine of decl of center of projection cell',
+  sdecProjectionCenter DOUBLE NOT NULL COMMENT 'Sine of decl of center of projection cell',
+  xSkycellCenter DOUBLE NOT NULL COMMENT 'X coordinate of the skycell center (in pixels, aka crpix1)',
+  ySkycellCenter DOUBLE NOT NULL COMMENT 'Y coordinate of the skycell center (in pixels, aka crpix2)'
+) ENGINE=InnoDB COMMENT='Contents of tesselation used for MOPS diffs mapped to a database table. Notes: (a) Scales on both X and Y axis (aka cdelt1, cdelt2) are 5 arcseconds. (b) Rotation coordinates (aka pc[12]_[12]) in from (ra,dec) coordinates to image plane coordinates are ((-1,0),(0,1)). (c) The database has been populated on 2014-10-20 by schastel using /local/ipp/gpc1/tess/RINGS.V3/Images.dat whose md5sum is 8eb517fe2e894e66488d154f5bb7c030; (d) Associated software: SVN repository: https://svn.pan-starrs.ifa.hawaii.edu/repo/ipp/trunk/mops/mops_ringsv3' ;
+""" % (tableName)
+    cursor.execute(statement)
+    cursor.close()
+
+def populate_database(database):
+    fits = pyfits.open(tesselation)
+    table = fits[1].data
+    raProjectionCenterRadians = numpy.radians(table['crval1'])
+    craProjectionCenter = numpy.cos(raProjectionCenterRadians)
+    sraProjectionCenter = numpy.sin(raProjectionCenterRadians)
+    decProjectionCenterRadians = numpy.radians(table['crval2'])
+    cdecProjectionCenter = numpy.cos(decProjectionCenterRadians)
+    sdecProjectionCenter = numpy.sin(decProjectionCenterRadians)
+    data = [ table['name'], 
+             table['crval1'], craProjectionCenter, sraProjectionCenter, 
+             table['crval2'], cdecProjectionCenter, sdecProjectionCenter,
+             table['crpix1'], table['crpix2'],
+             ]
+    cursor = database.cursor()
+    query = """INSERT INTO %s (skycell, 
+raProjectionCenter, craProjectionCenter, sraProjectionCenter, 
+decProjectionCenter, cdecProjectionCenter, sdecProjectionCenter,
+xSkycellCenter, ySkycellCenter) VALUES (%%s, %%s, %%s, %%s, %%s, %%s, %%s, %%s, %%s)""" % tableName
+    print query
+    remapped = zip(*data)
+    print
+    print query % remapped[0]
+    print
+    print remapped[0:5]
+    cursor.executemany(query, remapped)
+    fits.close()
+    cursor.close()
+
+if __name__ == "__main__":
+    database = MySQLdb.connect(dbhost, dbuser, dbpasswd, dbname)
+    create_database(database)
+    populate_database(database)
+    database.commit()
+    database.close()
