Exemple #1
0
def sdss_fits(coo, filtro='r'):
    
    try:
        n_coo=len(coo)
    except:
        n_coo=1
        coo=[coo]

    n_col=np.min([n_coo,4])
    n_row=np.ceil(n_coo*1./n_col)
    fig, ax = plt.subplots(figsize=(5*n_col,5*n_row), sharex=True, sharey=True)
    for i in range(len(coo)):
        ax = plt.subplot(n_row, n_col, i+1)
        print 'Procesando galaxia '+str(i)
        xid = SDSS.query_region(coo[i], spectro=True)
        image=SDSS.get_images(matches=xid, band=filtro)[0][0]

        im_h=image.header
        im_data=image.data

        im_median=np.median(im_data)
        im_std=np.std(im_data)

        im_wcs = WCS(im_h)
        im_data = Cutout2D(im_data, coo[i], u.Quantity((1., 1.), u.arcmin), wcs=im_wcs).data

#        ax.imshow(im_data,vmin=im_median-1*im_std, vmax=im_median+5*im_std, cmap=plt.get_cmap('gray'), interpolation='nearest')
        ax.imshow(imtoasinh(im_data.T), vmin=0.1, vmax=0.8, cmap=plt.get_cmap('gray'), interpolation='nearest', origin='lower')
        ax.axis('off')
        ax.text(0.05,0.05, str(i), transform=ax.transAxes, color='white', fontsize=16)
        ax.text(0.95,0.05, 'Filtro '+filtro, transform=ax.transAxes, color='white', fontsize=16, horizontalalignment='right')
        ax.invert_xaxis()
        
    fig.subplots_adjust(hspace=0.1, wspace=0.1)
Exemple #2
0
def save_data(index, ra, dec, req_par):

    output_file = 'Result.dat'

    #This function takes in coordinates and finds their SDSS objid and specobjid
    #The objid and specobjid is needed to find the required parameters
    #The required parameter is then extracted and saved on to a dat file

    for i in range(len(index)):

        c = SkyCoord(ra=ra[i], dec=dec[i], unit=(u.deg, u.deg), frame='icrs')
        result_SDSS = None
        try:
            result_SDSS = SDSS.query_region(c, spectro=True, radius='3s')

        except:
            print('Failed to query SDSS - ', i)
        if result_SDSS is None:
            print(i)

        if result_SDSS is not None:

            objid = str(result_SDSS['objid'][0])  # Find SDSS objid
            specobjid = str(result_SDSS['specobjid'][0])  # Find SDSS specobjid

            par_value = get_value(
                objid, specobjid, req_par
            )  # Calls the webscrapping function to extract the value of the required parameter

            f = open(output_file, "a")
            f.write('%d %f %f %f\n' % (index[i], ra[i], dec[i], par_value))
            f.close()
            print('%d %f %f %f' % (index[i], ra[i], dec[i], par_value))

    return None
def rand_loop_through__sep(f, length):
    sep_list = []
    while len(sep_list) < length:

        righta = np.random.uniform(-180, 180)
        declin = np.random.uniform(-90, 90)

        pos = coords.SkyCoord(ra=righta * u.degree, dec=declin * u.degree)
        galatic_pos = pos.galactic
        xid = SDSS.query_region(galatic_pos,
                                spectro=False,
                                radius=2.5 * u.arcmin,
                                photoobj_fields=['ra', 'dec'])

        if xid is None:
            continue

        xid = unique(xid, keys=['ra', 'dec'])
        righta_list = xid["ra"]
        declin_list = xid["dec"]

        sep = pos.separation(
            coords.SkyCoord(ra=righta_list * u.degree,
                            dec=declin_list * u.degree))
        sep_list = np.concatenate((sep_list, sep))
        print(len(sep_list))

    return sep_list
Exemple #4
0
    def query_region(self,
                     ra_hours: float,
                     dec_degs: float,
                     constraints: Optional[TDict[str, str]] = None,
                     limit: Optional[int] = None,
                     **region) -> TList[CatalogSource]:
        """
        Return SDSS catalog objects within the specified rectangular region

        :param ra_hours: right ascension of region center in hours
        :param dec_degs: declination of region center in degrees
        :param constraints: optional constraints on the column values
        :param limit: maximum number of rows to return
        :param region: keywords defining the query region

        :return: list of catalog objects within the specified rectangular region
        """
        sdss = SDSS()
        return self.table_to_sources(
            sdss.query_region(SkyCoord(ra=ra_hours,
                                       dec=dec_degs,
                                       unit=(hour, deg),
                                       frame='icrs'),
                              data_release=15,
                              photoobj_fields=self._columns,
                              cache=False,
                              **region))
Exemple #5
0
def querySDSS(ra, dec, radius):
    ''' using astroquery sdss system '''
    pos = SkyCoord(ra*u.deg, dec*u.deg, frame='fk5')
    table = SDSS.query_region(pos, radius=radius*u.deg,
                              photoobj_fields=['ra', 'dec', 'objid', 'type',
                                               'u', 'g', 'r', 'i', 'z'])
    return table[np.where(table['type'] == 6)[0]]
def cross_match_sdss(imageFile,
                     extension,
                     sources,
                     quality_detections,
                     extent,
                     search_radius=3):

    data, image, hdulist, size, mid_point = load_image_data(
        imageFile, extent, extension=extension)
    # garbage collect data and image
    data = None
    image = None

    # cross match quality detections with SDSS
    filter = hdulist[extension].header["HIERARCH FPA.FILTERID"].split(".")[0]

    # get WCS info from fits header
    wcs = WCS(hdulist[extension].header)

    reference_dict = {}
    pos_output = open(imageFile + ".quality.coo", "w")
    count = 0
    for index in quality_detections:
        if count == 30:
            break
        print
        pixcrd = np.array([[sources['xcentroid'][index]+((size[0]/2.0)-extent), \
                            sources['ycentroid'][index]+((size[1]/2.0)-extent)]], np.float_)

        world = wcs.wcs_pix2world(pixcrd, 1)
        pos = coords.SkyCoord(ra=world[0][0] * u.degree,
                              dec=world[0][1] * u.degree,
                              frame='icrs')
        # search ? arsec region in SDSS
        xid = SDSS.query_region(pos,
                                radius=search_radius * (1 / 3600.0) * u.degree)
        print xid
        try:
            for i in range(len(xid["objid"])):
                id = xid["objid"][i]
                mag, mag_err = get_reference_mags(id, filter)
                if mag == "NULL":
                    continue
                break
        except TypeError:
            continue
        print
        print "[*] %s, %s, %s, %s" % (
            id, str(
                (sources['xcentroid'][index], sources['ycentroid'][index])),
            str(mag), str(mag_err))
        if mag != "NULL" and mag_err != "NULL":
            reference_dict[index] = {"mag":float(mag), "mag_err":float(mag_err), \
                                     "id":id, "pos":(sources['xcentroid'][index], \
                                                     sources['ycentroid'][index])}
            pos_output.write("%s %s\n" %
                             (str(pixcrd[0][0]), str(pixcrd[0][1])))
        count += 1
    pos_output.close()
    return reference_dict
Exemple #7
0
 def download_sdss_data(self):
     sdss = SDSS()
     self.info_table = sdss.query_region(self.loc, radius=self.r * u.arcsec)
     self.photo_data_table = sdss.query_region(
         self.loc,
         radius=self.r * u.arcsec,
         photoobj_fields=['u', 'g', 'r', 'i', 'z'])
     while self.info_table is None and self.r <= 10:
         self.r = self.r + 0.4
         self.info_table = sdss.query_region(self.loc,
                                             radius=self.r * u.arcsec)
         self.photo_data_table = sdss.query_region(
             self.loc,
             radius=self.r * u.arcsec,
             photoobj_fields=['u', 'g', 'r', 'i', 'z'])
     self.objid = self.info_table['objid'][0]
Exemple #8
0
def fetch_spectra_SDSS(object_name, save_dir, coords):
    """
    saves a textfile in self.save_dir where the first column is the wavelength in angstroms and the second
    column is flux in erg cm-2 s-1 AA-1
    :return: the path to the saved spectrum file
    """
    if os.path.exists(save_dir + object_name + 'spectrum.dat'):
        getLogger(__name__).info('Spectrum already loaded, will not be reloaded')
        spectrum_file = save_dir + object_name + 'spectrum.dat'
        return spectrum_file
    getLogger(__name__).info('Looking for {} spectrum in SDSS catalog'.format(object_name))
    result = SDSS.query_region(coords, spectro=True)
    if not result:
        getLogger(__name__).warning('Could not find spectrum for {} at {},{} in SDSS catalog'.format(object_name, coords.ra, coords.dec))
        spectrum_file = None
        return spectrum_file
    spec = SDSS.get_spectra(matches=result)
    data = spec[0][1].data
    lamb = 10**data['loglam'] * u.AA
    flux = data['flux'] * 10 ** -17 * u.Unit('erg cm-2 s-1 AA-1')
    spectrum = Spectrum1D(spectral_axis=lamb, flux=flux)
    res = np.array([spectrum.spectral_axis, spectrum.flux])
    res = res.T
    spectrum_file = save_dir + object_name + 'spectrum.dat'
    np.savetxt(spectrum_file, res, fmt='%1.4e')
    getLogger(__name__).info('Spectrum loaded for {} from SDSS catalog'.format(object_name))
    return spectrum_file
