Skip to content

Commit ec0d89e

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 1a659ec + 8540277 commit ec0d89e

File tree

6 files changed

+105
-97
lines changed

6 files changed

+105
-97
lines changed

astroquery/alma/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Conf(_config.ConfigNamespace):
1616
'http://almascience.eso.org',
1717
'http://almascience.nrao.edu',
1818
'http://almascience.nao.ac.jp',
19-
'http://beta.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/aq', # the beta server (for testing)
19+
'http://beta.cadc-ccda.hia-iha.nrc-cnrc.gc.ca', # the beta server (for testing)
2020
],
2121
'The ALMA Archive mirror to use')
2222

astroquery/alma/core.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import re
1212
import tarfile
1313
import string
14+
import requests
1415
from pkg_resources import resource_filename
1516
from bs4 import BeautifulSoup
1617

@@ -545,7 +546,7 @@ def get_files_from_tarballs(self, downloaded_files, regex='.*\.fits$',
545546
log.info("Extracting {0} to {1}".format(member.name,
546547
path))
547548
tf.extract(member, path)
548-
filelist.append(urljoin(path, member.name))
549+
filelist.append(os.path.join(path, member.name))
549550

550551
return filelist
551552

@@ -596,8 +597,16 @@ def download_and_extract_files(self, urls, delete=True, regex='.*\.fits$',
596597
log.info("ASDM tarballs do not contain FITS files; skipping.")
597598
continue
598599

599-
tarball_name = self._request('GET', url, save=True,
600-
timeout=self.TIMEOUT)
600+
try:
601+
tarball_name = self._request('GET', url, save=True,
602+
timeout=self.TIMEOUT)
603+
except requests.ConnectionError as ex:
604+
self.partial_file_list = all_files
605+
log.error("There was an error downloading the file. "
606+
"A partially completed download list is "
607+
"in Alma.partial_file_list")
608+
raise ex
609+
601610
fitsfilelist = self.get_files_from_tarballs([tarball_name],
602611
regex=regex, path=path,
603612
verbose=verbose)

astroquery/alma/tests/test_alma_remote.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
@remote_data
1414
class TestAlma:
15+
16+
def setup_class(cls):
17+
Alma.archive_url = 'http://beta.cadc-ccda.hia-iha.nrc-cnrc.gc.ca'
18+
1519
@pytest.fixture()
1620
def temp_dir(self, request):
1721
my_temp_dir = tempfile.mkdtemp()

astroquery/splatalogue/utils.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,22 @@
55
import numpy as np
66
import astropy
77

8-
column_headings_map = {'Log<sub>10</sub> (A<sub>ij</sub>)': 'log10(Aij)',
9-
'E_U (K)': 'EU_K',
10-
'Resolved QNs': 'QNs'}
8+
# Remap column headings to something IPAC-compatible
9+
column_headings_map = {'Log<sub>10</sub> (A<sub>ij</sub>)': 'log10_Aij',
10+
'Resolved QNs': 'QNs',
11+
'CDMS/JPL Intensity':'CDMSJPL_Intensity',
12+
'S<sub>ij</sub>':'Sij',
13+
'Freq-GHz':'FreqGHz',
14+
'Meas Freq-GHz':'MeasFreqGHz',
15+
'Lovas/AST Intensity':'LovasAST_Intensity',
16+
'E_L (cm^-1)':'EL_percm',
17+
'E_L (K)':'EL_K',
18+
'E_U (cm^-1)':'EU_percm',
19+
'E_U (K)':'EU_K',
20+
'Chemical Name':'ChemicalName',
21+
'Freq Err':'FreqErr',
22+
'Meas Freq Err':'MeasFreqErr',
23+
}
1124

1225

1326
def clean_column_headings(table, renaming_dict=column_headings_map):

astroquery/template_module/core.py

Lines changed: 63 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,58 @@
1313

1414
# 3. local imports - use relative imports
1515
# commonly required local imports shown below as example
16-
from ..query import BaseQuery # all Query classes should inherit from this.
17-
from ..utils import commons # has common functions required by most modules
18-
from ..utils import prepend_docstr_noreturns # automatically generate docs for similar functions
19-
from ..utils import async_to_sync # all class methods must be callable as static as well as instance methods.
20-
from . import conf # import configurable items declared in __init__.py
16+
# all Query classes should inherit from BaseQuery.
17+
from ..query import BaseQuery
18+
# has common functions required by most modules
19+
from ..utils import commons
20+
# prepend_docstr is a way to copy docstrings between methods
21+
from ..utils import prepend_docstr_noreturns
22+
# async_to_sync generates the relevant query tools from _async methods
23+
from ..utils import async_to_sync
24+
# import configurable items declared in __init__.py
25+
from . import conf
2126

2227

2328
# export all the public classes and methods
24-
__all__ = ['Dummy', 'DummyClass']
29+
__all__ = ['Template', 'TemplateClass']
2530

2631
# declare global variables and constants if any
2732

2833

2934
# Now begin your main class
3035
# should be decorated with the async_to_sync imported previously
3136
@async_to_sync
32-
class DummyClass(BaseQuery):
37+
class TemplateClass(BaseQuery):
3338

3439
"""
35-
Not all the methods below are necessary but these cover most of the common cases, new methods may be added if necessary, follow the guidelines at <http://astroquery.readthedocs.org/en/latest/api.html>
40+
Not all the methods below are necessary but these cover most of the common
41+
cases, new methods may be added if necessary, follow the guidelines at
42+
<http://astroquery.readthedocs.org/en/latest/api.html>
3643
"""
37-
# use the Configuration Items imported from __init__.py to set the URL, TIMEOUT, etc.
44+
# use the Configuration Items imported from __init__.py to set the URL,
45+
# TIMEOUT, etc.
3846
URL = conf.server
3947
TIMEOUT = conf.timeout
4048

41-
def query_object(self, object_name, get_query_payload=False, verbose=False):
49+
# all query methods are implemented with an "async" method that handles
50+
# making the actual HTTP request and returns the raw HTTP response, which
51+
# should be parsed by a separate _parse_result method. The query_object
52+
# method is created by async_to_sync automatically. It would look like
53+
# this:
54+
"""
55+
def query_object(object_name, get_query_payload=False)
56+
response = self.query_object_async(object_name,
57+
get_query_payload=get_query_payload)
58+
if get_query_payload:
59+
return response
60+
result = self._parse_result(response, verbose=verbose)
61+
return result
62+
"""
63+
64+
def query_object_async(self, object_name, get_query_payload=False):
4265
"""
4366
This method is for services that can parse object names. Otherwise
44-
use :meth:`astroquery.template_module.DummyClass.query_region`.
67+
use :meth:`astroquery.template_module.TemplateClass.query_region`.
4568
Put a brief description of what the class does here.
4669
4770
Parameters
@@ -59,52 +82,16 @@ def query_object(self, object_name, get_query_payload=False, verbose=False):
5982
6083
Returns
6184
-------
62-
result : `~astropy.table.Table`
63-
The result of the query as a `~astropy.table.Table` object.
64-
All queries other than image queries should typically return
65-
results like this.
85+
response : `requests.Response`
86+
The HTTP response returned from the service.
87+
All async methods should return the raw HTTP response.
6688
6789
Examples
6890
--------
6991
While this section is optional you may put in some examples that
7092
show how to use the method. The examples are written similar to
7193
standard doctests in python.
72-
"""
7394
74-
# typically query_object should have the following steps:
75-
# 1. call the corresponding query_object_async method, and
76-
# get the HTTP response of the query
77-
# 2. check if 'get_query_payload' is set to True, then
78-
# simply return the dict of HTTP request parameters.
79-
# 3. otherwise call the parse_result method on the
80-
# HTTP response returned by query_object_async and
81-
# return the result parsed as astropy.Table
82-
# These steps are filled in below, but may be replaced
83-
# or modified as required.
84-
85-
response = self.query_object_async(object_name, get_query_payload=get_query_payload)
86-
if get_query_payload:
87-
return response
88-
result = self._parse_result(response, verbose=verbose)
89-
return result
90-
91-
# all query methods usually have a corresponding async method
92-
# that handles making the actual HTTP request and returns the
93-
# raw HTTP response, which should be parsed by a separate
94-
# parse_result method. Since these async counterparts take in
95-
# the same parameters as the corresponding query methods, but
96-
# differ only in the return value, they should be decorated with
97-
# prepend_docstr_noreturns which will automatically generate
98-
# the common docs. See below for an example.
99-
100-
@prepend_docstr_noreturns(query_object.__doc__)
101-
def query_object_async(self, object_name, get_query_payload=False):
102-
"""
103-
Returns
104-
-------
105-
response : `requests.Response`
106-
The HTTP response returned from the service.
107-
All async methods should return the raw HTTP response.
10895
"""
10996
# the async method should typically have the following steps:
11097
# 1. First construct the dictionary of the HTTP request params.
@@ -132,24 +119,25 @@ def query_object_async(self, object_name, get_query_payload=False):
132119

133120
if get_query_payload:
134121
return request_payload
135-
# commons.send_request takes 4 parameters - the URL to query, the dict of
136-
# HTTP request parameters we constructed above, the TIMEOUT which we imported
137-
# from __init__.py and the type of HTTP request - either 'GET' or 'POST', which
138-
# defaults to 'GET'.
139-
response = commons.send_request(self.URL,
140-
request_payload,
141-
self.TIMEOUT,
142-
request_type='GET')
122+
# BaseQuery classes come with a _request method that includes a
123+
# built-in caching system
124+
response = self._request('GET', self.URL, params=request_payload,
125+
timeout=self.TIMEOUT, cache=cache)
143126
return response
144127

145128
# For services that can query coordinates, use the query_region method.
146129
# The pattern is similar to the query_object method. The query_region
147130
# method also has a 'radius' keyword for specifying the radius around
148131
# the coordinates in which to search. If the region is a box, then
149132
# the keywords 'width' and 'height' should be used instead. The coordinates
150-
# may be accepted as an `astropy.coordinates` object or as a
151-
# string, which may be further parsed.
152-
def query_region(self, coordinates, radius, width, height, get_query_payload=False, verbose=False):
133+
# may be accepted as an `astropy.coordinates` object or as a string, which
134+
# may be further parsed.
135+
136+
# similarly we write a query_region_async method that makes the
137+
# actual HTTP request and returns the HTTP response
138+
139+
def query_region_async(self, coordinates, radius, height, width,
140+
get_query_payload=False, cache=True):
153141
"""
154142
Queries a region around the specified coordinates.
155143
@@ -168,40 +156,18 @@ def query_region(self, coordinates, radius, width, height, get_query_payload=Fal
168156
verbose : bool, optional
169157
Display VOTable warnings or not.
170158
171-
Returns
172-
-------
173-
result : `~astropy.table.Table`
174-
The result of the query as a `~astropy.table.Table` object.
175-
All queries other than image queries should typically return
176-
results like this.
177-
"""
178-
# the documentation and code are similar to the steps outlined in the
179-
# `query_object` method, but a rough sketch is provided below
180-
181-
response = self.query_region_async(coordinates, radius, height, width,
182-
get_query_payload=get_query_payload)
183-
result = self._parse_result(response, verbose=verbose)
184-
return result
185-
186-
# similarly we write a query_region_async method that makes the
187-
# actual HTTP request and returns the HTTP response
188-
189-
@prepend_docstr_noreturns(query_region.__doc__)
190-
def query_region_async(self, coordinates, radius, height, width, get_query_payload=False):
191-
"""
192159
Returns
193160
-------
194161
response : `requests.Response`
195162
The HTTP response returned from the service.
196163
All async methods should return the raw HTTP response.
197164
"""
198-
request_payload = self._args_to_payload(coordinates, radius, height, width)
165+
request_payload = self._args_to_payload(coordinates, radius, height,
166+
width)
199167
if get_query_payload:
200168
return request_payload
201-
response = commons.send_request(self.URL,
202-
request_payload,
203-
self.TIMEOUT,
204-
request_type='GET')
169+
response = self._request('GET', self.URL, params=request_payload,
170+
timeout=self.TIMEOUT, cache=cache)
205171
return response
206172

207173
# as we mentioned earlier use various python regular expressions, etc
@@ -233,7 +199,12 @@ def _parse_result(self, response, verbose=False):
233199
# return raw result/ handle in some way
234200
pass
235201

236-
# for image queries, the results should be returned as a
202+
# Image queries do not use the async_to_sync approach: the "synchronous"
203+
# version must be defined explicitly. The example below therefore presents
204+
# a complete example of how to write your own synchronous query tools if
205+
# you prefer to avoid the automatic approach.
206+
#
207+
# For image queries, the results should be returned as a
237208
# list of `astropy.fits.HDUList` objects. Typically image queries
238209
# have the following method family:
239210
# 1. get_images - this is the high level method that interacts with
@@ -328,7 +299,9 @@ def get_image_list(self, coordinates, radius, get_query_payload=False):
328299

329300
def extract_image_urls(self, html_str):
330301
"""
331-
Helper function that uses regex to extract the image urls from the given HTML.
302+
Helper function that uses regex to extract the image urls from the
303+
given HTML.
304+
332305
Parameters
333306
----------
334307
html_str : str
@@ -343,7 +316,7 @@ def extract_image_urls(self, html_str):
343316
pass
344317

345318
# the default tool for users to interact with is an instance of the Class
346-
Dummy = DummyClass()
319+
Template = TemplateClass()
347320

348321
# once your class is done, tests should be written
349322
# See ./tests for examples on this

docs/alma/alma.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
ALMA Queries (`astroquery.alma`)
77
********************************
88

9+
Example Notebooks
10+
=================
11+
A series of example notebooks can be found here:
12+
13+
* `What has ALMA observed toward all Messier objects? (an example of querying many sources) <http://nbviewer.ipython.org/gist/keflavich/e798e10e3bf9a93d1453>`_
14+
* `ALMA finder chart of the Cartwheel galaxy and public Cycle 1 data quicklooks <http://nbviewer.ipython.org/gist/keflavich/d5af22578094853e2d24>`_
15+
* `Finder charts toward many sources with different backgrounds <http://nbviewer.ipython.org/gist/keflavich/2ef877ec90d774645fee>`_
16+
* `Finder chart and downloaded data from Cycle 0 observations of Sombrero Galaxy <http://nbviewer.ipython.org/gist/keflavich/9934c9412d8f58299962>`_
17+
918
Getting started
1019
===============
1120

0 commit comments

Comments
 (0)