Exemple #1
0
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,
        )
Exemple #2
0
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()
Exemple #3
0
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()
Exemple #4
0
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])