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_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_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_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 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()
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])