Exemple #9
0
def query_sdss(coor, radius=None):
    try:
        pos = coords.SkyCoord(coor, frame='icrs')
    except:
        pos = coords.SkyCoord(raw_to_skycoor(coor), frame='icrs')
    if not radius:
        radius = 20. * units.arcsec
    return SDSS.query_region(pos ,radius=radius, spectro=True)
 def standardQuery(self):
     self.result = SDSS.query_region(self.position, radius=self.rad, spectro=True)
     try:
         for i in range(0, len(self.result)):
             self.ra.append(self.result[i]['ra'])
             self.dec.append(self.result[i]['dec'])
         return self.result
     except:
         raise ValueError("No Results found. Try a different search area.")
def query_sdss(ra, dec, unit='deg', frame='icrs', band='i', spectro=False):
    '''Query SDSS. Many filters are available. See this URL for more:
    http://skyserver.sdss.org/dr2/en/proj/advanced/color/sdssfilters.asp.'''

    position = coords.SkyCoord(ra, dec, unit=unit, frame=frame)
    id = SDSS.query_region(position, spectro=spectro)
    images = SDSS.get_images(matches=id, band=band)

    return images[0]  # use first image, which is the corrected one I think
Exemple #12
0
def sdss_fits(coo, filtro='r'):

    try:
        n_coo = len(coo)
    except:
        n_coo = 1
        coo = [coo]

    n_col = np.min([n_coo, 4])
    n_row = np.ceil(n_coo * 1. / n_col)
    fig, ax = plt.subplots(figsize=(5 * n_col, 5 * n_row),
                           sharex=True,
                           sharey=True)
    for i in range(len(coo)):
        ax = plt.subplot(n_row, n_col, i + 1)
        print 'Procesando galaxia ' + str(i)
        xid = SDSS.query_region(coo[i], spectro=True)
        image = SDSS.get_images(matches=xid, band=filtro)[0][0]

        im_h = image.header
        im_data = image.data

        im_median = np.median(im_data)
        im_std = np.std(im_data)

        im_wcs = WCS(im_h)
        im_data = Cutout2D(im_data,
                           coo[i],
                           u.Quantity((1., 1.), u.arcmin),
                           wcs=im_wcs).data

        #        ax.imshow(im_data,vmin=im_median-1*im_std, vmax=im_median+5*im_std, cmap=plt.get_cmap('gray'), interpolation='nearest')
        ax.imshow(imtoasinh(im_data.T),
                  vmin=0.1,
                  vmax=0.8,
                  cmap=plt.get_cmap('gray'),
                  interpolation='nearest',
                  origin='lower')
        ax.axis('off')
        ax.text(0.05,
                0.05,
                str(i),
                transform=ax.transAxes,
                color='white',
                fontsize=16)
        ax.text(0.95,
                0.05,
                'Filtro ' + filtro,
                transform=ax.transAxes,
                color='white',
                fontsize=16,
                horizontalalignment='right')
        ax.invert_xaxis()

    fig.subplots_adjust(hspace=0.1, wspace=0.1)
Exemple #13
0
def getimg(ira, idec, imsize, BW=False, DSS=None):
    ''' Grab an SDSS image from the given URL, if possible

    Parameters:
    ----------
    ira: (float or Quantity) RA in decimal degrees
    idec: (float or Quantity) DEC in decimal degrees
    imsize: Image size in arcmin (without units)
    '''
    import PIL
    from PIL import Image
    # Strip units as need be
    try:
        ra = ira.value
    except AttributeError:
        ra = ira
        dec = idec
    else:
        dec = idec.value

    # Get URL
    if DSS == None:  # Default
        url = sdsshttp(ra, dec, imsize)
    else:
        url = dsshttp(ra, dec, imsize)  # DSS

    # Request
    rtv = requests.get(url)

    # Query for photometry
    coord = SkyCoord(ra=ra * u.degree, dec=dec * u.degree)
    phot = SDSS.query_region(coord, radius=0.02 * u.deg)

    if phot is None:
        print('getimg: Pulling from DSS instead of SDSS')
        BW = 1
        url = dsshttp(ra, dec, imsize)  # DSS
        rtv = requests.get(url)

    # Python 3
    try:
        img = Image.open(StringIO(rtv.content))
    except:
        img = Image.open(BytesIO(rtv.content))

    # B&W ?
    if BW:
        import PIL.ImageOps
        img2 = img.convert("L")
        img2 = PIL.ImageOps.invert(img2)
        img = img2

    return img, BW
def loop_through__sep(table, f, length, mag_limit, z_lim):
    sep_list = []

    for i in np.arange(length):

        righta = table["RA"][i]
        declin = table["DEC"][i]

        if np.abs(table["GLAT"][i]) < 20:
            continue

        pos = coords.SkyCoord(ra=righta * u.degree, dec=declin * u.degree)
        galatic_pos = pos.galactic

        xid = SDSS.query_region(galatic_pos,
                                spectro=True,
                                radius=2.5 * u.arcmin,
                                specobj_fields=['Z', 'class'],
                                photoobj_fields=[
                                    'ra', 'dec', 'modelMag_u', 'modelMag_g',
                                    'modelMag_r', 'modelMag_i'
                                ])

        if xid is None:
            continue

        xid = unique(xid, keys=['ra', 'dec'])

        xid.sort(f)

        mask_redshift = xid['Z'] < z_lim
        xid = xid[mask_redshift]

        if xid is None:
            continue

        for v in np.arange(len(xid)):

            max_mag_object = xid[v]
            max_mag_object = Table(max_mag_object)

            if max_mag_object[f] > mag_limit and v > 0:
                break

            if max_mag_object["class"] == "GALAXY":
                righta_max = max_mag_object["ra"]
                declin_max = max_mag_object["dec"]
                sep = pos.separation(
                    coords.SkyCoord(ra=righta_max * u.degree,
                                    dec=declin_max * u.degree))
                sep_list.append(sep.arcsecond[0])

    return sep_list
Exemple #15
0
def lco_xid_sdss_query():
    """
    Get sdss star properties, rmag and wcs for 112 target fields in our LCOLSS program.
    Needed if want to do a ZP calibration to the LCO data.

    Note requires Visiblity.csv stored locally. Matches ra/dec of lco targets from Visibility.csv 
    to stars from sdss database
    """

    from astroquery.sdss import SDSS
    visibility = ascii.read('Visibility.csv')

    Source_IDs = visibility['col1']  # Source IDs
    ra_deg = visibility['col2']
    dec_deg = visibility['col3']
    for idx in range(1, len(visibility[1:]) + 1):
        print("------------------------------")
        obj, ra, dec = Source_IDs[idx], ra_deg[idx], dec_deg[idx]
        print(idx, obj, ra, dec)
        """
        full_radius ~ pixscale * 2048 is arcsec from center of an LCO exposure image
        go to 90% of that radius to account for target ra/dec dithers i.e. not being perfectly centered and edge effects
        """
        full_radius = 0.389 * (4096 / 2)
        radius = 0.85 * full_radius
        strradius = str(radius) + ' arcsec'
        print(
            radius, 'ra ~ [{:.2f},{:.2f}], dec ~ [{:.2f},{:.2f}]'.format(
                float(ra) - radius / 3600,
                float(ra) + radius / 3600,
                float(dec) - radius / 3600,
                float(dec) + radius / 3600))
        fields = [
            'ra', 'dec', 'objid', 'run', 'rerun', 'camcol', 'field', 'r',
            'mode', 'nChild', 'type', 'clean', 'probPSF', 'psfMag_r',
            'psfMagErr_r'
        ]
        pos = SkyCoord(ra, dec, unit="deg", frame='icrs')
        xid = SDSS.query_region(pos,
                                radius=strradius,
                                fields='PhotoObj',
                                photoobj_fields=fields)
        Star = xid[xid['probPSF'] == 1]
        Gal = xid[xid['probPSF'] == 0]
        print(len(xid), len(Star), len(Gal))
        Star = Star[Star['clean'] == 1]
        print(len(Star))
        ascii.write(Star, f"{obj}_SDSS_CleanStar.csv")

        idx += 1
