<jupyter_start><jupyter_text>SDSS Cross-matching----Example of using astroquery to search the SDSS spectroscopy catalog and turn the result into a table with active links to the SDSS Object Explorer pages.<jupyter_code>import numpy as np
from astropy.table import Table
from astropy import coordinates as coords
from astropy import units as u<jupyter_output><empty_output><jupyter_text>Note on RequirementsBe sure that you have astroquery installed - that's an additional dependency for this tutorial beyond Astropy.  If you're following the instructions from the workshop you should have gotten it when you did an update.  But run this cell to check:<jupyter_code>from astroquery.sdss import SDSS<jupyter_output><empty_output><jupyter_text>If that fails, you'll want to try this from a terminal (and you'll need to go to "Kernel->Restart" after doing it):```conda install -c astropy astroquery```or if you're not using conda, you'll need to do:```pip install astroquery``` Define the functions that will do the work<jupyter_code># Take a list of targets and search for them in the SDSS database. Add the spectroscopic IDs to the table.
def sdssquery(targets):
    specobjid = []
    for pos in targets['coords']:
        try: 
            xid = SDSS.query_region(pos,data_release=12,spectro=True,radius=10*u.arcsec)
            specobjid += [xid['specobjid'][0]]
        except:
            print("Not found: ",pos)
    targets['specobjid'] = specobjid
    return targets

# Create a URL to the Explorer from a spectroscopic ID.
def sdss_url(specobjid):
    urlbase = "http://skyserver.sdss.org/dr12/en/tools/explore/summary.aspx?"
    url = urlbase+"sid=%d" % (specobjid)
    href = '<a href=%s> sdss </a>' % url
    return href

# Run the two functions defined above for all the targets
def sdss(targets):
    targets = sdssquery(targets)
    targets['url'] = [sdss_url(sid) for sid in zip(targets['specobjid'])]
    return targets<jupyter_output><empty_output><jupyter_text>For demonstration purposes, here is a table with three sources (known to be in the SDSS catalog). We're creating columns for ID, RA and Dec, and then we are adding a column that contains the coordinates as a SkyCoord object, which will be used for the catalog matching.<jupyter_code>targets = Table()
targets['ID'] = [133,229,222]
targets['RA'] = [158.43144,195.36813,192.14461]
targets['Dec'] = [63.888338,51.080956,12.567532]
targets['coords']=coords.SkyCoord(targets['RA'],targets['Dec'],unit='deg',frame='icrs')
targets<jupyter_output><empty_output><jupyter_text>Now cross-match this catalog with the SDSS and output the table in notebook format, including a URL to the SDSS Object Explorer page for each target. We have to do a little editing of the HTML to get this to render in the notebook (although there may now be a better way to do this in astropy).<jupyter_code>newtable = sdss(targets)
ipy_html = newtable.show_in_notebook()
# the show_in_notebook format has changed the HTML brackets to &lt and &gt; we need to change them back
ipy_html.data = ipy_html.data.replace('&lt;','<')
ipy_html.data = ipy_html.data.replace('&gt;','>')
ipy_html<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09781576448243115, 'fraction_numerical': 0.02025957581513137, 'mean_word_length': 4.951035781544256, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '254254', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1040, 'n_tokens_neox': 948, 'n_words': 358, 'path': 'aas229_workshop/Tutorial_Notebooks/sdss_matching.ipynb', 'repo_name': 'astropy/astropy-workshops', 'size': '3159'}