Ejemplo n.º 1
0
def writeDB(filename, catalog, meta=None):
    """
    Output an sqlite3 database containing one table for each source type
    inputs:
    filename - output filename
    catalog - a catalog of sources to populated the database with
    """
    def sqlTypes(obj, names):
        """
        Return the sql type corresponding to each named parameter in obj
        """
        types = []
        for n in names:
            val = getattr(obj, n)
            if isinstance(val, bool):
                types.append("BOOL")
            elif isinstance(val, int):
                types.append("INT")
            elif isinstance(val, (float, np.float32)):  # float32 is bugged and claims not to be a float
                types.append("FLOAT")
            elif isinstance(val, (str, unicode)):
                types.append("VARCHAR")
            else:
                log.warn("Column {0} is of unknown type {1}".format(n, type(n)))
                log.warn("Using VARCHAR")
                types.append("VARCHAR)")
        return types

    if os.path.exists(filename):
        log.warn("overwriting {0}".format(filename))
        os.remove(filename)
    conn = sqlite3.connect(filename)
    db = conn.cursor()
    # determine the column names by inspecting the catalog class
    for t, tn in zip(classify_catalog(catalog), ["components", "islands", "simples"]):
        if len(t) < 1:
            continue  #don't write empty tables
        col_names = t[0].names
        col_types = sqlTypes(t[0], col_names)
        stmnt = ','.join(["{0} {1}".format(a, b) for a, b in zip(col_names, col_types)])
        db.execute('CREATE TABLE {0} ({1})'.format(tn, stmnt))
        stmnt = 'INSERT INTO {0} ({1}) VALUES ({2})'.format(tn, ','.join(col_names), ','.join(['?' for i in col_names]))
        # convert values of -1 into None
        nulls = lambda x: [x, None][x == -1]
        db.executemany(stmnt, [map(nulls, r.as_list()) for r in t])
        log.info("Created table {0}".format(tn))
    # metadata add some meta data
    db.execute("CREATE TABLE meta (key VARCHAR, val VARCHAR)")
    for k in meta:
        db.execute("INSERT INTO meta (key, val) VALUES (?,?)",(k,meta[k]))
    conn.commit()
    log.info(db.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall())
    conn.close()
    log.info("Wrote file {0}".format(filename))
    return
Ejemplo n.º 2
0
def write_catalog(filename, catalog, fmt=None, meta=None):
    """
    """
    if meta is None:
        meta = {}

    def writer(filename, catalog, fmt=None):
        # construct a dict of the data
        # this method preserves the data types in the VOTable
        tab_dict = {}
        for name in catalog[0].names:
            tab_dict[name] = [getattr(c, name, None) for c in catalog]
        t = Table(tab_dict, meta=meta)
        # re-order the columns
        t = t[[n for n in catalog[0].names]]
        if fmt is not None:
            if fmt in ["vot", "vo", "xml"]:
                vot = from_table(t)
                # description of this votable
                vot.description = repr(meta)
                writetoVO(vot, filename)
            elif fmt in ['hdf5']:
                t.write(filename, path='data', overwrite=True)
            elif fmt in ['fits']:
                writeFITSTable(filename, t)
            else:
                ascii.write(t, filename, fmt)
        else:
            ascii.write(t, filename)
        return

    # sort the sources into types and then write them out individually
    components, islands, simples = classify_catalog(catalog)

    if len(components) > 0:
        new_name = "{1}{0}{2}".format('_comp', *os.path.splitext(filename))
        writer(new_name, components, fmt)
        log.info("wrote {0}".format(new_name))
    if len(islands) > 0:
        new_name = "{1}{0}{2}".format('_isle', *os.path.splitext(filename))
        writer(new_name, islands, fmt)
        log.info("wrote {0}".format(new_name))
    if len(simples) > 0:
        new_name = "{1}{0}{2}".format('_simp', *os.path.splitext(filename))
        writer(new_name, simples, fmt)
        log.info("wrote {0}".format(new_name))
    return
