-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathfixkeywords.py
91 lines (82 loc) · 2.57 KB
/
fixkeywords.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
from __future__ import print_function, division
import numpy
import sys
import astropy.io.fits as pyfits
changes_needed = []
def should_be(fitsfile, header, keyword, expected=None):
v = header.get(keyword)
if expected is None:
print('%s: %s' % (keyword, v))
return []
elif v == expected:
print('%s: %s (as expected)' % (keyword, v))
return []
else:
print('%s: %s (expected: %s)' % (keyword, v, expected))
return [(fitsfile, header, keyword, expected)]
def apply_changes(f, filename):
changed_anything = False
for fitsfile, header, keyword, expected in changes_needed:
if fitsfile != f: continue
header[keyword] = expected
changed_anything = True
if changed_anything:
print(' -> writing ',filename) # + '.withkeywords.fits'
#fitsfile.writeto(filename + '.withkeywords.fits', clobber=True)
fitsfile.writeto(filename, overwrite=True)
else:
print(' -> no changes needed')
if len(sys.argv) != 5:
print('SYNOPSIS: fixkeywords.py src.pi bkg.pi rmf.rmf arf.arf')
print()
print('Checks and corrects the keywords connecting the files')
print('Corrects zero energy bounds')
print()
print('Johannes Buchner (C) 2017')
sys.exit(1)
src = sys.argv[1]
bkg = sys.argv[2]
rmf = sys.argv[3]
arf = sys.argv[4]
fsrc = pyfits.open(src)
fsrch = fsrc[1].header
print('Source file:', src)
print('AREASCAL:', fsrch.get('AREASCAL'))
print('BACKSCAL:', fsrch.get('BACKSCAL'))
print('EXPOSURE:', fsrch.get('EXPOSURE'))
changes_needed += should_be(fsrc, fsrch, 'BACKFILE', bkg)
changes_needed += should_be(fsrc, fsrch, 'RESPFILE', rmf)
changes_needed += should_be(fsrc, fsrch, 'ANCRFILE', arf)
apply_changes(fsrc, src)
print()
changes_needed = []
fbkg = pyfits.open(bkg)
fbkgh = fbkg[1].header
print('Background file:', bkg)
print('AREASCAL:', fbkgh.get('AREASCAL'))
print('BACKSCAL:', fbkgh.get('BACKSCAL'))
print('EXPOSURE:', fbkgh.get('EXPOSURE'))
changes_needed += should_be(fbkg, fbkgh, 'RESPFILE', rmf)
changes_needed += should_be(fbkg, fbkgh, 'ANCRFILE', arf)
apply_changes(fbkg, bkg)
print()
print('Response file:', rmf)
f = pyfits.open(rmf)
SMALL = 0.001
if f['EBOUNDS'].data[0]['E_MIN'] < SMALL or f['MATRIX'].data[0]['ENERG_LO'] < SMALL:
f['EBOUNDS'].data[0]['E_MIN'] = SMALL
f['MATRIX'].data[0]['ENERG_LO'] = SMALL
f.writeto(rmf, overwrite=True)
print(' -> energy bounds corrected')
else:
print(' -> file is ok')
print()
print('Ancillary file:', arf)
f = pyfits.open(arf)
if f['SPECRESP'].data[0]['ENERG_LO'] < SMALL:
f['SPECRESP'].data[0]['ENERG_LO'] = SMALL
f.writeto(arf, overwrite=True)
print(' -> energy bounds corrected')
else:
print(' -> file is ok')