-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathkepkey.py
executable file
·277 lines (234 loc) · 8.41 KB
/
kepkey.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
import kepmsg, kepio
import numpy
from astropy.io import fits as pyfits
# -----------------------------------------------------------
# get keyword value
def get(file,hdu,keyword,logfile,verbose):
status = 0
try:
value = hdu.header[keyword]
except:
message = 'ERROR -- KEPKEY.GET: Cannot read keyword ' + keyword
message += ' in file ' + file
status = kepmsg.err(logfile,message,verbose)
value = None
return value, status
# -----------------------------------------------------------
# delete keyword
def delete(keyword,hdu,file,logfile,verbose):
status = 0
try:
del hdu.header[keyword]
except:
message = 'ERROR -- KEPKEY.DELETE: Cannot delete keyword ' + keyword
message += ' in ' + file
status = kepmsg.err(logfile,message,verbose)
return status
# -----------------------------------------------------------
# add new keyword
def new(keyword,value,comment,hdu,file,logfile,verbose):
status = 0
try:
hdu.header[keyword] = (value, comment)
except:
message = 'ERROR -- KEPKEY.NEW: Cannot create keyword ' + keyword
message += ' in ' + file
status = kepmsg.err(logfile,message,verbose)
return status
# -----------------------------------------------------------
# add comment keyword
def comment(txt,hdu,file,logfile,verbose):
status = 0
try:
hdu.header.add_comment(txt)
except:
message = 'ERROR -- KEPKEY.COMMENT: Cannot create comment keyword'
message += ' in ' + file
status = kepmsg.err(logfile,message,verbose)
return status
# -----------------------------------------------------------
# add history keyword
def history(txt,hdu,file,logfile,verbose):
status = 0
try:
hdu.header.add_history(txt)
except:
message = 'ERROR -- KEPKEY.HISTORY: Cannot create history keyword'
message += ' in ' + file
status = kepmsg.err(logfile,message,verbose)
return status
# -----------------------------------------------------------
# change existing keyword value
def change(keyword,value,hdu,file,logfile,verbose):
status = 0
try:
hdu.header[keyword] = value
except:
message = 'ERROR -- KEPKEY.CHANGE: Cannot update keyword ' + keyword
message += ' in ' + file
status = kepmsg.err(logfile,message,verbose)
return status
# -----------------------------------------------------------
# calculate timestamp cadence from keywords
def cadence(struct,file,logfile,verbose):
# get keyword data
status = 0
try:
int_time, status = get(file,struct,'INT_TIME',logfile,verbose)
except:
txt = 'ERROR -- KEPKEY.CADENCE: Cannot read keyword INT_TIME in file ' + file + '[1]'
status = kepmsg.err(logfile,message,verbose)
try:
readtime, status = get(file,struct,'READTIME',logfile,verbose)
except:
txt = 'ERROR -- KEPKEY.CADENCE: Cannot read keyword READTIME in file ' + file + '[1]'
status = kepmsg.err(logfile,message,verbose)
try:
num_frm, status = get(file,struct,'NUM_FRM',logfile,verbose)
except:
txt = 'ERROR -- KEPKEY.CADENCE: Cannot read keyword NUM_FRM in file ' + file + '[1]'
status = kepmsg.err(logfile,message,verbose)
# calculate cadence
cadence = (float(int_time) + float(readtime)) * float(num_frm)
return cadence, status
# -----------------------------------------------------------
# get physical WCS keywords
def getWCSp(file,struct,logfile,verbose):
status = 0
crpix1p = 0.0
crpix2p = 0.0
crval1p = 0.0
crval2p = 0.0
cdelt1p = 0.0
cdelt2p = 0.0
try:
crpix1p, status = get(file,struct,'CRPIX1P',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CRPIX1P in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
crpix2p, status = get(file,struct,'CRPIX2P',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CRPIX2P in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
crval1p, status = get(file,struct,'CRVAL1P',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CRVAL1P in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
crval2p, status = get(file,struct,'CRVAL2P',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CRVAL2P in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
cdelt1p, status = get(file,struct,'CDELT1P',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CDELT1P in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
cdelt2p, status = get(file,struct,'CDELT2P',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSP: Cannot read keyword CDELT2P in file ' + file
kepmsg.warn(logfile,txt)
status = 1
return crpix1p, crpix2p, crval1p, crval2p, cdelt1p, cdelt2p, status
# -----------------------------------------------------------
# get physical WCS keywords
def getWCSs(file,struct,logfile,verbose):
status = 0
crpix1 = 0.0
crpix2 = 0.0
crval1 = 0.0
crval2 = 0.0
cdelt1 = 0.0
cdelt2 = 0.0
pc = [0.0,0.0,0.0,0.0]
try:
crpix1, status = get(file,struct,'CRPIX1',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CRPIX1 in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
crpix2, status = get(file,struct,'CRPIX2',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CRPIX2 in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
crval1, status = get(file,struct,'CRVAL1',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CRVAL1 in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
crval2, status = get(file,struct,'CRVAL2',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CRVAL2 in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
cdelt1, status = get(file,struct,'CDELT1',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CDELT1 in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
cdelt2, status = get(file,struct,'CDELT2',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword CDELT2 in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
pc1_1, status = get(file,struct,'PC1_1',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword PC1_1 in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
pc1_2, status = get(file,struct,'PC1_2',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword PC1_2 in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
pc2_1, status = get(file,struct,'PC2_1',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword PC2_1 in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
pc2_2, status = get(file,struct,'PC2_2',logfile,verbose)
except:
txt = 'WARNING -- KEPKEY.GETWCSS: Cannot read keyword PC2_2 in file ' + file
kepmsg.warn(logfile,txt)
status = 1
try:
pc = numpy.array([[pc1_1,pc1_2],[pc2_1,pc2_2]])
pc = numpy.linalg.inv(pc)
except:
pass
return crpix1, crpix2, crval1, crval2, cdelt1, cdelt2, pc, status
# -----------------------------------------------------------
# calculate coordinates from WCS data
def wcs(i,crpix,crval,cdelt):
return crval + (float(i + 1) - crpix) * cdelt
# -----------------------------------------------------------
# remove empty keywords within a FITS file
def emptykeys(struct,file,logfile,verbose):
# determine number of HDU from keyword search
nhdu = kepio.HDUnum(struct)
# delete empty keywords
for hdu in range(nhdu):
for keyword in struct[hdu].header.keys():
head = struct[hdu].header[keyword]
if ('pyfits' in str(head) and 'Undefined' in str(head)):
status = delete(keyword,struct[hdu],file,logfile,verbose)
return struct