Skip to content

Commit af315b4

Browse files
committed
Merge pull request #601 from keflavich/alma-private-fix
BUGFIX: ALMA private queries, plus checking that queries succeed
2 parents 89da133 + c2e172c commit af315b4

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

astroquery/alma/core.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,20 @@ def stage_data(self, uids):
293293
summary.raise_for_status()
294294
self._staging_log['json_data'] = json_data = summary.json()
295295

296+
username = self._username if hasattr(self, '_username') else 'anonymous'
297+
298+
# templates:
299+
# https://almascience.eso.org/dataPortal/requests/keflavich/946895898/ALMA/
300+
# 2013.1.00308.S_uid___A001_X196_X93_001_of_001.tar/2013.1.00308.S_uid___A001_X196_X93_001_of_001.tar
301+
# uid___A002_X9ee74a_X26f0/2013.1.00308.S_uid___A002_X9ee74a_X26f0.asdm.sdm.tar
302+
296303
url_decomposed = urlparse(data_page_url)
297304
base_url = ('{uri.scheme}://{uri.netloc}/'
298-
'dataPortal/requests/anonymous/'
305+
'dataPortal/requests/{username}/'
299306
'{staging_page_id}/ALMA'.format(uri=url_decomposed,
300-
staging_page_id=dpid))
307+
staging_page_id=dpid,
308+
username=username,
309+
))
301310
tbl = self._json_summary_to_table(json_data, base_url=base_url)
302311

303312
# staging_root = BeautifulSoup(data_page.content)
@@ -353,7 +362,7 @@ def download_files(self, files, cache=True):
353362
downloaded_files = []
354363
for fileLink in unique(files):
355364
filename = self._request("GET", fileLink, save=True,
356-
timeout=self.TIMEOUT)
365+
timeout=self.TIMEOUT, cache=cache)
357366
downloaded_files.append(filename)
358367
return downloaded_files
359368

@@ -444,6 +453,7 @@ def _login(self, username, store_password=False):
444453

445454
if authenticated:
446455
log.info("Authentication successful!")
456+
self._username = username
447457
else:
448458
log.exception("Authentication failed!")
449459
# When authenticated, save password in keyring if needed
@@ -882,18 +892,27 @@ def _json_summary_to_table(self, data, base_url):
882892
"""
883893
columns = {'uid':[], 'URL':[], 'size':[]}
884894
for entry in data['node_data']:
885-
if entry['file_name'] != 'null':
895+
is_file = (entry['de_type'] == 'MOUS'
896+
or (entry['file_name'] != 'null'
897+
and entry['file_key'] != 'null'))
898+
if is_file:
886899
# "de_name": "ALMA+uid://A001/X122/X35e",
887900
columns['uid'].append(entry['de_name'][5:])
888901
columns['size'].append((int(entry['file_size'])*u.B).to(u.Gbyte))
889902
# example template for constructing url:
890903
# https://almascience.eso.org/dataPortal/requests/keflavich/940238268/ALMA/
891904
# uid___A002_X9d6f4c_X154/2013.1.00546.S_uid___A002_X9d6f4c_X154.asdm.sdm.tar
892-
# above is WRONG
905+
# above is WRONG... except for ASDMs, when it's right
893906
# should be:
894907
# 2013.1.00546.S_uid___A002_X9d6f4c_X154.asdm.sdm.tar/2013.1.00546.S_uid___A002_X9d6f4c_X154.asdm.sdm.tar
908+
#
909+
# apparently ASDMs are different from others:
910+
# templates:
911+
# https://almascience.eso.org/dataPortal/requests/keflavich/946895898/ALMA/
912+
# 2013.1.00308.S_uid___A001_X196_X93_001_of_001.tar/2013.1.00308.S_uid___A001_X196_X93_001_of_001.tar
913+
# uid___A002_X9ee74a_X26f0/2013.1.00308.S_uid___A002_X9ee74a_X26f0.asdm.sdm.tar
895914
url = os.path.join(base_url,
896-
entry['file_name'],
915+
entry['file_key'],
897916
entry['file_name'],
898917
)
899918
columns['URL'].append(url)

astroquery/alma/tests/test_alma_remote.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ def test_stage_data(self, temp_dir):
7070
assert b'2011.0.00217.S' in result_s['Project code']
7171
uid = result_s['Asdm uid'][0]
7272

73-
alma.stage_data([uid])
73+
result = alma.stage_data([uid])
74+
75+
assert os.path.split(result['URL'][0])[1] == 'uid___A002_X47ed8e_X6c.asdm.sdm.tar'
7476

7577
def test_doc_example(self, temp_dir):
7678
alma = Alma()

astroquery/query.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ def _request(self, method, url, params=None, data=None, headers=None,
152152
local_filename = url.split('/')[-1]
153153
local_filepath = os.path.join(self.cache_location or savedir or
154154
'.', local_filename)
155-
log.info("Downloading {0}...".format(local_filename))
155+
# REDUNDANT: spinner has this log.info("Downloading {0}...".format(local_filename))
156156
self._download_file(url, local_filepath, timeout=timeout,
157-
auth=auth)
157+
auth=auth, cache=cache)
158158
return local_filepath
159159
else:
160160
query = AstroQuery(method, url, params=params, data=data,
@@ -174,24 +174,45 @@ def _request(self, method, url, params=None, data=None, headers=None,
174174
to_cache(response, query.request_file(self.cache_location))
175175
return response
176176

177-
def _download_file(self, url, local_filepath, timeout=None, auth=None):
177+
def _download_file(self, url, local_filepath, timeout=None, auth=None, cache=False):
178178
"""
179179
Download a file. Resembles `astropy.utils.data.download_file` but uses
180180
the local ``_session``
181181
"""
182182
response = self._session.get(url, timeout=timeout, stream=True,
183-
auth=auth)
183+
auth=auth)
184+
response.raise_for_status()
184185
if 'content-length' in response.headers:
185186
length = int(response.headers['content-length'])
186187
else:
187188
length = None
188189

190+
if cache and os.path.exists(local_filepath):
191+
if length is not None:
192+
statinfo = os.stat(local_filepath)
193+
if statinfo.st_size != length:
194+
log.warn("Found cached file {0} with size {1} that is "
195+
"different from expected size {2}"
196+
.format(local_filepath,
197+
statinfo.st_size,
198+
length))
199+
else:
200+
log.info("Found cached file {0} with expected size {1}."
201+
.format(local_filepath, statinfo.st_size))
202+
response.close()
203+
return
204+
else:
205+
log.info("Found cached file {0}.".format(local_filepath))
206+
response.close()
207+
return
208+
189209
blocksize = astropy.utils.data.conf.download_block_size
190210

191211
bytes_read = 0
192212

193213
with ProgressBarOrSpinner(length,
194-
'Downloading URL {0} ...'.format(url)) as pb:
214+
'Downloading URL {0} to {1} ...'.format(url,
215+
local_filepath)) as pb:
195216
with open(local_filepath, 'wb') as f:
196217
for block in response.iter_content(blocksize):
197218
f.write(block)

0 commit comments

Comments
 (0)