-
Notifications
You must be signed in to change notification settings - Fork 0
/
scamp_catalog_elvis_stars.py
208 lines (156 loc) · 7.21 KB
/
scamp_catalog_elvis_stars.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" Creates a catalog populated of stars from sextracted catalogs
of single CCDs images.
Versions:
- 0.1: Initial release.
Information:
-
Todo:
* Get out columns definition. Too long for a single function.
* Improve variable nomenclature.
* Explanations are still not so easy to understand.
* POO implementation?
* Unit testing.
*GNU Terry Pratchett*
"""
from multiprocessing import Process
from sys import stdout
from pandas import concat, DataFrame, read_csv
from misc import extract_settings_elvis, check_distance, check_source
from misc_cats import extract_cats_d, create_full_cats, extract_stars_df
from misc_cats import create_scamp_df
__author__ = "Samuel Góngora García"
__copyright__ = "Copyright 2018"
__credits__ = ["Samuel Góngora García"]
__version__ = "0.1"
__maintainer__ = "Samuel Góngora García"
__email__ = "sgongora@cab.inta-csic.es"
__status__ = "Development"
def create_empty_catalog_dict():
"""
:return: cat_d
"""
cat_d = {'DITHER': [], 'CATALOG_NUMBER': [], 'X_WORLD': [], 'Y_WORLD': [],
'MAG_AUTO': [], 'A_IMAGE': [], 'B_IMAGE': [], 'THETA_IMAGE': [],
'ERRA_IMAGE': [], 'ERRB_IMAGE': [], 'MAGERR_AUTO': [],
'ERRA_WORLD': [], 'ERRB_WORLD': [], 'ERRTHETA_WORLD': [],
'CLASS_STAR': [], 'PM': [], 'PMERR': []}
return cat_d
def create_catalog():
"""
:return:
"""
stars_df = extract_stars_df()
cats_d = extract_cats_d() # extracts dataframes from catalogues
full_d = create_full_cats(cats_d) # creates dataframe from CCDs catalogues
scamp_df = create_scamp_df()
unique_sources = stars_df['IDX']
total_stars = stars_df['IDX'].size
sub_list_size = total_stars / 18
sub_list_l = []
for idx_sub_list in range(0, 18, 1):
if idx_sub_list != (18 - 1):
idx_down = sub_list_size * idx_sub_list
idx_up = sub_list_size * (idx_sub_list + 1)
sub_list_l.append(unique_sources[idx_down:idx_up])
else:
idx_down = sub_list_size * idx_sub_list
sub_list_l.append(unique_sources[idx_down:])
extract_j = []
for idx_l in range(0, 18, 1):
extract_p = Process(target=create_stars_catalog_thread,
args=(idx_l, sub_list_l[idx_l],
stars_df, full_d, scamp_df))
extract_j.append(extract_p)
extract_p.start()
active_extract = list([job.is_alive() for job in extract_j])
while True in active_extract:
active_extract = list([job.is_alive() for job in extract_j])
pass
# Merges areas
# Merges catalogs
stars_list = []
for idx_csv in range(0, 18, 1):
stars_ = read_csv('tmp_stars/stars_{}.csv'.format(idx_csv),
index_col=0)
stars_list.append(stars_)
stars_df = concat(stars_list)
stars_df.to_csv('catalogues_detected/scamp_stars.csv')
return stars_df
def create_stars_catalog_thread(idx_l, sub_list, stars_df, full_d, scamp_df):
"""
:param idx_l:
:param sub_list:
:param stars_df:
:param full_d:
:return:
"""
keys = ['ALPHA_J2000', 'DELTA_J2000']
cat_d = create_empty_catalog_dict()
total_thread = len(sub_list)
stdout.write('total stars {} of thread {}\n'.format(total_thread, idx_l))
for idx, star in enumerate(sub_list):
source_df = stars_df[stars_df['IDX'].isin([star])]
alpha = source_df['RA2000(Gaia)'].iloc[0]
delta = source_df['DEC2000(Gaia)'].iloc[0]
source_d = create_empty_catalog_dict()
for dither in range(1, 5, 1):
sex_df = check_source(full_d[dither], alpha, delta, keys)
# Should check source in Scamp too!
if sex_df.empty is not True:
# Returns the index of the closest found source
index = check_distance(sex_df, alpha, delta)
sex_df = sex_df.iloc[[index]]
cat_number = sex_df['CATALOG_NUMBER'].iloc[0]
df = scamp_df[scamp_df['CATALOG_NUMBER'].isin([cat_number])]
df = check_source(df, alpha, delta, keys)
if df.empty is not True:
source_d['DITHER'].append(dither)
catalog_number = int(sex_df['CATALOG_NUMBER'].iloc[0])
source_d['CATALOG_NUMBER'].append(catalog_number)
x_world = float(sex_df['X_WORLD'].iloc[0])
source_d['X_WORLD'].append(x_world)
y_world = float(sex_df['Y_WORLD'].iloc[0])
source_d['Y_WORLD'].append(y_world)
mag_auto = float(sex_df['MAG_AUTO'].iloc[0])
source_d['MAG_AUTO'].append(mag_auto)
magerr_auto = float(sex_df['MAGERR_AUTO'].iloc[0])
source_d['MAGERR_AUTO'].append(magerr_auto)
a_image = float(sex_df['A_IMAGE'].iloc[0])
source_d['A_IMAGE'].append(a_image)
b_image = float(sex_df['B_IMAGE'].iloc[0])
source_d['B_IMAGE'].append(b_image)
theta_image = float(sex_df['THETA_IMAGE'].iloc[0])
source_d['THETA_IMAGE'].append(theta_image)
erra_image = float(sex_df['ERRA_IMAGE'].iloc[0])
source_d['ERRA_IMAGE'].append(erra_image)
errb_image = float(sex_df['ERRB_IMAGE'].iloc[0])
source_d['ERRB_IMAGE'].append(errb_image)
erra_world = float(sex_df['ERRA_WORLD'].iloc[0])
source_d['ERRA_WORLD'].append(erra_world)
errb_world = float(sex_df['ERRB_WORLD'].iloc[0])
source_d['ERRB_WORLD'].append(errb_world)
errtheta_world = float(sex_df['ERRTHETA_WORLD'].iloc[0])
source_d['ERRTHETA_WORLD'].append(errtheta_world)
class_star = float(sex_df['CLASS_STAR'].iloc[0])
source_d['CLASS_STAR'].append(class_star)
pm = float(df['PM'].iloc[0])
source_d['PM'].append(pm)
pmerr = float(df['PMERR'].iloc[0])
source_d['PMERR'].append(pmerr)
if len(source_d['DITHER']) != 0:
for key_ in source_d.keys():
for value_ in source_d[key_]:
cat_d[key_].append(value_)
cat_df = DataFrame(cat_d, columns=['DITHER', 'CATALOG_NUMBER',
'X_WORLD', 'Y_WORLD', 'MAG_AUTO',
'MAGERR_AUTO', 'A_IMAGE', 'B_IMAGE',
'THETA_IMAGE', 'ERRA_IMAGE',
'ERRB_IMAGE', 'ERRA_WORLD',
'ERRB_WORLD', 'ERRTHETA_WORLD',
'CLASS_STAR', 'PM', 'PMERR'])
cat_df.to_csv('tmp_stars/stars_{}.csv'.format(idx_l))
if __name__ == "__main__":
prfs_dict = extract_settings_elvis()
catalogue = create_catalog()