Skip to content

Commit 259daed

Browse files
authored
Merge pull request #2087 from keflavich/xmm_newton_download_refactor
Refactor xmm_newton download to use astroquery tools
2 parents 7609419 + 1a9fd22 commit 259daed

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

CHANGES.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ esa.xmm_newton
2323
^^^^^^^^^^^^^^
2424

2525
- Bug fixes. Fixed the generation of files with wrong extension. [#2017]
26+
- Use astroquery downloader tool to get progressbar, caching, and prevent
27+
memory leaks [#2087]
2628

2729
gaia
2830
^^^^
@@ -49,7 +51,6 @@ atomic
4951
- Change URL to https [#2088]
5052

5153

52-
5354
0.4.2 (2021-05-14)
5455
==================
5556

astroquery/esa/xmm_newton/core.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from . import conf
2525
from astroquery import log
2626
from astropy.coordinates import SkyCoord
27+
from ...exceptions import LoginError
2728

2829

2930
__all__ = ['XMMNewton', 'XMMNewtonClass']
@@ -47,7 +48,7 @@ def __init__(self, tap_handler=None):
4748
self._rmf_ftp = str("http://sasdev-xmm.esac.esa.int/pub/ccf/constituents/extras/responses/")
4849

4950
def download_data(self, observation_id, *, filename=None, verbose=False,
50-
**kwargs):
51+
cache=True, **kwargs):
5152
"""
5253
Download data from XMM-Newton
5354
@@ -110,10 +111,17 @@ def download_data(self, observation_id, *, filename=None, verbose=False,
110111
if verbose:
111112
log.info(link)
112113

113-
response = self._request('GET', link, save=False, cache=True)
114+
# we can cache this HEAD request - the _download_file one will check
115+
# the file size and will never cache
116+
response = self._request('HEAD', link, save=False, cache=cache)
114117

115118
# Get original extension
116-
_, params = cgi.parse_header(response.headers['Content-Disposition'])
119+
if 'Content-Type' in response.headers and 'text' not in response.headers['Content-Type']:
120+
_, params = cgi.parse_header(response.headers['Content-Disposition'])
121+
else:
122+
error = "Data protected by proprietary rights. Please check your credentials"
123+
raise LoginError(error)
124+
117125
r_filename = params["filename"]
118126
suffixes = Path(r_filename).suffixes
119127

@@ -122,9 +130,7 @@ def download_data(self, observation_id, *, filename=None, verbose=False,
122130

123131
filename += "".join(suffixes)
124132

125-
log.info("Copying file to {0}...".format(filename))
126-
with open(filename, 'wb') as f:
127-
f.write(response.content)
133+
self._download_file(link, filename, head_safe=True, cache=cache)
128134

129135
if verbose:
130136
log.info("Wrote {0} to {1}".format(link, filename))

0 commit comments

Comments
 (0)