-
Notifications
You must be signed in to change notification settings - Fork 2
/
im2tab.py
85 lines (68 loc) · 2.29 KB
/
im2tab.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
"""Convert Nora's image to table.
Examples
--------
>>> import im2tab
>>> im2tab.convert('myimagetable.fits', 'nicetable.fits')
Copied the header
Converted 3 columns
nicetable.fits written
>>> from astropy.table import Table
>>> tab = Table.read('nicetable.fits', format='fits')
>>> tab
<Table length=8486>
DATA QUALITY VAR
float32 float32 float32
----------- ------- -----------
1.44789e+08 8.0 7.50891e+15
1.52335e+08 8.0 7.48747e+15
1.08334e+08 8.0 7.66241e+15
9.42806e+07 8.0 7.62161e+15
7.86007e+07 1032.0 7.45407e+15
9.22977e+07 1032.0 7.33011e+15
... ... ...
1.14301e+09 8.0 1.30819e+16
1.12099e+09 8.0 1.30816e+16
1.22831e+09 8.0 1.30568e+16
1.20423e+09 8.0 1.28731e+16
1.18228e+09 8.0 1.29867e+16
1.17793e+09 8.0 1.30699e+16
1.1584e+09 8.0 1.31217e+16
"""
from __future__ import division, print_function
from astropy.io import fits
def convert(inputfile, outputfile, clobber=False, verbose=True):
"""Convert the following format to proper FITS table.
Input format::
No. Name Type Cards Dimensions
0 PRIMARY PrimaryHDU 293 ()
1 DATA ImageHDU 19 (8486,)
2 QUALITY ImageHDU 20 (8486,)
3 VAR ImageHDU 19 (8486,)
Output format::
No. Name Type Cards Dimensions
0 PRIMARY PrimaryHDU 293 ()
1 BinTableHDU 14 8486R x 3C [E, E, E]
Parameters
----------
inputfile, outputfile : str
Input and output filenames.
clobber : bool, optional
Overwrite existing file.
verbose : bool, optional
Print extra info.
"""
with fits.open(inputfile) as pf:
# Primary header
thdulist = fits.HDUList([pf[0]])
if verbose:
print('Copied the header')
# Convert the rest to columns
cols = [fits.Column(name=pfext.name, format='E', array=pfext.data)
for pfext in pf[1:]]
tbhdu = fits.BinTableHDU.from_columns(cols)
thdulist.append(tbhdu)
if verbose:
print('Converted {0} columns'.format(len(cols)))
thdulist.writeto(outputfile, clobber=clobber)
if verbose:
print(outputfile, 'written')