示例#1
0
def search():
    t = JINJA.get_template('search.template')
    conn = get_conn()
    cur = conn.cursor()
    keys = get_key_list(conn, any_subkey=True)
    tables = cur.execute('select table_name from reference_tables').fetchall()
    mocs = cur.execute('select moc_name, ifnull(description, moc_name) from mocs').fetchall()
    html_data = {'table_x': quoted_list_item(tables, 0),
                 'moc_names': quoted_list_item(mocs, 0),
                 'moc_descr': quoted_list_item(mocs, 1),
                 'key_x': quoted_list(keys)}
    return t.render(html_data)
示例#2
0
def edit_table(table):
    """
    Editing single table.
    """
    t = JINJA.get_template('edit_table.template')
    conn = get_conn()
    cur = conn.cursor()
    row = cur.execute("""select rt.table_name,
                                rt.uid_column,
                                rt.description,
                                rt.brief_columns
                           from reference_tables rt
                          where table_name = '%s'""" % table).fetchone()
    cur.close()
    columns = get_table_columns(table)
    columns[0] = 'None'
    html_data = {'table': table,
                 'description': row[2],
                 'uid_column': row[1],
                 'brief_columns': row[3],
                 'key_list': get_key_list(conn),
                 'column_names': columns
                 }
    t1 = from_db_cursor(conn.execute("""
             select r.uid, k.key, k.subkey, k.description, k.data_format,
                    r.reference_column, r.error_column_low,
                    r.error_column_high, r.comment,
                    "Delete"
               from keys k
               join reference_tables_keys r on r.key = k.key
                                    and ifnull(r.subkey, '') = ifnull(k.subkey, '')
              where r.reference_table = '%s'
                  order by k.key, k.subkey""" % (table)))
    html_data['keys'] = t1.get_html_string(attributes={'border': 1,
                                                       'id': 'keys_table'})
    t2 = from_db_cursor(conn.execute("""
      select column_name, data_type, data_unit,
             output_format, description, ucd
        from reference_tables_columns
       where reference_table = '%s'""" % table))
    html_data['columns'] = t2.get_html_string(attributes={'border': 1,
                                                          'id': 'columns_table'})
    conn.close()
    return t.render(html_data)
示例#3
0
def single_cluster(uid):
    t = JINJA.get_template("single_cluster.template")
    CONN = get_conn(dict_row=True)
    cur = CONN.cursor()
    html_data = {}
    result = CONN.execute(
        "select source, source_id, comment, "
        "xid_flag, xid_flag_source, xid_flag_comment,"
        "obs_flag, obs_flag_source, obs_flag_comment,"
        "ra, dec"
        " from clusters where uid = %s" % uid
    ).fetchone()
    html_data = dict(result)
    for key in ["xid_flag_source", "xid_flag_comment", "obs_flag_source", "obs_flag_comment"]:
        if html_data[key] is None:
            html_data[key] = ""
    html_data["uid"] = uid
    html_data["ra"] = "%.6f" % html_data["ra"]
    html_data["dec"] = "%.6f" % html_data["dec"]
    html_data["params"] = []
    html_data["tables"] = []
    for key in CONN.cursor().execute(
        """
    select k.key, k.subkey, k.description,
           v.value, v.value_error_low, v.value_error_high,
           v.comment,
           ifnull(k.data_format, 's') output_format
      from per_cluster_keys v
      join keys k on k.key = v.key
                 and ifnull(k.subkey, 'null') = ifnull(v.subkey, 'null')
     where v.uid = %s"""
        % uid
    ):
        if key["subkey"] is not None and key["subkey"] != "":
            key_name = "%s,%s" % (key["key"], key["subkey"])
        else:
            key_name = key["key"]
        par = {
            "name": key_name,
            "desc": key["description"],
            "value": format_value(key["value"], key["output_format"]),
            "err_low": key["value_error_low"],
            "err_high": key["value_error_high"],
            "comment": key["comment"],
            "source": "User defined",
        }
        html_data["params"].append(par)
    html_data["select_key"] = get_key_list(CONN)
    mocs = []
    for row in CONN.execute(
        """select m.moc_name,
                                      coalesce(m.description, m.moc_name) as description,
                                      m.vizier_catalog
                               from cluster_in_moc c
                            join mocs m on c.moc_name = m.moc_name
                           where uid = %s
                           union
                           select mm.moc_name, mm.description, mm.vizier_catalog
                             from mocs mm where mm.is_full_sky
                             """
        % uid
    ).fetchall():
        moc = {"moc": row[0], "description": row[1], "link": ""}
        if row[2] is not None:
            url_data = {
                "-source": row[2],
                "-out.max": "unlimited",
                "-out.form": "HTML Table",
                "-out.add": "_r,_RAJ,_DEJ",
                "-sort": "_r",
                "-oc.form": "dec",
                "-c": "%+f%+f" % (float(html_data["ra"]), float(html_data["dec"])),
                "-c.eq": "J2000",
                "-c.r": 2,
                "-c.u": "arcmin",
                "-c.geom": "r",
            }
            link = "http://vizier.u-strasbg.fr/viz-bin/VizieR-4?%s" % urlencode(url_data)
            moc["link"] = '<a href="%s">Vizier data</a>' % link
        mocs.append(moc)
    html_data["mocs"] = mocs
    for row in cur.execute(
        """select rt.table_name,
                                     rt.uid_column,
                                     rt.extra_column, rt.description,
                                     rt.brief_columns
                                from reference_tables rt
                               where exists (select 1
                                               from data_references d
                                              where d.reference_table = rt.table_name
                                                and d.cluster_uid = %s)
                                  """
        % uid
    ):
        row = dict(row)
        column_list = get_brief_columns(row["table_name"], row["brief_columns"].split(","), negate=False)
        column_list = ",".join(["x.[%s]" % x for x in column_list])

        t1 = from_db_cursor(
            CONN.execute(
                """
            select {column_list}
              from [{table_name}] x
              join data_references r on r.reference_uid = [{uid_column}]
                                    and r.reference_table = '{table_name}'
             where r.cluster_uid = {uid}""".format(
                    column_list=column_list, uid=uid, **row
                )
            )
        )
        t1.border = True
        t1.float_format = ".3e"
        for column_properties in CONN.execute(
            """
        select column_name, output_format, description,
               data_unit, lower(data_type) as data_type
          from reference_tables_columns
         where reference_table = '%s'"""
            % row["table_name"]
        ).fetchall():
            if column_properties["data_type"] == "integer":
                t1.int_format[column_properties["column_name"]] = column_properties["output_format"]
            elif column_properties["data_type"] in ("real", "double"):
                t1.float_format[column_properties["column_name"]] = column_properties["output_format"]

        # Full table record (initially hidden)
        cursor = CONN.execute(
            """
            select x.*
              from [{table_name}] x
              join data_references r on r.reference_uid = [{uid_column}]
                                    and r.reference_table = '{table_name}'
             where r.cluster_uid = {uid}""".format(
                uid=uid, **row
            )
        )
        full_table = PrettiestTable()
        full_table.add_column("Parameter", [item[0] for item in cursor.description])
        for irow, arow in enumerate(cursor.fetchall()):
            full_table.add_column(str(irow), arow)
        if len(t1._rows) > 0:
            html_data["tables"].append(
                {
                    "title": row["description"],
                    "id": row["table_name"],
                    "full_table": full_table.get_html_string(attributes={"border": 1}),
                    "html": t1.get_html_string(attributes={"border": 1, "id": row["table_name"]}),
                }
            )
            html_data["params"].extend(select_cluster_key(uid, row, CONN))
    return t.render(html_data)