astropy / astroquery
1
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2 1
from __future__ import (absolute_import, division, print_function,
3
                        unicode_literals)
4

5 1
import json
6 1
import warnings
7

8 1
from astropy.table import Table
9 1
from astropy.utils.data import get_pkg_data_contents
10 1
from astropy.utils.exceptions import AstropyUserWarning
11

12 1
from . import conf
13 1
from ..utils.mocks import MockResponse
14

15 1
__all__ = ['get_field_info', 'photoobj_defs', 'specobj_defs', 'crossid_defs']
16

17
# Default photometric and spectroscopic quantities to retrieve.
18 1
photoobj_defs = ['ra', 'dec', 'objid', 'run', 'rerun', 'camcol', 'field']
19 1
specobj_defs = ['z', 'plate', 'mjd', 'fiberID', 'specobjid', 'run2d',
20
                'instrument']
21 1
crossid_defs = ['ra', 'dec', 'psfMag_u', 'psfMagerr_u', 'psfMag_g',
22
                'psfMagerr_g', 'psfMag_r', 'psfMagerr_r', 'psfMag_i',
23
                'psfMagerr_i', 'psfMag_z', 'psfMagerr_z']
24

25

26 1
_cached_table_fields = {}
27

28

29 1
def get_field_info(cls, tablename, sqlurl, timeout=conf.timeout):
30 1
    key = (tablename, sqlurl)
31
    # Figure out the DR from the url
32 1
    data_release = int(sqlurl.split('/dr')[1].split('/')[0])
33
    # Empty tables could be cached when running local mock tests, those should
34
    # always be discarded
35 1
    if key not in _cached_table_fields or not _cached_table_fields[key]:
36 1
        request_payload = {'cmd': ("select * from dbo.fDocColumns('{0}')"
37
                                   .format(tablename)),
38
                           'format': 'json'}
39 1
        if data_release > 11:
40 1
            request_payload['searchtool'] = 'SQL'
41

42 1
        qryres = cls._request("GET", sqlurl, params=request_payload,
43
                              timeout=timeout)
44
        # we're compelled to use JSON because CSV responses are broken in
45
        # SDSS - sometimes there are improperly nested " characters.
46 1
        try:
47 1
            _cached_table_fields[key] = _columns_json_to_table(qryres.json())
48 1
        except ValueError:
49 1
            if isinstance(qryres, MockResponse):
50 1
                return _load_builtin_table_fields()[tablename]
51

52
            else:
53 0
                warnings.warn("Field info are not available for this data release",
54
                              AstropyUserWarning)
55 0
                _cached_table_fields[key] = Table(names=('name',))
56 0
    return _cached_table_fields[key]
57

58

59 1
def _columns_json_to_table(jsonobj):
60 1
    rows = jsonobj[0]['Rows']
61 1
    columns = dict([(nm, []) for nm in rows[0].keys()])
62

63 1
    for row in rows:
64 1
        for k, v in row.items():
65 1
            columns[k].append(v)
66

67 1
    return Table(columns)
68

69

70
# below here are builtin data files
71 1
def _load_builtin_table_fields():
72 1
    _cached_table_fields['PhotoObjAll'] = _columns_json_to_table(
73
        json.loads(get_pkg_data_contents('data/PhotoObjAll_dr12.json')))
74
    # PhotoObj and PhotoObjAll are the same in DR12
75 1
    _cached_table_fields['PhotoObj'] = _cached_table_fields['PhotoObjAll']
76

77 1
    _cached_table_fields['SpecObjAll'] = _columns_json_to_table(
78
        json.loads(get_pkg_data_contents('data/SpecObjAll_dr12.json')))
79
    # SpecObj and SpecObjAll are the same in DR12
80 1
    _cached_table_fields['SpecObj'] = _cached_table_fields['SpecObjAll']
81

82 1
    return _cached_table_fields

Read our documentation on viewing source code .

Loading