def datafetcher():
    # This function fetches a sample SDSS data for ra, dec, run from PhotoObj within certain magnitudes.
    # The resulting data table is then saved for future reference as a .csv file.
    
    query1 = """
            select
                ra, dec, run
            from PhotoObj
            where psfMag_g BETWEEN 16.96 and 16.99
            """
    query2 = """
            select
                ra, dec, run
            from PhotoObj
            where psfMag_g BETWEEN 17 and 17.041
            """
    query3 = """
            select
                ra, dec, run
            from PhotoObj
            where psfMag_g BETWEEN 17.041 and 17.08
            """
    query4 = """
            select
                ra, dec, run
            from PhotoObj
            where psfMag_g BETWEEN 17.08 and 18.02
            """
    res1 = SDSS.query_sql(query1, timeout=3600).to_pandas()
    res2 = SDSS.query_sql(query2, timeout=3600).to_pandas()
    res3 = SDSS.query_sql(query3, timeout=3600).to_pandas()
    res4 = SDSS.query_sql(query4, timeout=3600).to_pandas()
    concat_res = pd.concat([res1, res2, res3, res4], ignore_index=True)
    concat_res.to_csv('SDSSdatasample.csv')
    return concat_res
Example #2
0
def sdss_template(tipo='eliptica'):

    if tipo=='eliptica':
        template = SDSS.get_spectral_template('galaxy_early')
        title='Espectro de galaxia eliptica'
        lines=absorption_lines
    elif tipo=='espiral': 
        template = SDSS.get_spectral_template('galaxy_late')
        title='Espectro de galaxia espiral'
        lines=emission_lines

    spec_h=template[0][0].header
    spec_data=template[0][0].data
    wcs=WCS(spec_h)  

    index = np.arange(spec_h['NAXIS1'])
    loglam=wcs.wcs_pix2world(index, np.zeros(len(index)), 0)[0]
    flux=spec_data[0]

    gv=(loglam>np.log10(4000.)) & (loglam<np.log10(8000.))
    flux_scale=80./np.max(flux[gv])
  
    fig, ax = plt.subplots(figsize=(20,8), sharex=True, sharey=True)
    plt.plot(10**loglam, flux*flux_scale, '-', color='black', linewidth=1)

    for j in range(len(lines['name'])):
        ax.plot(lines['lambda'][j]*np.ones(2), [0., 1e5], lines['color']+'--')
        ax.text(lines['lambda'][j]+lines['offset'][j], lines['position'][j]*100., lines['name'][j], color='black', fontsize=18, horizontalalignment=lines['align'][j])
  
        ax.set_ylabel(r'Flujo [10$^{-17}$ ergs/cm$^2$/s/\AA]', fontsize=14)
        ax.set_xlabel(r'Longitud de onda [\AA]', fontsize=14)
        ax.set_title(title)
        ax.set_xlim(3500,8000)
        ax.set_ylim(0.,100.)
Example #3
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)
Example #4
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
Example #5
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))
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
Example #7
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)
    def SDSS_get_calib(self, rad_min = 5):
        '''
        Parameters
        ----------
        rad_min (optional, int or float) : search radius in decimal arcminutes

        Output
        ------
        file : writes processed calibration file and sets member variables
        '''

        # formulate SQL query and execute
        query = '''SELECT p.ra, p.dec, p.u, p.Err_u, p.g, p.Err_g, p.r, p.Err_r, p.i, p.Err_i, p.z, p.Err_z
                    FROM fGetNearbyObjEq({},{},{}) n, PhotoPrimary p
                    WHERE n.objID = p.objID AND p.type = 6'''.format(self.targetra, self.targetdec, rad_min)
        data = SDSS.query_sql(query)

        # process and return results
        if data is None:
            print('Search of SDSS for calibration information failed...')
            return None

        for filt in 'ugriz':
            data.rename_column('Err_' + filt, filt + 'Err')

        self.cal_source = 'SDSS'
        self.cal_filename = 'cal_{}_{}.dat'.format(self.targetname, self.cal_source)
        ascii.write(data, os.path.join(self.relative_path, self.cal_filename))
