14
14
import astropy .units
15
15
import astropy .io .votable as votable
16
16
from requests import HTTPError
17
+ from requests import ConnectionError
17
18
18
19
from ..query import BaseQuery
19
20
from ..utils import commons
@@ -131,8 +132,8 @@ def query_object_maps(self, position, missions=__ALL_STRING,
131
132
and observations available for the chosen missions and object.
132
133
It is structured in a TableList like this:
133
134
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)
136
137
137
138
Examples
138
139
--------
@@ -184,8 +185,8 @@ def query_object_catalogs(self, position, catalogs=__ALL_STRING,
184
185
of the catalogs available for the chosen mission and object.
185
186
It is structured in a TableList like this:
186
187
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)
189
190
190
191
Examples
191
192
--------
@@ -237,8 +238,8 @@ def query_object_spectra(self, position, missions=__ALL_STRING,
237
238
and spectra available for the chosen missions and object.
238
239
It is structured in a TableList like this:
239
240
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)
242
243
243
244
Examples
244
245
--------
@@ -291,8 +292,8 @@ def query_region_maps(self, position, radius, missions=__ALL_STRING,
291
292
and observations available for the chosen missions and region.
292
293
It is structured in a TableList like this:
293
294
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)
296
297
297
298
Examples
298
299
--------
@@ -358,8 +359,8 @@ def query_region_catalogs(self, position, radius, catalogs=__ALL_STRING,
358
359
the catalogs available for the chosen mission and region.
359
360
It is structured in a TableList like this:
360
361
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)
363
364
364
365
Examples
365
366
--------
@@ -426,8 +427,8 @@ def query_region_spectra(self, position, radius, missions=__ALL_STRING,
426
427
and observations available for the chosen missions and region.
427
428
It is structured in a TableList like this:
428
429
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)
431
432
432
433
Examples
433
434
--------
@@ -643,8 +644,7 @@ def get_spectra(self, position, radius=__ZERO_ARCMIN_STRING,
643
644
The response is structured in a dictionary like this:
644
645
dict: {
645
646
'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}, ...},
648
648
'HST-IR':[HDUList, HDUList, HDUList, HDUList, HDUList, ...],
649
649
'XMM-NEWTON' : [HDUList, HDUList, HDUList, HDUList, ...]
650
650
...
@@ -717,8 +717,7 @@ def get_spectra_from_table(self, query_table_list, missions=__ALL_STRING,
717
717
The response is structured in a dictionary like this:
718
718
dict: {
719
719
'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}, ...},
722
721
'HST-IR':[HDUList, HDUList, HDUList, HDUList, HDUList, ...],
723
722
'XMM-NEWTON' : [HDUList, HDUList, HDUList, HDUList, ...]
724
723
...
@@ -877,24 +876,31 @@ def _get_maps_for_mission(self, maps_table, mission, download_dir, cache, json,
877
876
maps .append (None )
878
877
879
878
else :
880
- response = self ._request (
881
- 'GET' ,
882
- product_url ,
883
- cache = cache ,
884
- headers = self ._get_header ())
885
-
886
879
try :
880
+ response = self ._request (
881
+ 'GET' ,
882
+ product_url ,
883
+ cache = cache ,
884
+ stream = True ,
885
+ headers = self ._get_header ())
886
+
887
887
response .raise_for_status ()
888
888
889
889
file_name = self ._extract_file_name_from_response_header (response .headers )
890
890
if (file_name == "" ):
891
891
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 :
898
904
log .error ("Download failed with {}." .format (err ))
899
905
maps .append (None )
900
906
@@ -912,7 +918,7 @@ def _get_maps_for_mission(self, maps_table, mission, download_dir, cache, json,
912
918
def _get_herschel_map (self , product_url , directory_path , cache ):
913
919
observation = dict ()
914
920
response = self ._request ('GET' , product_url , cache = cache ,
915
- headers = self ._get_header ())
921
+ stream = True , headers = self ._get_header ())
916
922
response .raise_for_status ()
917
923
918
924
with tarfile .open (fileobj = BytesIO (response .content )) as tar :
@@ -929,7 +935,7 @@ def _get_herschel_map(self, product_url, directory_path, cache):
929
935
def _get_herschel_spectra (self , product_url , directory_path , cache ):
930
936
spectra = dict ()
931
937
response = self ._request ('GET' , product_url , cache = cache ,
932
- headers = self ._get_header ())
938
+ stream = True , headers = self ._get_header ())
933
939
934
940
response .raise_for_status ()
935
941
@@ -999,7 +1005,11 @@ def _extract_file_name_from_response_header(self, headers):
999
1005
if (content_disposition [start_index ] == '\" ' ):
1000
1006
start_index += 1
1001
1007
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 ()):
1003
1013
end_index = (
1004
1014
content_disposition .lower ().index (self .__FITS_STRING , start_index + 1 ) +
1005
1015
len (self .__FITS_STRING ))
@@ -1009,6 +1019,10 @@ def _extract_file_name_from_response_header(self, headers):
1009
1019
content_disposition .upper ().index (self .__FTZ_STRING , start_index + 1 ) +
1010
1020
len (self .__FTZ_STRING ))
1011
1021
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 ]
1012
1026
elif (self .__TAR_STRING in content_disposition [start_index :].lower ()):
1013
1027
end_index = (
1014
1028
content_disposition .lower ().index (self .__TAR_STRING , start_index + 1 ) +
0 commit comments