-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitsmodhead.py
89 lines (73 loc) · 3.19 KB
/
fitsmodhead.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
#!/usr/bin/env python3
"""A python version of HEASOFT ftool fmodhead
Simple python tool to modify an existing FITS keyword.
Limitations:
- Currently modifies the keyword in every HDU it exists in.
- No ability to specify a single HDU
- No ability to add or delete keywords
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
"""
import argparse
import os
import os.path
import sys
from astropy.io import fits
__author__ = "Dave Strickland"
__copyright__ = "Copyright 2018, Dave Strickland"
__date__ = "2018/03/14"
__deprecated__ = False
__email__ = "[email protected]"
__license__ = "GPLv3"
__version__ = "0.2.0"
def command_line_opts():
parser = argparse.ArgumentParser()
# required command line arguments
parser.add_argument(dest='fitsfile', metavar='INP.fits',
help='Input FITS file to have keywords modified.')
parser.add_argument(dest='keyword', default=None, metavar='KEYWORD',
help='Keyword to have its value modified.')
parser.add_argument(dest='value', default=None, metavar='VALUE',
help='Value to set the keyword to.')
action_default='modify'
parser.add_argument('--action',
dest='action', default=action_default,
help='Action to take on specified keyword. (default: {})'.format(action_default))
parser.add_argument('-v', '--verbose',
dest='verbose', action='store_true',
help='Verbose output for each object processed. Useful for debugging purposes.')
args = parser.parse_args()
return args
def main():
p_args = command_line_opts()
# Don't have to worry about race conditions for this type of work
if not os.path.isfile(p_args.fitsfile):
print('Error: Input file {} not found. Current dir: {}'.format(p_args.fitsfile, os.getcwd()))
sys.exit(1)
# We can't use with open as... syntax because output_verify
# has to be called on close() in order to fix non-compliant
# fits files, even though verifying the headers fixes it in the
# 'in memory' headers.
hdu_list = fits.open(p_args.fitsfile,
mode='update')
if p_args.verbose:
print('Structure of {} follows:'.format(p_args.fitsfile))
print(hdu_list.info())
# for hdu in hdu_list:
# hdu.verify('silentfix+ignore')
# if p_args.keyword in hdu.header:
# hdr = hdu.header
# print('Original keyword={} value={}'.format(p_args.keyword, hdr[p_args.keyword]))
# hdr[p_args.keyword] = p_args.value
# print('Updated keyword={} value={}'.format(p_args.keyword, hdr[p_args.keyword]))
hdu_list.close(output_verify='silentfix+ignore')
return
if __name__ == "__main__":
main()