1
2
3
4 """
5 Removes FITS keywords from FITS headers of files in a given file list.
6
7 @author: E. Sutorius
8 @org: WFAU, IfA, University of Edinburgh
9 """
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import os
25 import sys
26 import time
27
28 import wsatools.FitsUtils as fits
29
30
32 """Read filenames from log file."""
33 xobj = open(filename,mode='r')
34 flist = xobj.readlines()
35 xobj.close()
36 filelist = []
37 for line in flist:
38 if line.find('#') < 0:
39 filelist.append(line[:-1])
40 return filelist
41
42
43
45 """Print the keyword and the one before and after to the screen
46 (in case you need to restore them later)
47 """
48 cardlist = header.ascardlist()
49 index = -1
50
51 for card in cardlist:
52 if repr(card).startswith(keyword):
53 index = cardlist.index(card)
54
55 if index >= 0:
56 if index > 0:
57 print cardlist[index-1]
58 print ">>>>",cardlist[index]
59 if index < len(cardlist)-1:
60 print cardlist[index+1]
61
62
64
65 if len(argv) !=2:
66 print "argv: ", argv
67 print "usage: RemoveFitsKeys.py removelog"
68 raise SystemExit
69
70 tflog = argv[1]
71
72 filelist = readfiles(tflog)
73 fitskeyline = filelist.pop(0)
74 fitskeys = fitskeyline.replace('(','!(').split(';')
75 fitskeydict = {}
76 for entry in fitskeys:
77 key, hdus = entry.split('!')
78 fitskeydict[key] = hdus[1:-1].split(',')
79
80 print fitskeydict
81 print "You are to delete:"
82 for key in fitskeydict.iterkeys():
83 print key,"from extension(s)",fitskeydict[key]
84 answer = raw_input("Proceed?[N]").upper()
85 if answer != 'Y':
86 raise SystemExit
87
88
89 for filename in filelist:
90 print filename
91 print "="*len(filename)
92 fimgptr = fits.open(filename,"update")
93 for key in fitskeydict.iterkeys():
94 for hdu in fitskeydict[key]:
95 if int(hdu) < len(fimgptr):
96 hdr = fimgptr[int(hdu)].header
97 printkey(hdr,key)
98 if hdr.has_key(key):
99 del hdr[key]
100 print " ",key,"deleted\n"
101 fimgptr.flush()
102
103
104
105 getTime = False
106 if fimgptr[0].header.has_key("WSA_TIME"):
107 wsaTimestamp = fimgptr[0].header.get("WSA_TIME")
108 timetpl = time.strptime(wsaTimestamp[6:],'%y%m%d%H%M%S')
109 getTime = True
110 else:
111 if fimgptr[1].header.has_key("DATE"):
112 timestamp = fimgptr[1].header.get("DATE")
113 timetpl = time.strptime(timestamp,'%Y-%m-%dT%H:%M:%S')
114 getTime = True
115
116 if getTime == True:
117 modtime = time.mktime(timetpl)
118 os.utime(filename,(time.time(),modtime))
119 print "file time reset done\n"
120 else:
121 print "WARNING: No WSA_TIME or DATE available, file time not modified.\n"
122
123
124 fimgptr.close()
125
126
127
128 if __name__ == '__main__':
129 main(sys.argv)
130
131
132
133
134
135
136
137