Skip to content

Commit 0959996

Browse files
author
Javier Duran
committed
Changes introduced to comply with astroquery admin recommendations
1 parent e41d456 commit 0959996

File tree

3 files changed

+203
-202
lines changed

3 files changed

+203
-202
lines changed

astroquery/esa/xmm_newton/core.py

Lines changed: 69 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def download_data(self, observation_id, *, filename=None, verbose=False,
100100
If verbose returns the filename
101101
"""
102102
if filename is not None:
103-
filename = filename.split('.')[0]
103+
filename = os.path.splitext(filename)[0]
104104

105105
link = self.data_aio_url + "obsno=" + observation_id
106106

@@ -303,7 +303,8 @@ def _parse_filename(self, filename):
303303

304304
def get_epic_spectra(self, filename, source_number, *,
305305
instrument=[], path="", verbose=False):
306-
"""Extracts the EPIC sources spectral products from a given TAR file
306+
"""Extracts in path (when set) the EPIC sources spectral products from a
307+
given TAR file.
307308
For a given TAR file obtained with:
308309
XMM.download_data(OBS_ID,level="PPS",extension="FTZ",filename=tarfile)
309310
This function extracts the EPIC sources spectral products in a given
@@ -312,17 +313,19 @@ def get_epic_spectra(self, filename, source_number, *,
312313
sources spectral products with key being the instrument
313314
If the instrument is not specified this function will
314315
return all the available instruments
316+
315317
Examples:
316318
Extracting all bands and instruments:
317319
result = XMM.get_epic_spectra(tarfile,83,
318320
instrument=['M1','M2','PN'])
319321
If we want to retrieve the source spectrum of the instrument PN
320322
fits_image = result['PN']
321323
fits_image will be the full path to the extracted FTZ file
324+
322325
Parameters
323326
----------
324327
filename : string, mandatory
325-
The name of the tarfile to be proccessed
328+
The name of the tarfile to be processed
326329
source_number : integer, mandatory
327330
The source number, in decimal, in the observation
328331
instruments : array of strings, optional, default []
@@ -337,6 +340,7 @@ def get_epic_spectra(self, filename, source_number, *,
337340
-------
338341
A dictionary with the full paths of the extracted EPIC sources
339342
spectral products. The key is the instrument
343+
340344
Notes
341345
-----
342346
The filenames will contain the source number in hexadecimal,
@@ -348,20 +352,21 @@ def get_epic_spectra(self, filename, source_number, *,
348352
_instrument = ["M1", "M2", "PN", "EP"]
349353
_product_type = ["SRSPEC", "BGSPEC", "SRCARF"]
350354
_path = ""
351-
ret = {}
355+
ret = None
352356
if instrument == []:
353357
instrument = _instrument
354358
else:
355-
for i in instrument:
356-
if i not in _instrument:
357-
log.warning("Invalid instrument %s" % i)
358-
instrument.remove(i)
359+
for inst in instrument:
360+
if inst not in _instrument:
361+
log.warning(f"Invalid instrument {inst}")
362+
instrument.remove(inst)
359363
if path != "" and os.path.exists(path):
360364
_path = path
361365
try:
362366
with tarfile.open(filename, "r") as tar:
363-
for i in tar.getmembers():
364-
paths = os.path.split(i.name)
367+
ret = {}
368+
for member in tar.getmembers():
369+
paths = os.path.split(member.name)
365370
fname = paths[1]
366371
paths = os.path.split(paths[0])
367372
if paths[1] != "pps":
@@ -375,29 +380,25 @@ def get_epic_spectra(self, filename, source_number, *,
375380
continue
376381
if int(fname_info["X-"], 16) != source_number:
377382
continue
378-
tar.extract(i, _path)
383+
tar.extract(member, _path)
379384
key = fname_info["I"]
380-
value = os.path.abspath(os.path.join(_path, i.name))
385+
path_inst_name = os.path.abspath(os.path.join(_path, member.name))
381386
if fname_info["T"] == "BGSPEC":
382387
key = fname_info["I"] + "_bkg"
383388
elif fname_info["T"] == "SRCARF":
384389
key = fname_info["I"] + "_arf"
385390
else:
386-
with fits.open(value) as hdul:
391+
with fits.open(path_inst_name) as hdul:
387392
for ext in hdul:
388393
if ext.name != "SPECTRUM":
389394
continue
390395
rmf_fname = ext.header["RESPFILE"]
391-
if fname_info["I"] == "M1" or \
392-
fname_info["I"] == "M2":
393-
inst = "MOS/" +\
394-
str(ext.header["SPECDELT"]) + "eV/"
396+
if fname_info["I"] == "M1" or fname_info["I"] == "M2":
397+
inst = "MOS/" + str(ext.header["SPECDELT"]) + "eV/"
395398
elif fname_info["I"] == "PN":
396399
inst = "PN/"
397-
file_name, file_ext =\
398-
os.path.splitext(rmf_fname)
399-
rmf_fname = file_name +\
400-
"_v18.0" + file_ext
400+
file_name, file_ext = os.path.splitext(rmf_fname)
401+
rmf_fname = file_name + "_v18.0" + file_ext
401402

402403
link = self._rmf_ftp + inst + rmf_fname
403404

@@ -406,31 +407,25 @@ def get_epic_spectra(self, filename, source_number, *,
406407

407408
response = self._request('GET', link)
408409

409-
rsp_filename =\
410-
os.path.join(_path,
411-
paths[0],
412-
paths[1],
413-
ext.header["RESPFILE"])
410+
rsp_filename = os.path.join(_path, paths[0], paths[1], ext.header["RESPFILE"])
414411

415412
with open(rsp_filename, 'wb') as f:
416413
f.write(response.content)
417-
ret[fname_info["I"] + "_rmf"] =\
418-
rsp_filename
414+
ret[fname_info["I"] + "_rmf"] = rsp_filename
419415

420416
if ret.get(key) and type(ret.get(key)) == str:
421-
log.warning("More than one file found with the "
422-
"instrument: %s" % key)
423-
ret[key] = [ret[key], value]
417+
log.warning("More than one file found with the instrument: %s" % key)
418+
ret[key] = [ret[key], path_inst_name]
424419
elif ret.get(key) and type(ret.get(key)) == list:
425-
ret[key].append(value)
420+
ret[key].append(path_inst_name)
426421
else:
427-
ret[key] = value
422+
ret[key] = path_inst_name
428423

429424
except FileNotFoundError:
430425
log.error("File %s not found" % (filename))
431-
return {}
426+
return None
432427

433-
if ret == {}:
428+
if ret is None or ret == {}:
434429
log.info("Nothing to extract with the given parameters:\n"
435430
" PPS: %s\n"
436431
" Source Number: %u\n"
@@ -439,7 +434,9 @@ def get_epic_spectra(self, filename, source_number, *,
439434

440435
return ret
441436

442-
def get_epic_images(self, filename, band=[], instrument=[], **kwargs):
437+
def get_epic_images(self, filename, band=[], instrument=[],
438+
get_detmask=False, get_exposure_map=False, path="", **kwargs):
439+
443440
"""Extracts the EPIC images from a given TAR file
444441
445442
For a given TAR file obtained with:
@@ -514,32 +511,32 @@ def get_epic_images(self, filename, band=[], instrument=[], **kwargs):
514511
_instrument = ["M1", "M2", "PN", "EP"]
515512
_band = [1, 2, 3, 4, 5, 8]
516513
_path = ""
517-
for arg in kwargs:
518-
if arg == "get_detmask" and kwargs[arg] is True:
519-
_product_type.append("DETMSK")
520-
if arg == "get_exposure_map" and kwargs[arg] is True:
521-
_product_type.append("EXPMAP")
522-
if arg == "path" and os.path.exists(kwargs[arg]):
523-
_path = kwargs[arg]
514+
if get_detmask:
515+
_product_type.append("DETMSK")
516+
if get_exposure_map:
517+
_product_type.append("EXPMAP")
518+
if path != "" and os.path.exists(path):
519+
_path = path
524520

525-
ret = {}
521+
ret = None
526522
if band == []:
527523
band = _band
528524
else:
529-
for i in band:
530-
if i not in _band:
531-
log.warning("Invalid band %u" % i)
532-
band.remove(i)
525+
for b in band:
526+
if b not in _band:
527+
log.warning("Invalid band %u" % b)
528+
band.remove(b)
533529

534530
if instrument == []:
535531
instrument = _instrument
536532
else:
537-
for i in instrument:
538-
if i not in _instrument:
539-
log.warning("Invalid instrument %s" % i)
540-
instrument.remove(i)
533+
for inst in instrument:
534+
if inst not in _instrument:
535+
log.warning("Invalid instrument %s" % inst)
536+
instrument.remove(inst)
541537
try:
542538
with tarfile.open(filename, "r") as tar:
539+
ret = {}
543540
for member in tar.getmembers():
544541
paths = os.path.split(member.name)
545542
fname = paths[1]
@@ -560,7 +557,7 @@ def get_epic_images(self, filename, band=[], instrument=[], **kwargs):
560557
ret[int(fname_info["S"])] = {}
561558
b = int(fname_info["S"])
562559
ins = fname_info["I"]
563-
value = os.path.abspath(os.path.join(_path, member.name))
560+
path_member_name = os.path.abspath(os.path.join(_path, member.name))
564561
if fname_info["T"] == "DETMSK":
565562
ins = fname_info["I"] + "_det"
566563
elif fname_info["T"] == "EXPMAP":
@@ -569,15 +566,15 @@ def get_epic_images(self, filename, band=[], instrument=[], **kwargs):
569566
log.warning("More than one file found with the "
570567
"band %u and "
571568
"the instrument: %s" % (b, ins))
572-
ret[b][ins] = [ret[b][ins], value]
569+
ret[b][ins] = [ret[b][ins], path_member_name]
573570
elif ret[b].get(ins) and type(ret[b].get(ins)) == list:
574-
ret[b][ins].append(value)
571+
ret[b][ins].append(path_member_name)
575572
else:
576-
ret[b][ins] = value
573+
ret[b][ins] = path_member_name
577574

578575
except FileNotFoundError:
579576
log.error("File %s not found" % (filename))
580-
return {}
577+
return None
581578

582579
return ret
583580

@@ -711,23 +708,24 @@ def get_epic_lightcurve(self, filename, source_number, *,
711708
_product_type = ["SRCTSR", "FBKTSR"]
712709
_path = ""
713710

714-
ret = {}
711+
ret = None
715712

716713
if instrument == []:
717714
instrument = _instrumnet
718715
else:
719-
for i in instrument:
720-
if i not in _instrumnet:
721-
log.warning("Invalid instrument %s" % i)
722-
instrument.remove(i)
716+
for inst in instrument:
717+
if inst not in _instrumnet:
718+
log.warning("Invalid instrument %s" % inst)
719+
instrument.remove(inst)
723720

724721
if path != "" and os.path.exists(path):
725722
_path = path
726723

727724
try:
728725
with tarfile.open(filename, "r") as tar:
729-
for i in tar.getmembers():
730-
paths = os.path.split(i.name)
726+
ret = {}
727+
for member in tar.getmembers():
728+
paths = os.path.split(member.name)
731729
fname = paths[1]
732730
paths = os.path.split(paths[0])
733731
if paths[1] != "pps":
@@ -743,25 +741,25 @@ def get_epic_lightcurve(self, filename, source_number, *,
743741
continue
744742
if int(fname_info["X-"], 16) != source_number:
745743
continue
746-
tar.extract(i, _path)
744+
tar.extract(member, _path)
747745
key = fname_info["I"]
748-
value = os.path.abspath(os.path.join(_path, i.name))
746+
path_inst_name = os.path.abspath(os.path.join(_path, member.name))
749747
if fname_info["T"] == "FBKTSR":
750748
key = fname_info["I"] + "_bkg"
751749
if ret.get(key) and type(ret.get(key)) == str:
752750
log.warning("More than one file found with the "
753751
"instrument: %s" % key)
754-
ret[key] = [ret[key], value]
752+
ret[key] = [ret[key], path_inst_name]
755753
elif ret.get(key) and type(ret.get(key)) == list:
756-
ret[key].append(value)
754+
ret[key].append(path_inst_name)
757755
else:
758-
ret[key] = value
756+
ret[key] = path_inst_name
759757

760758
except FileNotFoundError:
761759
log.error("File %s not found" % (filename))
762-
return {}
760+
return None
763761

764-
if ret == {}:
762+
if ret is None or ret == {}:
765763
log.info("Nothing to extract with the given parameters:\n"
766764
" PPS: %s\n"
767765
" Source Number: %u\n"

0 commit comments

Comments
 (0)