-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathband.py
147 lines (108 loc) · 4.42 KB
/
band.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
#!/usr/bin/env python
""" Module of bits to process mags to fluxes and spectra to band fluxes """
from __future__ import print_function
import scipy as sp
import numpy as np
from scipy import interpolate
from scipy.interpolate import interp1d
__author__ = "Ben Burningham"
__copyright__ = "Copyright 2015 - Ben Burningham"
__credits__ = ["Ben Burningham", "The EMCEE DOCS"]
__license__ = "GPL"
__version__ = "0.1"
__maintainer__ = "Ben Burningham"
__email__ = "[email protected]"
__status__ = "Development"
""" This is all about taking a magnitude and getting total flux through a filter
or taking a spectrum and getting the same.
All in Vega until further notice.
Vega fluxes are hardwired for speed.
Units are W/m2/um
Where not calculated WISE details are taken from Jarrett et al (2011)
"""
def getfilt(filtname):
if filtname == "Jmko":
rawfilt = np.loadtxt("UKIRT-UKIDSS.J.dat", unpack=True, skiprows=0)
tempfilt = rawfilt[1, :] * rawfilt[0, :]
rawfilt[1, :] = tempfilt / np.amax(tempfilt)
elif filtname == "Hmko":
rawfilt = np.loadtxt("UKIRT-UKIDSS.H.dat", unpack=True, skiprows=0)
tempfilt = rawfilt[1, :] * rawfilt[0, :]
rawfilt[1, :] = tempfilt / np.amax(tempfilt)
elif filtname == "nirc_Lp":
rawfilt = np.loadtxt("nirc_Lp.txt", unpack=True, skiprows=1)
bw = 0.700
isow = 3.776
tempfilt = rawfilt[1, :] * rawfilt[0, :]
rawfilt[1, :] = tempfilt / np.amax(tempfilt)
elif filtname == "w1":
rawfilt = np.loadtxt("RSR-W1.EE.txt", unpack=True, skiprows=0)
bw = 6.6256e-01
isow = 3.35
elif filtname == "w2":
rawfilt = np.loadtxt("RSR-W2.EE.txt", unpack=True, skiprows=0)
bw = 1.0423
isow = 4.60
elif filtname == "w3":
rawfilt = np.loadtxt("RSR-W3.EE.txt", unpack=True, skiprows=0)
bw = 5.5069
isow = 11.56
elif filtname == "w4":
rawfilt = np.loadtxt("RSR-W4.EE.txt", unpack=True, skiprows=0)
bw = 4.1013
isow = 22.08
else:
print("Filter ", filtname, " not recognised")
return np.nan
return rawfilt, bw, isow
def mag2flux(mag, magerr, filtname, iso=False):
rawfilt, bw, isow = getfilt(filtname)
# First trim vega to match filter, and put filter on same wave grid as vega
rawvega = np.loadtxt("STSci_Vega.txt", unpack=True, skiprows=0)
rawvega[0, :] = rawvega[0, :] / 10000.
rawvega[1, :] = rawvega[1, :] * 10. # erg/cm/s/A to W/m2/um
w1 = rawfilt[0, 0]
w2 = rawfilt[0, rawfilt.shape[1] - 1]
vega = rawvega[:, np.logical_not(np.logical_or(rawvega[0, :] > w2, rawvega[0, :] < w1))]
filt = np.zeros_like(vega)
wfit = sp.interpolate.splrep(rawfilt[0, :], rawfilt[1, :], s=0)
filt[0, :] = vega[0, :]
filt[1, :] = sp.interpolate.splev(filt[0, :], wfit, der=0)
# Now we'll sum flux across the filter
# need the bin width first
sbin = np.zeros(filt.shape[1])
for i in range(1, filt.shape[1]-1):
sbin[i] = ((vega[0, i] - vega[0, i-1]) + (vega[0, i+1] - vega[0, i])) / 2.
# Deal with the ends...
sbin[0] = sbin[1]
sbin[sbin.size-1] = sbin[sbin.size-2]
bandvega = sum(sbin*filt[1, :]*vega[1, :])
bandflux = bandvega * 10.**(-mag/2.5)
banderr = abs(bandflux * (-1./2.5) * np.log(10) * magerr)
if iso:
isoflux = bandflux / bw
isofluxerr = banderr / bw
return isow, isoflux, isofluxerr
return bandflux, banderr
def spec2flux(rawspec, filtname, iso=False):
rawfilt, bw, isow = getfilt(filtname)
# Now trim target spectrum to wavelength region of filter, and rebin filter onto new target grid
w1 = rawfilt[0, 0]
w2 = rawfilt[0, rawfilt.shape[1] - 1]
trimspec = rawspec[:, np.logical_not(np.logical_or(rawspec[0, :] > w2, rawspec[0, :] < w1))]
filt = np.zeros_like(trimspec)
filt[0, :] = trimspec[0, :]
wfit = sp.interpolate.splrep(rawfilt[0, :], rawfilt[1, :], s=0)
filt[1, :] = sp.interpolate.splev(filt[0, :], wfit, der=0)
# Now we want to sum over the filter
sbin = np.zeros(filt.shape[1])
for i in range(1, filt.shape[1]-1):
sbin[i] = ((trimspec[0, i] - trimspec[0, i-1]) + (trimspec[0, i+1] - trimspec[0, i])) / 2.
# Deal with the ends...
sbin[0] = sbin[1]
sbin[sbin.size-1] = sbin[sbin.size-2]
bandflux = sum(sbin*filt[1, :]*trimspec[1, :])
if iso:
isoflux = bandflux / bw
return isow, isoflux
return bandflux