/
latex_lick_table.py
executable file
·143 lines (132 loc) · 4.85 KB
/
latex_lick_table.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
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 30 17:49:32 2014
@author: cbarbosa
Make LateX table for the population paper.
"""
import os
from re import split
import numpy as np
from config import *
from mcmc_model import get_model_lims
def ra2str(ra):
ra /= 15.
hours = ra.astype(int)
minutes = ((ra - hours) * 60.)
minutes = minutes.astype(int)
seconds = (3600*ra - hours * 3600 - minutes * 60)
seconds = np.around(seconds, 2)
rastr = []
for h,m,s in zip(hours, minutes, seconds):
if s < 10:
sfill = "0"
else:
sfill = ""
rastr.append(r"{0}:{1}:{3}{2}".format(h, m, s, sfill))
return rastr
def dec2str(dec):
sign=2*np.sign(dec) - 1
signstr = []
for sg in sign:
signstr.append("+" if sg == 1 else "-")
dec = np.abs(dec)
hours = dec.astype(int)
minutes = (dec - hours) * 60
minutes = minutes.astype(int)
seconds = (3600*dec - hours * 3600 - minutes * 60)
seconds = np.around(seconds, 2)
decstr = []
for sig, h,m,s in zip(signstr, hours, minutes, seconds):
if s < 10:
sfill = "0"
else:
sfill = ""
decstr.append(r"{3}{0}:{1}:{4}{2}".format(h, m, s, sig, sfill))
return decstr
def val2str(s, serr):
""" Convert value and error to string in LateX."""
vals = []
for v, verr in zip(s, serr):
if np.isnan(v) or np.isnan(verr):
vals.append(r"--")
elif np.log10(verr) < 1:
sigfig = -int(np.floor(np.log10(verr)))
newerr = np.around(verr, sigfig)
if newerr == np.power(10., -sigfig):
sigfig += 1
newerr = np.around(verr, sigfig)
v = np.around(v, sigfig)
vals.append(r"${0}\pm{1}$".format(v, newerr))
else:
vals.append(r"${0}\pm{1}$".format(int(np.around(v)),
int(np.around(verr))))
return vals
def pm_string(pp, ppm, ppp):
table = []
for (p, pm, pp) in zip(pp, ppm, ppp):
line = []
for i in range(len(p)):
s = r"${0:.2f}_{{-{1:.2f}}}^{{+{2:.2f}}}$".format(p[i], pm[i], pp[i])
line.append(s)
table.append(" & ".join(line))
return table
def test_output():
outcds = os.path.join(home, spectype, "table1.dat")
with open(outcds) as f:
table = f.readlines()
lims = [[262,271]]
for cols in lims:
v = [x[cols[0]-1:cols[1]-1] for x in table]
print v
if __name__ == "__main__":
spectype = "single2"
table = os.path.join(home, spectype, "results.tab")
spec = np.genfromtxt(table, dtype=None, usecols=(0,))
ids = [x.split("n3311")[-1].replace(".fits", "").replace("_", " ") for x \
in spec]
sns = np.loadtxt(table, usecols=(14,))
rs, pas = np.loadtxt(table, usecols=(3,4)).T
# idx = np.where(sns > sn_cut)[0]
cols = np.array([39,41,47,49,51,53,55])
model_table = os.path.join(tables_dir, "models_thomas_2010.dat")
lims, ranges = get_model_lims(model_table)
idx = np.array([12,13,16,17,18,19])
cols2 = np.array([69,72,75])
lims = lims[idx]
data = np.loadtxt(table, usecols=cols)
errs = np.loadtxt(table, usecols=cols+1)
pop = np.loadtxt(table, usecols=cols2)
popm = pop - np.loadtxt(table, usecols=cols2 + 1)
popp = np.loadtxt(table, usecols=cols2 + 2) - pop
pstring = pm_string(pop, popm, popp)
for i in range(len(data)):
for j in range(len(lims)):
if data[i,j] < lims[j,0] or data[i,j] > lims[j,1]:
data[i,j] = np.nan
results = []
cds_table = []
for iid, d, err, r, pa, sn, p in zip(ids, data, errs, rs, pas, sns, pstring):
err[np.isnan(d)] = np.nan
cols2_4 = [r, pa, sn]
cols2_4 = ["{0:.1f}".format(x) for x in cols2_4]
cols5_11 = [item for sublist in zip(d,err) for item in sublist]
cols5_11 = ["{0:.2f}".format(x) for x in cols5_11]
cols12_14 = p.replace("$", "").replace("{", "").replace("}","").split("&")
cols12_14 = [x.split("_") for x in cols12_14]
cols12_14 = [item for sublist in cols12_14 for item in sublist]
cols12_14 = [x.split("^") for x in cols12_14]
cols12_14 = [item for sublist in cols12_14 for item in sublist]
ascii = cols2_4 + cols5_11 + cols12_14
ascii = ["{0:9s}".format(iid.replace(" ", "").strip())] + ["{0:>10s}".format(x) for x in ascii]
cds_table.append("".join(ascii))
s = "{0} & {1:.1f} & {2:.1f} & {3:.1f} & ".format(iid, r, pa, sn) + \
" & ".join(val2str(d, err)) + " & " + p
results.append(s)
output = os.path.join(home, spectype, "lick.tex")
results.sort()
with open(output, "w") as f:
f.write("\\\\\n".join(results) + "\\\\")
output2 = os.path.join(home, spectype, "table1.dat")
with open(output2, "w") as f:
f.write("\n".join(cds_table))
test_output()