Exemple #16
0
def getimg(ira, idec, imsize, BW=False, DSS=None):
    ''' Grab an SDSS image from the given URL, if possible

    Parameters:
    ----------
    ira: (float or Quantity) RA in decimal degrees
    idec: (float or Quantity) DEC in decimal degrees
    imsize: Image size in arcmin (without units)
    '''
    import PIL
    from PIL import Image
    # Strip units as need be
    try:
        ra = ira.value
    except AttributeError:
        ra = ira
        dec = idec
    else:
        dec = idec.value
    
    # Get URL
    if DSS == None:  # Default
        url = sdsshttp(ra,dec,imsize)
    else:
        url = dsshttp(ra,dec,imsize) # DSS

    # Request
    rtv = requests.get(url) 

    # Query for photometry
    coord = SkyCoord(ra=ra*u.degree, dec=dec*u.degree)
    phot = SDSS.query_region(coord, radius=0.02*u.deg)

    if phot is None:
        print('getimg: Pulling from DSS instead of SDSS')
        BW = 1
        url = dsshttp(ra,dec,imsize) # DSS
        rtv = requests.get(url) 

    img = Image.open(StringIO(rtv.content))

    # B&W ?
    if BW:
        import PIL.ImageOps
        img2 = img.convert("L")
        img2 = PIL.ImageOps.invert(img2)
        img = img2

    return img, BW
Exemple #17
0
def loop_through_planck_petro(table, f, length, mag_limit, z_lim):

    new_table_galaxy = Table(names=(table.colnames))

    for i in np.arange(length):

        righta = table["RA"][i]
        declin = table["DEC"][i]

        if np.abs(table["GLAT"][i]) < 20:
            continue

        pos = coords.SkyCoord(ra=righta * u.degree, dec=declin * u.degree)
        galatic_pos = pos.galactic

        xid = SDSS.query_region(galatic_pos,
                                spectro=True,
                                radius=2.5 * u.arcmin,
                                specobj_fields=['Z', 'class'],
                                photoobj_fields=[
                                    'ra', 'dec', 'petroMag_u', 'petroMag_g',
                                    'petroMag_r', 'petroMag_i'
                                ])

        if xid is None:
            continue

        xid = unique(xid, keys=['ra', 'dec'])

        mask_redshift = xid['Z'] < z_lim
        xid = xid[mask_redshift]

        if xid is None:
            continue

        xid.sort(f)

        for v in np.arange(len(xid)):

            max_mag_object = xid[v]
            max_mag_object = Table(max_mag_object)

            if max_mag_object[f] > mag_limit and v > 0:
                break

            if max_mag_object["class"] == "GALAXY":
                new_table_galaxy = vstack([new_table_galaxy, max_mag_object])

    return [new_table_galaxy]
def cross_match_sdss(imageFile, extension, sources, quality_detections, extent, search_radius=3):
    
    
    data, image, hdulist, size, mid_point = load_image_data(imageFile, extent, extension=extension)
    # garbage collect data and image
    data = None
    image = None
    
    # cross match quality detections with SDSS
    filter = hdulist[extension].header["HIERARCH FPA.FILTERID"].split(".")[0]
    
    # get WCS info from fits header
    wcs = WCS(hdulist[extension].header)
    
    reference_dict = {}
    pos_output = open(imageFile+".quality.coo", "w")
    count = 0
    for index in quality_detections:
        if count == 30:
            break
        print
        pixcrd = np.array([[sources['xcentroid'][index]+((size[0]/2.0)-extent), \
                            sources['ycentroid'][index]+((size[1]/2.0)-extent)]], np.float_)
            
        world = wcs.wcs_pix2world(pixcrd, 1)
        pos = coords.SkyCoord(ra=world[0][0]*u.degree,dec=world[0][1]*u.degree, frame='icrs')
        # search ? arsec region in SDSS
        xid = SDSS.query_region(pos, radius=search_radius*(1/3600.0)*u.degree)
        print xid
        try:
            for i in range(len(xid["objid"])):
                id = xid["objid"][i]
                mag, mag_err = get_reference_mags(id, filter)
                if mag == "NULL":
                    continue
                break
        except TypeError:
            continue
        print
        print "[*] %s, %s, %s, %s" % (id, str((sources['xcentroid'][index], sources['ycentroid'][index])), str(mag), str(mag_err))
        if mag != "NULL" and mag_err != "NULL":
            reference_dict[index] = {"mag":float(mag), "mag_err":float(mag_err), \
                                     "id":id, "pos":(sources['xcentroid'][index], \
                                                     sources['ycentroid'][index])}
            pos_output.write("%s %s\n" % (str(pixcrd[0][0]), str(pixcrd[0][1])))
        count += 1
    pos_output.close()
    return reference_dict
def loop_through__redshift(table, f, length, mag_limit, z_lim):

    new_table_galaxy = Table()

    for i in np.arange(length):

        righta = table["RA"][i]
        declin = table["DEC"][i]

        if np.abs(table["GLAT"][i]) < 20:
            continue

        pos = coords.SkyCoord(ra=righta * u.degree, dec=declin * u.degree)
        galatic_pos = pos.galactic

        xid = SDSS.query_region(galatic_pos,
                                spectro=True,
                                radius=2.5 * u.arcmin,
                                specobj_fields=['Z', 'class'],
                                photoobj_fields=[
                                    'ra', 'dec', 'modelMag_u', 'modelMag_g',
                                    'modelMag_r', 'modelMag_i'
                                ])

        if xid is None:
            continue

        xid = unique(xid, keys=['ra', 'dec'])

        mask_gal = (xid["class"] == "GALAXY")
        xid = xid[mask_gal]

        if len(xid) == 0:
            continue

        xid.sort(f)
        data_mask = xid[f] > mag_limit
        data = xid[data_mask]

        if len(data) == 0:
            data = Table(xid[0])

        new_table_galaxy = vstack([new_table_galaxy, data])

        print(len(new_table_galaxy))

#    print(new_table_galaxy)
    return new_table_galaxy
Exemple #20
0
 def download_sdss_spec(self, add_r=0):
     sdss = SDSS()
     self.spec_data_table = sdss.query_region(self.loc,
                                              radius=(self.r + add_r) *
                                              u.arcsec,
                                              spectro=True)
     if self.has_spec() == True:
         print("在r=%f角秒内找到了%d个光谱" %
               (self.r + add_r, len(self.spec_data_table)))
         self.spec = SDSS.get_spectra(matches=self.spec_data_table)
         i = 0
         for match in self.spec_data_table:
             self.spec[i] = Spectra(match['objid'], self.spec[i])
             i = i + 1
     else:
         print("在r=%f角秒内没有找到光谱" % (self.r + add_r))
Exemple #21
0
def xgetsdss(ra, dec):
    #hcg7_center = SkyCoord.from_name('HCG 7')
    #hcg7_center = "20.81625, 0.88806"
    print("ra=%f, dec=%f\n" % (ra, dec))
    hcg7_center = str(ra) + "," + str(dec)
    print("hcg7 is %s\n" % (hcg7_center))

    sdss = SDSS.query_region(
        coordinates=hcg7_center,
        radius=20 * u.arcsec,
        spectro=False,
        photoobj_fields=['ra', 'dec', 'u', 'g', 'r', 'i', 'z'])
    print(sdss)
    filename = str(hcg7_center) + "_sdss.txt"
    #  with open(filename, 'a') as f:  # 如果filename不存在会自动创建, 'a'表示追加数据
    #      f.write(str(sdss))

    np.savetxt(filename, sdss, fmt='%f', header='ra, dec, u, g, r, i, z')
def get_spec_img(ra, dec):

    from PIL import Image
    from cStringIO import StringIO

    # Coord
    coord = SkyCoord(ra=ra*u.degree, dec=dec*u.degree)

    # Query database
    radius = 1*u.arcsec
    spec_catalog = SDSS.query_region(coord,spectro=True, radius=radius.to('degree'))

    # Request
    url = 'http://skyserver.sdss.org/dr12/en/get/SpecById.ashx?id='+str(int(spec_catalog['specobjid']))
    rtv = requests.get(url) 
    img = Image.open(StringIO(rtv.content))

    return img
def get_spec_img(ra, dec):

    from PIL import Image
    from cStringIO import StringIO

    # Coord
    coord = SkyCoord(ra=ra * u.degree, dec=dec * u.degree)

    # Query database
    radius = 1 * u.arcsec
    spec_catalog = SDSS.query_region(coord,
                                     spectro=True,
                                     radius=radius.to('degree'))

    # Request
    url = 'http://skyserver.sdss.org/dr12/en/get/SpecById.ashx?id=' + str(
        int(spec_catalog['specobjid']))
    rtv = requests.get(url)
    img = Image.open(StringIO(rtv.content))

    return img
