-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfitspecv1.py
More file actions
64 lines (50 loc) · 1.58 KB
/
fitspecv1.py
File metadata and controls
64 lines (50 loc) · 1.58 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
# Reads and fit the redshift and flux from sdss spectra
import matplotlib
matplotlib.use('Agg')
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']
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
jname, vx, vy = spread(ispath)
print jname
peaks, _ = find_peaks(vy, height=0.15*max(vy))
olines = vx[peaks]
z, zErr = redshift(dpath, olines)
print z, zErr
# break