-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_balbinot_2018.py
135 lines (111 loc) · 4.47 KB
/
parse_balbinot_2018.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
import os
import sys
import numpy
import logging
from matplotlib import pyplot
BASEDIR = "/".join(__file__.split("/")[:-1]) + "/MW_GCS_Balbinot2018/"
def fix_gc_names(name):
from utils import convert_gc_names_from_sh_to_any
return convert_gc_names_from_sh_to_any(name, reverse=True) # from deBoer to SH
def parse_balbinot_2018(logger, fname="{0}latex_table_semi_cleaned.txt".format(BASEDIR)):
with open(fname) as f:
raw_data = f.readlines()
# Clean up LaTeX table
names = []
values = []
errors = []
for i, row in enumerate(raw_data):
row_values = []
row_errors = []
if i == 1: continue # units
row = row.replace("\\", "").replace("$", ""
).replace("{", "").replace("}", "").replace("*", "")
columns = row.split("&")
for j, column in enumerate(columns):
if i == 0: # header
names.append(column.strip())
continue
if "pm" in column:
value, error = column.split("pm")
value = value.strip()
else:
value = column.strip()
error = numpy.nan
value = numpy.nan if "--" in value else value
# Not needed to fix gc names. In fact, the method has bug for
# 'Eridanus' --> 'Eridanus danus' TODO
# if j is 0:
# value = fix_gc_names(value)
try:
value = float(value)
except ValueError:
pass
try:
error = float(error)
except ValueError:
pass
row_values.append(value)
row_errors.append(error)
if i == 0: continue # header
values.append(row_values)
errors.append(row_errors)
for name, value, error in zip(names, row_values, row_errors):
if type(value) == float:
info = "{0:<20s}{1:>10.2f}".format(name, value)
else:
info = "{0:<20s}{1:<10s}".format(name, str(value))
if numpy.isfinite(error):
info += " +/- {0:<10.2f}".format(error)
# if i >= 3: logger.debug(info)
# Now build structured array
dtype = {"names": [], "formats": []}
for name in names:
if name in ["Name", "Refs"]:
dtype["names"].append(name)
dtype["formats"].append("U16")
else:
dtype["names"].append(name)
dtype["formats"].append("f8")
dtype["names"].append(name + "_err")
dtype["formats"].append("f8")
data = numpy.empty(len(raw_data)-2, dtype=dtype)
for i, (row_value, row_error) in enumerate(zip(values, errors)):
for name, value, error in zip(names, row_value, row_error):
data[i][name] = value
if name not in ["Name", "Refs"]:
data[i][name+"_err"] = error
return data # or return names, values, errors
def print_balbinot_2018(logger, data):
header = "".join(["{0:<18s}".format(name) for name in data.dtype.names if "err" not in name])
ncols = sum(1 for name in data.dtype.names if "err" not in name)
logger.info("-"*18*ncols)
logger.info(header)
logger.info("-"*18*ncols)
for row in data:
info = ""
for i, name in enumerate(data.dtype.names):
if "err" not in name:
value = row[name]
error = row[name+"_err"] if name+"_err" in data.dtype.names else numpy.nan
if name == "Refs": # because cosmetics
info += " {:<5s}".format(" ")
try:
info += "{: >8.2f}".format(value)
except ValueError:
info += "{:<8s}".format(str(value))
if i == 0: continue # b/c Name does not have _err
if numpy.isfinite(error):
info += " +/- {:<5.2f}".format(error)
else:
info += " {:<5s}".format("")
logger.info(info)
logger.info("-"*18*ncols)
if __name__ == "__main__":
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format="%(message)s")
logger = logging.getLogger(__file__)
logger.info("Running {0}".format(__file__))
data = parse_balbinot_2018(logger)
logger.info("\nFound {0} GCs".format(len(data)))
logger.info("\nAvailable fields:\n{0}".format(data.dtype))
logger.info("\nAvailable clusters:\n{0}".format(data["Name"]))
# print_balbinot_2018(logger, data)