-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfits2tbl
executable file
·95 lines (83 loc) · 2.58 KB
/
fits2tbl
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
#!/usr/bin/python3
# Convert from fits format to a table
# Copyright (C) 2015, 2021 Bob Mottram
# bob@libreserver.org
#
# 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 sys
from astropy.io import fits
if len(sys.argv) < 2:
print("Please specify a fits filename")
sys.exit(-1)
filename = sys.argv[1]
try:
hdulist = fits.open(filename)
except:
print("File not found " + filename)
sys.exit(-2)
listname="PHOTOMETRY"
if len(sys.argv) > 2:
if sys.argv[2]:
if sys.argv[2]>='0' and sys.argv[2]<='9':
listname=int(sys.argv[2])
else:
listname=sys.argv[2]
try:
scidata = hdulist[listname].data
except:
print("Photometry table not found")
print(hdulist.info(0))
sys.exit(-3)
no_of_fields = len(scidata[0])
# Extract the headers
headers = repr(hdulist[listname].header).split("\n")
col = 0
field_units = []
field_name = []
for h in headers:
typestr=h.split("=")[0].strip()
if typestr.startswith("TTYPE"):
name = h.split("=")[1].strip()
if "'" in name:
title = name.split("'")[1].strip()
if len(title) > 0:
field_name.append(title)
if typestr.startswith("TUNIT"):
name = h.split("=")[1].strip()
if "'" in name:
title = name.split("'")[1].strip()
if len(title) > 0:
field_units.append(title)
# Field name header
sys.stdout.write("| ")
for i in range(len(field_name)):
if i < (len(field_name)-1):
sys.stdout.write(field_name[i] + " ")
else:
print(field_name[i])
# Field units header
sys.stdout.write("| ")
for i in range(len(field_units)):
if i < (len(field_units)-1):
sys.stdout.write('"' + field_units[i] + '" ')
else:
print('"' + field_units[i] + '"')
# Show the data
for i in range(len(scidata)):
for j in range(no_of_fields):
if j < no_of_fields-1:
sys.stdout.write(str(scidata[i][j]) + " ")
else:
print(scidata[i][j])
sys.exit(0)