@@ -100,7 +100,7 @@ def download_data(self, observation_id, *, filename=None, verbose=False,
100
100
If verbose returns the filename
101
101
"""
102
102
if filename is not None :
103
- filename = filename . split ( '.' )[0 ]
103
+ filename = os . path . splitext ( filename )[0 ]
104
104
105
105
link = self .data_aio_url + "obsno=" + observation_id
106
106
@@ -303,7 +303,8 @@ def _parse_filename(self, filename):
303
303
304
304
def get_epic_spectra (self , filename , source_number , * ,
305
305
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.
307
308
For a given TAR file obtained with:
308
309
XMM.download_data(OBS_ID,level="PPS",extension="FTZ",filename=tarfile)
309
310
This function extracts the EPIC sources spectral products in a given
@@ -312,17 +313,19 @@ def get_epic_spectra(self, filename, source_number, *,
312
313
sources spectral products with key being the instrument
313
314
If the instrument is not specified this function will
314
315
return all the available instruments
316
+
315
317
Examples:
316
318
Extracting all bands and instruments:
317
319
result = XMM.get_epic_spectra(tarfile,83,
318
320
instrument=['M1','M2','PN'])
319
321
If we want to retrieve the source spectrum of the instrument PN
320
322
fits_image = result['PN']
321
323
fits_image will be the full path to the extracted FTZ file
324
+
322
325
Parameters
323
326
----------
324
327
filename : string, mandatory
325
- The name of the tarfile to be proccessed
328
+ The name of the tarfile to be processed
326
329
source_number : integer, mandatory
327
330
The source number, in decimal, in the observation
328
331
instruments : array of strings, optional, default []
@@ -337,6 +340,7 @@ def get_epic_spectra(self, filename, source_number, *,
337
340
-------
338
341
A dictionary with the full paths of the extracted EPIC sources
339
342
spectral products. The key is the instrument
343
+
340
344
Notes
341
345
-----
342
346
The filenames will contain the source number in hexadecimal,
@@ -348,20 +352,21 @@ def get_epic_spectra(self, filename, source_number, *,
348
352
_instrument = ["M1" , "M2" , "PN" , "EP" ]
349
353
_product_type = ["SRSPEC" , "BGSPEC" , "SRCARF" ]
350
354
_path = ""
351
- ret = {}
355
+ ret = None
352
356
if instrument == []:
353
357
instrument = _instrument
354
358
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 )
359
363
if path != "" and os .path .exists (path ):
360
364
_path = path
361
365
try :
362
366
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 )
365
370
fname = paths [1 ]
366
371
paths = os .path .split (paths [0 ])
367
372
if paths [1 ] != "pps" :
@@ -375,29 +380,25 @@ def get_epic_spectra(self, filename, source_number, *,
375
380
continue
376
381
if int (fname_info ["X-" ], 16 ) != source_number :
377
382
continue
378
- tar .extract (i , _path )
383
+ tar .extract (member , _path )
379
384
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 ))
381
386
if fname_info ["T" ] == "BGSPEC" :
382
387
key = fname_info ["I" ] + "_bkg"
383
388
elif fname_info ["T" ] == "SRCARF" :
384
389
key = fname_info ["I" ] + "_arf"
385
390
else :
386
- with fits .open (value ) as hdul :
391
+ with fits .open (path_inst_name ) as hdul :
387
392
for ext in hdul :
388
393
if ext .name != "SPECTRUM" :
389
394
continue
390
395
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/"
395
398
elif fname_info ["I" ] == "PN" :
396
399
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
401
402
402
403
link = self ._rmf_ftp + inst + rmf_fname
403
404
@@ -406,31 +407,25 @@ def get_epic_spectra(self, filename, source_number, *,
406
407
407
408
response = self ._request ('GET' , link )
408
409
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" ])
414
411
415
412
with open (rsp_filename , 'wb' ) as f :
416
413
f .write (response .content )
417
- ret [fname_info ["I" ] + "_rmf" ] = \
418
- rsp_filename
414
+ ret [fname_info ["I" ] + "_rmf" ] = rsp_filename
419
415
420
416
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 ]
424
419
elif ret .get (key ) and type (ret .get (key )) == list :
425
- ret [key ].append (value )
420
+ ret [key ].append (path_inst_name )
426
421
else :
427
- ret [key ] = value
422
+ ret [key ] = path_inst_name
428
423
429
424
except FileNotFoundError :
430
425
log .error ("File %s not found" % (filename ))
431
- return {}
426
+ return None
432
427
433
- if ret == {}:
428
+ if ret is None or ret == {}:
434
429
log .info ("Nothing to extract with the given parameters:\n "
435
430
" PPS: %s\n "
436
431
" Source Number: %u\n "
@@ -439,7 +434,9 @@ def get_epic_spectra(self, filename, source_number, *,
439
434
440
435
return ret
441
436
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
+
443
440
"""Extracts the EPIC images from a given TAR file
444
441
445
442
For a given TAR file obtained with:
@@ -514,32 +511,32 @@ def get_epic_images(self, filename, band=[], instrument=[], **kwargs):
514
511
_instrument = ["M1" , "M2" , "PN" , "EP" ]
515
512
_band = [1 , 2 , 3 , 4 , 5 , 8 ]
516
513
_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
524
520
525
- ret = {}
521
+ ret = None
526
522
if band == []:
527
523
band = _band
528
524
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 )
533
529
534
530
if instrument == []:
535
531
instrument = _instrument
536
532
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 )
541
537
try :
542
538
with tarfile .open (filename , "r" ) as tar :
539
+ ret = {}
543
540
for member in tar .getmembers ():
544
541
paths = os .path .split (member .name )
545
542
fname = paths [1 ]
@@ -560,7 +557,7 @@ def get_epic_images(self, filename, band=[], instrument=[], **kwargs):
560
557
ret [int (fname_info ["S" ])] = {}
561
558
b = int (fname_info ["S" ])
562
559
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 ))
564
561
if fname_info ["T" ] == "DETMSK" :
565
562
ins = fname_info ["I" ] + "_det"
566
563
elif fname_info ["T" ] == "EXPMAP" :
@@ -569,15 +566,15 @@ def get_epic_images(self, filename, band=[], instrument=[], **kwargs):
569
566
log .warning ("More than one file found with the "
570
567
"band %u and "
571
568
"the instrument: %s" % (b , ins ))
572
- ret [b ][ins ] = [ret [b ][ins ], value ]
569
+ ret [b ][ins ] = [ret [b ][ins ], path_member_name ]
573
570
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 )
575
572
else :
576
- ret [b ][ins ] = value
573
+ ret [b ][ins ] = path_member_name
577
574
578
575
except FileNotFoundError :
579
576
log .error ("File %s not found" % (filename ))
580
- return {}
577
+ return None
581
578
582
579
return ret
583
580
@@ -711,23 +708,24 @@ def get_epic_lightcurve(self, filename, source_number, *,
711
708
_product_type = ["SRCTSR" , "FBKTSR" ]
712
709
_path = ""
713
710
714
- ret = {}
711
+ ret = None
715
712
716
713
if instrument == []:
717
714
instrument = _instrumnet
718
715
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 )
723
720
724
721
if path != "" and os .path .exists (path ):
725
722
_path = path
726
723
727
724
try :
728
725
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 )
731
729
fname = paths [1 ]
732
730
paths = os .path .split (paths [0 ])
733
731
if paths [1 ] != "pps" :
@@ -743,25 +741,25 @@ def get_epic_lightcurve(self, filename, source_number, *,
743
741
continue
744
742
if int (fname_info ["X-" ], 16 ) != source_number :
745
743
continue
746
- tar .extract (i , _path )
744
+ tar .extract (member , _path )
747
745
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 ))
749
747
if fname_info ["T" ] == "FBKTSR" :
750
748
key = fname_info ["I" ] + "_bkg"
751
749
if ret .get (key ) and type (ret .get (key )) == str :
752
750
log .warning ("More than one file found with the "
753
751
"instrument: %s" % key )
754
- ret [key ] = [ret [key ], value ]
752
+ ret [key ] = [ret [key ], path_inst_name ]
755
753
elif ret .get (key ) and type (ret .get (key )) == list :
756
- ret [key ].append (value )
754
+ ret [key ].append (path_inst_name )
757
755
else :
758
- ret [key ] = value
756
+ ret [key ] = path_inst_name
759
757
760
758
except FileNotFoundError :
761
759
log .error ("File %s not found" % (filename ))
762
- return {}
760
+ return None
763
761
764
- if ret == {}:
762
+ if ret is None or ret == {}:
765
763
log .info ("Nothing to extract with the given parameters:\n "
766
764
" PPS: %s\n "
767
765
" Source Number: %u\n "
0 commit comments