Ejemplo n.º 3
0
def write_catalog(filename, catalog, fmt=None, meta=None):
    """
    """
    if meta is None:
        meta = {}

    def writer(filename, catalog, fmt=None):
        # construct a dict of the data
        # this method preserves the data types in the VOTable
        tab_dict = {}
        for name in catalog[0].names:
            tab_dict[name] = [getattr(c, name, None) for c in catalog]
        t = Table(tab_dict,meta=meta)
        # re-order the columns
        t = t[[n for n in catalog[0].names]]
        if fmt is not None:
            if fmt in ["vot", "vo", "xml"]:
                vot = from_table(t)
                # description of this votable
                vot.description = repr(meta)
                writetoVO(vot, filename)
            elif fmt in ['hdf5']:
                t.write(filename,path='data',overwrite=True)
            elif fmt in ['fits']:
                writeFITSTable(filename,t)
            else:
                ascii.write(t, filename, fmt)
        else:
            ascii.write(t, filename)
        return

    # sort the sources into types and then write them out individually
    components, islands, simples = classify_catalog(catalog)

    if len(components) > 0:
        new_name = "{1}{0}{2}".format('_comp', *os.path.splitext(filename))
        writer(new_name, components, fmt)
        log.info("wrote {0}".format(new_name))
    if len(islands) > 0:
        new_name = "{1}{0}{2}".format('_isle', *os.path.splitext(filename))
        writer(new_name, islands, fmt)
        log.info("wrote {0}".format(new_name))
    if len(simples) > 0:
        new_name = "{1}{0}{2}".format('_simp', *os.path.splitext(filename))
        writer(new_name, simples, fmt)
        log.info("wrote {0}".format(new_name))
    return
Ejemplo n.º 4
0
def writeAnn(filename, catalog, fmt):
    """
    Write an annotation file that can be read by Kvis (.ann) or DS9 (.reg).
    Uses ra/dec from catalog.
    Draws ellipses if bmaj/bmin/pa are in catalog
    Draws 30" circles otherwise
    Input:
        filename - file to write to
        catalog - a list of OutputSource or SimpleSource
        fmt - [.ann|.reg] format to use
    """
    components, islands, simples = classify_catalog(catalog)
    if len(components) > 0:
        catalog = sorted(components)
        suffix = "comp"
    elif len(simples) > 0:
        catalog = simples
        suffix = "simp"
    else:
        catalog = []

    if len(catalog) > 0:
        ras = [a.ra for a in catalog]
        decs = [a.dec for a in catalog]
        if not hasattr(catalog[0], 'a'):  #a being the variable that I used for bmaj.
            bmajs = [30 / 3600.0 for a in catalog]
            bmins = bmajs
            pas = [0 for a in catalog]
        else:
            bmajs = [a.a / 3600.0 for a in catalog]
            bmins = [a.b / 3600.0 for a in catalog]
            pas = [a.pa for a in catalog]

        names = [a.__repr__() for a in catalog]
        if fmt == 'ann':
            new_file = re.sub('.ann$', '_{0}.ann'.format(suffix), filename)
            out = open(new_file, 'w')
            print >> out, "#Aegean version {0}-({1})".format(__version__,__date__)
            print >> out, 'PA SKY'
            print >> out, 'FONT hershey12'
            print >> out, 'COORD W'
            formatter = "ELLIPSE W {0} {1} {2} {3} {4:+07.3f} #{5}\nTEXT W {0} {1} {5}"
        elif fmt == 'reg':
            new_file = re.sub('.reg$', '_{0}.reg'.format(suffix), filename)
            out = open(new_file, 'w')
            print >> out, "#Aegean version {0}-({1})".format(__version__,__date__)
            print >> out, "fk5"
            formatter = 'ellipse {0} {1} {2}d {3}d {4:+07.3f}d # text="{5}"'
            #DS9 has some strange ideas about position angle
            pas = [a - 90 for a in pas]

        for ra, dec, bmaj, bmin, pa, name in zip(ras, decs, bmajs, bmins, pas, names):
            #comment out lines that have invalid or stupid entries
            if np.nan in [ra, dec, bmaj, bmin, pa] or bmaj >= 180:
                print >> out, '#',
            print >> out, formatter.format(ra, dec, bmaj, bmin, pa, name)
        out.close()
        log.info("wrote {0}".format(new_file))
    if len(islands) > 0:
        if fmt == 'reg':
            new_file = re.sub('.reg$', '_isle.reg', filename)
        elif fmt == 'ann':
            log.warn('kvis islands are currently not working')
            return
        else:
            log.warn('format {0} not supported for island annotations'.format(fmt))
            return
        #writeIslandBoxes(new_file,islands,fmt)
        writeIslandContours(new_file, islands, fmt)
        log.info("wrote {0}".format(new_file))

    return