Example #9
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
def SDSS_QUERY_SPEC_Z(TILE_RA_LOWER_LIMIT, TILE_DEC_LOWER_LIMIT,
                      TILE_RA_UPPER_LIMIT, TILE_DEC_UPPER_LIMIT):

    query = "SELECT objID, ra, dec,z FROM SpecPhoto WHERE (ra between %s and %s) and (dec between %s and %s)" % (
        TILE_RA_UPPER_LIMIT, TILE_RA_LOWER_LIMIT, TILE_DEC_LOWER_LIMIT,
        TILE_DEC_UPPER_LIMIT)
    data = SDSS.query_sql(query, data_release=15)

    name = 'SDSS_Tile_Sources.csv'
    file = open(os.path.join(Main_Path, name), 'w')

    file.write('specobjID,ra,dec,z')
    file.write('\n')

    specobjID = data['objID']
    ra = data['ra']
    dec = data['dec']
    z = data['z']
    for i in range(len(z)):
        info = '%s,%s,%s,%s' % (specobjID[i], ra[i], dec[i], z[i])
        file.write(info)
        file.write('\n')
    file.close()

    print data
Example #12
0
    def __init__(self, query=None, table=None):
        # Either query or table, but not both (query xor table).
        if (query and table is not None) or\
           (not query and table is None):
            raise Exception('Either the query or the table parameter is '
                            'required. But not both.')
        if table is not None:
            # Use table if it was provided.
            self.table = table
        else:
            # Otherwise use the query to create the table.
            self.table = SDSS.query_sql(query)
        self.catalog = np.array(self.table)
        self.catalog = rename_fields(self.catalog, {'objID': 'id'})

        # Calculate B and V like the VizieR data.
        # Use Robert Lupton's derived equations found here:
        # http://www.sdss3.org/dr8/algorithms/sdssUBVRITransform.php

        g = self.catalog['g']
        r = self.catalog['r']

        B = g + 0.3130 * (g - r) + 0.2271  # sigma = 0.0107
        V = g - 0.5784 * (g - r) - 0.0038  # sigma = 0.0054

        self.catalog = append_fields(self.catalog, 'B', B)
        self.catalog = append_fields(self.catalog, 'V', V)
Example #13
0
def query_sdss(query_str, filename, obj_key='objID', data_release=14):
    objid = -1
    cnt = 0
    row_count = 500000

    print('querying', filename)
    while row_count == 500000:
        start = time.time()
        print('query number', cnt)
        table = SDSS.query_sql(query_str.format(objid),
                               timeout=600,
                               data_release=data_release)
        print('seconds taken:', int(time.time() - start))

        row_count = len(table)
        objid = table[row_count - 1][obj_key]
        print('row_count', row_count)
        print('head')
        print(table[:5])
        print('tail')
        print(table[-5:])

        ascii.write(table,
                    filename.format(cnt),
                    format='csv',
                    fast_writer=False)
        print('saved to csv')
        cnt += 1
