Example #1
0
def sfd_ebmv(ra, dec, equinox=2000, obsepoch=None, service='IRSA'):
    """
    Fetch SFD Milky Way E(B-V) from IRSA or NED.

    `IRSA <http://irsa.ipac.caltech.edu/>`_ (slow)
    `NED <http://ned.ipac.caltech.edu/>`_.

    :param service: can be 'IRSA' or 'NED'
    """
    assert equinox in [1950, 2000], 'equinox should be 1950 or 2000'

    if obsepoch is None:
        obsepoch = datetime.utcnow().year

    ra, dec = SkyCoord(ra, dec, unit='deg').to_string('hmsdms').split()
    equinox = {1950: 'B1950.0', 2000: 'J2000.0'}[equinox]

    if service.lower() == 'ned':

        query = dict(in_csys='Equatorial',
                     in_equinox=equinox,
                     obs_epoch=str(obsepoch),
                     lon=ra.__str__(),
                     lat=dec.__str__(),
                     pa='0.0',
                     out_csys='Equatorial',
                     out_equinox=equinox)

        try:  # python 2
            items = query.iteritems()
        except:  # python 3
            items = query.items()
        url = urlparse.urlunparse(('http', 'ned.ipac.caltech.edu',
                                   'cgi-bin/nph-calc', '',
                                   '&'.join(['%s=%s' % (i, j)
                                             for i, j in items]),
                                   ''))
        html = urllib.urlopen(url).read()

        # grep into the HTML
        mwebv = re.search(r'(?<=E\(B-V\)\s=\s)\d+\.\d+(?=\smag)', html)
        if mwebv is not None:
            return float(mwebv.group(0))
        else:
            return None

    elif service.lower() == 'irsa':

        query = dict(locstr=urllib.quote('%s %s Equatorial %s' %
                                         (ra.__str__(), dec.__str__(), equinox)))

        try:  # python 2
            items = query.iteritems()
        except:  # python 3
            items = query.items()
        url = urlparse.urlunparse(('http', 'irsa.ipac.caltech.edu',
                                   'cgi-bin/DUST/nph-dust', '',
                                   '&'.join(['%s=%s' % (i, j)
                                             for i, j in items]),
                                   ''))

        # parse the XML
        opened_url = urllib.urlopen(url)
        if 'read' in dir(opened_url):
            # python 2
            infos = xml_parser(opened_url.read())
        else:
            # python 3 
            infos = xml_parser(opened_url)
        if infos is not None:
            # the other 'result' are not in mag
            mwebv = infos['results']['result'][0]['statistics']['refPixelValueSFD']
            return float(mwebv.split()[0])
        else:
            return None