Exemple #24
0
def sdss_check(x, y):
  """
  Check whether stars are in the SDSS catalogue.
  This function accepts either a single x and y coordinate,
  or an array of each.
  """
  w = WCS('a100.fits')
  sfilt = []
  # Check which format x and y are given in.
  if not (isinstance(x, (np.ndarray, list, float, int)) &
          isinstance(y, (np.ndarray, list, float, int)) &
          (np.shape(x) == np.shape(y))):
    print('Error: Need a set of pixel coordinates.')
    print('       X and Y must have same non-zero size.')
    raise TypeError
  x = [x] if (np.shape(x) == ()) else x
  y = [y] if (np.shape(y) == ()) else y
  lon, lat = w.all_pix2world(x, y, 1)
  pos = coords.SkyCoord(lon, lat, unit="deg")
  if len(pos) == 1:
    pos = [pos]
  table_fields = ['RA', 'Dec', 'psfMag_r', 'psfMagErr_r',
                  'psffwhm_r', 'nDetect', 'X_pixel', 'Y_pixel']
  sfilt = AstroTable(names=table_fields)
  for index, position in enumerate(pos):
    sfull = SDSS.query_region(position, radius='1arcsec', data_release=13,
                              photoobj_fields=table_fields[:-2])
    try:
      sline = (sfull[np.where((sfull['nDetect'] > 0) &
                              (sfull['psfMag_r'] > -99))[0]][0])
      slist = [sl for sl in sline]
      slist.append(x[index])
      slist.append(y[index])
      sfilt.add_row(slist)
    except (TypeError, IndexError):
      print("Star at " + str(position)[39:-1] + " not found :-(.")
      slist = np.zeros(len(table_fields))
      slist[-2:] = x[index], y[index]
      sfilt.add_row(slist)
  return sfilt
Exemple #25
0
def get_obj_data(ra, dec):
    # Coordinates from SDSS SQL search
    objCoord = SkyCoord(ra=ra * u.degree, dec=dec * u.degree)
    # Get SDSS images
    xObj = SDSS.query_region(objCoord, spectro=True)
    imgObj = SDSS.get_images(matches=xObj)
    image_i = imgObj[0][0]
    data_i = image_i.data
    # Get image coordinates
    wcs = WCS(image_i.header)
    # Cut the Field of view 25.0 x 25.0 arcsec
    FoV = np.array([ra_cut, ra_cut])
    FoV_dimen = u.Quantity((FoV[0], FoV[1]), u.arcsec)
    cutout = Cutout2D(data_i, objCoord, FoV_dimen, wcs=wcs)
    wcs_cut = cutout.wcs
    ret = cutout.data.astype(float)
    #normalizing cutout data
    ret = normalize_data(ret)
    #for i in cutout.data.size: ret.append(cutout.data[i])
    #plot(objCoord,cutout)
    #print(cutout.data.size)
    return ret
Exemple #26
0
def getPlateFits(position,bandName):
    """Get SDSS plate in fits format
    
    This function takes a position and band and returns the available fits images from 
    SDSS using Astroquery
    
    Parameters
    ----------
    position: string
        Ra/Dec position with units e.g. ' 35.2345342d 5.3452346d'
    bandName: string
        Name of band e.g. i from ugrizy


    Returns
    -------
    images astropy.table.Table
        i think the return is an HDUlist of all the available fits for a position/band.
    """
    #First check if it is already there by looping through files
    pos = coordinates.SkyCoord(position, frame='icrs')
    xid = SDSS.query_region(pos)
    #    bandName = 'g'
    #    plate = xid[0]['plate']
    images = SDSS.get_images(matches=xid, band=bandName)
    #    filename = str(plate) + '-' + bandName + '.fits'
    #    alreadyDownloaded = False
    #    for f in os.listdir('/Users/rs548/Documents/Science/PeteHurley/SDSS/'):
    #        print(f)
    #        if f == filename:
    #            alreadyDownloaded = True
    #    
    #    if alreadyDownloaded == False:
    #        im = SDSS.get_images(matches=xid, band=bandName)
    #        im.writeto('/Users/rs548/Documents/Science/PeteHurley/SDSS/' + filename)
    #    else:
    #        im = fits.open('/Users/rs548/Documents/Science/PeteHurley/SDSS/' + filename)
        
    return images
Exemple #27
0
def sdss_query(ra,dec,radius=5*u.arcmin,minmag=16.5,maxmag=20):
    from astroquery.sdss import SDSS
    from astropy import coordinates as coords
    import astropy.units as u

    pos = coords.SkyCoord(ra,dec,unit="deg", frame='icrs')
    table = SDSS.query_region(pos,fields=['type','ra','dec','u','g','r','i','z','err_u','err_g','err_r','err_i','err_z'],radius=5*u.arcmin)
    mask = (table["type"]==6) * (table["r"] > minmag) * (table["r"] < maxmag) * (table["g"] > minmag) * (table["g"] < maxmag)* (table["i"] > minmag) * (table["i"] < maxmag)* (table["u"] > minmag) * (table["u"] < maxmag)
    table = table[mask]
    
    newtable = np.zeros(len(table), dtype=[("ra", np.double), ("dec", np.double), ("rmag", np.float),("umag", np.float), ("gmag", np.float), ("imag", np.float),("zmag", np.float),("Imag", np.float),("Umag", np.float)])
    newtable["ra"] = table["ra"]
    newtable["dec"] = table["dec"]
    newtable["gmag"] = table["g"]
    newtable["rmag"] = table["r"]
    newtable["umag"] = table["u"]
    newtable["imag"] = table["i"]
    newtable["zmag"] = table["z"]
    newtable["Imag"] = table["i"]-0.386*(table["i"]-table["z"])-0.397
    B = table["g"]+0.313*(table["g"]-table["r"])+0.2271
    newtable["Umag"] = B+0.77*(table["u"]-table["g"])-0.88
    
    return newtable
def rand_loop_through__redshift(table, f, length, width):
    new_table_galaxy = Table()

    while len(new_table_galaxy) < length:
        righta = np.random.uniform(-180, 180)
        declin = np.random.uniform(-90, 90)
        pos = coords.SkyCoord(ra=righta * u.degree, dec=declin * u.degree)
        galatic_pos = pos.galactic

        xid = SDSS.query_region(galatic_pos,
                                spectro=True,
                                radius=width * u.arcmin,
                                specobj_fields=['Z'],
                                photoobj_fields=['ra', 'dec'])

        if xid is None:
            continue

        xid = unique(xid, keys=['ra', 'dec'])
        new_table_galaxy = vstack([new_table_galaxy, xid])

        print(len(new_table_galaxy))
#    print(new_table_galaxy)
    return new_table_galaxy
from astroquery.simbad import Simbad
result_table = Simbad.query_object("m1")
result_table.pprint(show_unit=True)