Example #14
0
def get_photometry(N=10000):
    """Get photometry from the SDSS Database
    
    Parameters
    ----------
    N : int
        specifies the number of objects to query
        
    Returns
    -------
    data : astropy.Table
        table of queried photometry
    """
    
    query = """
    SELECT TOP {N} 
    p.psfMag_r, p.fiberMag_r, p.fiber2Mag_r, p.petroMag_r, 
    p.deVMag_r, p.expMag_r, p.modelMag_r, p.cModelMag_r, 
    s.class
    FROM PhotoObjAll AS p JOIN specObjAll s ON s.bestobjid = p.objid
    WHERE p.mode = 1 AND s.sciencePrimary = 1 AND p.clean = 1 AND s.class != 'QSO'
    ORDER BY p.objid ASC
    """
    data = SDSS.query_sql(query.format(N=N))
    return data
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
Example #16
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
Example #17
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))
Example #18
0
def objid2dr7id(objID):
    """
    This function takes a SDSS DR13 objID and returns a DR7 objID based on
    RA/DEC
    """
    
    sql = """
SELECT
 p.ra,p.dec
FROM PhotoObj AS p
WHERE p.objid = {objID}
    """.format(**vars())
    result = SDSS.query_sql_async(sql)
    radecstring = result.content.split('\n')
    try:    
        ra,dec = radecstring[2].split(',')
    except:
        return False, 'empty'
    ra,dec = float(ra),float(dec)
    print( ra,dec)
    sql2 = """
SELECT
 p.objid,p.ra,p.dec
FROM PhotoObj AS p
JOIN dbo.fGetNearestObjEq({ra},{dec}, 1) AS pN
ON p.objID = pN.objID
    """.format(**vars())
    result2 = SDSS.query_sql_async(sql2, data_release=7)
    datastring = result2.content
    try:    
        data = datastring.split('\n')[1]
        dr7objid,ra2,dec2 = data.split(',')
        ra2,dec2 = float(ra2),float(dec2)
        print(dr7objid,ra2,dec2 )
    except:
        print('No neighbour found for ',objID)
        dr7objid = 'empty'
        

    try:
        assert abs(ra - ra2) < 0.0003 #1 arcsec = 0.000277 deg
        assert abs(dec - dec2) < 0.0003
        matchfound = True
    except:
        print('No match found for ',objID)
        matchfound = False
    return matchfound,dr7objid
Example #19
0
 def download_and_save_image(self):
     pos = coords.SkyCoord(ra=self.ra * u.degree,
                           dec=self.dec * u.degree,
                           frame='fk5')
     img = SDSS.get_images(self.sdss_id, band='g')[0]
     print(pos)
     print(img[0])
     img.writeto('test.fits', overwrite=True)
Example #20
0
    def query_objects(self, names: TList[str]) -> TList[CatalogSource]:
        """
        Return a list of SDSS catalog objects with the specified names

        :param names: object names

        :return: list of catalog objects with the specified names
        """
        sdss = SDSS()
        rows = []
        for name in names:
            rows.append(
                sdss.query_object(name,
                                  data_release=15,
                                  photoobj_fields=self._columns,
                                  cache=False)[0])
        return self.table_to_sources(rows)
Example #21
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 querySpectra(self):
     try:
         self.standardQuery()
         coord = []
         for i in range(0, len(self.ra)):
             coord.append(coords.SkyCoord(self.ra[i], self.dec[i], frame='icrs', unit='deg'))
         self.spectra = SDSS.query_crossid(coord, photoobj_fields=['modelMag_g', 'modelMag_r'])
         return self.spectra
     except:
         raise ValueError("No Results found. Try a different search area.")
Example #24
0
    def load_image(self):

        im_size = self.cutout_size.to(u.arcsec).value
        mag_aperture = self.aperture.to(u.arcsec).value

        # keep original coords of image for bounds checking later
        self.orig_coords = self.coords

        with self.bottombox:
            try:
                self.cutout = self.catlib.get_cutouts(
                    position=self.coords,
                    side=im_size,
                    aperture=mag_aperture,
                    dynamic=False,
                    filter=["r", "g", "f606W"],
                    first=True,
                )[0]

                im = NDData(self.cutout["cutout"].data,
                            wcs=self.cutout["cutout"].wcs)
                self.im_path = self.cutout["path"]
                self.imw.load_nddata(im)

            except:
                try:
                    sdss_im = SDSS.get_images(coordinates=self.coords,
                                              band="g")
                    im = sdss_im[0][0]
                except:
                    sdss_im = SDSS.get_images(coordinates=self.coords,
                                              band="g",
                                              radius=30.0 * u.arcsec)
                    im = sdss_im[0][0]

                self.im_path = "SDSS Astroquery result"
                self.imw.load_fits(im)

            self.imw.center_on(self.coords)
            self.imw.zoom_level = self.zoom
            self.textimpath.value = self.im_path
