"""
A quick library to deal with searching simbad for info
 about a SN and parsing the results.

Author: Isaac Shivvers, ishivvers@berkeley.edu, 2014


example SIMBAD uri query:
http://simbad.u-strasbg.fr/simbad/sim-id?output.format=ASCII&Ident=sn%201998S
"""

import re
from urllib2 import urlopen

def get_SN_info( name ):
    """
    Queries simbad for SN coords, redshift, and host galaxy.
    If redshift is not given for SN, attempts to resolve link to 
     host galaxy and report its redshift.
    Returns ( (ra,dec), redshift, host_name, redshift_citation ), with
     values of None inserted whenever it cannot resolve the value.
    """
    simbad_uri = "http://simbad.u-strasbg.fr/simbad/sim-id?output.format=ASCII&Ident=%s"
    regex_coords = "Coordinates\(FK5.+\): .+"
    regex_redshift = "Redshift:\s+\d+\.\d+.+"
    regex_host = "apparent\s+host\s+galaxy\s+.+?\{(.*?)\}"

    result = urlopen( simbad_uri % name.replace(' ','%20') ).read()
    rescoords = re.search( regex_coords, result )
    resred = re.search( regex_redshift, result )
    reshost = re.search( regex_host, result )
    
    try:
        cs = rescoords.group().split(':')[1].strip()
        ra = cs[:12].strip()
        dec = cs[12:].strip()
    except:
        ra,dec = None,None
        
    try:
        redshift = float(resred.group().strip('Redshift: ').split(' ')[0])
        citation = resred.group().split(' ')[-1]
    except AttributeError:
        redshift = None
        citation = None
    
    try:
        host = reshost.group().split('{')[1].split('}')[0]
    except AttributeError:
        host = None

    if (redshift == None) and (host != None):
        # get the redshift from the host galaxy
        result = urlopen( simbad_uri % host.replace(' ','%20') ).read()
        resred = re.search( regex_redshift, result )
        try:
            redshift = float(resred.group().strip('Redshift: ').split(' ')[0])
            citation = resred.group().split(' ')[-1]
        except AttributeError:
            pass

    return ((ra,dec), redshift, host, citation)


[(29, 're.search', 're.search', False, 'import re\n'), (30, 're.search', 're.search', False, 'import re\n'), (31, 're.search', 're.search', False, 'import re\n'), (55, 're.search', 're.search', False, 'import re\n')]