1
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2 1
"""
3
Utilities used by irsa_dust.py to query the dust service and parse the results.
4
"""
5 1
import re
6 1
import xml.etree.ElementTree as tree
7 1
import astropy.units as u
8

9

10 1
def parse_number(string):
11
    """
12
    Retrieve a number from the string.
13

14
    Parameters
15
    ----------
16
    string : str
17
        the string to parse
18

19
    Returns
20
    -------
21
    number : float
22
        the number contained in the string
23
    """
24 1
    num_str = string.split(None, 1)[0]
25 1
    number = float(num_str)
26 1
    return number
27

28

29 1
def parse_coords(string):
30
    """
31
    Retrieve coordinates from the string.
32

33
    Parameters
34
    ----------
35
    string : str
36
        the string to parse
37

38
    Returns
39
    -------
40
    coords : list(float, float, str)
41
        list containing RA, Dec, and coordinate system description
42
    """
43 1
    ra = float(string.split()[0])
44 1
    dec = float(string.split()[1])
45 1
    coord_sys = string.split(None, 2)[2].strip()
46 1
    coords = [ra, dec, coord_sys]
47 1
    return coords
48

49

50 1
def parse_units(string):
51
    """
52
    Retrieve units from the string.
53

54
    Parameters
55
    ----------
56
    string: str
57
        the string to parse
58

59
    Returns
60
    -------
61
    units : `~astropy.units.Unit`
62
        the units contained in the string
63
    """
64 1
    unit_str = string.split(None, 1)[1]
65 1
    unit_str = re.sub("[()]", "", unit_str).strip()
66 1
    units = u.format.Generic().parse(unit_str)
67 1
    return units
68

69

70 1
def find_result_node(desc, xml_tree):
71
    """
72
    Returns the <result> node with a <desc> child matching the given text.
73
    Eg: if desc = "text to match", this function will find the following
74
        result node:
75
        <result>
76
            <desc>text to match</desc>
77
        </result>
78

79
    Parameters
80
    -----
81
    xmlTree : `xml.etree.ElementTree`
82
        the xml tree to search for the <result> node
83
    desc : string
84
        the text contained in the desc node
85

86
    Returns
87
    -----
88
    node : the <result> node containing the child with the given desc
89
    """
90 1
    result_nodes = xml_tree.findall("result")
91

92 1
    for result_node in result_nodes:
93 1
        result_desc = result_node.find("desc").text.strip()
94 1
        if result_desc == desc:
95 1
            return result_node
96 0
    return None
97

98

99 1
def xml(response):
100
    """
101
    Parse raw xml and return as an xml tree. If status is not ``ok``, raise
102
    an exception.
103

104
    Parameters
105
    ----------
106
    response : str
107
        unicode string containing xml
108

109
    Returns
110
    -------
111
    xml_tree : `xml.etree.ElementTree`
112
        an xml tree
113
    """
114
    # get the root element from the xml string
115 1
    root = tree.fromstring(response)
116 1
    status = root.attrib["status"]
117 1
    if status == "error":
118 1
        message = root.find("message").text
119 1
        raise Exception("The dust service responded with an error: " + message)
120 1
    elif status != "ok":
121 0
        raise Exception("Response status was not 'ok'.")
122
    # construct the ElementTree from the root
123 1
    xml_tree = tree.ElementTree(root)
124 1
    return xml_tree

Read our documentation on viewing source code .

Loading