Ejemplo n.º 5
0
def writeDB(filename, catalog, meta=None):
    """
    Output an sqlite3 database containing one table for each source type
    inputs:
    filename - output filename
    catalog - a catalog of sources to populated the database with
    """
    def sqlTypes(obj, names):
        """
        Return the sql type corresponding to each named parameter in obj
        """
        types = []
        for n in names:
            val = getattr(obj, n)
            if isinstance(val, bool):
                types.append("BOOL")
            elif isinstance(val, int):
                types.append("INT")
            elif isinstance(
                    val, (float, np.float32
                          )):  # float32 is bugged and claims not to be a float
                types.append("FLOAT")
            elif isinstance(val, (str, unicode)):
                types.append("VARCHAR")
            else:
                log.warn("Column {0} is of unknown type {1}".format(
                    n, type(n)))
                log.warn("Using VARCHAR")
                types.append("VARCHAR)")
        return types

    if os.path.exists(filename):
        log.warn("overwriting {0}".format(filename))
        os.remove(filename)
    conn = sqlite3.connect(filename)
    db = conn.cursor()
    # determine the column names by inspecting the catalog class
    for t, tn in zip(classify_catalog(catalog),
                     ["components", "islands", "simples"]):
        if len(t) < 1:
            continue  #don't write empty tables
        col_names = t[0].names
        col_types = sqlTypes(t[0], col_names)
        stmnt = ','.join(
            ["{0} {1}".format(a, b) for a, b in zip(col_names, col_types)])
        db.execute('CREATE TABLE {0} ({1})'.format(tn, stmnt))
        stmnt = 'INSERT INTO {0} ({1}) VALUES ({2})'.format(
            tn, ','.join(col_names), ','.join(['?' for i in col_names]))
        # convert values of -1 into None
        nulls = lambda x: [x, None][x == -1]
        db.executemany(stmnt, [map(nulls, r.as_list()) for r in t])
        log.info("Created table {0}".format(tn))
    # metadata add some meta data
    db.execute("CREATE TABLE meta (key VARCHAR, val VARCHAR)")
    for k in meta:
        db.execute("INSERT INTO meta (key, val) VALUES (?,?)", (k, meta[k]))
    conn.commit()
    log.info(
        db.execute(
            "SELECT name FROM sqlite_master WHERE type='table';").fetchall())
    conn.close()
    log.info("Wrote file {0}".format(filename))
    return
