-
Notifications
You must be signed in to change notification settings - Fork 2
/
fits2text.py
68 lines (46 loc) · 1.47 KB
/
fits2text.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
"""Convert FITS to text.
Examples
--------
>>> import fits2text
Convert IMP FITS to text:
>>> fits2txt.imp2txt('w3m17171j_imp.fits', 'w3m17171j_imp.txt')
Convert SYN FITS to text:
>>> fits2txt.syn2txt('acs_f814w_005_syn.fits', 'acs_f814w_005_syn.txt')
"""
# THIRD-PARTY
import pyfits
__author__ = 'Pey Lian Lim'
__organization__ = 'Space Telescope Science Institute'
def imp2txt(inputFile, outFile, ext=1):
"""Convert IMP FITS table to text.
Parameters
----------
inputFile : string
IMP FITS table.
outFile : string
Output text table.
ext : int, optional
Table extension to convert.
"""
with pyfits.open(inputFile) as pf:
tabdata = pf[ext].data
# Write to text file (overwrite)
with open(outFile, 'w') as fout:
for row in tabdata:
cols = [str(col).replace('\n', ' ') for col in row]
fout.write('\t'.join(cols) + '\n')
def syn2txt(inputFile, outFile, ext=1):
"""Convert SYNPHOT throughput table to text.
Parameters
----------
inputFile : string
SYNPHOT throughput table with WAVELENGTH and THROUGHPUT.
outFile : string
Output text table.
ext : int, optional
Table extension to convert.
"""
tabdata = pyfits.getdata(inputFile, ext)
with open(outFile, 'w') as fout:
for w, t in zip(tabdata['WAVELENGTH'], tabdata['THROUGHPUT']):
fout.write('{:10.3f} {:15.7E}\n'.format(w, t))