astropy / astroquery
1
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2 1
"""Download GAMA data"""
3 1
import re
4 1
import os
5 1
from astropy.table import Table
6 1
from ..query import BaseQuery
7 1
from ..utils import commons, async_to_sync
8

9 1
__all__ = ['GAMA', 'GAMAClass']
10

11

12 1
@async_to_sync
13 1
class GAMAClass(BaseQuery):
14
    """
15
    TODO: document
16
    """
17

18 1
    request_url = 'http://www.gama-survey.org/dr3/query/'
19 1
    timeout = 60
20

21 1
    def query_sql_async(self, *args, **kwargs):
22
        """
23
        Query the GAMA database
24

25
        Returns
26
        -------
27
        url : The URL of the FITS file containing the results.
28
        """
29

30 0
        payload = self._parse_args(*args, **kwargs)
31

32 0
        if kwargs.get('get_query_payload'):
33 0
            return payload
34

35 0
        result = self._request("POST", url=self.request_url,
36
                               data=payload, timeout=self.timeout)
37

38 0
        result_url_relative = find_data_url(result.text)
39 0
        result_url = os.path.join(self.request_url, result_url_relative)
40

41 0
        return result_url
42

43 1
    def _parse_args(self, sql_query):
44
        """
45
        Parameters
46
        ----------
47
        sql_query : str
48
            An SQL query
49

50
        Returns
51
        -------
52
        payload_dict : Requests payload in a dictionary
53
        """
54

55 0
        payload = {'query': sql_query,
56
                   'format': 'fits'}
57

58 0
        return payload
59

60 1
    def _parse_result(self, result, verbose=False, **kwargs):
61
        """
62
        Use get_gama_datafile to download a result URL
63
        """
64 0
        return get_gama_datafile(result)
65

66

67 1
GAMA = GAMAClass()
68

69

70 1
def get_gama_datafile(result, **kwargs):
71
    """Turn a URL into an HDUList object."""
72 1
    fitsfile = commons.FileContainer(result,
73
                                     encoding='binary',
74
                                     **kwargs)
75 1
    hdulist = fitsfile.get_fits()
76 1
    return Table(hdulist[1].data)
77

78

79 1
def find_data_url(result_page):
80
    """Find and return the URL of the data, given a results page."""
81 1
    result_relative_url_re = re.compile(r'Download the result file: '
82
                                        r'<a href="(\.\./tmp/.*?)">')
83 1
    re_result = result_relative_url_re.findall(result_page)
84 1
    if len(re_result) == 0:
85 0
        raise ValueError("Results did not contain a result url")
86 1
    return re_result[0]

Read our documentation on viewing source code .

Loading