Ejemplo n.º 6
0
def writeAnn(filename, catalog, fmt):
    """
    Write an annotation file that can be read by Kvis (.ann) or DS9 (.reg).
    Uses ra/dec from catalog.
    Draws ellipses if bmaj/bmin/pa are in catalog
    Draws 30" circles otherwise
    Input:
        filename - file to write to
        catalog - a list of OutputSource or SimpleSource
        fmt - [.ann|.reg] format to use
    """
    components, islands, simples = classify_catalog(catalog)
    if len(components) > 0:
        catalog = sorted(components)
        suffix = "comp"
    elif len(simples) > 0:
        catalog = simples
        suffix = "simp"
    else:
        catalog = []

    if len(catalog) > 0:
        ras = [a.ra for a in catalog]
        decs = [a.dec for a in catalog]
        if not hasattr(catalog[0],
                       'a'):  #a being the variable that I used for bmaj.
            bmajs = [30 / 3600.0 for a in catalog]
            bmins = bmajs
            pas = [0 for a in catalog]
        else:
            bmajs = [a.a / 3600.0 for a in catalog]
            bmins = [a.b / 3600.0 for a in catalog]
            pas = [a.pa for a in catalog]

        names = [a.__repr__() for a in catalog]
        if fmt == 'ann':
            new_file = re.sub('.ann$', '_{0}.ann'.format(suffix), filename)
            out = open(new_file, 'w')
            print >> out, "#Aegean version {0}-({1})".format(
                __version__, __date__)
            print >> out, 'PA SKY'
            print >> out, 'FONT hershey12'
            print >> out, 'COORD W'
            formatter = "ELLIPSE W {0} {1} {2} {3} {4:+07.3f} #{5}\nTEXT W {0} {1} {5}"
        elif fmt == 'reg':
            new_file = re.sub('.reg$', '_{0}.reg'.format(suffix), filename)
            out = open(new_file, 'w')
            print >> out, "#Aegean version {0}-({1})".format(
                __version__, __date__)
            print >> out, "fk5"
            formatter = 'ellipse {0} {1} {2}d {3}d {4:+07.3f}d # text="{5}"'
            #DS9 has some strange ideas about position angle
            pas = [a - 90 for a in pas]

        for ra, dec, bmaj, bmin, pa, name in zip(ras, decs, bmajs, bmins, pas,
                                                 names):
            #comment out lines that have invalid or stupid entries
            if np.nan in [ra, dec, bmaj, bmin, pa] or bmaj >= 180:
                print >> out, '#',
            print >> out, formatter.format(ra, dec, bmaj, bmin, pa, name)
        out.close()
        log.info("wrote {0}".format(new_file))
    if len(islands) > 0:
        if fmt == 'reg':
            new_file = re.sub('.reg$', '_isle.reg', filename)
        elif fmt == 'ann':
            log.warn('kvis islands are currently not working')
            return
        else:
            log.warn(
                'format {0} not supported for island annotations'.format(fmt))
            return
        #writeIslandBoxes(new_file,islands,fmt)
        writeIslandContours(new_file, islands, fmt)
        log.info("wrote {0}".format(new_file))

    return
Ejemplo n.º 7
0
def writeDB(filename, catalog, meta=None):
    """
    Output an sqlite3 database containing one table for each source type

    Parameters
    ----------
    filename : str
        Output filename

    catalog : list
        List of sources of type :class:`AegeanTools.models.OutputSource`,
        :class:`AegeanTools.models.SimpleSource`, or :class:`AegeanTools.models.IslandSource`.

    meta : dict
        Meta data to be written to table `meta`

    Returns
    -------
    None
    """
    def sqlTypes(obj, names):
        """
        Return the sql type corresponding to each named parameter in obj
        """
        types = []
        for n in names:
            val = getattr(obj, n)
            if isinstance(val, bool):
                types.append("BOOL")
            elif isinstance(val, (int, np.int64, np.int32)):
                types.append("INT")
            elif isinstance(
                    val, (float, np.float64, np.float32
                          )):  # float32 is bugged and claims not to be a float
                types.append("FLOAT")
            elif isinstance(val, six.string_types):
                types.append("VARCHAR")
            else:
                log.warning("Column {0} is of unknown type {1}".format(
                    n, type(n)))
                log.warning("Using VARCHAR")
                types.append("VARCHAR")
        return types

    if os.path.exists(filename):
        log.warning("overwriting {0}".format(filename))
        os.remove(filename)
    conn = sqlite3.connect(filename)
    db = conn.cursor()
    # determine the column names by inspecting the catalog class
    for t, tn in zip(classify_catalog(catalog),
                     ["components", "islands", "simples"]):
        if len(t) < 1:
            continue  #don't write empty tables
        col_names = t[0].names
        col_types = sqlTypes(t[0], col_names)
        stmnt = ','.join(
            ["{0} {1}".format(a, b) for a, b in zip(col_names, col_types)])
        db.execute('CREATE TABLE {0} ({1})'.format(tn, stmnt))
        stmnt = 'INSERT INTO {0} ({1}) VALUES ({2})'.format(
            tn, ','.join(col_names), ','.join(['?' for i in col_names]))
        # expend the iterators that are created by python 3+
        data = list(map(nulls, list(r.as_list() for r in t)))
        db.executemany(stmnt, data)
        log.info("Created table {0}".format(tn))
    # metadata add some meta data
    db.execute("CREATE TABLE meta (key VARCHAR, val VARCHAR)")
    for k in meta:
        db.execute("INSERT INTO meta (key, val) VALUES (?,?)", (k, meta[k]))
    conn.commit()
    log.info(
        db.execute(
            "SELECT name FROM sqlite_master WHERE type='table';").fetchall())
    conn.close()
    log.info("Wrote file {0}".format(filename))
    return
