This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
star_reader.py
81 lines (58 loc) · 2.14 KB
/
star_reader.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
import csv
from timeit import default_timer as timer
from star import Star
STAR_CLASSES = 'OBAFGKMC'
KEPT_DATA = ['rv', 'absmag', 'ci', 'lum']
def make_star(header, row, fields=None):
data = {}
types = []
for field, value in zip(header, row):
try:
num = float(value)
if num == int(num):
num = int(num)
else:
num = round(num, 2)
value = num
except ValueError:
if value == '':
value = None
if field == 'dist' and value >= 100000:
# Discarding star with dubious value
return None
if field == 'spect':
if value is None:
# Discarding unclassified star
return None
type_list = value.split('/')
# now, for each star_type in typelist
# and each sp_type in STAR_CLASSES
# add sp_type to the list if
# sp_type in star_type.upper()
# Basically, look if the star class letters appear in each possible star type, and add them, ignoring empty star types
types = [sp_type if star_type and sp_type in star_type.upper() else '' for star_type in type_list for sp_type in STAR_CLASSES]
value = ''.join(set(types))
if value == '':
return None
data[field] = value
display_name = data['proper'] or data['bf'] or ('ID ' + str(data['id']))
fields = fields or header
return Star(data['spect'], display_name, data, fields)
def read_stars(fields=KEPT_DATA):
print("Parsing stars...")
star_list = []
header = None
t_start = timer()
with open('hygdata_v3.csv', 'r') as csv_file:
reader = csv.reader(csv_file)
header = next(reader)
for row in reader:
star = make_star(header, row, fields)
if star is not None:
star_list.append(star)
csv_file.close()
t_end = timer()
print("Parsed {} stars.\nElapsed time: {:.3f}\n".format(len(star_list), t_end-t_start))
return star_list, fields or header
if __name__ == "__main__":
read_stars()