Skip to content

Commit 777172b

Browse files
committed
attempting to avoid unnecessary decoding
1 parent 54392a8 commit 777172b

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

astroquery/heasarc/core.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

33
import warnings
4-
from six import BytesIO
4+
from six import StringIO, BytesIO
55
from astropy.io.fits.column import _AsciiColumnFormat
66
from astropy.table import Table
77
from astropy.io import fits
@@ -62,8 +62,7 @@ def query_mission_list(self, cache=True, get_query_payload=False):
6262
url=conf.server,
6363
cache=cache
6464
)
65-
data = BytesIO(response.content)
66-
data_str = data.read().decode('utf-8')
65+
data_str = response.text
6766
data_str = data_str.replace('Table xxx does not seem to exist!\n\n\n\nAvailable tables:\n', '')
6867
table = Table.read(data_str, format='ascii.fixed_width_two_line',
6968
delimiter='+', header_start=1, position_line=2,
@@ -175,7 +174,7 @@ def query_region_async(self, position, mission, radius,
175174
# Submit the request
176175
return self.query_async(request_payload, cache=cache)
177176

178-
def _old_w3query_fallback(self, content):
177+
def _old_w3query_fallback(self, content: bytes):
179178
# old w3query (such as that used in ISDC) return very strange fits, with all ints
180179

181180
f = fits.open(BytesIO(content))
@@ -193,21 +192,21 @@ def _old_w3query_fallback(self, content):
193192

194193
return Table.read(I)
195194

196-
def _fallback(self, content):
195+
def _fallback(self, text: str):
197196
"""
198197
Blank columns which have to be converted to float or in fail so
199198
lets fix that by replacing with -1's
200199
"""
201200

202-
data = BytesIO(content)
201+
data = StringIO(text)
203202
header = fits.getheader(data, 1) # Get header for column info
204203
colstart = [y for x, y in header.items() if "TBCOL" in x]
205204
collens = [int(float(y[1:]))
206205
for x, y in header.items() if "TFORM" in x]
207206

208207
new_table = []
209208

210-
old_table = content.decode().split("END")[-1].strip()
209+
old_table = text.split("END")[-1].strip()
211210
for line in old_table.split("\n"):
212211
newline = []
213212
for n, tup in enumerate(zip(colstart, collens), start=1):
@@ -220,7 +219,7 @@ def _fallback(self, content):
220219
newline[-1] = "-1".rjust(clen) + " "
221220
new_table.append("".join(newline))
222221

223-
data = BytesIO(content.replace(old_table, "\n".join(new_table)))
222+
data = StringIO(text.replace(old_table, "\n".join(new_table)))
224223
return Table.read(data, hdu=1)
225224

226225
def _parse_result(self, response, verbose=False):
@@ -246,7 +245,7 @@ def _parse_result(self, response, verbose=False):
246245
return table
247246
except ValueError:
248247
try:
249-
return self._fallback(response.content)
248+
return self._fallback(response.text)
250249
except Exception as e:
251250
return self._old_w3query_fallback(response.content)
252251

@@ -299,9 +298,8 @@ def _args_to_payload(self, **kwargs):
299298
"""
300299
# User-facing parameters are lower case, while parameters as passed to the HEASARC service are capitalized according to the HEASARC requirements.
301300
# The necessary transformations are done in this function.
302-
303-
# Define the basic query for this object
304301

302+
# Define the basic query for this object
305303
mission = kwargs.pop('mission')
306304

307305
request_payload = dict(

astroquery/heasarc/tests/test_heasarc_remote_isdc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_compare_time(self):
7979

8080
heasarc = Heasarc()
8181

82-
month_ago = (Time.now() - TimeDelta(30)).isot[:10]
82+
month_ago = (Time.now() - TimeDelta(15)).isot[:10]
8383
today = Time.now().isot[:10]
8484
T = month_ago + " .. " + today
8585

@@ -97,7 +97,8 @@ def Q(mission):
9797

9898
table_heasarc = Q('intscw')
9999

100-
assert len(table_isdc) > len(table_heasarc)
100+
# heasarc synchronizes twice a month, and it might or might not be the same at the request time
101+
assert len(table_isdc) >= len(table_heasarc)
101102

102103
def test_ra_validity(self):
103104
object_name = 'Crab'

0 commit comments

Comments
 (0)