Esempio n. 1
0
def test_xml_to_dict5():
    """
    should return dict of xml string
    with 2 layer nesting
    """
    source_xml = "<outer>\
                    <mid1>\
                        <inner1>one-one</inner1>\
                    </mid1>\
                    <mid2>\
                        <inner2>two-one</inner2>\
                    </mid2>\
                 </outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {
        u'outer': {
            u'mid2': {
                u'inner2': u'two-one'
            },
            u'mid1': {
                u'inner1': u'one-one'
            }
        }
    }

    assert xml_dict == expected_dict
Esempio n. 2
0
def get_header(filepath):
    """
    Reads the header from the file

    Parameters
    ----------
    filepath : string
        The file to be read

    Returns
    -------
    headers : list
        A list of headers read from the file
    """
    jp2 = Jp2k(filepath)
    xml_box = [box for box in jp2.box if box.box_id == 'xml ']
    xmlstring = ET.tostring(xml_box[0].xml.find('fits'))
    pydict = xml_to_dict(xmlstring)["fits"]

    #Fix types
    for k, v in pydict.items():
        if v.isdigit():
            pydict[k] = int(v)
        elif _is_float(v):
            pydict[k] = float(v)

    # Remove newlines from comment
    if 'comment' in pydict:
        pydict['comment'] = pydict['comment'].replace("\n", "")

    return [FileHeader(pydict)]
Esempio n. 3
0
def get_header(filepath):
    """
    Reads the header from the file

    Parameters
    ----------
    filepath : `str`
        The file to be read

    Returns
    -------
    headers : list
        A list of headers read from the file
    """
    jp2 = Jp2k(filepath)
    xml_box = [box for box in jp2.box if box.box_id == 'xml ']
    xmlstring = ET.tostring(xml_box[0].xml.find('fits'))
    pydict = xml_to_dict(xmlstring)["fits"]

    # Fix types
    for k, v in pydict.items():
        if v.isdigit():
            pydict[k] = int(v)
        elif _is_float(v):
            pydict[k] = float(v)

    # Remove newlines from comment
    if 'comment' in pydict:
        pydict['comment'] = pydict['comment'].replace("\n", "")

    return [FileHeader(pydict)]
Esempio n. 4
0
def get_header(filepath):
    """
    Reads the header form the file
    
    Parameters
    ----------
    filepath : string
        The file to be read
        
    Returns
    -------
    headers : list
        A list of headers read from the file
    """
    xmlstring = _read_xmlbox(filepath, "fits")
    pydict = xml_to_dict(xmlstring)["fits"]
    
    #Fix types
    for k, v in pydict.items():
        if v.isdigit():
            pydict[k] = int(v)
        elif _is_float(v):
            pydict[k] = float(v)
            
    # Remove newlines from comment
    if 'comment' in pydict:
        pydict['comment'] = pydict['comment'].replace("\n", "")
            
    return [FileHeader(pydict)]
Esempio n. 5
0
    def get_jp2_header(self, date, observatory=None, instrument=None, detector=None, measurement=None, jp2_id=None):
        """
        Get the XML header embedded in a JPEG2000 image. Includes the FITS header as well as a section
        of Helioviewer-specific metadata.

        This uses `getJP2Header <https://api.helioviewer.org/docs/v2/#JPEG2000>`_
        from the Helioviewer API.

        .. note::
            We can use ``observatory`` and ``measurement`` or ``instrument`` and ``measurement``
            to get the value for source ID which can then be used to get required information.

        Parameters
        ----------
        date : `astropy.time.Time`, `str`
            A `~sunpy.time.parse_time` parsable string or `~astropy.time.Time`
            object for the desired date of the image
        observatory : `str`
            Observatory name
        instrument : `str`
            Instrument name
        measurement : `str`
            Measurement name
        detector : `str`
            Detector name
        jp2_id : `int`
            Unique JP2 image identifier.
            This can be used directly instead of using the previous parameters.

        Returns
        -------
        out : `dict`
            Returns a dictionary containing the header information of JPEG 2000 image.
            The returned dictionary may have either one or two keys: *fits* and *helioviewer*.

        Examples
        --------
        >>> from sunpy.net import helioviewer
        >>> hv = helioviewer.HelioviewerClient()  # doctest: +REMOTE_DATA
        >>> header = hv.get_jp2_header('2012/07/03', observatory='SDO',
        ...                            instrument='HMI', detector=None, measurement='continuum')  # doctest: +REMOTE_DATA
        >>> # The key 'fits' can be used to get the fits header information
        >>> fits_header = header['fits']  # doctest: +REMOTE_DATA
        >>> # The keys 'helioviewer' can be used to extract the helioviewer specific metadata.
        >>> helioviewer_meta_data = header['helioviewer']  # doctest: +REMOTE_DATA
        """
        if jp2_id is None:
            jp2_id = self.get_closest_image(
                date, observatory, instrument, detector, measurement)['id']

        params = {
            "action": "getJP2Header",
            "id": jp2_id,
        }

        responses = self._request(params)
        # Reads the output from HTTPResponse object and decodes it.
        responses = responses.read().decode('utf-8')
        return xml_to_dict(responses)['meta']
