Beispiel #1
0
def test_get_spectra():
    """
    Check grabbing spectra from the specDB

    Note:  This requires that specdb be installed..
    """
    try:
        import specdb
    except ImportError:
        assert True
        return
    # Check for specDB file
    if utils.load_specdb() is None:
        assert True
        return
    # Do it!
    host180924 = frbgalaxy.FRBHost.by_name('180924')
    meta, xspec = host180924.get_metaspec()
    # Test
    assert isinstance(meta, Table)
    assert isinstance(xspec, xspectrum1d.XSpectrum1D)
    assert xspec.nspec == 1
    #
    meta, xspec = host180924.get_metaspec(instr='MUSE')
    # Test
    assert isinstance(xspec, xspectrum1d.XSpectrum1D)
    assert xspec.nspec == 1
    #
    meta, xspecs = host180924.get_metaspec(return_all=True)
    assert xspecs.nspec == 2
Beispiel #2
0
    def get_metaspec(self, instr=None, return_all=False, specdb_file=None):
        """
        Return the meta data and spectra for this FRBGalaxy
        from the specDB

        If there is more than one spectrum, the code returns the first
        unless return_all=True

        Args:
            instr (str, optional):
                Restrict to the input Instrument
            return_all (bool, optional):
                Return all of the meta, spectra
            specdb_file (str, optional):
                Path+name of the specDB file to use (over-ride the default)

        Returns:
            astropy.table.Table, linetools.spectra.XSpectrum1D: meta data, spectra

        """

        specDB = gutils.load_specdb(specdb_file=specdb_file)
        if specDB is None:
            print(f"{specdb_file} yielded nothing. Returning None")
            return

        # Grab the spectra
        xspec, meta = specDB.spectra_from_coord(
            self.coord)  # Tolerance is 0.5''

        # Return all?
        if return_all:
            return meta, xspec

        # Cut down
        if instr is None:
            if len(meta) > 1:
                warnings.warn(
                    "Multiple spectra returned for this galaxy.  Taking the first, but you may wish to specify your instrument"
                )
                xspec = xspec[0]
                meta = meta[0:1]
        else:
            idx = meta['GROUP'] == instr
            if np.sum(idx) == 0:
                warnings.warn("No spectrum with instrument = {}".format(instr))
                return
            elif np.sum(idx) > 1:
                warnings.warn(
                    "Multiple spectra returned for this galaxy.  Taking the first, but you may wish to specify your instrument"
                )
            xspec = xspec[np.where(idx)[0][0]]
            meta = meta[np.where(idx)[0][0]]
        # Return
        return meta, xspec
Beispiel #3
0
def main(pargs):
    """ Run
    """
    import warnings
    import numpy as np

    from astropy import units

    from frb import frb
    from frb.galaxies import utils as gutils

    from linetools.scripts.utils import coord_arg_to_coord

    try:
        from specdb import group_utils
    except ImportError:
        print("You need to first install specdb")
        return

    # Load it up
    specDB = gutils.load_specdb(specdb_file=pargs.specdb)
    if specDB is None:
        return

    if pargs.coord[0:3].upper() == 'FRB':
        frb = frb.FRB.by_name(pargs.coord.upper())
        icoord = frb.coord.ra.value, frb.coord.dec.value
    else:
        icoord = coord_arg_to_coord(pargs.coord)

    # Meta?
    if pargs.cat:
        if pargs.ang_offset is not None:
            _, cat, _ = specDB.qcat.query_position(
                icoord, pargs.ang_offset * units.arcsec)
        else:
            _, cat, _ = specDB.qcat.query_position(icoord,
                                                   pargs.rho * units.kpc)
        # Show
        ckeys = ['RA', 'DEC', 'zem', 'ZQ']
        group_utils.show_group_meta(cat, show_all_keys=False, meta_keys=ckeys)
    else:  # Meta
        if pargs.ang_offset is not None:
            meta = specDB.meta_from_position(icoord,
                                             pargs.ang_offset * units.arcsec)
        else:
            meta = specDB.meta_from_position(icoord, pargs.rho * units.kpc)

        # Keys
        mkeys = ['GROUP', 'RA_GROUP', 'DEC_GROUP', 'zem_GROUP', 'ZQ', 'Ref']
        # Show
        if meta is None:
            print(
                "No source found, try another location or a larger tolerance.")
            return
        else:
            group_utils.show_group_meta(
                meta, show_all_keys=False,
                meta_keys=mkeys)  #, max_lines=10000000)

    # Plot
    if pargs.plot:
        if pargs.cat:
            print("Cannot mix --plot with --cat.  Try again!")
            return
        plot_spec(specDB, meta, frb=frb)