Example #25
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
Example #27
0
def sdss_template(tipo='eliptica'):

    if tipo == 'eliptica':
        template = SDSS.get_spectral_template('galaxy_early')
        title = 'Espectro de galaxia eliptica'
        lines = absorption_lines
    elif tipo == 'espiral':
        template = SDSS.get_spectral_template('galaxy_late')
        title = 'Espectro de galaxia espiral'
        lines = emission_lines

    spec_h = template[0][0].header
    spec_data = template[0][0].data
    wcs = WCS(spec_h)

    index = np.arange(spec_h['NAXIS1'])
    loglam = wcs.wcs_pix2world(index, np.zeros(len(index)), 0)[0]
    flux = spec_data[0]

    gv = (loglam > np.log10(4000.)) & (loglam < np.log10(8000.))
    flux_scale = 80. / np.max(flux[gv])

    fig, ax = plt.subplots(figsize=(20, 8), sharex=True, sharey=True)
    plt.plot(10**loglam, flux * flux_scale, '-', color='black', linewidth=1)

    for j in range(len(lines['name'])):
        ax.plot(lines['lambda'][j] * np.ones(2), [0., 1e5],
                lines['color'] + '--')
        ax.text(lines['lambda'][j] + lines['offset'][j],
                lines['position'][j] * 100.,
                lines['name'][j],
                color='black',
                fontsize=18,
                horizontalalignment=lines['align'][j])

        ax.set_ylabel(r'Flujo [10$^{-17}$ ergs/cm$^2$/s/\AA]', fontsize=14)
        ax.set_xlabel(r'Longitud de onda [\AA]', fontsize=14)
        ax.set_title(title)
        ax.set_xlim(3500, 8000)
        ax.set_ylim(0., 100.)
Example #28
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
Example #29
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
Example #30
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
Example #31
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
Example #32
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
Example #34
0
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
Example #35
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')
Example #36
0
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 res(ra, dec, ang):
    """Fetches SDSS data with GetNearbyObj function and given parameters"""
    query = """
        SELECT
            s.ra, s.dec,
            s.dered_g as g, s.dered_r as r,
            s.err_g, s.err_r,
            s.flags
  
        FROM
            dbo.fGetNearbyObjEq({}, {}, {}) AS n
        JOIN Star AS s ON n.objID = s.objID
  
        WHERE
            g - r BETWEEN -0.5 AND 2.5
            AND g BETWEEN 14 and 24
        """.format(ra,dec,ang)
        
    return SDSS.query_sql(query, timeout = 600)
# 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)
Example #39
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
Example #40
0
def get_sdss_spectra(outfile = "outfile", N_spec = 5):
    from urllib2 import HTTPError
    from astroquery.sdss import SDSS
    # query = "SELECT TOP 1000 p.objid, p.dec, p.r,p.i, p.run, p.rerun, p.camcol, p.field, s.specobjid, s.class, s.z as redshift FROM PhotoObj AS p JOIN SpecObj AS s ON s.bestobjid = p.objid WHERE p.r BETWEEN 0 AND 17.0 AND s.class = 'QSO' AND s.z BETWEEN 1.0 AND 2.3 AND p.dec >= 15.0"
    query = "SELECT TOP "+str(N_spec)+" specObjID, plate, mjd, subClass, fiberID FROM SpecPhoto WHERE (class = 'QSO') AND" \
                                      " (psfmag_r <= 17.0) AND (dec >= 15.0) AND (z BETWEEN 1.0 AND 2.3) AND zwarning = 0 AND" \
                                      " (subClass = 'BROADLINE') AND nChild = 0 AND (mode = 1) AND ((0x10000000) != 0)" \
                                      " AND (bossprimary= 0) AND programname = 'legacy'"



    res = SDSS.query_sql(query)