Ejemplo n.º 8
0
def writeAnn(filename, catalog, fmt):
    """
    Write an annotation file that can be read by Kvis (.ann) or DS9 (.reg).
    Uses ra/dec from catalog.
    Draws ellipses if bmaj/bmin/pa are in catalog. Draws 30" circles otherwise.

    Only :class:`AegeanTools.models.OutputSource` will appear in the annotation file
    unless there are none, in which case :class:`AegeanTools.models.SimpleSource` (if present)
    will be written. If any :class:`AegeanTools.models.IslandSource` objects are present then
    an island contours file will be written.

    Parameters
    ----------
    filename : str
        Output filename base.

    catalog : list
        List of sources.

    fmt : ['ann', 'reg']
        Output file type.

    Returns
    -------
    None

    See Also
    --------
    AegeanTools.catalogs.writeIslandContours
    """
    """
    Input:
        filename - file to write to
        catalog - a list of OutputSource or SimpleSource
        fmt - [.ann|.reg] format to use
    """
    if fmt not in ['reg', 'ann']:
        log.warning("Format not supported for island boxes{0}".format(fmt))
        return  # fmt not supported

    components, islands, simples = classify_catalog(catalog)
    if len(components) > 0:
        cat = sorted(components)
        suffix = "comp"
    elif len(simples) > 0:
        cat = simples
        suffix = "simp"
    else:
        cat = []

    if len(cat) > 0:
        ras = [a.ra for a in cat]
        decs = [a.dec for a in cat]
        if not hasattr(cat[0],
                       'a'):  # a being the variable that I used for bmaj.
            bmajs = [30 / 3600.0 for a in cat]
            bmins = bmajs
            pas = [0 for a in cat]
        else:
            bmajs = [a.a / 3600.0 for a in cat]
            bmins = [a.b / 3600.0 for a in cat]
            pas = [a.pa for a in cat]

        names = [a.__repr__() for a in cat]
        if fmt == 'ann':
            new_file = re.sub('.ann$', '_{0}.ann'.format(suffix), filename)
            out = open(new_file, 'w')
            print("#Aegean version {0}-({1})".format(__version__, __date__),
                  file=out)
            print('PA SKY', file=out)
            print('FONT hershey12', file=out)
            print('COORD W', file=out)
            formatter = "ELLIPSE W {0} {1} {2} {3} {4:+07.3f} #{5}\nTEXT W {0} {1} {5}"
        else:  # reg
            new_file = re.sub('.reg$', '_{0}.reg'.format(suffix), filename)
            out = open(new_file, 'w')
            print("#Aegean version {0}-({1})".format(__version__, __date__),
                  file=out)
            print("fk5", file=out)
            formatter = 'ellipse {0} {1} {2:.9f}d {3:.9f}d {4:+07.3f}d # text="{5}"'
            # DS9 has some strange ideas about position angle
            pas = [a - 90 for a in pas]

        for ra, dec, bmaj, bmin, pa, name in zip(ras, decs, bmajs, bmins, pas,
                                                 names):
            # comment out lines that have invalid or stupid entries
            if np.nan in [ra, dec, bmaj, bmin, pa] or bmaj >= 180:
                print('#', end=' ', file=out)
            print(formatter.format(ra, dec, bmaj, bmin, pa, name), file=out)
        out.close()
        log.info("wrote {0}".format(new_file))
    if len(islands) > 0:
        if fmt == 'reg':
            new_file = re.sub('.reg$', '_isle.reg', filename)
        elif fmt == 'ann':
            log.warning('kvis islands are currently not working')
            return
        else:
            log.warning(
                'format {0} not supported for island annotations'.format(fmt))
            return
        writeIslandContours(new_file, islands, fmt)
        log.info("wrote {0}".format(new_file))

    return
