-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfitspec.py
More file actions
86 lines (71 loc) · 2.34 KB
/
fitspec.py
File metadata and controls
86 lines (71 loc) · 2.34 KB
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
# Reads and fit the redshift and flux from sdss spectra
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import os
from os import listdir
from os.path import isfile, join
from astropy.io import fits
from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.table import Table
import numpy as np
from scipy.signal import find_peaks
def spread(spath):
hdul = fits.open(spath)
data = hdul[1].data
# print hdul.info()
# print(repr(hdul[1].header))
RA = hdul[1].header['RAOBJ']
DEC = hdul[1].header['DECOBJ']
Z = hdul[1].header['Z']
ZERR = hdul[1].header['Z_ERR']
vflux = data.flux
vwave = data.wavelength
c = SkyCoord(ra=RA*u.degree, dec=DEC*u.degree, frame='icrs')
jname = 'J{0}{1}'.format(c.ra.to_string(unit=u.hourangle, sep='' \
, precision=0, pad=True), c.dec.to_string(sep='', precision=0 \
, alwayssign=True, pad=True))
return jname, vwave, vflux
def redshift(dpath, lines):
lpath = dpath + 'indat/lines.dat'
ldat = Table.read(lpath, format='csv')
elin = ldat['Air']
olin = sorted(lines, reverse = True)
ix = min(len(ldat), len(olin))
vz = (olin[0:ix] - elin[0:ix])/elin[0:ix]
z = np.mean(vz)
zErr = np.std(vz)
return z, zErr
# Main Code
mcd = os.path.dirname(os.path.abspath(__file__))
dpath = mcd + '/dat/'
spath = dpath + 'indat/spec/DR8spectra/'
nspec = [f for f in listdir(spath) if isfile(join(spath, f))]
for i in nspec:
ispath = spath + i
# print ispath
jname, vx, vy = spread(ispath)
print jname
peaks, _ = find_peaks(vy, height = 0.15*max(vy))
olines = vx[peaks]
z, zErr = redshift(dpath, olines)
# Ploting
path = dpath + 'results/sp' + jname + '.pdf'
xmin = 3500.0
xmax = 9500.0
ymin = min(vy) - 0.05*max(vy)
ymax = max(vy) + 0.05*max(vy)
xl = "$\lambda\ (\mathrm{\AA})$"
yl = "$f(\lambda)\ (10^{-17} \mathrm{erg\ s^{-1}\ cm^{-2}\ \AA^{-1}}$)"
plt.figure()
plt.plot(vx, vy, '-', lw = 1, c = 'black', drawstyle='steps')
plt.plot(vx[peaks], vy[peaks], "x", markersize=8)
plt.text(3550.0, ymax - 0.05*ymax, r'z = ' + str("%5.4f" % z) + \
r'$\pm$' + str("%5.5f" % zErr))
plt.axis([xmin, xmax, ymin, ymax])
plt.xlabel(xl, fontsize=14)
plt.ylabel(yl, fontsize=14)
plt.savefig(path)
plt.close()
# break