Esempio n. 6
0
    def get_jp2_header(self, date, observatory=None, instrument=None, detector=None, measurement=None, jp2_id=None):
        """
        Get the XML header embedded in a JPEG2000 image. Includes the FITS header as well as a section
        of Helioviewer-specific metadata.

        This uses `getJP2Header <https://api.helioviewer.org/docs/v2/#JPEG2000>`_ from the Helioviewer API.

        Parameters
        ----------
        date : `astropy.time.Time`, `str`
            A `~sunpy.time.parse_time` parsable string or `~astropy.time.Time`
            object for the desired date of the image
        observatory : `str`
            Observatory name
        instrument : `str`
            Instrument name
        measurement : `str`
            Measurement name
        detector : `str`
            Detector name
        jp2_id : `int`
            Unique JP2 image identifier.
            This can be used directly instead of using the previous parameters.

        Returns
        -------
        out : `dict`
            Returns a dictionary containing the header information of JPEG 2000 image.
            The returned dictionary may have either one or two keys: *fits* and *helioviewer*.

        Examples
        --------
        >>> from sunpy.net import helioviewer
        >>> hv = helioviewer.HelioviewerClient()  # doctest: +REMOTE_DATA
        >>> header = hv.get_jp2_header('2012/07/03', observatory='SDO',
        ...                            instrument='HMI', detector=None, measurement='continuum')  # doctest: +REMOTE_DATA
        >>> # The key 'fits' can be used to get the fits header information
        >>> fits_header = header['fits']  # doctest: +REMOTE_DATA
        >>> # The keys 'helioviewer' can be used to extract the helioviewer specific metadata.
        >>> helioviewer_meta_data = header['helioviewer']  # doctest: +REMOTE_DATA
        """
        if jp2_id is None:
            try:
                jp2_id = self.get_closest_image(date, observatory, instrument, detector, measurement)['id']
            except KeyError:
                raise KeyError("The values used for observatory, instrument, detector, measurement "
                               "do not correspond to a source_id. Please check the list using "
                               "HelioviewerClient.data_sources.")

        params = {
            "action": "getJP2Header",
            "id" : jp2_id,
        }

        responses = self._request(params)
        # Reads the output from HTTPResponse object and decodes it.
        responses = responses.read().decode('utf-8')
        return xml_to_dict(responses)['meta']
Esempio n. 7
0
def test_xml_to_dict4():
    """
    should return dict of xml string
    with empty value if there are no inner elements
    """
    source_xml = "<outer></outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u'outer': ''}

    assert xml_dict == expected_dict
