Skip to content

Commit 9c238f1

Browse files
authored
Merge pull request #1926 from imbasimba/esasky-test-and-doc
ESASky test updates
2 parents cde199c + c19306e commit 9c238f1

File tree

6 files changed

+148
-1780
lines changed

6 files changed

+148
-1780
lines changed

astroquery/esasky/core.py

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import astropy.units
1515
import astropy.io.votable as votable
1616
from requests import HTTPError
17+
from requests import ConnectionError
1718

1819
from ..query import BaseQuery
1920
from ..utils import commons
@@ -131,8 +132,8 @@ def query_object_maps(self, position, missions=__ALL_STRING,
131132
and observations available for the chosen missions and object.
132133
It is structured in a TableList like this:
133134
TableList with 2 tables:
134-
'0:HERSCHEL' with 12 column(s) and 152 row(s)
135-
'1:HST-OPTICAL' with 12 column(s) and 6 row(s)
135+
'0:HERSCHEL' with 12 column(s) and 152 row(s)
136+
'1:HST-OPTICAL' with 12 column(s) and 6 row(s)
136137
137138
Examples
138139
--------
@@ -184,8 +185,8 @@ def query_object_catalogs(self, position, catalogs=__ALL_STRING,
184185
of the catalogs available for the chosen mission and object.
185186
It is structured in a TableList like this:
186187
TableList with 2 tables:
187-
'0:HSC' with 9 column(s) and 232 row(s)
188-
'1:XMM-OM' with 11 column(s) and 2 row(s)
188+
'0:HSC' with 9 column(s) and 232 row(s)
189+
'1:XMM-OM' with 11 column(s) and 2 row(s)
189190
190191
Examples
191192
--------
@@ -237,8 +238,8 @@ def query_object_spectra(self, position, missions=__ALL_STRING,
237238
and spectra available for the chosen missions and object.
238239
It is structured in a TableList like this:
239240
TableList with 2 tables:
240-
'0:HERSCHEL' with 12 column(s) and 12 row(s)
241-
'1:HST-OPTICAL' with 12 column(s) and 19 row(s)
241+
'0:HERSCHEL' with 12 column(s) and 12 row(s)
242+
'1:HST-OPTICAL' with 12 column(s) and 19 row(s)
242243
243244
Examples
244245
--------
@@ -291,8 +292,8 @@ def query_region_maps(self, position, radius, missions=__ALL_STRING,
291292
and observations available for the chosen missions and region.
292293
It is structured in a TableList like this:
293294
TableList with 2 tables:
294-
'0:HERSCHEL' with 12 column(s) and 152 row(s)
295-
'1:HST-OPTICAL' with 12 column(s) and 71 row(s)
295+
'0:HERSCHEL' with 12 column(s) and 152 row(s)
296+
'1:HST-OPTICAL' with 12 column(s) and 71 row(s)
296297
297298
Examples
298299
--------
@@ -358,8 +359,8 @@ def query_region_catalogs(self, position, radius, catalogs=__ALL_STRING,
358359
the catalogs available for the chosen mission and region.
359360
It is structured in a TableList like this:
360361
TableList with 2 tables:
361-
'0:HIPPARCOS-2' with 7 column(s) and 2 row(s)
362-
'1:HSC' with 9 column(s) and 10000 row(s)
362+
'0:HIPPARCOS-2' with 7 column(s) and 2 row(s)
363+
'1:HSC' with 9 column(s) and 10000 row(s)
363364
364365
Examples
365366
--------
@@ -426,8 +427,8 @@ def query_region_spectra(self, position, radius, missions=__ALL_STRING,
426427
and observations available for the chosen missions and region.
427428
It is structured in a TableList like this:
428429
TableList with 2 tables:
429-
'0:HERSCHEL' with 12 column(s) and 264 row(s)
430-
'1:IUE' with 12 column(s) and 14 row(s)
430+
'0:HERSCHEL' with 12 column(s) and 264 row(s)
431+
'1:IUE' with 12 column(s) and 14 row(s)
431432
432433
Examples
433434
--------
@@ -643,8 +644,7 @@ def get_spectra(self, position, radius=__ZERO_ARCMIN_STRING,
643644
The response is structured in a dictionary like this:
644645
dict: {
645646
'HERSCHEL': {'1342211195': {'red' : {'HPSTBRRS' : HDUList}, 'blue' : {'HPSTBRBS': HDUList},
646-
'1342180796': {'WBS' : {'WBS-H_LSB_5a' : HDUList}, 'HRS' : {'HRS-H_LSB_5a': HDUList},
647-
...},
647+
'1342180796': {'WBS' : {'WBS-H_LSB_5a' : HDUList}, 'HRS' : {'HRS-H_LSB_5a': HDUList}, ...},
648648
'HST-IR':[HDUList, HDUList, HDUList, HDUList, HDUList, ...],
649649
'XMM-NEWTON' : [HDUList, HDUList, HDUList, HDUList, ...]
650650
...
@@ -717,8 +717,7 @@ def get_spectra_from_table(self, query_table_list, missions=__ALL_STRING,
717717
The response is structured in a dictionary like this:
718718
dict: {
719719
'HERSCHEL': {'1342211195': {'red' : {'HPSTBRRS' : HDUList}, 'blue' : {'HPSTBRBS': HDUList},
720-
'1342180796': {'WBS' : {'WBS-H_LSB_5a' : HDUList}, 'HRS' : {'HRS-H_LSB_5a': HDUList},
721-
...},
720+
'1342180796': {'WBS' : {'WBS-H_LSB_5a' : HDUList}, 'HRS' : {'HRS-H_LSB_5a': HDUList}, ...},
722721
'HST-IR':[HDUList, HDUList, HDUList, HDUList, HDUList, ...],
723722
'XMM-NEWTON' : [HDUList, HDUList, HDUList, HDUList, ...]
724723
...
@@ -877,24 +876,31 @@ def _get_maps_for_mission(self, maps_table, mission, download_dir, cache, json,
877876
maps.append(None)
878877

879878
else:
880-
response = self._request(
881-
'GET',
882-
product_url,
883-
cache=cache,
884-
headers=self._get_header())
885-
886879
try:
880+
response = self._request(
881+
'GET',
882+
product_url,
883+
cache=cache,
884+
stream=True,
885+
headers=self._get_header())
886+
887887
response.raise_for_status()
888888

889889
file_name = self._extract_file_name_from_response_header(response.headers)
890890
if (file_name == ""):
891891
file_name = self._extract_file_name_from_url(product_url)
892-
893-
fits_data = response.content
894-
with open(directory_path + file_name, 'wb') as fits_file:
895-
fits_file.write(fits_data)
896-
maps.append(fits.open(directory_path + file_name))
897-
except HTTPError as err:
892+
if(file_name.lower().endswith(self.__TAR_STRING)):
893+
with tarfile.open(fileobj=BytesIO(response.content)) as tar:
894+
for member in tar.getmembers():
895+
tar.extract(member, directory_path)
896+
maps.append(fits.open(directory_path + member.name))
897+
else:
898+
fits_data = response.content
899+
with open(directory_path + file_name, 'wb') as fits_file:
900+
fits_file.write(fits_data)
901+
fits_file.flush()
902+
maps.append(fits.open(directory_path + file_name))
903+
except (HTTPError, ConnectionError) as err:
898904
log.error("Download failed with {}.".format(err))
899905
maps.append(None)
900906

@@ -912,7 +918,7 @@ def _get_maps_for_mission(self, maps_table, mission, download_dir, cache, json,
912918
def _get_herschel_map(self, product_url, directory_path, cache):
913919
observation = dict()
914920
response = self._request('GET', product_url, cache=cache,
915-
headers=self._get_header())
921+
stream=True, headers=self._get_header())
916922
response.raise_for_status()
917923

918924
with tarfile.open(fileobj=BytesIO(response.content)) as tar:
@@ -929,7 +935,7 @@ def _get_herschel_map(self, product_url, directory_path, cache):
929935
def _get_herschel_spectra(self, product_url, directory_path, cache):
930936
spectra = dict()
931937
response = self._request('GET', product_url, cache=cache,
932-
headers=self._get_header())
938+
stream=True, headers=self._get_header())
933939

934940
response.raise_for_status()
935941

@@ -999,7 +1005,11 @@ def _extract_file_name_from_response_header(self, headers):
9991005
if (content_disposition[start_index] == '\"'):
10001006
start_index += 1
10011007

1002-
if (self.__FITS_STRING in content_disposition[start_index:].lower()):
1008+
if (".gz" in content_disposition[start_index:].lower()):
1009+
end_index = (
1010+
content_disposition.lower().index(".gz", start_index + 1) + len(".gz"))
1011+
return content_disposition[start_index: end_index]
1012+
elif (self.__FITS_STRING in content_disposition[start_index:].lower()):
10031013
end_index = (
10041014
content_disposition.lower().index(self.__FITS_STRING, start_index + 1) +
10051015
len(self.__FITS_STRING))
@@ -1009,6 +1019,10 @@ def _extract_file_name_from_response_header(self, headers):
10091019
content_disposition.upper().index(self.__FTZ_STRING, start_index + 1) +
10101020
len(self.__FTZ_STRING))
10111021
return content_disposition[start_index: end_index]
1022+
elif (".fit" in content_disposition[start_index:].upper()):
1023+
end_index = (
1024+
content_disposition.upper().index(".fit", start_index + 1) + len(".fit"))
1025+
return content_disposition[start_index: end_index]
10121026
elif (self.__TAR_STRING in content_disposition[start_index:].lower()):
10131027
end_index = (
10141028
content_disposition.lower().index(self.__TAR_STRING, start_index + 1) +

0 commit comments

Comments
 (0)