-
Notifications
You must be signed in to change notification settings - Fork 40
/
oneSource.py
162 lines (133 loc) · 5.61 KB
/
oneSource.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
import os, sys
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
from lightkurve import KeplerTargetPixelFile as ktpf
from astropy.wcs import WCS
from astropy.io import fits
import astropy.units as u
from ellie import find_sources as ctpf
def files_in_dir(dir):
""" Finds all FITS files in a given directory """
""" Returns dir+filenames list """
fns = np.array(os.listdir(dir))
fns = fns[np.array([i for i,item in enumerate(fns) if "fits" in item])]
fns = [dir+i for i in fns]
return sort_by_date(fns)
def sort_by_date(fns):
""" Sorts FITS files by start date of observation """
""" Returns: sorted filenames """
dates = []
for f in fns:
mast, header = fits.getdata(f, header=True)
dates.append(header['DATE-OBS'])
dates, fns = np.sort(np.array([dates, fns]))
return fns
def find_camera_chip(id, pos):
"""
Uses center of each camera/chip pair and position of source to find
where the source is located in
Parameters
----------
id: ID of the source
pos: [RA,Dec] position of the source
Returns
----------
dir: directory the files are in on the users computer
xy : translated [RA,Dec] into [x,y] coordinates in file
camera : the camera number
chip : the chip number
"""
for camera in np.arange(1,5,1):
for chip in np.arange(1,5,1):
dir = './2019/2019_1_{}-{}/ffis/'.format(camera, chip)
files = files_in_dir(dir)
mast, mheader = fits.getdata(files[0], header=True)
xy = WCS(mheader).all_world2pix(pos[0], pos[1], 1, quiet=True)
if xy[0] >= 0. and xy[0] <= len(mast) and xy[1] >= 0. and xy[1] <= len(mast[0]):
return files, xy, camera, chip
return 'None', [], 0, 0
def edit_header(output_fn, tic_id, gaia_id, pos, xy):
""" Adds extra comments to each TPF header """
""" Returns """
names = ['TIC_ID', 'GAIA_ID', 'CEN_RA', 'CEN_DEC', 'CEN_X', 'CEN_Y']
values = [tic_id, gaia_id, pos[0], pos[1], float(xy[0]), float(xy[1])]
for i in range(len(values)):
fits.setval(output_fn, str(names[i]), value=values[i])
return
def from_class(id, mission):
# """ Get crossmatching information """
# """ Returns: table """
if mission == 'tic':
locate = ctpf(tic=id)
tic_id, pos, tmag = locate.tic_pos_by_ID()
locate = ctpf(tic=id, pos=pos)
table = locate.find_by_position()
# elif mission == 'gaia':
# locate = ctpf(gaia=id)
# gaia_id, pos, gmag, pmra, pmdec, plx = locate.gaia_pos_by_ID()
# locate = ctpf(gaia=id, pos=pos)
# table = locate.find_by_position()
return table
def add_shift(tpf):
""" Creates an additional shift to put source at (4,4) of TPF file """
""" Returns: TPF """
tpf_init = tpf.flux[0]
tpf_com = tpf_init[2:7, 2:7]
com = ndimage.measurements.center_of_mass(tpf_com.T - np.median(tpf_com))
shift = [com[0]-2, com[1]-2]
shift = [np.round(shift[0]+4.0,0), np.round(shift[1]+4.0,0)]
return shift
def find_source_of_mag(files):
mast, header = fits.getdata(files[0], header=True)
pos = [header['CRVAL1'], header['CRVAL2']]
print(pos)
find = ctpf(pos=pos)
sources = find.cone_search(3*np.sqrt(2), 'Mast.Catalogs.Tic.Cone')
inds = np.where(sources['Tmag'].data <= 9.0)
sources = sources[inds]
xy = WCS(header).all_world2pix(sources['ra'].data, sources['dec'].data, 1)
close = []
for i in range(len(sources)):
x, y = xy[0][i], xy[1][i]
tempX = np.delete(xy[0], np.where(xy[0]==x))
tempY = np.delete(xy[1], np.where(xy[1]==y))
closest = np.sqrt( (x-tempX)**2 + (y-tempY)**2 ).argmin()
if np.sqrt( (x-tempX[closest])**2 + (y-tempY[closest])**2 ) <= 3.0:
close.append(i)
close = np.array(close)
return sources[close[0]], [xy[0][close[0]], xy[1][close[0]]]
def main(id, mission):
""" Temporary main function """
info = from_class(id, mission)
pos = [info['RA'].data[0], info['Dec'].data[0]]
files, xy, camera, chip = find_camera_chip(id, pos)
# Shifts (x,y) coordinates based on pointing model
initShift = np.loadtxt('pointingModel_{}-{}.txt'.format(camera, chip), skiprows=1,
usecols=(1,2,3))[0]
initShift[0] = np.radians(initShift[0])
x = xy[0]*np.cos(initShift[0]) - xy[1]*np.sin(initShift[0]) - initShift[1]
y = xy[0]*np.sin(initShift[0]) + xy[1]*np.cos(initShift[0]) - initShift[2]
# Creates first TPF
tpf = ktpf.from_fits_images(images=files, position=[x,y], size=(9,9))
# Shifts (x,y) coordinates to center on centroid
xy_new = add_shift(tpf)
xy_new_start = [x+xy_new[0]-4.0, y+xy_new[1]-4.0]
new_tpf = ktpf.from_fits_images(images=files, position=xy_new_start, size=(9,9))
# Converts new (x,y) center to world coordinates
flux, header = fits.getdata(files[0], header=True)
pos = WCS(header).all_pix2world(xy_new_start[0], xy_new_start[1], 1)
pos = [float(pos[0]), float(pos[1])]
# Saves TPF to FITS file and edits header
output_fn = 'TIC{}_tpf.fits'.format(id)
# new_tpf.to_fits(output_fn=output_fn)
print(xy_new_start)
mast = fits.getdata(files[0])
plt.imshow(mast, vmin=50, vmax=200, origin='lower')
plt.show()
# print(info)
# edit_header(output_fn, int(info['TIC_ID']), int(info['Gaia_ID']), pos, xy_new_start)
#main(219870537, 'tic')
#main(229669377, 'tic')
#main(420888018, 'tic')
#main(198593129, 'tic')