def select_stars(magnitude, constellation=None, ra_range=None, dec_range=None): """ Select a set of stars brighter than the given magnitude, based on coordinate range and/or constellation membership. Args: magnitude (float): The maximum magnitude to include constellation (string): The constellation name; if given, only stars from that constellation are returned ra_range (tuple): The range (min_ra, max_ra) of right ascension to include, in degrees dec_range (tuple): The range (min_dec, max_dec) of declination to include, in degrees Returns: list: All Star objects in the database matching the criteria """ # Build the query q = """SELECT * FROM skymap_stars WHERE magnitude<={0}""".format(magnitude) if constellation: q += """ AND constellation='{0}'""".format(constellation) if ra_range: min_ra, max_ra = ra_range min_ra = ensure_angle_range(min_ra) max_ra = ensure_angle_range(max_ra) if min_ra < max_ra: q += """ AND right_ascension>={0} AND right_ascension<={1}""".format( min_ra, max_ra ) elif max_ra < min_ra: q += """ AND (right_ascension>={0} OR right_ascension<={1})""".format( min_ra, max_ra ) else: # min_ra is equal to max_ra: full circle: no ra limits pass if dec_range: min_dec, max_dec = dec_range if ( min_dec < -90 or min_dec > 90 or max_dec < -90 or max_dec > 90 or max_dec <= min_dec ): raise ValueError("Illegal DEC range!") q += """ AND declination>={0} AND declination<={1}""".format(min_dec, max_dec) # Order stars from brightest to weakest so displaying them is easier q += """ ORDER BY magnitude ASC""" # Execute the query db = SkyMapDatabase() rows = db.query(q) result = [Star(row) for row in rows] db.close() return result
def get_label_size(text, fontsize): db = SkyMapDatabase() res = db.query_one( """SELECT * FROM skymap_labels WHERE label_text="{}" AND fontsize="{}" """ .format(text, fontsize)) db.close() if res is None: return None return res['width'], res['height']
def get_milky_way_curve(id): db = SkyMapDatabase() q = "SELECT * FROM milkyway WHERE curve_id={0} ORDER BY id ASC".format(id) result = db.query(q) curve = [] for row in result: curve.append(SphericalPoint(row['ra'], row['dec'])) if curve[0] == curve[-1]: curve = curve[:-1] db.close() return curve
def find_multiples(stars, criterion): """ Find all multiple stars for the given distance criterion. :param stars: A list of star database records :param criterion: The angular separation below which stars are considered part of a multiple system :return: ? """ db = SkyMapDatabase() for s in stars: candidates = get_stars_around_coordinate(s['right_ascension'], s['declination'], criterion, db) for i, ci in enumerate(candidates): for cj in candidates[i + 1:]: ra1 = ci['right_ascension'] de1 = ci['declination'] ra2 = cj['right_ascension'] de2 = cj['declination'] d = angular_separation_seconds_of_arc(ra1, de1, ra2, de2) if d < criterion: #print "{}: {} - {} -> {}".format(current_process().name, ci['id'], cj['id'], d) pass
def build_constellation_boundary_database(): print() print("Creating constellation boundary database") db = SkyMapDatabase() db.drop_table("skymap_constellation_boundaries") db.create_table( "skymap_constellation_boundaries", ["ra1", "dec1", "ra2", "dec2"], [float, float, float, float], ) # Retrieve data from Vizier print() print("Retrieving data from Vizier") Vizier.ROW_LIMIT = -1 catalog = Vizier.get_catalogs("VI/49") constbnd = catalog["VI/49/constbnd"] print() print("Building edges from {} points".format(len(constbnd))) prev_hash = None edges = [] for row in constbnd: if not row["adj"]: prev_hash = None current_hash = point_hash(row["RAB1875"], row["DEB1875"]) if prev_hash is not None: e = QuickEdge(prev_hash, current_hash) if e not in edges: edges.append(e) prev_hash = current_hash print() print("Connecting {} edges".format(len(edges))) for i, e1 in enumerate(edges): for e2 in edges[i + 1 :]: e1.connect(e2) print() print("Building extended edges") new_edges = [] for i, e in enumerate(edges): new_edge = e.extended_edge if new_edge not in new_edges: new_edges.append(new_edge) print() print(f"Loading {len(new_edges)} edges to database") for i, e in enumerate(new_edges): db.insert_row( "skymap_constellation_boundaries", ["ra1", "dec1", "ra2", "dec2"], e.coordinates, )
def build_stellar_database(): db = SkyMapDatabase() create_table(db) hipparcos_single(db) hipparcos_multiple(db) tycho2(db) add_indices(db) add_cross_index(db) add_bright_star_catalog(db) add_proper_names(db)
def get_constellation_boundaries_for_area(min_longitude, max_longitude, min_latitude, max_latitude, epoch=REFERENCE_EPOCH): # Convert longitude to 0-360 values # TODO: sometimes boundaries cross the map but have no vertices within the map area + margin and are not plotted min_longitude = ensure_angle_range(min_longitude) max_longitude = ensure_angle_range(max_longitude) if max_longitude == min_longitude: max_longitude += 360 db = SkyMapDatabase() q = "SELECT * FROM skymap_constellation_boundaries WHERE" if min_longitude < max_longitude: q += " ((ra1>={0} AND ra1<={1}".format(min_longitude, max_longitude) else: q += " (((ra1>={0} OR ra1<={1})".format(min_longitude, max_longitude) q += " AND dec1>={0} AND dec1<={1}) OR".format(min_latitude, max_latitude) if min_longitude < max_longitude: q += " (ra2>={0} AND ra2<={1}".format(min_longitude, max_longitude) else: q += " ((ra2>={0} OR ra2<={1})".format(min_longitude, max_longitude) q += " AND dec2>={0} AND dec2<={1}))".format(min_latitude, max_latitude) res = db.query(q) result = [] pc = PrecessionCalculator(CONST_BOUND_EPOCH, epoch) for row in res: p1 = SphericalPoint(row['ra1'], row['dec1']) p2 = SphericalPoint(row['ra2'], row['dec2']) e = BoundaryEdge(p1, p2) e.precess(pc) result.append(e) db.close() return result
def get_constellation_boundaries_for_area( min_longitude, max_longitude, min_latitude, max_latitude, epoch="J2000.0" ): # Convert longitude to 0-360 values # TODO: sometimes boundaries cross the map but have no vertices within the map area + margin and are not plotted min_longitude = ensure_angle_range(min_longitude) max_longitude = ensure_angle_range(max_longitude) if max_longitude == min_longitude: max_longitude += 360 db = SkyMapDatabase() q = "SELECT * FROM skymap_constellation_boundaries WHERE" if min_longitude < max_longitude: q += " ((ra1>={0} AND ra1<={1}".format(min_longitude, max_longitude) else: q += " (((ra1>={0} OR ra1<={1})".format(min_longitude, max_longitude) q += " AND dec1>={0} AND dec1<={1}) OR".format(min_latitude, max_latitude) if min_longitude < max_longitude: q += " (ra2>={0} AND ra2<={1}".format(min_longitude, max_longitude) else: q += " ((ra2>={0} OR ra2<={1})".format(min_longitude, max_longitude) q += " AND dec2>={0} AND dec2<={1}))".format(min_latitude, max_latitude) res = db.query(q) result = [] for row in res: p1 = SkyCoordDeg(row["ra1"], row["dec1"]) p2 = SkyCoordDeg(row["ra2"], row["dec2"]) e = ConstellationBoundaryEdge(p1, p2) e.precess() result.append(e) db.close() return result
def build_database(catalogue, foldername, indices=(), extra_function=None): print print "Building database for {} ({})".format(catalogue, foldername) t1 = time.time() files = get_files(catalogue, foldername) datadicts = parse_readme(foldername) db = SkyMapDatabase() for f, dds in datadicts.items(): table = "{}_{}".format(foldername, f.split(".")[0]) db.drop_table(table) columns = [] lc_columns = [] datatypes = [] for dd in dds: c = dd["label"] # Check for columns that have equivalent names i = 1 while c.lower() in lc_columns: if i == 1: c += "_1" else: c = c[:-2] + "_{}".format(i) i += 1 lc_columns.append(c.lower()) columns.append(c) datatypes.append(dd['format']) db.create_table(table, columns, datatypes) real_files = [fn for fn in files if fn.startswith(f)] for real_file in real_files: parse_datafile(db, foldername, real_file, table, dds, columns) for ind in indices: if ind in columns: db.add_index(table, ind) t2 = time.time() print print print "Time: {} s".format(t2 - t1) if extra_function: extra_function()
def build_star_database(): """ Builds the SkyMapDatabase using data from Tycho 2, Tycho, Hipparcos. Adds supplementary data from the HD-DM-GC-HR-HIP-Bayer-Flamsteed Cross Index, the Bright Star Catalog and the IAU list of proper names. Adds constellations. """ db = SkyMapDatabase() create_table(db) add_tycho2(db) add_tycho1(db) add_hipparcos(db) add_indexes(db) add_cross_index(db) add_bright_star_catalog(db) add_proper_names(db) add_constellations(db)
class ConstellationFinder(object): """ Find the constellation for a given coordinate. """ def __init__(self, epoch=None): self.db = SkyMapDatabase() self.precessor = PointInConstellationPrecession(epoch) def find(self, ra, de): ra, de = self.precessor.precess(ra, de) ra /= 15.0 # Convert to fractional hours of right ascension q = """ SELECT const FROM skymap.cst_id_data WHERE DE_low < {0} AND RA_low <= {1} AND RA_up > {1} ORDER BY pk LIMIT 1 """.format(de, ra) return self.db.query_one(q)['const'].lower()
def build_milkyway_database(): print("") print("Building milky way boundary database") db = SkyMapDatabase() # Drop table db.drop_table("milkyway") # Create table db.commit_query("""CREATE TABLE milkyway ( id INT PRIMARY KEY , curve_id INT, ra REAL, dec REAL )""") # Fill table point_id = 0 curve_id = 0 curve_id, point_id = parse_file(os.path.join(DATA_FOLDER, "milkyway.txt"), db, curve_id, point_id) curve_id, point_id = parse_file(os.path.join(DATA_FOLDER, "magellanic_clouds.txt"), db, curve_id, point_id) db.close()
def add_tyc2_index(): db = SkyMapDatabase() db.add_multiple_column_index("tyc2_tyc2", ("TYC1", "TYC2", "TYC3"), "TYC", unique=True)
def split_tyc(): db = SkyMapDatabase() db.commit_query(""" ALTER TABLE hiptyc_tyc_main ADD COLUMN `TYC1` INT AFTER `TYC`, ADD COLUMN `TYC2` INT AFTER `TYC1`, ADD COLUMN `TYC3` INT AFTER `TYC2` """) db.commit_query("""DROP FUNCTION IF EXISTS SPLIT_TYC""") db.commit_query(""" CREATE FUNCTION SPLIT_TYC(str VARCHAR(255), pos INT) RETURNS INT BEGIN SET str = TRIM(str); WHILE INSTR(str, ' ') > 0 DO SET str = REPLACE(str, ' ', ' '); END WHILE; SET str = REPLACE( SUBSTRING( SUBSTRING_INDEX(str, ' ', pos), CHAR_LENGTH( SUBSTRING_INDEX(str, ' ', pos - 1) ) + 1 ) , ' ', '' ); RETURN CAST(str AS UNSIGNED); END; """) db.commit_query(""" UPDATE hiptyc_tyc_main SET TYC1=SPLIT_TYC(TYC, 1), TYC2=SPLIT_TYC(TYC, 2), TYC3=SPLIT_TYC(TYC, 3) """) db.add_index("hiptyc_tyc_main", "TYC1") db.add_index("hiptyc_tyc_main", "TYC2") db.add_index("hiptyc_tyc_main", "TYC3") db.add_multiple_column_index("hiptyc_tyc_main", ("TYC1", "TYC2", "TYC3"), "TYC", unique=True)
def build_database(catalogue, foldername, indices=(), extra_function=None): """Downloads the datafiles for a catalog and builds a local database for it. Args: catalogue (str): the name of the catalog foldername (str): the folder where to save the data indices (list): the columns to generate indices for extra_function (function): a function to call after the database is built """ print() print(f"Building database for {catalogue} ({foldername})") t1 = time.time() files = download_files(catalogue, foldername) datadicts = parse_readme(foldername) db = SkyMapDatabase() column_name_dict = {} for filename, coldefs in datadicts.items(): datatypes = [coldef['format'] for coldef in coldefs] # SQL is case insensitive, and Vizier sometimes has column names in the same file that # have equivalent names. So, the column names are checked and updated when needed. column_names = [] for coldef in coldefs: column_name = coldef["label"] i = 1 lowercase_column_names = [x.lower() for x in column_names] while column_name.lower() in lowercase_column_names: if i > 1: column_name = column_name[:-2] column_name += "_{}".format(i) i += 1 column_names.append(column_name) table = "{}_{}".format(foldername, filename.split(".")[0]) column_name_dict[table] = column_names # Clear the database table db.drop_table(table) db.create_table(table, column_names, datatypes) # For large catalogs, the data can be spread over multiple files, so loop over all files real_files = [fn for fn in files if fn.startswith(filename)] for real_file in real_files: parse_datafile(db, foldername, real_file, table, coldefs, column_names) # Add indices for table, column_names in column_name_dict.items(): for ind in indices: if ind in column_names: db.add_index(table, ind) t2 = time.time() print() print() print(f"Time: {t2-t1} s") if extra_function: extra_function()
from tkinter import * from PIL import Image, ImageTk from astroquery.skyview import SkyView import numpy from skymap.database import SkyMapDatabase if __name__ == "__main__": sv = SkyView() db = SkyMapDatabase() res = db.query_one( """SELECT * FROM ngc_ngc2000 WHERE Name=(SELECT name FROM ngc_names WHERE object LIKE '%{}%')""" .format("M 104")) rah = res['RAh'] ram = res['RAm'] ra = rah * 15.0 + 15 * ram / 60.0 des = res['DE-'] ded = res['DEd'] dem = res['DEm'] dec = ded + dem / 60.0 if des == "-": dec *= -1 print("{}h {}m -> {}".format(rah, ram, ra)) print("{}{}deg {}m -> {}".format(des, ded, dem, dec)) image = sv.get_images(position="{}, {}".format(ra, dec), survey="DSS", pixels=(1000, 1000),
def setUp(self): self.db = SkyMapDatabase()
class StarDatabaseTest(unittest.TestCase): def setUp(self): self.db = SkyMapDatabase() # Unicity def test_hip_unique(self): """Check whether HIP identification is unique within Hipparcos""" q = """ SELECT COUNT(*) AS n FROM ( SELECT HIP FROM hiptyc_hip_main GROUP BY HIP HAVING COUNT(HIP)>1 ) AS h """ self.assertEqual(self.db.query_one(q)['n'], 0) def test_tyc1_unique(self): """Check whether TYC identification is unique within Tycho-1""" q = """ SELECT COUNT(*) AS n FROM ( SELECT tyc FROM ( SELECT CONCAT(TYC1, '-', TYC2, '-', TYC3) as tyc FROM hiptyc_tyc_main ) AS t1 GROUP BY tyc HAVING COUNT(tyc)>1 ) AS t2 """ self.assertEqual(self.db.query_one(q)['n'], 0) def test_tyc2_unique(self): """Check whether TYC identification is unique within Tycho-2. Strangely, 254 duplicates are found, all between main and supplement 1. In all cases except for 1 (there it is the AB-component), the A-component is in the main component, the other (in all but 4 cases the B-component, three times it is the C-component, once the P-component) in supplement 1. Positions differ slightly within each pair. All supplement 1 stars are Hipparcos stars. Stars seem to be in Tycho-1 and Hipparcos as single entries with multiple components (Double/Multiple annex). Not resolved yet! """ # TODO: solve q = """ SELECT COUNT(*) AS n FROM ( SELECT tyc FROM ( SELECT CONCAT(TYC1, '-', TYC2, '-', TYC3) as tyc FROM tyc2_tyc2 UNION ALL SELECT CONCAT(TYC1, '-', TYC2, '-', TYC3) as tyc FROM tyc2_suppl_1 UNION ALL SELECT CONCAT(TYC1, '-', TYC2, '-', TYC3) as tyc FROM tyc2_suppl_2 ) AS t1 GROUP BY tyc HAVING COUNT(tyc)>1 ) AS t2 """ self.assertEqual(self.db.query_one(q)['n'], 254) # Hipparcos - Hipparcos New Reduction def test_hip_hipnew(self): """Check whether all Hipparcos stars are in Hipparcos New Reduction; 263 stars missing (all stars without proper astrometry)""" q = """ SELECT COUNT(*) AS n FROM hiptyc_hip_main AS h LEFT JOIN hipnew_hip2 AS h2 ON h2.HIP=h.HIP WHERE h2.HIP IS NULL """ self.assertEqual(self.db.query_one(q)['n'], 263) def test_hipnew_hip(self): """Check whether all Hipparcos New Reduction Stars are in Hipparcos""" q = """ SELECT COUNT(*) AS n FROM hipnew_hip2 AS h2 LEFT JOIN hiptyc_hip_main AS h ON h2.HIP=h.HIP WHERE h.HIP IS NULL """ self.assertEqual(self.db.query_one(q)['n'], 0) # Hipparcos - Tycho-1 def test_tyc1_hip(self): """Check whether all Tycho-1 stars labeled as Hipparcos stars are in Hipparcos""" q = """ SELECT COUNT(*) AS n FROM hiptyc_tyc_main AS t LEFT JOIN hiptyc_hip_main AS h ON t.HIP=h.HIP WHERE t.HIP IS NOT NULL AND h.HIP IS NULL """ self.assertEqual(self.db.query_one(q)['n'], 0) def test_hip_tyc1(self): """Check whether all Hipparcos stars are in Tycho-1. Only the Hipparcos stars without astrometric solutions (263 stars) are not found in Tycho-1. """ q = """ SELECT COUNT(*) AS n FROM hiptyc_hip_main AS h LEFT JOIN hiptyc_tyc_main AS t ON t.HIP=h.HIP WHERE t.HIP IS NULL """ self.assertEqual(self.db.query_one(q)['n'], 263) def test_hip_to_multiple_tyc1(self): """Check multiple stars between Hipparcos and Tycho-1. These are multiple stars of which the components are recorded in both databases. The query checks for all components of a given HIP star to see if they are found in Tycho-1. Component identifiers should match or if not, the number of components from Hipparcos should be equal to the number of letters in the concatenated Tycho-1 component identifiers.""" q = """ SELECT COUNT(*) AS n FROM ( SELECT t.HIP, t.m_HIP AS T_comp, h.m_HIP AS H_comp, h.Ncomp AS H_ncomp, t.Ncomp AS T_ncomp FROM ( SELECT HIP, GROUP_CONCAT(TRIM(m_HIP) SEPARATOR '') AS m_HIP, COUNT(HIP) AS Ncomp FROM hiptyc_tyc_main GROUP BY HIP HAVING COUNT(HIP)>1 ) AS t LEFT JOIN ( SELECT HIP, m_HIP, Ncomp FROM hiptyc_hip_main ) AS h ON t.HIP=h.HIP WHERE NOT ( (t.m_HIP = h.m_HIP OR t.m_HIP = REVERSE(h.m_HIP)) OR LENGTH(t.m_HIP) = h.Ncomp ) ) AS t """ self.assertEqual(self.db.query_one(q)['n'], 0) # Tycho-1 - Tycho-2 def test_tyc1_tyc2(self): """Check whether all Tycho-1 stars are in Tycho-2 (including supplement 1 and 2). Stars with astrometric quality of 9 are not included in Tycho-2.""" q = """ SELECT COUNT(*) AS n FROM hiptyc_tyc_main AS t1 LEFT JOIN ( SELECT TYC1, TYC2, TYC3 FROM tyc2_tyc2 UNION ALL SELECT TYC1, TYC2, TYC3 FROM tyc2_suppl_1 UNION ALL SELECT TYC1, TYC2, TYC3 FROM tyc2_suppl_2 ) AS t2 ON t1.TYC1=t2.TYC1 AND t1.TYC2=t2.TYC2 AND t1.TYC3=t2.TYC3 WHERE t2.TYC1 IS NULL AND t1.Q != 9 """ self.assertEqual(self.db.query_one(q)['n'], 0) def test_tyc2_tyc1(self): """Check whether all Tycho-2 stars that are labeled as Tycho-1 stars are in Tycho-1. Hipparcos stars that were not measured by Tycho-1 were included in Tycho-1, but are not labeled as Tycho-1 stars in Tycho-2. These stars are labeled in Tycho-1 with an 'H' in the 'Source' field. Tycho-1 stars that are resolved into multiple Tycho-2 stars are all labeled as Tycho-1 stars, but have different Tycho-2 ids. Such multiple stars share the same TYC1 and TYC2. The original Tycho-1 stars has TYC3 equal to 1, so the other components have higher TYC3 numbers. There are 1866 such stars. """ q = """ SELECT COUNT(*) AS n FROM ( SELECT TYC1, TYC2, TYC3 FROM tyc2_tyc2 WHERE TYC='T' UNION ALL SELECT TYC1, TYC2, TYC3 FROM tyc2_suppl_1 WHERE TYC='T' UNION ALL SELECT TYC1, TYC2, TYC3 FROM tyc2_suppl_2 WHERE TYC='T' ) AS t2 LEFT JOIN ( SELECT TYC1, TYC2, TYC3 FROM hiptyc_tyc_main WHERE Source!='H' ) AS t1 ON t1.TYC1=t2.TYC1 AND t1.TYC2=t2.TYC2 AND t1.TYC3=t2.TYC3 WHERE t1.TYC1 IS NULL """ self.assertEqual(self.db.query_one(q)['n'], 1866) # Tycho-2 internal def test_tyc2_supplement1(self): """Check that there is no overlap between Tycho-2 main and Tycho-2 supplement 1. See test_tyc2_unique for more info on the 254 stars that do overlap.""" q = """ SELECT COUNT(*) AS n FROM tyc2_tyc2 as t2 INNER JOIN tyc2_suppl_1 as ts1 ON t2.TYC1=ts1.TYC1 AND t2.TYC2=ts1.TYC2 AND t2.TYC3=ts1.TYC3 """ self.assertEqual(self.db.query_one(q)['n'], 254) def test_tyc2_supplement2(self): """Check that there is no overlap between Tycho-2 main and Tycho-2 supplement 1""" q = """ SELECT COUNT(*) AS n FROM tyc2_tyc2 as t2 INNER JOIN tyc2_suppl_2 as ts2 ON t2.TYC1=ts2.TYC1 AND t2.TYC2=ts2.TYC2 AND t2.TYC3=ts2.TYC3 """ self.assertEqual(self.db.query_one(q)['n'], 0) def test_tyc2_supplement1_supplement2(self): """Check that there is no overlap between Tycho-2 supplement 1 and Tycho-2 supplement 2""" q = """ SELECT COUNT(*) AS n FROM tyc2_suppl_1 as ts1 INNER JOIN tyc2_suppl_2 as ts2 ON ts1.TYC1=ts2.TYC1 AND ts1.TYC2=ts2.TYC2 AND ts1.TYC3=ts2.TYC3 """ self.assertEqual(self.db.query_one(q)['n'], 0) # Hipparcos - Tycho-2 def test_hip_tyc2(self): """Check whether all Hipparcos stars are found in Tycho-2. The 263 Hipparcos star without an astrometric solution are not included in Tycho-2.""" q = """ SELECT COUNT(*) AS n FROM hiptyc_hip_main AS h LEFT JOIN ( SELECT TYC1, TYC2, TYC3, HIP FROM tyc2_tyc2 UNION ALL SELECT TYC1, TYC2, TYC3, HIP FROM tyc2_suppl_1 UNION ALL SELECT TYC1, TYC2, TYC3, HIP FROM tyc2_suppl_2 ) AS t2 ON t2.HIP=h.HIP WHERE t2.HIP IS NULL """ self.assertEqual(self.db.query_one(q)['n'], 263) def test_tyc2_hip(self): """Check whether all Tycho-2 stars labeled as Hipparcos stars are found in Hipparcos""" q = """ SELECT COUNT(*) AS n FROM ( SELECT TYC1, TYC2, TYC3, HIP FROM tyc2_tyc2 UNION ALL SELECT TYC1, TYC2, TYC3, HIP FROM tyc2_suppl_1 UNION ALL SELECT TYC1, TYC2, TYC3, HIP FROM tyc2_suppl_2 ) AS t2 LEFT JOIN hiptyc_hip_main AS h ON t2.HIP=h.HIP WHERE h.HIP IS NULL AND t2.HIP IS NOT NULL """ self.assertEqual(self.db.query_one(q)['n'], 0) def test_hip_to_multiple_tyc2(self): """Check multiple stars between Hipparcos and Tycho-2. These are multiple stars of which the components are recorded in both databases. The query checks for all components of a given HIP star to see if they are found in Tycho-2. Component identifiers should match or if not, the number of components from Hipparcos should be equal to the number of letters in the concatenated Tycho-2 component identifiers.""" q = """ SELECT COUNT(*) AS n FROM ( SELECT t.HIP, t.m_HIP AS T_comp, h.m_HIP AS H_comp, h.Ncomp AS H_ncomp, t.Ncomp AS T_ncomp FROM ( SELECT HIP, GROUP_CONCAT(TRIM(CCDM) SEPARATOR '') AS m_HIP, COUNT(HIP) AS Ncomp FROM ( SELECT HIP, CCDM FROM tyc2_tyc2 UNION ALL SELECT HIP, CCDM FROM tyc2_suppl_1 ) as tt GROUP BY HIP HAVING COUNT(HIP)>1 ) AS t LEFT JOIN ( SELECT HIP, m_HIP, Ncomp FROM hiptyc_hip_main ) AS h ON t.HIP=h.HIP WHERE NOT ( (t.m_HIP = h.m_HIP OR t.m_HIP = REVERSE(h.m_HIP)) OR LENGTH(t.m_HIP) = h.Ncomp ) ) AS t """ self.assertEqual(self.db.query_one(q)['n'], 0) # Tycho-2 supplement 1 - Hipparcos/Tycho-1 def test_tyc2_supplement1_hip(self): """Check whether all Tycho-2 supplement 1 stars are in Tycho-1 and/or Hipparcos""" q = """ SELECT COUNT(*) AS n FROM tyc2_suppl_1 AS ts1 LEFT JOIN hiptyc_hip_main AS h ON ts1.HIP=h.HIP WHERE ts1.flag='H' AND h.HIP IS NULL """ self.assertEqual(self.db.query_one(q)['n'], 0) q = """ SELECT COUNT(*) AS n FROM tyc2_suppl_1 AS ts1 LEFT JOIN hiptyc_tyc_main AS t ON ts1.TYC1=t.TYC1 AND ts1.TYC2=t.TYC2 AND ts1.TYC3=t.TYC3 WHERE ts1.flag='T' AND t.TYC1 IS NULL """ self.assertEqual(self.db.query_one(q)['n'], 0) # Tycho-2 supplement 2 - Hipparcos/Tycho-1 def test_tyc2_supplement2_hip(self): """Check whether all Tycho-2 supplement 2 stars are in Tycho-1 and/or Hipparcos""" q = """ SELECT COUNT(*) AS n FROM tyc2_suppl_2 AS ts2 LEFT JOIN hiptyc_hip_main AS h ON ts2.HIP=h.HIP WHERE ts2.flag='H' AND h.HIP IS NULL """ self.assertEqual(self.db.query_one(q)['n'], 0) q = """ SELECT COUNT(*) AS n FROM tyc2_suppl_2 AS ts2 LEFT JOIN hiptyc_tyc_main AS t ON ts2.TYC1=t.TYC1 AND ts2.TYC2=t.TYC2 AND ts2.TYC3=t.TYC3 WHERE ts2.flag='T' AND t.TYC1 IS NULL """ self.assertEqual(self.db.query_one(q)['n'], 0) def test_hd_hip_tyc1(self): pass def test_hd_tyc1_tyc2(self): pass
def __init__(self, epoch=None): self.db = SkyMapDatabase() self.precessor = PointInConstellationPrecession(epoch)
def build_label_database(): db = SkyMapDatabase() db.drop_table("skymap_labels") # Create table db.commit_query("""CREATE TABLE skymap_labels ( label_id INT PRIMARY KEY, label_text TEXT, fontsize TEXT, width REAL, height REAL)""") stars = [ Star(r) for r in db.query( """SELECT * FROM skymap_stars WHERE proper_name is not null""") ] p = Point(0, 0) i = 0 nstars = len(stars) for n, s in enumerate(stars): sys.stdout.write("\r{}%".format(int(round(100 * n / float(nstars))))) sys.stdout.flush() if not s.proper_name.strip() and not s.identifier_string.strip(): continue if s.proper_name: i += 1 if db.query_one( """SELECT * FROM skymap_labels WHERE label_text="{}" AND fontsize="{}" """ .format(s.proper_name, "tiny")) is None: l = Label(p, s.proper_name, fontsize="tiny", render_size=True) size = l.size db.commit_query( """INSERT INTO skymap_labels VALUES ({}, "{}", "{}", {}, {})""" .format(i, s.proper_name, "tiny", size[0], size[1])) if s.identifier_string: i += 1 if db.query_one( """SELECT * FROM skymap_labels WHERE label_text="{}" AND fontsize="{}" """ .format(s.identifier_string, "tiny")) is None: l = Label(p, s.identifier_string.strip(), fontsize="tiny", render_size=True) size = l.size db.commit_query( """INSERT INTO skymap_labels VALUES ({}, "{}", "{}", {}, {})""" .format(i, s.identifier_string, "tiny", size[0], size[1])) db.close()
def build_constellation_boundary_database(): print print "Building constellation boundary database" db = SkyMapDatabase() db.drop_table("skymap_constellation_boundaries") db.create_table("skymap_constellation_boundaries", ["ra1", "dec1", "ra2", "dec2"], [float, float, float, float]) edges = [] rows = db.query("""SELECT * FROM cst_bound_constbnd""") print "Creating raw edges" prev_point = None nrecords = len(rows) next_id = 1 for i, row in enumerate(rows): sys.stdout.write("\r{0:.1f}%".format(i * 100.0 / (nrecords - 1))) sys.stdout.flush() if not row['adj']: prev_point = None ra = 15.0 * row['RAhr'] dec = row['DEdeg'] point = SphericalPoint(ra, dec) if prev_point is not None: e = BoundaryEdge(prev_point, point) if e not in edges: edges.append(e) next_id += 1 prev_point = point print print "Connecting edges" n = 0 nrecords = len(edges) * (len(edges) - 1) / 2 for i, e1 in enumerate(edges): for e2 in edges[i + 1:]: sys.stdout.write("\r{0:.1f}%".format(n * 100.0 / (nrecords - 1))) sys.stdout.flush() n += 1 if e1 == e2: continue e1.connect(e2) print print "Building extended edges" new_edges = [] nrecords = len(edges) for i, e in enumerate(edges): sys.stdout.write("\r{0:.1f}%".format(i * 100.0 / (nrecords - 1))) sys.stdout.flush() new_edge = e.extended_edge if new_edge not in new_edges: new_edges.append(new_edge) print nrecords = len(new_edges) print "Loading {} edges to database".format(nrecords) for i, e in enumerate(new_edges): sys.stdout.write("\r{0:.1f}%".format(i * 100.0 / (nrecords - 1))) sys.stdout.flush() db.insert_row("skymap_constellation_boundaries", ["ra1", "dec1", "ra2", "dec2"], [e.p1.ra, e.p1.dec, e.p2.ra, e.p2.dec])
def split_tyc(): db = SkyMapDatabase() db.commit_query(""" ALTER TABLE hiptyc_tyc_main ADD COLUMN `TYC1` INT AFTER `TYC`, ADD COLUMN `TYC2` INT AFTER `TYC1`, ADD COLUMN `TYC3` INT AFTER `TYC2` """) db.commit_query(""" UPDATE hiptyc_tyc_main SET TYC1=CAST(substr(TYC, 1, 4) AS UNSIGNED), TYC2=CAST(substr(hiptyc_tyc_main.TYC, 5, 6) AS UNSIGNED), TYC3=CAST(substr(hiptyc_tyc_main.TYC, 11, 2) AS UNSIGNED) """) db.add_index("hiptyc_tyc_main", "TYC1") db.add_index("hiptyc_tyc_main", "TYC2") db.add_index("hiptyc_tyc_main", "TYC3") db.add_multiple_column_index("hiptyc_tyc_main", ("TYC1", "TYC2", "TYC3"), "TYC", unique=True)
:return: A generator """ newn = int(1.0 * len(l) / n + 0.5) for i in xrange(0, n - 1): yield l[i * newn:i * newn + newn] yield l[n * newn - newn:] if __name__ == "__main__": #build_star_database() nprocs = 100 # Optimum seems to be around 40: must have something to do with the database I suppose n = nprocs * 500 criterion = 100 db = SkyMapDatabase() stars = db.query( """SELECT id, right_ascension, declination FROM skymap_stars LIMIT {}""" .format(n)) print len(stars) procs = [] t1 = time.time() for i, l in enumerate(chunks(stars, nprocs)): print len(l) p = Process(target=find_multiples, args=(l, criterion), name="Chunk #{}".format(i + 1)) procs.append(p) p.start()