6
6
import sys
7
7
import re
8
8
from io import BytesIO
9
+ from zipfile import ZipFile
9
10
10
11
from astropy .io import fits
11
12
from astroquery import log
@@ -1141,7 +1142,7 @@ def _get_maps_for_mission(self, maps_table, mission, download_dir, cache, json,
1141
1142
url_key = self .__PRODUCT_URL_STRING
1142
1143
if url_key == "" and self .__ACCESS_URL_STRING in maps_table .keys ():
1143
1144
url_key = self .__ACCESS_URL_STRING
1144
- if url_key == "" or mission == "ALMA" or mission == "INTEGRAL" :
1145
+ if url_key == "" or mission == "ALMA" :
1145
1146
log .info (mission + " does not yet support downloading of fits files" )
1146
1147
return maps
1147
1148
@@ -1203,20 +1204,26 @@ def _get_maps_for_mission(self, maps_table, mission, download_dir, cache, json,
1203
1204
1204
1205
response .raise_for_status ()
1205
1206
1206
- file_name = self ._extract_file_name_from_response_header (response .headers )
1207
- if (file_name == "" ):
1208
- file_name = self ._extract_file_name_from_url (product_url )
1209
- if (file_name .lower ().endswith (self .__TAR_STRING )):
1210
- with tarfile .open (fileobj = BytesIO (response .content )) as tar :
1211
- for member in tar .getmembers ():
1212
- tar .extract (member , directory_path )
1213
- maps .append (fits .open (directory_path + member .name ))
1207
+ if mission .lower () == "integral" :
1208
+ with ZipFile (file = BytesIO (response .content )) as zip :
1209
+ for info in zip .infolist ():
1210
+ if self ._ends_with_fits_like_extentsion (info .filename ):
1211
+ maps .append (fits .open (zip .extract (info .filename )))
1214
1212
else :
1215
- fits_data = response .content
1216
- with open (directory_path + file_name , 'wb' ) as fits_file :
1217
- fits_file .write (fits_data )
1218
- fits_file .flush ()
1219
- maps .append (fits .open (directory_path + file_name ))
1213
+ file_name = self ._extract_file_name_from_response_header (response .headers )
1214
+ if (file_name == "" ):
1215
+ file_name = self ._extract_file_name_from_url (product_url )
1216
+ if (file_name .lower ().endswith (self .__TAR_STRING )):
1217
+ with tarfile .open (fileobj = BytesIO (response .content )) as tar :
1218
+ for member in tar .getmembers ():
1219
+ tar .extract (member , directory_path )
1220
+ maps .append (fits .open (directory_path + member .name ))
1221
+ else :
1222
+ fits_data = response .content
1223
+ with open (directory_path + file_name , 'wb' ) as fits_file :
1224
+ fits_file .write (fits_data )
1225
+ fits_file .flush ()
1226
+ maps .append (fits .open (directory_path + file_name ))
1220
1227
log .info ("[Done]" )
1221
1228
except (HTTPError , ConnectionError ) as err :
1222
1229
log .error ("Download failed with {}." .format (err ))
@@ -1230,6 +1237,18 @@ def _get_maps_for_mission(self, maps_table, mission, download_dir, cache, json,
1230
1237
1231
1238
return maps
1232
1239
1240
+ def _ends_with_fits_like_extentsion (self , name ):
1241
+ lower_case_name = name .lower ()
1242
+ return (lower_case_name .endswith ("fits" )
1243
+ or lower_case_name .endswith ("fits.gz" )
1244
+ or lower_case_name .endswith ("ftz" )
1245
+ or lower_case_name .endswith ("ftz.gz" )
1246
+ or lower_case_name .endswith ("fit" )
1247
+ or lower_case_name .endswith ("fit.gz" )
1248
+ or lower_case_name .endswith ("fts" )
1249
+ or lower_case_name .endswith ("fts.gz" )
1250
+ )
1251
+
1233
1252
def _get_herschel_map (self , product_url , directory_path , cache ):
1234
1253
observation = dict ()
1235
1254
response = self ._request ('GET' , product_url , cache = cache ,
0 commit comments