Esempio n. 8
0
def test_with_multiple_children_in_list():
    """
    Setting the 'multiple' attribute of parent node should put child nodes in a
    list.
    """
    def getChild(lst_of_children, key, value):
        for child in lst_of_children:
            if child[key] == value:
                return child

        raise ValueError("No children with key {0} set to {1} found.".format(key, value))

    source = '''<?xml version="1.0" encoding="UTF-8"?>
                <Config>
                    <Name>With multiple children</Name>
                    <Children multiple="true">
                      <Child>
                        <Name>First Child</Name>
                        <Value>Value 1</Value>
                      </Child>
                      <Child>
                        <Name>Second Child</Name>
                        <Value>Value 2</Value>
                      </Child>
                    </Children>
                </Config>'''

    expected = {'Config': {'Children': [{'Name': 'First Child', 'Value': 'Value 1'},
                                        {'Name': 'Second Child', 'Value': 'Value 2'}],
                           'Name': 'With multiple children'}}

    actual = xml.xml_to_dict(source)

    assert len(expected['Config']) == len(actual['Config'])
    assert expected['Config']['Name'] == actual['Config']['Name']

    assert len(actual['Config']['Children']) == 2

    # As the child dictionaries are in lists we cannot be certain what order
    # they are in. Test individualy.

    expected_children = expected['Config']['Children']
    actual_children = actual['Config']['Children']

    expected_first_child = getChild(expected_children, key='Name', value='First Child')
    actual_first_child = getChild(actual_children, key='Name', value='First Child')

    assert expected_first_child == actual_first_child

    expected_second_child = getChild(expected_children, key='Name', value='Second Child')
    actual_second_child = getChild(actual_children, key='Name', value='Second Child')

    assert expected_second_child == actual_second_child
Esempio n. 9
0
def get_header(filepath):
    """Reads the header in and saves it as a dictionary"""
    xmlstring = read_xmlbox(filepath, "fits")
    pydict = xml_to_dict(xmlstring)["fits"]
    
    #Fix types
    for k, v in pydict.items():
        if v.isdigit():
            pydict[k] = int(v)
        elif is_float(v):
            pydict[k] = float(v)
            
    return pydict
Esempio n. 10
0
def test_xml_to_dict1():
    """
    should return dict of xml string
    """
    source_xml = "<outer>\
          <inner1>one</inner1>\
          <inner2>two</inner2>\
        </outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u'outer': {u'inner2': u'two', u'inner1': u'one'}}

    assert xml_dict == expected_dict
Esempio n. 11
0
def test_xml_to_dict1():
    """
    should return dict of xml string
    """
    source_xml = "<outer>\
          <inner1>one</inner1>\
          <inner2>two</inner2>\
        </outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u"outer": {u"inner2": u"two", u"inner1": u"one"}}

    assert xml_dict == expected_dict
Esempio n. 12
0
def test_xml_to_dict2():
    """
    should return dict of xml string
    and if a tag is duplicated it takes the last one.
    """
    source_xml = "<outer>\
                    <inner1>one-one</inner1>\
                    <inner1>one-two</inner1>\
                    <inner2>two-one</inner2>\
                    <inner2>two-two</inner2>\
                 </outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u'outer': {u'inner2': u'two-two', u'inner1': u'one-two'}}

    assert xml_dict == expected_dict
Esempio n. 13
0
def test_xml_to_dict2():
    """
    should return dict of xml string
    and if a tag is duplicated it takes the last one.
    """
    source_xml = "<outer>\
                    <inner1>one-one</inner1>\
                    <inner1>one-two</inner1>\
                    <inner2>two-one</inner2>\
                    <inner2>two-two</inner2>\
                 </outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u"outer": {u"inner2": u"two-two", u"inner1": u"one-two"}}

    assert xml_dict == expected_dict
Esempio n. 14
0
    def get_voevent(self, as_dict=True, base_url="http://www.lmsal.com/hek/her?"):
        """Retrieves the VOEvent object associated with a given event and
        returns it as either a Python dictionary or an XML string."""

        # Build URL
        params = {"cmd": "export-voevent", "cosec": 1, "ivorn": self["kb_archivid"]}
        url = base_url + urllib.parse.urlencode(params)

        # Query and read response
        response = urllib.request.urlopen(url).read()

        # Return a string or dict
        if as_dict:
            return xml_to_dict(response)
        else:
            return response
