-
Notifications
You must be signed in to change notification settings - Fork 0
/
testingconvolvingpsf.py
228 lines (195 loc) · 7.54 KB
/
testingconvolvingpsf.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 19 15:16:54 2018
@author: ppxee
"""
import numpy as np
from astropy.io import fits
import matplotlib.pyplot as plt
from scipy.stats import norm
from astropy.convolution import Gaussian2DKernel
from astropy.convolution import convolve
from photutils import CircularAperture
from photutils import aperture_photometry
plt.close('all')
def radial_profile(data, center):
y, x = np.indices((data.shape)) #create coordinate grid
r = np.sqrt((x - center[0])**2 + (y - center[1])**2) #get radius values for grid
r = r.astype(np.int)
tbin = np.bincount(r.ravel(), data.ravel()) # counts number of times value
# of radius occurs in the psf
# weighted by the data
nr = np.bincount(r.ravel()) # counts number of radii values in psf
radialprofile = tbin / nr # as weighted is r*data then get profile by
# dividing by unweighted counts of r values.
return radialprofile
def FWHM2sigma(FWHM, const):
''' Function to convert the FWHM of a distribution into a sigma for that
distribution. It assumes the distribution is gaussian.
Input:
FWHM = Full width half maximum of a distriubtution (in my case usually
of an object from SExtractor)
Output:
sigma = standard deviation value of a guassian distribution with the
given FWHM. This roughly equates to the psf of the object. '''
FWHM /= const
return FWHM/np.sqrt(8*np.log(2))
def fluxrad2sigma(fluxrad):
return fluxrad/np.sqrt(8*np.log(2))
def convolve_psf(sem, psf, newpsf, sigmakernel):
## Open image ###
im05B = psf[sem]
## Convolve Image ###
print('Convolving ', sem)
kernel = Gaussian2DKernel(sigmakernel)
newpsf[sem] = convolve(im05B, kernel, normalize_kernel=True)
sdata = fits.open('mag_flux_tables/stars_mag_flux_table_extra.fits')[1].data
oldsdata = fits.open('mag_flux_tables/stars_mag_flux_table.fits')[1].data
hdr08B = fits.getheader('Images/UDS_08B_K.fits') # random year (same in all)
const = -hdr08B['CD1_1'] # constant that defines unit conversion for FWHM
colname = 'FWHM_WORLD_'
#data = sem05B[colname][:,1]
semesters = ['05B', '06B', '07B', '08B', '09B', '10B', '11B', '12B']#['05B','10B']
centre = [63,63]
avgFWHM = np.zeros(8)
oldavgFWHM = np.zeros(8)
psf = {}
oldpsf = {}
for n, sem in enumerate(semesters):
# for new
colnames = colname+sem
mag = sdata['MAG_APER_'+sem][:,4]
mask1 = mag > 15 #removes saturated
mask2 = mag < 20 #removes very faint stars
mask = mask1 * mask2
tempsdata = sdata[mask]
avgFWHM[n] = np.median(tempsdata[colnames]) #* 3600
if sem == '10B':
psf[sem] = fits.open('PSFs/limited_'+sem+'_K_PSF.fits')[0].data
else:
psf[sem] = fits.open('PSFs/extra_'+sem+'_K_PSF.fits')[0].data
# for old
oldmag = sdata['MAG_APER_'+sem][:,4]
mask1 = mag > 15 #removes saturated
mask2 = mag < 20 #removes very faint stars
oldmask = mask1 * mask2
tempsdata = oldsdata[oldmask]
oldavgFWHM[n] = np.median(tempsdata[colnames]) #* 3600
oldpsf[sem] = fits.open('PSFs/limited_'+sem+'_K_PSF.fits')[0].data
### Find maximum FWHM as this is what all the others willl become ###
aimind = np.argmax(oldavgFWHM)
aimsem = semesters[aimind]
aimpsf = oldpsf[aimsem]
### Convert FWHM into a sigma ###
sigmaold = np.array([FWHM2sigma(fwhm, const) for fwhm in oldavgFWHM])
sigmabroad = sigmaold[aimind]
phot = {}
flux = np.zeros(8)
oldphot = {}
oldflux = np.zeros(8)
pixelr = (1.5/3600) / const
aperture = CircularAperture(centre, pixelr)
for n, sem in enumerate(semesters):
### Determine flux within 3 arcsec apertures ###
phot[sem] = aperture_photometry(psf[sem], aperture)
flux[n] = phot[sem]['aperture_sum'][0]
oldphot[sem] = aperture_photometry(oldpsf[sem], aperture)
oldflux[n] = oldphot[sem]['aperture_sum'][0]
plt.figure()
plt.plot(oldflux, 'bs')
plt.plot(flux,'ro')
plt.ylabel('Flux within 3 arcsec aperture')
plt.figure()
ax = plt.subplot(111)
plt.plot(oldavgFWHM, 'bs')
plt.plot(avgFWHM, 'ro')
plt.ylabel('FWHM')
ax.invert_yaxis()
### testing the extra factor method ###
tests =[0.35]# [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4]
plt.figure()
for n, extra in enumerate(tests):
# plt.subplot(4,2,n+1)
### Find required sigma ###
# sigker^2 = sigbroad^2 - signar^2
sigmakernel = np.array([np.sqrt(sigmabroad**2 - sigma**2) - extra for sigma in sigmaold])
newpsf = {}
newphot = {}
newflux = np.zeros(8)
for n, sem in enumerate(semesters):
if sem == semesters[aimind]:
newpsf[sem] = oldpsf[sem]
else:
convolve_psf(sem, oldpsf, newpsf, sigmakernel[n])
# for sem in semesters:
r = np.arange(0,90,1) * const * 3600 # define radius values
#
# plot radial profile on same plot with its model
if sem == '10B':
# find radial profiles
radialprofile = radial_profile(oldpsf[sem], centre)
# radialprofile = normalise(radialprofile)
sqrtrp = np.sqrt(radialprofile)
### Determine flux within 3 arcsec apertures ###
newphot[sem] = aperture_photometry(oldpsf[sem], aperture)
newflux[n] = newphot[sem]['aperture_sum'][0]
# plt.subplot(311)
# plt.plot(r, radialprofile, label=sem)
# plt.xlabel('Radius (arcsecs)')
# plt.ylabel('Flux')
# plt.xlim(xmax=1.5)
# # plt.legend()
# plt.subplot(312)
plt.plot(r, sqrtrp, label=sem)
plt.xlabel('Radius (arcsecs)')
plt.ylabel('sqrt(Flux)')
plt.xlim(xmax=1.5)
# plt.legend()
# plt.subplot(313)
# plt.plot(r, radialprofile, label=sem)
# plt.yscale('log')
# plt.xlabel('Radius (arcsecs)')
# plt.ylabel('log(Flux)')
# plt.xlim(xmax=1.5)
# # plt.legend()
# plt.tight_layout(pad=1)
else:
# find radial profiles
radialprofile = radial_profile(newpsf[sem], [63,63])
# radialprofile = normalise(radialprofile)
sqrtrp = np.sqrt(radialprofile)
### Determine flux within 3 arcsec apertures ###
newphot[sem] = aperture_photometry(newpsf[sem], aperture)
newflux[n] = newphot[sem]['aperture_sum'][0]
# plt.subplot(311)
# plt.plot(r, radialprofile, '--', label=sem)
# plt.xlabel('Radius (arcsecs)')
# plt.ylabel('Flux')
# plt.xlim(xmax=1.5)
# plt.legend()
# plt.subplot(312)
plt.plot(r, sqrtrp, '--', label=extra)
plt.xlabel('Radius (arcsecs)')
plt.ylabel('sqrt(Flux)')
plt.xlim(xmax=1.5)
plt.title(extra)
# plt.legend()
# plt.subplot(313)
# plt.plot(r, radialprofile, '--', label=sem)
# plt.yscale('log')
# plt.xlabel('Radius (arcsecs)')
# plt.ylabel('log(Flux)')
# plt.xlim(xmax=1.5)
# # plt.legend()
# plt.tight_layout(pad=1)
# # plot psf (logged so you can see it)
# plt.figure(4)
## plt.subplot(121)
# plt.imshow(np.log(newpsf[sem]))
# plt.title('new PSF')
# plt.figure()
# plt.plot(oldflux, 'bs')
# plt.plot(newflux,'o', label=extra)
# plt.ylabel('Flux')
# plt.legend()