-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplot1Dspec.py
193 lines (165 loc) · 7.34 KB
/
plot1Dspec.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
'''
This function is designed to plot reduced DEIMOS 1D spectra.
'''
from __future__ import division
import numpy
import pylab
import pyfits
import sys
## User Input
#datapath = '/sandbox/deimos/1rxs3A/2013sep05/'
#maskname = '1rxs3A'
#slit = 23 # three digit slit number that you want to plot
#redshift = 0.23507
#which_trace = 0
#pixbin = 10
def plot1D(datapath,maskname,slit,redshift,which_trace=0,pixbin=10):
###########################
### PROGRAM
###########################
binfile = maskname+'.bintabs.fits'
# Gather the basic slit info tables from the bintabs.fits file
hdubin = pyfits.open(datapath+binfile)
# Target table information, similar to dsim .lst information
tb_targets = hdubin[1].data
# Table of slitmask information
tb_mask = hdubin[2].data
# Table of each slit's physical properties
tb_slits = hdubin[3].data
# Table that maps the id's of the previous two tables
tb_map = hdubin[4].data
#Filter the tables keeping only the current slit
if slit < 10:
slit = '00'+str(slit)
elif slit < 100:
slit = '0'+str(slit)
else:
slit = str(slit)
tb_s = tb_slits[tb_slits.field('SLITNAME')==slit]
# slitid
dslitid = tb_s.field('DSLITID')
tb_m = tb_map[tb_map.field('DSLITID')==dslitid]
# objectid
objectid = tb_m.field('OBJECTID')
tb_t = tb_targets[tb_targets.field('OBJECTID')==objectid]
# object (e.g.: DLS photometric objid)
obj = tb_t.field('OBJECT')[0]
# check if there is a 1d trace for this slit
if which_trace == 0:
try:
hdutrace = pyfits.open(datapath+'spec1d.{0}.{1}.{2}.fits'.format(maskname,slit,obj))
except IOError:
print 'plot1Dspec: Error, there is no spec1d trace file for slit number {}, exiting'.format(slit)
sys.exit()
else:
hdutrace = pyfits.open(datapath+'spec1d.{0}.{1}.{2}.fits'.format(maskname,slit,obj))
fig_title = 'spec1d.{0}.{1}.{2}.fits'.format(maskname,slit,obj)
if which_trace > 0:
try:
hdutrace = pyfits.open(datapath+'spec1d.{0}.{1}.serendip{2}.fits'.format(maskname,slit,which_trace))
except IOError:
print 'plot1Dspec: Error, there is no serendip{0} spec1d trace file for slit number {1}, exiting'.format(which_trace,slit)
sys.exit()
else:
# the file exists, read in the data
hdutrace = pyfits.open(datapath+'spec1d.{0}.{1}.serendip{2}.fits'.format(maskname,slit,which_trace))
fig_title = 'spec1d.{0}.{1}.serendip{2}.fits'.format(maskname,slit,which_trace)
# read in the blue side data
blue = hdutrace[3].data # this grabs the Horne extraction
spec_b = blue[0][0] #trace flux
lambda_b = blue[0][1] #trace observed wavelendth
# read in the red side data
red = hdutrace[4].data # this grabs the Horne extraction
spec_r = red[0][0]
lambda_r = red[0][1]
# get rid of the zeroed out spectrum values (typically just at the very ends of
# the blue and red sides
mask_b = spec_b > 0
mask_r = spec_r > 0
spec_b = spec_b[mask_b]
lambda_b = lambda_b[mask_b]
spec_r = spec_r[mask_r]
lambda_r = lambda_r[mask_r]
# trim off the first and last 10 pixels of the spectra since these are usually
# just noisy pixels
spec_b = spec_b[10:]
lambda_b = lambda_b[10:]
spec_r = spec_r[:-10]
lambda_r = lambda_r[:-10]
if pixbin > 1:
# convolve the spectra with a boxcar of size pixbin
boxcar = numpy.ones(pixbin)
spec_b = numpy.convolve(spec_b,boxcar)
spec_r = numpy.convolve(spec_r,boxcar)
# trim the new edges from the convolved spectra
# determine the added number of pixels during the convolution
pix_added = pixbin-1
trim_left = pix_added // 2
trim_right = pix_added - trim_left
spec_b = spec_b[trim_left:-trim_right]
spec_r = spec_r[trim_left:-trim_right]
fig = pylab.figure(figsize=(20,5))
pylab.plot(lambda_b,spec_b,'k')
pylab.plot(lambda_r,spec_r,'k')
xl = pylab.xlim()
yl = pylab.ylim()
#Plot common spectral lines
x_Hb = 4861*(1+redshift)
x_OIII_1 = 4960*(1+redshift)
x_OIII_2 = 5008*(1+redshift)
x_Mgb = 5176*(1+redshift)
x_FeI = 5269*(1+redshift)
x_NaD = 5893*(1+redshift)
x_NII_1 = 6548*(1+redshift)
x_Ha = 6563*(1+redshift)
x_NII_2 = 6585*(1+redshift)
x_SII = 6726*(1+redshift)
pylab.plot((x_Hb,x_Hb),yl,'--k')
pylab.plot((x_OIII_1,x_OIII_1),yl,'--k')
pylab.plot((x_OIII_2,x_OIII_2),yl,'--k')
pylab.plot((x_Mgb,x_Mgb),yl,'--k')
pylab.plot((x_FeI,x_FeI),yl,'--k')
pylab.plot((x_NaD,x_NaD),yl,'--k')
pylab.plot((x_NII_1,x_NII_1),yl,'--k')
pylab.plot((x_Ha,x_Ha),yl,'--k')
pylab.plot((x_NII_2,x_NII_2),yl,'--k')
pylab.text(x_Hb, 0.05*(yl[0]+yl[1]), 'Hb', horizontalalignment='right',verticalalignment='center', rotation='vertical')
pylab.text(x_OIII_1, 0.05*(yl[0]+yl[1]), '[OIII]', horizontalalignment='right',verticalalignment='center', rotation='vertical')
pylab.text(x_OIII_2, 0.05*(yl[0]+yl[1]), '[OIII]', horizontalalignment='right',verticalalignment='center', rotation='vertical')
pylab.text(x_Mgb, 0.05*(yl[0]+yl[1]), 'Mg I(b)', horizontalalignment='right',verticalalignment='center', rotation='vertical')
pylab.text(x_FeI, 0.05*(yl[0]+yl[1]), 'Fe I', horizontalalignment='right',verticalalignment='center', rotation='vertical')
pylab.text(x_NaD, 0.05*(yl[0]+yl[1]), 'Na I (D)', horizontalalignment='right',verticalalignment='center', rotation='vertical')
pylab.text(x_NII_1, 0.05*(yl[0]+yl[1]), '[NII]', horizontalalignment='right',verticalalignment='center', rotation='vertical')
pylab.text(x_Ha, 0.05*(yl[0]+yl[1]), 'Ha', horizontalalignment='right',verticalalignment='center', rotation='vertical')
pylab.text(x_NII_2, 0.05*(yl[0]+yl[1]), '[NII]', horizontalalignment='right',verticalalignment='center', rotation='vertical')
pylab.xlabel('$\lambda_{observed}$')
pylab.ylabel('Flux')
pylab.title(fig_title)
## Flats
## check if there is a blue side calibration for this slit, and read it in
#try:
#hducalib_b = pyfits.open(datapath+'calibSlit.{0}.{1}B.fits.gz'.format(maskname,slit))
#except IOError:
#print 'plot1Dspec: Error, there is no blue side calibration file for slit number {}, exiting'.format(slit)
#sys.exit()
#else:
#hducalib_b = pyfits.open(datapath+'calibSlit.{0}.{1}B.fits.gz'.format(maskname,slit))
## check if there is a red side calibration for this slit, and read it in
#try:
#hducalib_r = pyfits.open(datapath+'calibSlit.{0}.{1}R.fits.gz'.format(maskname,slit))
#except IOError:
#print 'plot1Dspec: Error, there is no red side calibration file for slit number {}, exiting'.format(slit)
#sys.exit()
#else:
#hducalib_r = pyfits.open(datapath+'calibSlit.{0}.{1}R.fits.gz'.format(maskname,slit))
#blue_flat = hducalib_b[1].data
#red_flat = hducalib_r[1].data
#flat_b = blue_flat[0][0]
#flat_r = red_flat[0][0]
#Nlam_b = numpy.size(lambda_b)
#Nlam_r = numpy.size(lambda_r)
#dlam_b = (lambda_b[-1]-lambda_b[0])/Nlam_b
#dlam_r = (lambda_r[-1]-lambda_r[0])/Nlam_r
#lam1_b = lambda_b[-1]+dlam_b
pylab.ylim(yl)
pylab.show()