-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsavart.py
305 lines (263 loc) · 12 KB
/
savart.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import random
import astropy
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from photutils import DAOStarFinder
from astropy.stats import sigma_clipped_stats
from os import listdir
from os.path import isfile, join
from photutils import CircularAperture
from astropy.visualization import SqrtStretch
from astropy.visualization.mpl_normalize import ImageNormalize
from astropy.visualization import simple_norm
from astropy.table import Table, setdiff
import math
import configparser
import os
import sys
import logging
from datetime import date
from astropy.coordinates import SkyCoord
import subprocess as sub
from astropy import wcs
def get_hdr(hdr):
ra = hdr['RA']
dec = hdr['DEC']
coo = SkyCoord(ra=ra, dec=dec, frame='icrs', unit=('hour','deg'))
return coo
def run_astrometry(image_path, coo, **kwargs):
logging.info('ASTROMETRY: start')
image_base_name = os.path.basename(image_path)
solve_field_command = [
'docker', 'exec', 'nova', 'solve-field',
'--ra', '%s' % coo.ra.deg,
'--dec', '%s' % coo.dec.deg,
'--radius', '1',
'--depth', '700',
'--cpulimit', '3000',
'--scale-units', 'arcsecperpix',
'--scale-low', '0.8',
'--scale-high', '1.3',
'--x-column', 'x',
'--y-column', 'y',
'--width', '1024',
'--height', '1024',
'--overwrite', '--no-verify', '--no-plots',
'/data_market/' + image_base_name + 'new.fits']
text_fits_command = [
'docker', 'exec', 'nova', 'text2fits',
'-f', 'ff',
'-H', 'x y',
'/data_market/' + image_base_name,
'/data_market/' + image_base_name + 'new.fits']
print(image_base_name)
print(text_fits_command)
p = sub.Popen(text_fits_command, stdout=sub.PIPE,
stderr=sub.PIPE)
output, error = p.communicate()
print(output)
print(error)
sub.Popen(solve_field_command, stdout=sub.PIPE,
stderr=sub.PIPE).communicate()
wcs_file_path = './coordinates_output_table/' + image_base_name + 'new.wcs'
if os.path.exists(wcs_file_path):
logging.info('ASTROMETRY: success')
return wcs_file_path
logging.info('ASTROMETRY: FAILED')
return None, False
def get_ra_dec_table(ghost_table, wcs_path, file_name):
print(wcs_path)
hdr = fits.open(wcs_path)[0].header
w = wcs.WCS(hdr)
x = ghost_table['real_xcentroid'].data
y = ghost_table['real_ycentroid'].data
xyli = np.c_[x,y]
world = w.wcs_pix2world(xyli, 1)
ghost_table['RA'] = world[:,0]
ghost_table['DEC'] = world[:,1]
print(ghost_table)
file_name = 'coordinates_output_table/' + os.path.basename(file_name).split('.')[0]
ghost_table.write(file_name + '_ghosts', format='ascii', overwrite=True)
def open_image(my_path):
""" Returns fits data from path provided by the user.
"""
logging.info('Opening image data')
image_data = fits.getdata(my_path)
hdul = fits.open(my_path)
hdr = hdul[0].header
logging.info('Finished')
return image_data, hdr
def create_mask(image_data):
mask = np.zeros_like(image_data, dtype=bool)
mask[...] = False
return mask
def star_data_gatherer(image_data, mask, **kwargs):
""" finds stars based on config parameters and returns list sorted by flux
"""
logging.info('Finding stars on image...')
fwhm = kwargs['fwhm']
threshold = kwargs['threshold']
sigma = kwargs['sigma']
mean, median, std = sigma_clipped_stats(image_data, sigma=sigma) #calculation of mean, median and standard deviation from the data
daofind = DAOStarFinder(fwhm=fwhm, threshold=threshold)
sources = daofind(image_data - median, mask=mask)
norm = simple_norm(image_data, 'sqrt', percent=kwargs['norm_percent'])
sources.sort('flux')
logging.info('Found %s stars', str(len(sources)))
logging.info('Finished')
return sources, norm, mask
def plot_photo(image_data, mask, norm, **kwargs):
""" Plots a photo from fits image data.
"""
logging.info('Ploting...')
plt.rcParams['figure.figsize'] = (20,10)
plt.imshow(image_data, cmap='Greys', norm=norm)
plt.imshow(mask, interpolation='none', alpha=kwargs['alpha'])
plt.show()
def get_ghost_distance(**kwargs):
savart_radian_angle = np.deg2rad(kwargs['angle']) #changes angle value of star line to radians
x_distance = kwargs['savart_distance'] * np.cos(savart_radian_angle) #calculates the distance in X axis
y_distance = kwargs['savart_distance'] * np.sin(savart_radian_angle) #and Y axis from possible star ghosts
return x_distance, y_distance
def ghostbuster_2(sources, mask, **kwargs):
logging.info('Masking ghost with ghostbuster_2')
right_x, right_y = kwargs['right_x'],kwargs['right_y']
logging.info('X, Y shifts = %s %s', str(right_x), str(right_y))
square_size = kwargs['square_size']
for row in sources:
mask[int(row['ycentroid']+right_y)-10:int(row['ycentroid']+right_y)+10,
int(row['xcentroid']+right_x)-10:int(row['xcentroid']+right_x)+10] = True
logging.info('Ghosts masked')
return mask
def sources_to_list(sources, ghost_xy, file_name):
logging.info('Saving normal and ghosts list')
file_name = 'coordinates_output_table/' + os.path.basename(file_name).split('.')[0]
sources.write(file_name, format='ascii', include_names = ['xcentroid','ycentroid'],overwrite=True)
with open(file_name, 'r') as fin:
data = fin.read().splitlines(True)
with open(file_name, 'w') as fout:
fout.writelines(data[1:])
ghost_xy.write(file_name + '_ghosts', format='ascii', overwrite=True)
logging.info('Saved normal and ghosts list')
return file_name
def ghostbuster(all_sources, clean_sources, **kwargs):
""" Ghostbuster finds stars duplicated by savart plate
"""
logging.info('Looking for ghosts started..')
chosen_stars = clean_sources.copy() #creates empty astropy.table object, where we can store our chosen stars
coordinates_chart = Table(names=('ID','real_xcentroid', 'real_ycentroid',
'ghost_xcentroid', 'ghost_ycentroid'), dtype=('i4', 'f8', 'f8', 'f8', 'f8'))
ghost_sources = setdiff(all_sources, clean_sources, keys=['xcentroid', 'ycentroid'])
i = 0
for star in chosen_stars:
""" using ghostfinder to find a star that has te closest values of x and y distances
"""
real, ghost = ghostfinder(star, ghost_sources, **kwargs)
if ghost == 'no_match':
pass
else:
coordinates_chart.add_row([int(i), real['xcentroid'], real['ycentroid'],
ghost['xcentroid'], ghost['ycentroid']])
i += 1
logging.info('Found %s ghosts', str(len(coordinates_chart)))
logging.info('Finished')
return(coordinates_chart)
def ghostfinder(star, ghost_sources, **kwargs):
""" Used by ghostbuster to find the star and define which one is a ghost
"""
distance = kwargs['distance'] #variable for cleaner coding
right_x, right_y, = star['xcentroid'] + kwargs['right_x'], star['ycentroid'] + kwargs['right_y']
for possible_ghost in ghost_sources:
if abs(possible_ghost['xcentroid'] - right_x) < distance and abs(possible_ghost['ycentroid'] - right_y) < distance:
return star, possible_ghost
return star, 'no_match'
def get_apertures(sources):
logging.info('Creating apertures')
positions_real = np.transpose((sources['real_xcentroid'], sources['real_ycentroid']))
positions_ghost = np.transpose((sources['ghost_xcentroid'], sources['ghost_ycentroid']))
for number, (pos_real, pos_ghost) in enumerate(zip(positions_real, positions_ghost)):
apertures = CircularAperture([pos_real, pos_ghost], r=4)
plt.text(pos_ghost[0]+5, pos_ghost[1]+5, number)
plt.text(pos_real[0]+5, pos_real[1]+5, number)
apertures.plot(color=np.random.rand(3,))
logging.info('Plotting apertures for %s stars', str(len(sources)))
logging.info('Finished')
return apertures
def read_config():
logging.info('Reading config.ini file')
path = 'config.ini'
config = configparser.ConfigParser()
config.readfp(open('config.ini'))
config_dict = {
'fwhm': int(config['DEFAULT']['fwhm']),
'threshold': int(config['DEFAULT']['threshold']),
'sigma': int(config['DEFAULT']['sigma']),
'min_star_limit': int(config['DEFAULT']['min_star_limit']),
'norm_percent': float(config['DEFAULT']['norm_percent']),
'plot_no_mask': config['EXECUTABLE_PARAMETERS']['plot_no_mask'],
'plot_mask': config['EXECUTABLE_PARAMETERS']['plot_mask'],
'distance': float(config['EXECUTABLE_PARAMETERS']['search_distance']),
'square_size': int(config['EXECUTABLE_PARAMETERS']['mask_square_size']),
'alpha': float(config['PLOT_PARAMETERS']['alpha']),
'figsize': (config['PLOT_PARAMETERS']['figsize_rcparams_width'], config['PLOT_PARAMETERS']['figsize_rcparams_height']),
'draw_aperture': config['PLOT_PARAMETERS']['draw_aperture'],
'save_no_mask': config['PLOT_PARAMETERS']['save_plot_no_mask'],
'save_mask': config['PLOT_PARAMETERS']['save_plot_mask'],
'save_aperture': config['PLOT_PARAMETERS']['save_plot_aperture'],
}
if sys.argv[2] == 'p1':
config_dict['savart_distance'] = config['SAVART_PARAMETERS_P1']['savart_distance'],
config_dict['angle'] = config['SAVART_PARAMETERS_P1']['angle'],
config_dict['right_x'] = float(config['SAVART_PARAMETERS_P1']['x_distance']),
config_dict['right_y'] = float(config['SAVART_PARAMETERS_P1']['y_distance']),
elif sys.argv[2] == 'p3':
config_dict['right_x'] = float(config['SAVART_PARAMETERS_P3']['x_distance']),
config_dict['angle'] = config['SAVART_PARAMETERS_P1']['angle'],
config_dict['savart_distance'] = config['SAVART_PARAMETERS_P3']['savart_distance'],
config_dict['right_y'] = float(config['SAVART_PARAMETERS_P3']['y_distance']),
logging.info('Finished')
return config_dict
def exec():
logging.basicConfig(filename='logger.log', level=logging.DEBUG)
logging.info('----------- %s -----------', date.today())
file_path = sys.argv[1]
if os.path.isfile(file_path):
logging.info('Opening file: %s', str(os.path.basename(file_path)))
print(os.path.basename(file_path))
else:
logging.ERROR('File does not exist')
print('File does not exist')
sys.exit()
image_data, hdr = open_image(file_path)
coo = get_hdr(hdr)
file_name = os.path.basename(file_path)
file_name = file_name.split('.')[0]
config = read_config()
all_sources, norm, mask = star_data_gatherer(image_data, create_mask(image_data), **config)
if config['plot_no_mask'] == 'True':
plot_photo(image_data, mask, norm, **config)
logging.info('Ploted image without masks')
if config['save_no_mask'] == True:
plt.savefig('plots_no_mask/' + str(file_name.strip) + '.png')
mask = ghostbuster_2(all_sources, mask, **config)
clean_sources, norm, mask = star_data_gatherer(image_data, mask, **config)
logging.info('Found real stars')
if config['plot_mask'] == 'True':
plot_photo(image_data, mask, norm, **config)
logging.info('Plotted image with masked ghosts')
if config['save_mask'] == 'True':
plt.savefig('plots_mask/' + str(file_name) + '_mask.png')
ghost_xy = ghostbuster(all_sources, clean_sources, **config)
if config['draw_aperture'] == 'True':
get_apertures(ghost_xy)
plot_photo(image_data,create_mask(image_data), norm, **config)
if config['save_aperture'] == 'True':
plt.savefig('plots_aperture/' + str(file_name) + '_ap.png')
nova_path = sources_to_list(clean_sources, ghost_xy, file_path)
wcs = run_astrometry(nova_path, coo)
get_ra_dec_table(ghost_xy, wcs, file_path)
if __name__ == '__main__':
mpl_logger = logging.getLogger('matplotlib')
mpl_logger.setLevel(logging.WARNING)
exec()