Esempio n. 15
0
File: jp2.py Progetto: jslhs/sunpy
def get_header(filepath):
    """Reads the header in and saves it as a dictionary"""
    xmlstring = read_xmlbox(filepath, "fits")
    pydict = xml_to_dict(xmlstring)["fits"]
    
    #Fix types
    for k, v in pydict.items():
        if v.isdigit():
            pydict[k] = int(v)
        elif is_float(v):
            pydict[k] = float(v)
            
    # Remove newlines from comment
    pydict['comment'] = pydict['comment'].replace("\n", "")
            
    return MapHeader(pydict)
Esempio n. 16
0
def get_header(filepath):
    """Reads the header in and saves it as a dictionary"""
    xmlstring = read_xmlbox(filepath, "fits")
    pydict = xml_to_dict(xmlstring)["fits"]
    
    #Fix types
    for k, v in pydict.items():
        if v.isdigit():
            pydict[k] = int(v)
        elif is_float(v):
            pydict[k] = float(v)
            
    # Remove newlines from comment
    pydict['comment'] = pydict['comment'].replace("\n", "")
            
    return MapHeader(pydict)
Esempio n. 17
0
def test_xml_to_dict5():
    """
    should return dict of xml string with 2 layer nesting.
    """
    source_xml = "<outer>\
                    <mid1>\
                        <inner1>one-one</inner1>\
                    </mid1>\
                    <mid2>\
                        <inner2>two-one</inner2>\
                    </mid2>\
                 </outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u'outer': {u'mid2': {u'inner2': u'two-one'}, u'mid1': {u'inner1': u'one-one'}}}

    assert xml_dict == expected_dict
Esempio n. 18
0
def test_xml_to_dict5():
    """
    should return dict of xml string
    with 2 layer nesting
    """
    source_xml = "<outer>\
                    <mid1>\
                        <inner1>one-one</inner1>\
                    </mid1>\
                    <mid2>\
                        <inner2>two-one</inner2>\
                    </mid2>\
                 </outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u"outer": {u"mid2": {u"inner2": u"two-one"}, u"mid1": {u"inner1": u"one-one"}}}

    assert xml_dict == expected_dict
Esempio n. 19
0
def test_xml_to_dict6():
    """
    should return dict of xml string
    with 2 layer nesting and if a tag is duplicated it takes the last one.
    """
    source_xml = "<outer>\
                    <mid>\
                        <inner1>one-one</inner1>\
                    </mid>\
                    <mid>\
                        <inner2>two-one</inner2>\
                    </mid>\
                 </outer>"

    xml_dict = xml.xml_to_dict(source_xml)
    expected_dict = {u'outer': {u'mid': {u'inner2': u'two-one'}}}

    assert xml_dict == expected_dict
Esempio n. 20
0
    def get_voevent(self, as_dict=True,
                    base_url="http://www.lmsal.com/hek/her?"):
        """Retrieves the VOEvent object associated with a given event and
        returns it as either a Python dictionary or an XML string."""

        # Build URL
        params = {
            "cmd": "export-voevent",
            "cosec": 1,
            "ivorn": self['kb_archivid']
        }
        url = base_url + urllib.parse.urlencode(params)

        # Query and read response
        response = urllib.request.urlopen(url).read()

        # Return a string or dict
        if as_dict:
            return xml_to_dict(response)
        else:
            return response
Esempio n. 21
0
def get_header(filepath):
    """
    Reads the header from the file.

    Parameters
    ----------
    filepath : `str`
        The file to be read.

    Returns
    -------
    headers : list
        A list of headers read from the file.
    """
    # Put import here to speed up sunpy.io import time
    from glymur import Jp2k
    jp2 = Jp2k(filepath)
    xml_box = [box for box in jp2.box if box.box_id == 'xml ']
    xmlstring = ET.tostring(xml_box[0].xml.find('fits'))
    pydict = xml_to_dict(xmlstring)["fits"]

    # Fix types
    for k, v in pydict.items():
        if v.isdigit():
            pydict[k] = int(v)
        elif _is_float(v):
            pydict[k] = float(v)

    # Remove newlines from comment
    if 'comment' in pydict:
        pydict['comment'] = pydict['comment'].replace("\n", "")

    # Is this file a Helioviewer Project JPEG2000 file?
    pydict['helioviewer'] = xml_box[0].xml.find('helioviewer') is not None

    return [FileHeader(pydict)]