Ejemplo n.º 9
0
def write_catalog(filename, catalog, fmt=None, meta=None):
    """
    Write a catalog (list of sources) to a file with format determined by extension.

    Sources must be of type :class:`AegeanTools.models.OutputSource`,
    :class:`AegeanTools.models.SimpleSource`, or :class:`AegeanTools.models.IslandSource`.

    Parameters
    ----------
    filename : str
        Base name for file to write. `_simp`, `_comp`, or `_isle` will be added to differentiate
        the different types of sources that are being written.

    catalog : list
        A list of source objects. Sources must be of type :class:`AegeanTools.models.OutputSource`,
        :class:`AegeanTools.models.SimpleSource`, or :class:`AegeanTools.models.IslandSource`.

    fmt : str
        The file format extension.

    meta : dict
        A dictionary to be used as metadata for some file types (fits, VOTable).

    Returns
    -------
    None
    """
    if meta is None:
        meta = {}

    def writer(filename, catalog, fmt=None):
        # construct a dict of the data
        # this method preserves the data types in the VOTable
        tab_dict = {}
        name_list = []
        for name in catalog[0].names:
            col_name = name
            if catalog[0].galactic:
                if name.startswith('ra'):
                    col_name = 'lon' + name[2:]
                elif name.endswith('ra'):
                    col_name = name[:-2] + 'lon'
                elif name.startswith('dec'):
                    col_name = 'lat' + name[3:]
                elif name.endswith('dec'):
                    col_name = name[:-3] + 'lat'

            tab_dict[col_name] = [getattr(c, name, None) for c in catalog]
            name_list.append(col_name)
        t = Table(tab_dict, meta=meta)
        # re-order the columns
        t = t[[n for n in name_list]]

        if fmt is not None:
            if fmt in ["vot", "vo", "xml"]:
                vot = from_table(t)
                # description of this votable
                vot.description = repr(meta)
                writetoVO(vot, filename)
            elif fmt in ['hdf5']:
                t.write(filename, path='data', overwrite=True)
            elif fmt in ['fits']:
                writeFITSTable(filename, t)
            else:
                ascii.write(t, filename, fmt)
        else:
            ascii.write(t, filename)
        return

    # sort the sources into types and then write them out individually
    components, islands, simples = classify_catalog(catalog)

    if len(components) > 0:
        new_name = "{1}{0}{2}".format('_comp', *os.path.splitext(filename))
        writer(new_name, components, fmt)
        log.info("wrote {0}".format(new_name))
    if len(islands) > 0:
        new_name = "{1}{0}{2}".format('_isle', *os.path.splitext(filename))
        writer(new_name, islands, fmt)
        log.info("wrote {0}".format(new_name))
    if len(simples) > 0:
        new_name = "{1}{0}{2}".format('_simp', *os.path.splitext(filename))
        writer(new_name, simples, fmt)
        log.info("wrote {0}".format(new_name))
    return