Exemplo n.º 1
0
    def get_catalog(self,
                    query=None,
                    query_fields=None,
                    print_query=False,
                    **kwargs):
        """
        Grab a catalog of sources around the input coordinate to the search radius

        Args:
            query: Not used
            query_fields (list, optional): Over-ride list of items to query
            print_query (bool): Print the SQL query generated

        Returns:
            astropy.table.Table:  Catalog of sources returned.  Includes WISE
            photometry for matched sources.
        """
        # Main DES query
        main_cat = super(DES_Survey,
                         self).get_catalog(query_fields=query_fields,
                                           print_query=print_query,
                                           **kwargs)
        if len(main_cat) == 0:
            main_cat = catalog_utils.clean_cat(main_cat, photom['DES'])
            return main_cat
        main_cat = catalog_utils.clean_cat(main_cat, photom['DES'])

        # WISE
        wise_query = self._gen_cat_query(qtype='wise')
        wise_cat = super(DES_Survey, self).get_catalog(query=wise_query,
                                                       print_query=print_query,
                                                       **kwargs)
        wise_cat = catalog_utils.clean_cat(wise_cat,
                                           photom['DES-WISE'],
                                           fill_mask=-999.)
        # Match em up
        if len(wise_cat) > 0:
            idx = catalog_utils.match_ids(wise_cat['DES_ID'],
                                          main_cat['DES_ID'],
                                          require_in_match=False)
            # Fill me
            for band in DES_WISE_bands:
                main_cat['WISE_{:s}'.format(band)] = -999.
                main_cat['WISE_{:s}'.format(band)][idx] = wise_cat[
                    band.replace("W", "WISE")]
                main_cat['WISE_{:s}_err'.format(band)] = -999.
                main_cat['WISE_{:s}_err'.format(band)][idx] = wise_cat[
                    '{:s}_err'.format(band.replace("W", "WISE"))]

        # Finish
        self.catalog = main_cat
        self.validate_catalog()
        return self.catalog
Exemplo n.º 2
0
Arquivo: sdss.py Projeto: FRBs/FRB
    def get_catalog(self, photoobj_fields=None, timeout=120, print_query=False):
        """
        Query SDSS for all objects within a given
        radius of the input coordinates.

        Merges photometry with photo-z

        TODO -- Expand to include spectroscopy
        TODO -- Consider grabbing all of the photometry fields

        Args:
            coord: astropy.coordiantes.SkyCoord
            radius: Angle, optional
              Search radius
            photoobj_fields: list
              Fields for querying
            timeout: float, optional
              Default value - 120 s.
            print_query: bool, optional
              Print the SQL query for the photo-z values

        Returns:
            catalog: astropy.table.Table
              Contains all measurements retieved
              *WARNING* :: The SDSS photometry table frequently has multiple entries for a given
              source, with unique objid values

        """
        if photoobj_fields is None:
            photoobj_fs = ['ra', 'dec', 'objid', 'run', 'rerun', 'camcol', 'field','type']
            mags = ['modelMag_'+band for band in SDSS_bands]
            magsErr = ['modelMagErr_'+band for band in SDSS_bands]
            extinct = ["extinction_"+band for band in SDSS_bands]
            photoobj_fields = photoobj_fs+mags+magsErr+extinct

        # Call
        photom_catalog = SDSS.query_region(self.coord, radius=self.radius, timeout=timeout,
                                           photoobj_fields=photoobj_fields)
        if photom_catalog is None:
            self.catalog = Table()
            self.catalog.meta['radius'] = self.radius
            self.catalog.meta['survey'] = self.survey
            # Validate
            self.validate_catalog()
            return

        # Now query for photo-z
        query = "SELECT GN.distance, "
        query += "p.objid, "

        query += "pz.z as redshift, pz.zErr as redshift_error\n"
        query += "FROM PhotoObj as p\n"
        query += "JOIN dbo.fGetNearbyObjEq({:f},{:f},{:f}) AS GN\nON GN.objID=p.objID\n".format(
            self.coord.ra.value,self.coord.dec.value,self.radius.to('arcmin').value)
        query += "JOIN Photoz AS pz ON pz.objID=p.objID\n"
        query += "ORDER BY distance"

        if print_query:
            print(query)

        # SQL command
        photz_cat = SDSS.query_sql(query,timeout=timeout)

        # Match em up
        if photz_cat is not None:
            matches = catalog_utils.match_ids(photz_cat['objid'], photom_catalog['objid'], require_in_match=False)
        else:
            matches = -1 * np.ones(len(photom_catalog), dtype=int)
        gdz = matches > 0
        # Init
        photom_catalog['photo_z'] = -9999.
        photom_catalog['photo_zerr'] = -9999.
        # Fill
        if np.any(gdz):
            photom_catalog['photo_z'][matches[gdz]] = photz_cat['redshift'][np.where(gdz)]
            photom_catalog['photo_zerr'][matches[gdz]] = photz_cat['redshift_error'][np.where(gdz)]

        # Trim down catalog
        trim_catalog = trim_down_catalog(photom_catalog, keep_photoz=True)

        # Clean up
        trim_catalog = catalog_utils.clean_cat(trim_catalog, photom['SDSS'])

        # Spectral info
        spec_fields = ['ra', 'dec', 'z', 'run2d', 'plate', 'fiberID', 'mjd', 'instrument']
        spec_catalog = SDSS.query_region(self.coord,spectro=True, radius=self.radius,
                                         timeout=timeout, specobj_fields=spec_fields) # Duplicates may exist
        if spec_catalog is not None:
            trim_spec_catalog = trim_down_catalog(spec_catalog)
            # Match
            spec_coords = SkyCoord(ra=trim_spec_catalog['ra'], dec=trim_spec_catalog['dec'], unit='deg')
            phot_coords = SkyCoord(ra=trim_catalog['ra'], dec=trim_catalog['dec'], unit='deg')
            idx, d2d, d3d = match_coordinates_sky(spec_coords, phot_coords, nthneighbor=1)
            # Check
            if np.max(d2d).to('arcsec').value > 1.5:
                raise ValueError("Bad match in SDSS")
            # Fill me
            zs = -1 * np.ones_like(trim_catalog['ra'].data)
            zs[idx] = trim_spec_catalog['z']
            trim_catalog['z_spec'] = zs
        else:
            trim_catalog['z_spec'] = -1.

        # Sort by offset
        catalog = trim_catalog.copy()
        self.catalog = catalog_utils.sort_by_separation(catalog, self.coord, radec=('ra','dec'), add_sep=True)

        # Meta
        self.catalog.meta['radius'] = self.radius
        self.catalog.meta['survey'] = self.survey

        # Validate
        self.validate_catalog()

        # Return
        return self.catalog.copy()