from astroquery.sdss import SDSS
from astropy import coordinates as coords
#pos = coords.SkyCoord(packet['candidate']['ra'],packet['candidate']['dec'], unit="deg”) #to use info from avro packet
pos = coords.SkyCoord(244.566202,16.138884, unit="deg”) #to test query

xid = SDSS.query_region(pos, spectro=True)
print(xid)
print(xid.columns)
xid['z']

sp = SDSS.get_spectra(matches=xid)
im = SDSS.get_images(matches=xid, band='r')
from __future__ import print_function

import matplotlib.pyplot as plt

from astroquery.sdss import SDSS
from astropy import units

# Photoobj_fields: UGRIZ
# UGRIZ: http://www1.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/community/CFHTLS-SG/docs/extra/sdsscfhtlsugriz.gif

# Let's get g and r magnitudes of stars in the globular cluster M13, in the 10 arcmin radius
result_table = SDSS.query_region('m13',
                                 radius=10 * units.deg / 60,
                                 photoobj_fields=['ra', 'dec', 'g', 'r'])

# Stars in the random part of the sky, with no clusters (stars at r = 22 and g-r = 0.5 are halo stars)
#result_table = SDSS.query_region('22h46m24.00s +31d42m00.0s', radius=0.5*units.deg, photoobj_fields=['ra', 'dec', 'g', 'r'])

# Start in the random part of the sky (with a cluster in the halo!) - Monoceros Ring
# result_table = SDSS.query_region('7h40m48.00s +32d42m00.0s', radius=0.5*units.deg, photoobj_fields=['ra', 'dec', 'g', 'r'])

print(result_table)

r_mags = []
gr_mags = []

for row in result_table:

    g = row[2]
    r = row[3]
Exemple #31
0
def grab_sdss_spectra(radec, radius=0.1*u.deg, outfil=None,
                      debug=False, maxsep=None, timeout=600., zmin=None):
    """ Grab SDSS spectra

    Parameters
    ----------
    radec : tuple
      RA, DEC in deg
    radius : float, optional (0.1*u.deg)
      Search radius -- Astroquery actually makes a box, not a circle
    timeout : float, optional
      Timeout limit for connection with SDSS
    outfil : str ('tmp.fits')
      Name of output file for FITS table
    maxsep : float (None) :: Mpc
      Maximum separation to include
    zmin : float (None)
      Minimum redshift to include

    Returns
    -------
    tbl : Table

    """

    cC = coords.SkyCoord(ra=radec[0], dec=radec[1])

    # Query
    photoobj_fs = ['ra', 'dec', 'objid', 'run', 'rerun', 'camcol', 'field']
    mags = ['petroMag_u', 'petroMag_g', 'petroMag_r', 'petroMag_i', 'petroMag_z']
    magsErr = ['petroMagErr_u', 'petroMagErr_g', 'petroMagErr_r', 'petroMagErr_i', 'petroMagErr_z']

    phot_catalog = SDSS.query_region(cC,spectro=True,radius=radius, timeout=timeout,
                                     photoobj_fields=photoobj_fs+mags+magsErr) # Unique
    spec_catalog = SDSS.query_region(cC,spectro=True, radius=radius, timeout=timeout) # Duplicates exist
    nobj = len(phot_catalog)

    #
    print('grab_sdss_spectra: Found {:d} sources in the search box.'.format(nobj))

    # Coordinates
    cgal = SkyCoord(ra=phot_catalog['ra']*u.degree, dec=phot_catalog['dec']*u.degree)
    sgal = SkyCoord(ra=spec_catalog['ra']*u.degree, dec=spec_catalog['dec']*u.degree)
    sepgal = cgal.separation(cC) #in degrees

    # Check for problems and parse z
    zobj = np.zeros(nobj)
    idx, d2d, d3d = coords.match_coordinates_sky(cgal, sgal, nthneighbor=1)
    if np.max(d2d) > 1.*u.arcsec:
        print('No spectral match!')
        xdb.set_trace()
    else:
        zobj = spec_catalog['z'][idx]

    idx, d2d, d3d = coords.match_coordinates_sky(cgal, cgal, nthneighbor=2)
    if np.min(d2d.to('arcsec')) < 1.*u.arcsec:
        print('Two photometric sources with same RA/DEC')
        xdb.set_trace()

    #xdb.set_trace()


    # Cut on Separation
    if not maxsep is None:
        print('grab_sdss_spectra: Restricting to {:g} Mpc separation.'.format(maxsep))
        sepgal_kpc = cosmo.kpc_comoving_per_arcmin(zobj) * sepgal.to('arcmin')
        sepgal_mpc = sepgal_kpc.to('Mpc')
        gdg = np.where( sepgal_mpc < (maxsep * u.Unit('Mpc')))[0]
        phot_catalog = phot_catalog[gdg]
        #xdb.set_trace()

    nobj = len(phot_catalog)
    print('grab_sdss_spectra: Grabbing data for {:d} sources.'.format(nobj))

    # Grab Spectra from SDSS

    # Generate output table
    attribs = galaxy_attrib()
    npix = 5000 #len( spec_hdus[0][1].data.flux )
    spec_attrib = [(str('FLUX'), np.float32, (npix,)),
                   (str('SIG'), np.float32, (npix,)),
                   (str('WAVE'), np.float64, (npix,))]
    tbl = np.recarray( (nobj,), dtype=attribs+spec_attrib)

    tbl['RA'] = phot_catalog['ra']
    tbl['DEC'] = phot_catalog['dec']
    tbl['TELESCOPE'] = str('SDSS 2.5-M')

    # Deal with spectra separately (for now)
    npix = 5000 #len( spec_hdus[0][1].data.flux )

    for idx,obj in enumerate(phot_catalog):
        #print('idx = {:d}'.format(idx))

        # Grab spectra (there may be duplicates)
        mt = np.where( sgal.separation(cgal[idx]).to('arcsec') < 1.*u.Unit('arcsec'))[0]
        if len(mt) > 1:
            # Use BOSS if you have it
            mmt = np.where( spec_catalog[mt]['instrument'] == 'BOSS')[0]
            if len(mmt) > 0:
                mt = mt[mmt[0]]
            else:
                mt = mt[0]
        elif len(mt) == 0:
            xdb.set_trace()
        else:
            mt = mt[0]

        # Grab spectra
        spec_hdus = SDSS.get_spectra(matches=Table(spec_catalog[mt]))

        tbl[idx]['INSTRUMENT'] = spec_catalog[mt]['instrument']
        spec = spec_hdus[0][1].data
        npp = len(spec.flux)
        tbl[idx]['FLUX'][0:npp] = spec.flux
        sig = np.zeros(npp)
        gdi = np.where(spec.ivar > 0.)[0]
        if len(gdi) > 0:
            sig[gdi] = np.sqrt( 1./spec.ivar[gdi] )
        tbl[idx]['SIG'][0:npp] = sig
        tbl[idx]['WAVE'][0:npp] = 10.**spec.loglam

        # Redshifts
        meta = spec_hdus[0][2].data
        for attrib in ['Z','Z_ERR']:
            tbl[idx][attrib] = meta[attrib]

        if debug:
            sep_to_qso = cgal[idx].separation(cC).to('arcmin')
            print('z = {:g}, Separation = {:g}'.format(tbl[idx].Z, sep_to_qso))
            xdb.set_trace()

        # Fill in rest
        tbl[idx].SDSS_MAG = np.array( [obj[phot] for phot in mags])
        tbl[idx].SDSS_MAGERR = np.array( [obj[phot] for phot in magsErr])

    # Clip on redshift to excise stars/quasars
    if zmin is not None:
        gd = np.where(tbl['Z'] > zmin)[0]
        tbl = tbl[gd]

    # Write to FITS file
    if outfil is not None:
        prihdr = fits.Header()
        prihdr['COMMENT'] = 'SDSS Spectra'
        prihdu = fits.PrimaryHDU(header=prihdr)

        tbhdu = fits.BinTableHDU(tbl)

        thdulist = fits.HDUList([prihdu, tbhdu])
        thdulist.writeto(outfil,clobber=True)

    print('Wrote SDSS table to {:s}'.format(outfil))
    return tbl
Exemple #32
0
def sdss_spectra(coo, redshift=0., columns=1):
    
    try:
        n_coo=len(coo)
    except:
        n_coo=1
        coo=[coo]

    try:
        n_redshift=len(redshift)
    except:
        n_redshift=1
        redshift=[redshift]


    if n_coo>1 & n_redshift==1:
      redshift=np.ones(n_coo)*redshift[0]

    n_col=np.min([n_coo,columns])
    n_row=np.ceil(n_coo*1./n_col)
    fig, ax = plt.subplots(figsize=(16,6*n_row/(n_col*1.)), sharex=True, sharey=True)
#    fig, ax = plt.subplots(figsize=(20,8*n_coo), sharex=True, sharey=True)
    for i in range(len(coo)):
        ax = plt.subplot(n_row, n_col, i+1)
        #ax = plt.subplot(n_coo, 1,i+1)
        print 'Procesando galaxia '+str(i)
        xid = SDSS.query_region(coo[i], spectro=True)
        spec=SDSS.get_spectra(matches=xid)[0][1]

        spec_h=spec.header
        spec_data=spec.data

        loglam=spec_data['loglam']  # Logaritmo de la longitud de onda
        flux=spec_data['flux'] # Flujo medido en unidades de Ergs/cm^2/s/AA

        window_len=9
        s=np.r_[flux[window_len-1:0:-1],flux,flux[-1:-window_len:-1]]
        w=np.ones(window_len,'d')
        w=eval('np.bartlett(window_len)')
        flux_smooth=np.convolve(w/w.sum(),s,mode='valid')[(window_len-1)/2:-(window_len-1)/2]

        gv=(loglam>np.log10(4000.)) & (loglam<np.log10(8000.))
        flux_scale=80./np.percentile(flux_smooth[gv],98)

        #ax.plot(10.**loglam, flux*flux_scale, label=xid['instrument'][0], color='black', linewidth=1)
        ax.plot(10.**loglam, flux_smooth*flux_scale, label=xid['instrument'][0], color='black', linewidth=1)

        for j in range(len(absorption_lines['name'])):
          ax.plot(absorption_lines['lambda'][j]*np.ones(2)*(1.+redshift[i]), [0., 1e5], absorption_lines['color']+'--') 
          ax.text(absorption_lines['lambda'][j]*(1.+redshift[i])+absorption_lines['offset'][j], absorption_lines['position'][j]*100., absorption_lines['name'][j], color=absorption_lines['color'], alpha=0.7, fontsize=16/(n_col*0.8), horizontalalignment=absorption_lines['align'][j])

        for j in range(len(emission_lines['name'])):
          ax.plot(emission_lines['lambda'][j]*np.ones(2)*(1.+redshift[i]), [0., 1e5], emission_lines['color']+'--') 
          ax.text(emission_lines['lambda'][j]*(1.+redshift[i])+emission_lines['offset'][j], emission_lines['position'][j]*100., emission_lines['name'][j], color=emission_lines['color'], alpha=0.7, fontsize=16/(n_col*0.8), horizontalalignment=emission_lines['align'][j])

        if (i % n_col == 0):
            ax.set_ylabel(r'Flujo [10$^{-17}$ ergs/cm$^2$/s/$\AA$]', fontsize=14/(n_col*0.8))
        if (i >= (n_col*(n_row-1))):
            ax.set_xlabel(r'Longitud de onda [$\AA$]', fontsize=14)
        ax.set_title('Galaxia '+str(i))
        ax.set_xlim(3500,8000)
        ax.set_ylim(0.,100.)
        
    fig.subplots_adjust(hspace=0.3, wspace=0.1)
Exemple #33
0
def loop_through_planck(table,f,length,mag_limit,z_lim):
    
    new_table_galaxy = Table(names=(table.colnames))
    col = Column(name='Aperture Type', length = len(new_table_galaxy))
    new_table_galaxy.add_column(col)
    
    new_table_star = Table(names=(table.colnames))
    new_table_other = Table(names=(table.colnames))
    
    sep_list = []
        
    mag_list = []
    mag_list_diff_one_two = []
    mag_list_diff_two_three = []
    
    for i in np.arange(length):
        
        righta = table["RA"][i]
        declin = table["DEC"][i]
        
        if np.abs(table["GLAT"][i]) < 20:
            continue
        
        pos = coords.SkyCoord( ra = righta*u.degree , dec = declin*u.degree)
        galatic_pos = pos.galactic
        
        #objID not working correctly gives error "invalid load key, '\x00'." 
        #print("HI")
        xid = SDSS.query_region(galatic_pos, spectro = True , radius = 2.5*u.arcmin , specobj_fields=['Z','class'], photoobj_fields=['ra','dec','modelMag_u','modelMag_g','modelMag_r','modelMag_i','lnLExp_u','lnLDeV_u'])
        #xid =  SDSS.query_region(galatic_pos, spectro = True , radius = 2.5*u.arcmin , fields=['Z','objID','class','ra','dec','modelMag_u','modelMag_g','modelMag_r','modelMag_i'],field_help = 'deVlnL')

#        print("HEEEEYYYYYYYYYYYYYYYYYYYYYYYY")
#        for q in np.arange(len(xid["photoobj_all"])):
#            print(xid["photoobj_all"][q])
#        
#        print('HOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO')
#        for q in np.arange(len(xid['specobj_all'])):
#            print(xid['specobj_all'][q])
        
        if xid is None:
            continue
        
        xid = unique(xid,keys=['ra', 'dec'])
        
        xid.sort(f)
        
        mag_list = np.concatenate((mag_list,xid[f]))
        
#        if len(xid) == 1:
#            mag_list_diff_one_two.append(xid[f][0])
        if len(xid) == 2:
            mag_list_diff_one_two.append(xid[f][1] - xid[f][0])
        elif len(xid) > 2:
            mag_list_diff_one_two.append(xid[f][1] - xid[f][0])
            mag_list_diff_two_three.append(xid[f][2] - xid[f][1])
            
        mask_redshift = xid['Z'] < z_lim
        xid = xid[mask_redshift]
        
        if xid is None:
            continue
        
        for v in np.arange(len(xid)):
            
            max_mag_object = xid[v]
            max_mag_object = Table(max_mag_object)
            
            if max_mag_object[f] > mag_limit and v > 0:
                break
            
            if max_mag_object["class"] == "GALAXY":
                if max_mag_object['lnLDeV_u'] > max_mag_object['lnLExp_u']:
                    max_mag_object['Aperture Type'] = 0
                else:
                    max_mag_object['Aperture Type'] = 1
                    
                new_table_galaxy = vstack([new_table_galaxy, max_mag_object])
                
                righta_max = max_mag_object["ra"]
                declin_max = max_mag_object["dec"]
                sep = pos.separation(coords.SkyCoord( ra = righta_max*u.degree , dec = declin_max*u.degree))
                sep_list.append(sep.arcsecond)
                    
            elif max_mag_object["class"] == "STAR":
                new_table_star = vstack([new_table_star, max_mag_object])
            else:
                new_table_other = vstack([new_table_other, max_mag_object])
                
    return [new_table_galaxy,new_table_star,new_table_other,sep_list,mag_list,mag_list_diff_one_two,mag_list_diff_two_three]
Exemple #34
0
def grab_sdss_spectra(radec,
                      radius=0.1 * u.deg,
                      outfil=None,
                      debug=False,
                      maxsep=None,
                      timeout=600.,
                      zmin=None):
    """ Grab SDSS spectra

    Parameters
    ----------
    radec : tuple
      RA, DEC in deg
    radius : float, optional (0.1*u.deg)
      Search radius -- Astroquery actually makes a box, not a circle
    timeout : float, optional
      Timeout limit for connection with SDSS
    outfil : str ('tmp.fits')
      Name of output file for FITS table
    maxsep : float (None) :: Mpc
      Maximum separation to include
    zmin : float (None)
      Minimum redshift to include

    Returns
    -------
    tbl : Table

    """

    cC = coords.SkyCoord(ra=radec[0], dec=radec[1])

    # Query
    photoobj_fs = ['ra', 'dec', 'objid', 'run', 'rerun', 'camcol', 'field']
    mags = [
        'petroMag_u', 'petroMag_g', 'petroMag_r', 'petroMag_i', 'petroMag_z'
    ]
    magsErr = [
        'petroMagErr_u', 'petroMagErr_g', 'petroMagErr_r', 'petroMagErr_i',
        'petroMagErr_z'
    ]

    phot_catalog = SDSS.query_region(cC,
                                     spectro=True,
                                     radius=radius,
                                     timeout=timeout,
                                     photoobj_fields=photoobj_fs + mags +
                                     magsErr)  # Unique
    spec_catalog = SDSS.query_region(cC,
                                     spectro=True,
                                     radius=radius,
                                     timeout=timeout)  # Duplicates exist
    nobj = len(phot_catalog)

    #
    print('grab_sdss_spectra: Found {:d} sources in the search box.'.format(
        nobj))

    # Coordinates
    cgal = SkyCoord(ra=phot_catalog['ra'] * u.degree,
                    dec=phot_catalog['dec'] * u.degree)
    sgal = SkyCoord(ra=spec_catalog['ra'] * u.degree,
                    dec=spec_catalog['dec'] * u.degree)
    sepgal = cgal.separation(cC)  #in degrees

    # Check for problems and parse z
    zobj = np.zeros(nobj)
    idx, d2d, d3d = coords.match_coordinates_sky(cgal, sgal, nthneighbor=1)
    if np.max(d2d) > 1. * u.arcsec:
        print('No spectral match!')
        xdb.set_trace()
    else:
        zobj = spec_catalog['z'][idx]

    idx, d2d, d3d = coords.match_coordinates_sky(cgal, cgal, nthneighbor=2)
    if np.min(d2d.to('arcsec')) < 1. * u.arcsec:
        print('Two photometric sources with same RA/DEC')
        xdb.set_trace()

    #xdb.set_trace()

    # Cut on Separation
    if not maxsep is None:
        print('grab_sdss_spectra: Restricting to {:g} Mpc separation.'.format(
            maxsep))
        sepgal_kpc = cosmo.kpc_comoving_per_arcmin(zobj) * sepgal.to('arcmin')
        sepgal_mpc = sepgal_kpc.to('Mpc')
        gdg = np.where(sepgal_mpc < (maxsep * u.Unit('Mpc')))[0]
        phot_catalog = phot_catalog[gdg]
        #xdb.set_trace()

    nobj = len(phot_catalog)
    print('grab_sdss_spectra: Grabbing data for {:d} sources.'.format(nobj))

    # Grab Spectra from SDSS

    # Generate output table
    attribs = galaxy_attrib()
    npix = 5000  #len( spec_hdus[0][1].data.flux )
    spec_attrib = [(str('FLUX'), np.float32, (npix, )),
                   (str('SIG'), np.float32, (npix, )),
                   (str('WAVE'), np.float64, (npix, ))]
    tbl = np.recarray((nobj, ), dtype=attribs + spec_attrib)

    tbl['RA'] = phot_catalog['ra']
    tbl['DEC'] = phot_catalog['dec']
    tbl['TELESCOPE'] = str('SDSS 2.5-M')

    # Deal with spectra separately (for now)
    npix = 5000  #len( spec_hdus[0][1].data.flux )

    for idx, obj in enumerate(phot_catalog):
        #print('idx = {:d}'.format(idx))

        # Grab spectra (there may be duplicates)
        mt = np.where(
            sgal.separation(cgal[idx]).to('arcsec') < 1. * u.Unit('arcsec'))[0]
        if len(mt) > 1:
            # Use BOSS if you have it
            mmt = np.where(spec_catalog[mt]['instrument'] == 'BOSS')[0]
            if len(mmt) > 0:
                mt = mt[mmt[0]]
            else:
                mt = mt[0]
        elif len(mt) == 0:
            xdb.set_trace()
        else:
            mt = mt[0]

        # Grab spectra
        spec_hdus = SDSS.get_spectra(matches=Table(spec_catalog[mt]))

        tbl[idx]['INSTRUMENT'] = spec_catalog[mt]['instrument']
        spec = spec_hdus[0][1].data
        npp = len(spec.flux)
        tbl[idx]['FLUX'][0:npp] = spec.flux
        sig = np.zeros(npp)
        gdi = np.where(spec.ivar > 0.)[0]
        if len(gdi) > 0:
            sig[gdi] = np.sqrt(1. / spec.ivar[gdi])
        tbl[idx]['SIG'][0:npp] = sig
        tbl[idx]['WAVE'][0:npp] = 10.**spec.loglam

        # Redshifts
        meta = spec_hdus[0][2].data
        for attrib in ['Z', 'Z_ERR']:
            tbl[idx][attrib] = meta[attrib]

        if debug:
            sep_to_qso = cgal[idx].separation(cC).to('arcmin')
            print('z = {:g}, Separation = {:g}'.format(tbl[idx].Z, sep_to_qso))
            xdb.set_trace()

        # Fill in rest
        tbl[idx].SDSS_MAG = np.array([obj[phot] for phot in mags])
        tbl[idx].SDSS_MAGERR = np.array([obj[phot] for phot in magsErr])

    # Clip on redshift to excise stars/quasars
    if zmin is not None:
        gd = np.where(tbl['Z'] > zmin)[0]
        tbl = tbl[gd]

    # Write to FITS file
    if outfil is not None:
        prihdr = fits.Header()
        prihdr['COMMENT'] = 'SDSS Spectra'
        prihdu = fits.PrimaryHDU(header=prihdr)

        tbhdu = fits.BinTableHDU(tbl)

        thdulist = fits.HDUList([prihdu, tbhdu])
        thdulist.writeto(outfil, clobber=True)

    print('Wrote SDSS table to {:s}'.format(outfil))
    return tbl
Exemple #35
0
def downloadsdss(_ra,_dec,_band,_radius=20, force=False):
    from astroquery.sdss import SDSS
    from astropy import coordinates as coords
    import astropy.units as u
    from astropy.io import fits
    import os
    import sys
    import string
    import numpy as np
    from scipy import interpolate
    pos = coords.SkyCoord(ra=float(_ra)*u.deg,dec=float(_dec)*u.deg)
    print 'pos =', pos
    xid = SDSS.query_region(pos, spectro=False, radius=_radius*u.arcsec)
    print xid
    if xid:
       pointing=[]
       for i in xid:
          if i['run'] > 300:
             if (i['run'],i['camcol'],i['field']) not in pointing:
                pointing.append((i['run'],i['camcol'],i['field']))
       # if too many pointing, take only first 40
       if len(pointing) > 50:
          nn=50
       else: 
          nn=len(pointing)
       filevec=[]
       print len(pointing)
       for run, camcol, field in pointing[:nn]:
          #  naomaggie image
          output1 = _band+'_SDSS_'+str(run)+'_'+str(camcol)+'_'+str(field)+'.fits'
          #  image in count
          output2 = _band+'_SDSS_'+str(run)+'_'+str(camcol)+'_'+str(field)+'c.fits'
          #  weight image
          output3 = _band+'_SDSS_'+str(run)+'_'+str(camcol)+'_'+str(field)+'.weight.fits'
          #  sky image
          output4 = _band+'_SDSS_'+str(run)+'_'+str(camcol)+'_'+str(field)+'.sky.fits'
          if os.path.isfile(output1) and not force:
              print 'already downloaded', output1
              filevec.append(output2)
              filevec.append(output3)
              continue
          im = SDSS.get_images(run=run, camcol=camcol, field=field, band=_band, cache=True)
          if os.path.isfile(output1):
             os.system('rm '+output1)
          if os.path.isfile(output2):
             os.system('rm '+output2)
          if os.path.isfile(output3):
             os.system('rm '+output3)
          if os.path.isfile(output4):
             os.system('rm '+output4)
          im[0].writeto(output1)
#         im[0][0].writeto(output2)

          FITS_file = fits.open(output1)
          new_header = FITS_file[0].header
          camcol     = FITS_file[0].header['CAMCOL']  # camcol
          ugriz      = FITS_file[0].header['FILTER']  # ugriz filter
          run1        = FITS_file[0].header['RUN']     # run
          gain, dark_var = SDSS_gain_dark(camcol, ugriz, run1)
          new_header['gain']  = gain
          new_header['dark']  = dark_var
          new_header['BUNIT']  = 'counts'
          new_header['rdnoise']  = 2
          frame_image = FITS_file[0].data.transpose()
          allsky     = FITS_file[2].data['ALLSKY'].transpose()
          allsky     = allsky[:,:,0]
          xinterp    = FITS_file[2].data['XINTERP'].transpose()
          xinterp    = xinterp[:,0]
          yinterp    = FITS_file[2].data['YINTERP'].transpose()
          yinterp    = yinterp[:,0]
          sky_function = interpolate.interp2d(np.arange(allsky.shape[1]),\
                                              np.arange(allsky.shape[0]), allsky, kind='linear')
          sky_image    = sky_function(yinterp, xinterp) # in counts
          calib     = FITS_file[1].data #  nanomaggies per count
          calib_image = np.empty_like(frame_image)
          for i in np.arange(calib_image.shape[1]):
             calib_image[:,i] = calib
          # Calculate the error in the frame image for use fitting algorithms later.
          dn_image        = frame_image / calib_image  # + sky_image # counts
          dn_err_image    = np.sqrt((dn_image + sky_image)/ gain + dark_var)
          # frame_image_err = dn_err_image * calib_image  converts to nanomaggies
          
          frame_weight = 1 / ((dn_err_image)**2)
          new_header['SKYLEVEL']  = np.mean(sky_image)
          #  save image in count
          fits.writeto(output2, dn_image.transpose(), new_header,overwrite=True)
          #  save weight image
          fits.writeto(output3, frame_weight.transpose(), new_header,overwrite=True)
          #  save sky image 
          fits.writeto(output4, sky_image.transpose(), new_header,overwrite=True)
          filevec.append(output2)
          filevec.append(output3)
       return filevec
    else:
       return ''
Exemple #36
0
def sdss(obsids_table,
         data_folder,
         moc_folder,
         nir_moc=None,
         data_release=14,
         radius=15 * u.arcmin,
         moc_order=15,
         overwrite=True):
    """
    Get SDSS data using astroquery.
    For each observation in obsids_table, saves a fits file with
    name 'OBS_ID.fits' in 'data_folder/groups'.

    The function sends a query and selects all sources within 'radius'
    of the RA,DEC of the observation, then it filters the result
    selecting the sources in the corresponding MOC stored in 'moc_folder/mocs'
    (moc_order must be consistent with the order used to calculate the MOC).

    If overwrite is True, always create a new fits file. If False, checks for
    an existing file and uses it to calculate the number of SDSS sources
    in the field. If it doesn't exist, creates the file.

    The function returns obsids_table with an additional column 'NSRC_SDSS'
    with the number of sources in the field.
    """
    # Groups folder
    if nir_moc is None:
        groups_folder = os.path.join(data_folder, 'groups')

    else:
        root, _ = os.path.splitext(os.path.basename(nir_moc))
        survey = root.split('_')[-1]
        groups_folder = os.path.join(data_folder, 'groups_' + survey)

        moc_nirsurvey = MOC()
        read_moc_fits(moc_nirsurvey, nir_moc)

    if not os.path.exists(groups_folder):
        os.makedirs(groups_folder)

    moc_folder = os.path.join(moc_folder, 'mocs')

    nsources_field = np.full((len(obsids_table), ), np.nan)
    hp = HEALPix(nside=2**moc_order, order='nested', frame=ICRS())
    photoobj_fields = ['objID', 'mode', 'ra', 'dec', 'raErr', 'decErr']

    for i, row in enumerate(tqdm(obsids_table, desc="Making SDSS groups")):
        ## Group file name
        field_table_file = os.path.join(data_folder, groups_folder,
                                        '{}.fits'.format(row['OBS_ID']))
        is_field_table = os.path.exists(field_table_file)

        if overwrite or (not overwrite and not is_field_table):
            ## Select all sources in the field
            field_coords = SkyCoord(ra=row['RA'] * u.deg,
                                    dec=row['DEC'] * u.deg)

            src_table = SDSS.query_region(field_coords,
                                          radius=radius,
                                          photoobj_fields=photoobj_fields,
                                          data_release=data_release)
            # Filter table
            # In ARCHES, the only filter is selecting primary objects,
            # no filtering in the quality of photometry (clean).
            src_table = src_table[src_table['mode'] == 1]

            ## Select sources in the non-overlaping area
            moc_field = MOC()
            read_moc_fits(
                moc_field,
                os.path.join(moc_folder, '{}.moc'.format(row['OBS_ID'])))
            if nir_moc is not None:
                moc_field = moc_nirsurvey.intersection(moc_field)

            inmoc_table = sources_inmoc(src_table,
                                        hp,
                                        moc_field,
                                        moc_order=moc_order,
                                        ra='ra',
                                        dec='dec',
                                        units=u.deg)
            ## Save sources
            inmoc_table.remove_columns(['mode'])
            inmoc_table.meta['description'] = 'SDSS'
            inmoc_table.write(field_table_file, overwrite=True)

        else:
            inmoc_table = Table.read(field_table_file)

        nsources_field[i] = len(inmoc_table)

    colsrc = Table.Column(nsources_field, name='NSRC_SDSS')
    obsids_table.add_column(colsrc)

    return obsids_table
def downloadsdss(_ra, _dec, _band, _radius=20, force=False):
    from astroquery.sdss import SDSS
    from astropy import coordinates as coords
    import astropy.units as u
    from astropy.io import fits
    import os
    import sys
    import string
    import numpy as np
    from scipy import interpolate
    pos = coords.SkyCoord(ra=float(_ra) * u.deg, dec=float(_dec) * u.deg)
    print 'pos =', pos
    xid = SDSS.query_region(pos, spectro=False, radius=_radius * u.arcsec)
    print xid
    if xid:
        pointing = []
        for i in xid:
            if i['run'] > 300:
                if (i['run'], i['camcol'], i['field']) not in pointing:
                    pointing.append((i['run'], i['camcol'], i['field']))
        # if too many pointing, take only first 40
        if len(pointing) > 50:
            nn = 50
        else:
            nn = len(pointing)
        filevec = []
        print len(pointing)
        for run, camcol, field in pointing[:nn]:
            #  naomaggie image
            output1 = _band + '_SDSS_' + str(run) + '_' + str(
                camcol) + '_' + str(field) + '.fits'
            #  image in count
            output2 = _band + '_SDSS_' + str(run) + '_' + str(
                camcol) + '_' + str(field) + 'c.fits'
            #  weight image
            output3 = _band + '_SDSS_' + str(run) + '_' + str(
                camcol) + '_' + str(field) + '.weight.fits'
            #  sky image
            output4 = _band + '_SDSS_' + str(run) + '_' + str(
                camcol) + '_' + str(field) + '.sky.fits'
            if os.path.isfile(output1) and not force:
                print 'already downloaded', output1
                filevec.append(output2)
                filevec.append(output3)
                continue
            im = SDSS.get_images(run=run,
                                 camcol=camcol,
                                 field=field,
                                 band=_band,
                                 cache=True)
            if os.path.isfile(output1):
                os.system('rm ' + output1)
            if os.path.isfile(output2):
                os.system('rm ' + output2)
            if os.path.isfile(output3):
                os.system('rm ' + output3)
            if os.path.isfile(output4):
                os.system('rm ' + output4)
            im[0].writeto(output1)
            #         im[0][0].writeto(output2)

            FITS_file = fits.open(output1)
            new_header = FITS_file[0].header
            camcol = FITS_file[0].header['CAMCOL']  # camcol
            ugriz = FITS_file[0].header['FILTER']  # ugriz filter
            run1 = FITS_file[0].header['RUN']  # run
            gain, dark_var = SDSS_gain_dark(camcol, ugriz, run1)
            new_header['gain'] = gain
            new_header['dark'] = dark_var
            new_header['BUNIT'] = 'counts'
            new_header['rdnoise'] = 2
            frame_image = FITS_file[0].data.transpose()
            allsky = FITS_file[2].data['ALLSKY'].transpose()
            allsky = allsky[:, :, 0]
            xinterp = FITS_file[2].data['XINTERP'].transpose()
            xinterp = xinterp[:, 0]
            yinterp = FITS_file[2].data['YINTERP'].transpose()
            yinterp = yinterp[:, 0]
            sky_function = interpolate.interp2d(np.arange(allsky.shape[1]),\
                                                np.arange(allsky.shape[0]), allsky, kind='linear')
            sky_image = sky_function(yinterp, xinterp)  # in counts
            calib = FITS_file[1].data  #  nanomaggies per count
            calib_image = np.empty_like(frame_image)
            for i in np.arange(calib_image.shape[1]):
                calib_image[:, i] = calib
            # Calculate the error in the frame image for use fitting algorithms later.
            dn_image = frame_image / calib_image  # + sky_image # counts
            dn_err_image = np.sqrt((dn_image + sky_image) / gain + dark_var)
            # frame_image_err = dn_err_image * calib_image  converts to nanomaggies

            frame_weight = 1 / ((dn_err_image)**2)
            new_header['SKYLEVEL'] = np.mean(sky_image)
            #  save image in count
            fits.writeto(output2,
                         dn_image.transpose(),
                         new_header,
                         clobber=True)
            #  save weight image
            fits.writeto(output3,
                         frame_weight.transpose(),
                         new_header,
                         clobber=True)
            #  save sky image
            fits.writeto(output4,
                         sky_image.transpose(),
                         new_header,
                         clobber=True)
            filevec.append(output2)
            filevec.append(output3)
        return filevec
    else:
        return ''
Exemple #38
0
            additional["BAL"].append(row[8])
            additional["z"].append(row[9])
            additional["AV"].append(row[10])

masterframe = pd.DataFrame.from_dict(additional)
masternames = masterframe['NTT_study'].to_numpy(dtype=str)

# Because there's no spectral IDs, use the coordinates to search on SDSS
ra = masterframe['RA'].to_numpy()
dec = masterframe['Dec'].to_numpy()
extra_specid = list()

for r, d in tqdm(zip(ra, dec)):
    co = coords.SkyCoord(r, d, unit=(u.hourangle, u.deg))
    # Perform the search
    result = SDSS.query_region(co, data_release=12, spectro=True)
    if result == None:
        extra_specid.append('')
    else:
        # Save the id, if it is there
        extra_specid.append(result['specobjid'][-1])

# Similar to what was done for the fits file objects
masterurls = np.array(extra_specid, dtype=object)
for n, s in enumerate(extra_specid):
    if s != '':
        masterurls[
            n] = f'http://skyserver.sdss.org/dr12/en/get/SpecById.ashx?id={s}'
    else:
        masterurls[n] = ''
# Get position of object for 2000 time and and given time

RA2000 = objd.get_ra()
DEC2000 = objd.get_dec()
RACurr = objd.get_ra(basetime)
DECCurr = objd.get_dec(basetime)

Objcoord = coordinates.SkyCoord(ra = RACurr, dec = DECCurr, unit=u.deg)

fields = ['objID','type', 'ra','dec']
for l in 'urigzHJK':
    fields.append(l)
    fields.append('Err_' + l)

Sdobjs = SDSS.query_region(Objcoord, photoobj_fields=fields,radius=radius*u.deg)

if Sdobjs is None:
    print("No objects found in region of", objname, file=sys.stderr)
    sys.exit(1)

# Convert to temp type for fiddling with eliminating too faint objs and ones with no mags

convsdobjs = []
for r in Sdobjs:
    if onlytype is not None and r['type'] != onlytype:
        continue
    maglist = []
    for i in 'urigz':
        maglist.append(r[i])
    mm = np.min(maglist)