-
Notifications
You must be signed in to change notification settings - Fork 3
/
qsoSave.py
189 lines (155 loc) · 9.08 KB
/
qsoSave.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
import os
import numpy as np
from SDSSObject import SDSSObject
# Matplotlib trick
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
from matplotlib import gridspec
from matplotlib.font_manager import FontProperties
from utils import SDSSname, make_sure_path_exists
def qsoSave(obj,peak_candidates, savedir, em_lines):
# TODO: complete the function/parameters/returns
'''
qsoSave.qsoSave(obj,peak_candidates, savedir, em_lines)
=============================================
Save info and plots of QSO-Galaxy candidates
Parameters:
obj: inspected spectra
peak_candidates: Peak_candidates on the spectra
savedir: Directory to save the plots and info
em_lines: rest-frame ELG emission lines
Returns:
- Nothing. Prints peak info and save plots
'''
fileQSO = open(os.path.join(savedir, 'candidates_QSOGal.txt'), 'a')
k = 0
for peak in peak_candidates:
z_backgal = peak.redshift
#peak = peak_candidates[k]
# compute OII,OIII flux (of lensed galaxy)
temp_fluxes_OII = np.zeros(5)
temp_fluxes_OIII = np.zeros(5)
dwave = np.array([obj.wave[gen_i+1]-obj.wave[gen_i] if gen_i<len(obj.wave)-1 else 0 for gen_i in range(len(obj.wave))])
if z_backgal < 1:
for j in range(4,9):
temp_bounds = np.linspace(obj.wave2bin((1+z_backgal)*3727)-j,obj.wave2bin((1+z_backgal)*3727)+j,2*j+1,dtype = np.int16)
temp_fluxes_OII[j-4] = np.sum((obj.flux[temp_bounds]-obj.synflux[temp_bounds])*dwave[temp_bounds])
temp_bounds = np.linspace(obj.wave2bin((1+z_backgal)*5007)-j,obj.wave2bin((1+z_backgal)*5007,)+j,2*j+1,dtype = np.int16)
temp_fluxes_OIII[j-4] = np.sum((obj.flux[temp_bounds]-obj.synflux[temp_bounds])*dwave[temp_bounds])
OII_flux = np.median(temp_fluxes_OII)
OIII_flux = np.median(temp_fluxes_OIII)
fileQSO.write('\n' + str(obj.RA) + " " + str(obj.DEC) +
" " + str(obj.plate) + " " + str(obj.mjd) + " " +
str(obj.fiberid) + str(obj.spectroflux[1]) + " " +
str(obj.spectroflux[3]) + " " + str(obj.z) + " " + str(obj.rchi2) +
" " + str(peak.wavelength) + " " + str(peak.sn) +
" " + str(peak.reduced_sn) + " " + str(peak.redshift)
+ " " + str(OII_flux) + " " + str(OIII_flux) )
#fileQSO.write('\n' + str([RA[i],
# DEC[i], int(plate), int(mjd), fiberid[i], z[i], peak[0],peak[4],peak[5],peak[6],spectroflux[i,1], spectroflux[i,3], OII_flux, OIII_flux ]))
plotQSOGal(obj,peak,savedir,em_lines, k)
#plot_QSOGal(k=k,RA = RA[i],DEC= DEC[i],plate = int(plate), mjd = int(mjd), fiberid = fiberid[i],z=z[i], z_backgal= z_backgal,flux=flux[i,:],wave=wave,synflux=synflux[i,:],ivar= ivar[i,:], \
# reduced_flux = reduced_flux[i,:], c0=c0,c1=c1,Nmax=Nmax,show = plot_show, savedir=savedir, HB_wave = HB_wave , params_beta=params_beta, line_coeff =line_coeff)
# plot with +-1 Angstrom for paper
plotQSOGal(obj,peak,savedir,em_lines, k+len(peak_candidates))
plotQSOGal(obj,peak,savedir,em_lines, k+len(peak_candidates)+1)
#plot_QSOGal(k=k+len(peak_candidates),RA = RA[i],DEC= DEC[i],plate = int(plate), mjd = int(mjd), fiberid = fiberid[i],z=z[i], z_backgal= z_backgal+0.0005,flux=flux[i,:],wave=wave,synflux=synflux[i,:],ivar= ivar[i,:], \
# reduced_flux = reduced_flux[i,:], c0=c0,c1=c1,Nmax=Nmax,show = plot_show, savedir=savedir, HB_wave = HB_wave , params_beta=params_beta, line_coeff =line_coeff)
#plot_QSOGal(k=k+len(peak_candidates)+1,RA = RA[i],DEC= DEC[i],plate = int(plate), mjd = int(mjd), fiberid = fiberid[i],z=z[i], z_backgal= z_backgal-0.0005,flux=flux[i,:],wave=wave,synflux=synflux[i,:],ivar= ivar[i,:], \
# reduced_flux = reduced_flux[i,:], c0=c0,c1=c1,Nmax=Nmax,show = plot_show, savedir=savedir, HB_wave = HB_wave , params_beta=params_beta, line_coeff =line_coeff)
k+=1
fileQSO.close()
def plotQSOGal(obj, peak, savedir,em_lines, n ):
'''
qsoSave.plotQSOGal(obj,peak_candidates, savedir)
====================================
Parameters:
obj: inspected spectra
peak: Inquired peak on the spectra
savedir: Directory to save the plots and info
em_lines: rest frame ELG emission lines
n: numerotation of plots
Returns:
- Nothing. Prints peak info and save plots
'''
make_sure_path_exists(savedir +'/plots/')
z_backgal = peak.redshift
fontP = FontProperties()
fontP.set_size('medium')
plt.suptitle(SDSSname(obj.RA,obj.DEC)+'\n'+'RA='+str(obj.RA)+
', Dec='+str(obj.DEC) +', $z_{QSO}='+'{:03.3}'.format(obj.z)+ '$')
gs = gridspec.GridSpec(2,4)
p1 = plt.subplot(gs[0,:4])
smoothed_flux = np.array([np.mean(obj.flux[ii-2:ii+3])
for ii in range(len(obj.flux)) if (ii>4 and ii<len(obj.flux)-4)])
p1.plot(obj.wave[5:-4], smoothed_flux, 'k', label = 'BOSS Flux', drawstyle='steps-mid')
p1.plot(obj.wave, obj.synflux, 'r', label = 'PCA fit')
#if z<1 and show == True:
# p1.plot(HB_wave, lorentz(HB_wave, params_beta[0],params_beta[1],params_beta[2]) +HB_wave*line_coeff[0] + line_coeff[1], '--g')
box = p1.get_position()
p1.set_position([box.x0,box.y0+0.02,box.width*0.9,box.height])
p1.set_ylim(np.min(obj.synflux)-3, np.max(obj.synflux)+3)
p1.vlines(x = em_lines*(1+z_backgal),ymin= -100,ymax= 100,colors= 'g',linestyles='dashed')
p1.legend(loc='upper right', bbox_to_anchor = (1.2,1), ncol = 1, prop=fontP)
p1.set_xlim(3500,10500)
plt.ylabel('Flux [$10^{-17} erg\, s^{-1} cm^{-2} \AA^{-1}]$')
p2 = plt.subplot(gs[1,:1])
p2.vlines(x = em_lines*(1+z_backgal),ymin= -100,ymax= 100,colors= 'g',linestyles='dashed')
loc_flux =obj.flux[obj.wave2bin((1+z_backgal)*(3727-10)) : obj.wave2bin((1+z_backgal)*(3727+10))]
p2.plot(obj.wave[obj.wave2bin((1+z_backgal)*(3727-10)) :obj.wave2bin((1+z_backgal)*(3727+10))],
loc_flux,'k', label = 'OII', drawstyle='steps-mid')
p2.plot(obj.wave[obj.wave2bin((1+z_backgal)*(3727-10)) :obj.wave2bin((1+z_backgal)*(3727+10))],
obj.synflux[obj.wave2bin((1+z_backgal)*(3727-10)) :obj.wave2bin((1+z_backgal)*(3727+10))],
'r', label = 'OII', drawstyle='steps-mid')
if loc_flux != []:
p2.set_ylim(np.min(loc_flux)-1,np.max(loc_flux)+1)
plt.title('[OII] 3727')
p2.set_xlim((1+z_backgal)*(3727-10),(1+z_backgal)*(3727+10))
x1 = int((1+z_backgal)*3727)
plt.xticks([x1-15,x1,x1+15])
plt.ylabel('Flux [$10^{-17} erg\, s^{-1} cm^{-2} \AA^{-1}]$')
#If Ha is below 9500 A, show it
if obj.z>0.44:
p3 = plt.subplot(gs[1,1:4])
else:
p3 = plt.subplot(gs[1,1:3])
p3.vlines(x = em_lines*(1+z_backgal),ymin= -100,ymax= 100,colors= 'g',linestyles='dashed')
loc_flux = obj.flux[obj.wave2bin((1+z_backgal)*(4861-10)) :obj.wave2bin((1+z_backgal)*(5007+10))]
p3.plot(obj.wave[obj.wave2bin((1+z_backgal)*(4861-10)):obj.wave2bin((1+z_backgal)*(5007+10))],
loc_flux,'k', label = 'OIII, Hb', drawstyle='steps-mid')
p3.plot(obj.wave[obj.wave2bin((1+z_backgal)*(4861-10)):obj.wave2bin((1+z_backgal)*(5007+10))],
obj.synflux[obj.wave2bin((1+z_backgal)*(4861-10)):obj.wave2bin((1+z_backgal)*(5007+10))],
'r', label = 'OIII, Hb', drawstyle='steps-mid')
if loc_flux != []:
p3.set_ylim(np.min(loc_flux)-1,np.max(loc_flux)+1)
plt.title(r'H$\beta$,[OIII] 4959, [OIII] 5007')
plt.xlabel(r'Observed wavelength [$\AA$]')
p3.set_xlim((1+z_backgal)*(4861-10),(1+z_backgal)*(5007+10))
x1 = int((1+z_backgal)*4862/10.)*10
if x1<7600:
plt.xticks([x1,x1+50 , x1+100 , x1 +150 ,x1+200])
else:
plt.xticks([x1,x1+50 , x1+100 , x1 +150 ,x1+200, x1+ 250])
box = p3.get_position()
p3.set_position([box.x0+0.02,box.y0,box.width*0.9,box.height])
if obj.z<0.44:
p4 = plt.subplot(gs[1,3:4])
p4.vlines(x = em_lines*(1+z_backgal),ymin= -100,ymax= 100,colors= 'g',linestyles='dashed')
loc_flux = obj.flux[obj.wave2bin((1+z_backgal)*(6562-10)):obj.wave2bin((1+z_backgal)*(6562+10))]
p4.plot(obj.wave[obj.wave2bin((1+z_backgal)*(6562-10)):obj.wave2bin((1+z_backgal)*(6562+10))],
loc_flux,'k', label = 'Ha', drawstyle='steps-mid')
p4.plot(obj.wave[obj.wave2bin((1+z_backgal)*(6562-10)):obj.wave2bin((1+z_backgal)*(6562+10))],
obj.synflux[obj.wave2bin((1+z_backgal)*(6562-10)) :obj.wave2bin((1+z_backgal)*(6562+10))],
'r', label = 'Ha', drawstyle='steps-mid')
if loc_flux != []:
p4.set_ylim(np.min(loc_flux)-1,np.max(loc_flux)+1)
plt.title(r'H$\alpha$')
p4.set_xlim((1+z_backgal)*(6562-10),(1+z_backgal)*(6562+10))
x1 = int((1+z_backgal)*6562)
if x1 < 9900:
plt.xticks([x1-10,x1,x1+10], [str(x1-10),str(x1),str(x1+10)])
else:
plt.xticks([x1-10,x1,x1+10], [str(x1-10),'',str(x1+10)])
plt.savefig(savedir +'/plots/'+SDSSname(obj.RA,obj.DEC)+ '-' + str(obj.plate) + '-' + str(obj.mjd) + '-' + str(obj.fiberid) + '-'+str(n) +'.png')
plt.close()