# (subClass = 'BROADLINE') AND

    # print(res['subClass'])
    spectra = []
    var = []
    waves = []
    mask = []
    z = []

    # print(res['plate'], res['mjd'], res['fiberID'])
    num_skipped = 0
    count = 1
    n_spec = len(res['specObjID'])

    for i in range(n_spec):
        # print(res['subClass'][i])
        try:
            sp = SDSS.get_spectra(plate=res['plate'][i], mjd=res['mjd'][i], fiberID=res['fiberID'][i])[0]
            data = (sp[1].data)

            wave = (10**data.field('loglam'))
            flux = data.field('flux')
            err = data.field('ivar')
            masking = data.field('and_mask')


            mask.append(masking)
            z.append(sp[2].data.field('Z'))
            spectra.append(flux)
            var.append(err)
            waves.append(wave)
            # print(res['plate'][i],res['mjd'][i], res['fiberID'][i])
            # pl.plot(wave, flux)
            # pl.show()
            count += 1
        except HTTPError:
            num_skipped += 1
            print("%i, %i, %i not found" % (res['plate'][i], res['mjd'][i], res['fiberID'][i]))
            continue
        except ValueError:
            num_skipped += 1
            print("%i, %i, %i ValueError" % (res['plate'][i], res['mjd'][i], res['fiberID'][i]))
            continue
        except TypeError:
            num_skipped += 1
            print("%i, %i, %i TypeError" % (res['plate'][i], res['mjd'][i], res['fiberID'][i]))
            continue

        print('Number of spectrum processed: {0} out of {1}'.format(count, n_spec - num_skipped))
    print("   %i spectra skipped" % num_skipped)
    # exit()


    np.savez(outfile,
             wave = waves,
             spectra=spectra,
             var = var,
             mask = mask,
             plate = res['plate'],
             mjd = res['mjd'],
             fiberID = res['fiberID'],
             z = z
             )
Example #41
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)
Example #42
0
    #Get the object SDSS parameters
    Name, Catalogue, mjd, plate, fiberID, alfalfa_code = Candiates_frame.index[i], row['catalogue'].values[0], row['mjd'].values[0], row['plate'].values[0], row['fiber'].values[0], row['alfalfa_name'].values[0] 
     
    #Generate object folder
    CodeName    = pv.generate_catalogue_tree(Catalogue_Dic, obj = Name)
    FileFolder  = Catalogue_Dic['Obj_Folder'] + CodeName + '/'
 
    #Store parameter in object log file
    pv.SetLogFile(CodeName + pv.ObjectLog_extension, FileFolder)
         
    #Query the object table
    #----------------Sloan objects----------------------
    if Catalogue == 'sloan':
        mjd, plate, fiberID = int(mjd), int(plate), int(fiberID)
        
        obj_table = SDSS.query_specobj(mjd = mjd, plate = plate, fiberID=fiberID)
        
        if obj_table != None:
            #print Name, str(ephem.hours(math.radians(obj_table['ra'][0]))), str(ephem.degrees(math.radians(obj_table['dec'][0]))), '\n'
            SDSS_RA         = obj_table['ra'][0]
            SDSS_DEC        = obj_table['dec'][0]
            SDSS_RA_hours   = str(ephem.hours(math.radians(obj_table['ra'][0])))
            SDSS_DEC_hours  = str(ephem.degrees(math.radians(obj_table['dec'][0])))
            
            co                  = coords.SkyCoord(float(SDSS_RA), float(SDSS_DEC), unit="deg")
            Obj_query           = SDSS.query_crossid(co, photoobj_fields=['modelMag_u', 'modelMag_g', 'modelMag_r'])
            mag_u, mag_g, mag_r = Obj_query['modelMag_u'][0], Obj_query['modelMag_g'][0], Obj_query['modelMag_r'][0]
            website = "http://dr12.sdss3.org/spectrumDetail?mjd={mjd}&fiber={fiber}&plateid={plateid}".format(mjd = mjd, fiber = fiberID, plateid = plate)
            
            print Name, website
Example #43
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 ''