-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefSD.py
387 lines (327 loc) · 18.3 KB
/
DefSD.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# coding: utf-8
# In[1]:
import numpy as np #Herramientas paa manejar arreglos multidimensionales
import matplotlib.pyplot as plt #Gráficos, histogramas, gráfico de dispersión
from astropy import coordinates as coords #Conversion, sistemas y marcos de referencia
from astropy import units as u #Conversion y desarrollo de operaciones aritméticas de instancias Quantity
from astropy.io import fits
from astropy.table import Table, Column ,vstack
from astropy.modeling import models, fitting #Representación y ajuste de modelos en 1D y 2D
from scipy.integrate import quad
import pyneb as pn
import random
from scipy.integrate import simps
import sys
import traceback
import math as mt
# In[ ]:
# ----------------------------------------------------------------------------------------------------------------------
gaussian = lambda x,a,b,c : a * np.exp(-0.5* (x - b)**2 / c**2)
diags = pn.Diagnostics()
# In[ ]:
# ----------------------------------------------------------------------------------------------------------------------
def FWHM(X,Y):
half_max = (max(Y)+min(Y)) / 2.
#find when function crosses line half_max (when sign of diff flips)
#take the 'derivative' of signum(half_max - Y[])
d = np.sign(half_max - np.array(Y[0:-1])) - np.sign(half_max - np.array(Y[1:]))
#plot(X,d) #if you are interested
#find the left and right most indexes
left_idx = np.where(d > 0)[0]
right_idx = np.where(d < 0)[-1]
return X[right_idx] - X[left_idx] #return the difference (full width)
# In[ ]:
# ----------------------------------------------------------------------------------------------------------------------
# a function that obtain the slope and the intersection given two points within it
def linModel(x0, y0, x1, y1):
m=(y1-y0)/(x1-x0)
b=y1-m*x1
return m, b
# linear function with an np.array : x, a slope : a and intersection : c
def lines(x, a, c):
return a*x + c
lorentzFunc = lambda x,A,f,x0 : (A*f**2)/(f**2 + (x-x0)**2)
#def lorentzFunc(lal, A, f, x0):
# return (A*f**2)/(f**2 + (x-x0)**2)
# ----------------------------------------------------------------------------------------------------------------------
def fit2(i, spec,lambd,ListLines,ListGal,iterr):
lineP = lineProfile(i,spec,lambd,ListLines,ListGal)
f1 = fitting.LevMarLSQFitter()
Lorentz = []
lineStdDev = 0.5 #3.5
for x in range(len(ListLines)):
lineAmplitude = ListGal[i]['flux'][lineP['lambda'][x]]
v = np.where((lambd >= ListLines['LAMBDA VAC ANG'][x]-1) & (lambd <= ListLines['LAMBDA VAC ANG'][x]+1))
ampMax= ListGal[i]['flux'][v[0][0]]
Lorentz.append(models.Lorentz1D(amplitude=lineAmplitude,x_0=ListLines['LAMBDA VAC ANG'][x],fwhm=2.355*lineStdDev, bounds={'amplitude':(0, ampMax)}))
sum_Lorentz=Lorentz[0]+Lorentz[1]+Lorentz[2]+Lorentz[3]+Lorentz[4]+Lorentz[5]+Lorentz[6]+Lorentz[7]+Lorentz[8]+Lorentz[9]+Lorentz[10]+Lorentz[11]+Lorentz[12]
sum_Lo2=Lorentz[0]+Lorentz[1]
if len(ListLines) >= 14:
sum_Lorentz=Lorentz[0]+Lorentz[1]+Lorentz[2]+Lorentz[3]+Lorentz[4]+Lorentz[5]+Lorentz[6]+Lorentz[7]+Lorentz[8]+Lorentz[9]+Lorentz[10]+Lorentz[11]+Lorentz[12]+Lorentz[13]+Lorentz[14]
lorentz_fit = f1(sum_Lorentz, lambd, ListGal[i]['flux']-spec, maxiter=iterr)
lorentz_fit2 = f1(sum_Lo2, lambd, ListGal[i]['flux'] -spec, maxiter=iterr)
LOR=[]
LOR.append(lorentz_fit)
LOR.append(lorentz_fit2)
return LOR
# ----------------------------------------------------------------------------------------------------------------------
# Obtain the index (Table linePr) of the feet line profile given a index of the galaxy : i,
# the continuum spectrum np.array : spec, the wavelenghts : lambd, data of the lines : ListLines,
# the list of spectrum galaxies : ListGal, and minimum resolution of the spectrum : limResoluc
# Aqui encontré muchos problemas porque cuando se encuentran las lineas acopladas de OII en 3726-29
# se debe encontrar los límites de los parámetros
def lineProfile(i,spec, lambd,ListLines,ListGal, limResoluc):
linePr = Table(names=('lambda', 'inf', 'sup'), dtype=('i','i', 'i'))
for c in range(0, len(ListLines)):
lambdLine=ListLines['LAMBDA VAC ANG'][c] #Líneas obtenidas de la base de datos Astroquery
v = np.where((lambd >= lambdLine-limResoluc) & (lambd <= lambdLine+limResoluc))
if len(v[0]) == 0:
ind = 0
l_inf = 0
l_sup = 0
iM=0
else:
ind = 1
try:
f = FWHM(lambd[v[0][0]-ind:v[0][0]+ind], ListGal[i][v[0][0]-ind:v[0][0]+ind])
except IndexError:
f=[]
while (len(f)==0):
ind = ind+1
try:
f = FWHM(lambd[v[0][0]-ind:v[0][0]+ind], ListGal[i][v[0][0]-ind:v[0][0]+ind])
except IndexError:
ind = ind+1
f=[]
l_inf = v[0][0]-ind
l_sup = v[0][0]+ind
# Una vez encontrado el intervalo se busca el indice donde
# tiene el máximo valor de intensidad
indMaxInt = np.where(ListGal[i][l_inf:l_sup]==np.max(ListGal[i][l_inf:l_sup]))
# print v
indMax=indMaxInt[0][0]+l_inf
iM = indMax
l_inf = 1
l_sup = 1
a=ListGal[i][indMax]
while (ListGal[i][indMax-l_inf] <= a): # | ((ListGal[i][indMax-l_inf]-spec[indMax-l_inf])>10):
a = ListGal[i][indMax-l_inf]
l_inf = l_inf+1
a=ListGal[i][indMax]
while (ListGal[i][indMax+l_sup] <= a): # | ((ListGal[i][indMax+l_sup]-spec[indMax+l_sup])>10):
a = ListGal[i][indMax+l_sup]
l_sup = l_sup+1
l_inf = indMax-l_inf+1
l_sup = indMax+l_sup
linePr.add_row((iM, l_inf, l_sup))
return linePr
# In[ ]:
# ----------------------------------------------------------------------------------------------------------------------
# Esta función se llama cuando la amplitud encontrada es negativa antes de hacer el ajuste (intensidad observada - continuo)
# entonces se calcula nuevamente el continuo en esta linea y alrededores para hacer el ajuste.
#lineContCorrection(lambd, spec, x, lineP, ListGal, i)
def lineContCorrection(lambdLineRe, fluxLineRe, indexLineProf, tableLineProf, spectraF, indexG):
medLineRe = tableLineProf[indexLineProf]['inf']+(tableLineProf[indexLineProf]['sup']-tableLineProf[indexLineProf]['inf'])/2
minFluxLineRe = np.min(spectraF[indexG][tableLineProf[indexLineProf]['inf']:tableLineProf[indexLineProf]['sup']])
max = len(lambdLineRe)-1
for iarr in range(1, 20):
indexL = medLineRe - iarr
indexS = medLineRe + iarr-1
if indexL>=0 :
fluxLineRe[indexL] = (minFluxLineRe + fluxLineRe[indexL])/2
if indexS<=max :
fluxLineRe[indexS] = (minFluxLineRe + fluxLineRe[indexS])/2
return fluxLineRe
# ----------------------------------------------------------------------------------------------------------------------
# obtain the continuum of the spectrum given a the observed spec : spectrum, wavelenght : lambd, and
# a parameter of how variable is the continuum : ppend)
def r(spec,lambd, ppend):
for b in range(0,len(spec)-1,1):
pend=(spec[b+1]-spec[b])/(lambd[b+1]-lambd[b])
cont = 0
m=spec[0]
if b>0:
m= np.mean(spec[0:b])
if b>=30:
m= np.mean(spec[b-30:b-20])
# if (np.fabs(pend)<1):
# specSN.append(spec[b])
# lambdSN.append(lambd[b])
while (np.fabs(pend)>ppend):
cont = cont +1
if spec[b]>m:
if (spec[b]<spec[b+1]):
spec[b+1]=(spec[b+1]+ spec[b])/2
else:
spec[b]=(spec[b+1]+ spec[b])/2
if spec[b]<=m:
if (spec[b]<spec[b+1]):
spec[b]=(spec[b+1]+ spec[b])/2
else:
spec[b+1]=(spec[b+1]+ spec[b])/2
pend=(spec[b+1]-spec[b])/(lambd[b+1]-lambd[b])
f=len(spec)
for b in range(1,len(spec)-1,1):
v=f-b
pend=(spec[v-1]-spec[v])/(lambd[v-1]-lambd[v])
m=spec[f-1]
if v<(f-1):
m= np.mean(spec[v:f-1])
if v<(f-30):
m= np.mean(spec[v+20:v+30])
while (np.fabs(pend)>ppend/2):
if spec[v]>m:
if (spec[v]<spec[v-1]):
spec[v-1]=(spec[v-1]+ spec[v])/2
else:
spec[v]=(spec[v-1]+ spec[v])/2
if spec[v]<=m:
if (spec[v]<spec[v-1]):
spec[v]=(spec[v-1]+ spec[v])/2
else:
spec[v-1]=(spec[v-1]+ spec[v])/2
pend=(spec[v-1]-spec[v])/(lambd[v-1]-lambd[v])
return spec,lambd
# In[ ]:
# ----------------------------------------------------------------------------------------------------------------------
# finalFit(index int :i, continuum spectrum np.array :spec,
# wavelengths np.array :lambd, wavelenghts of lines Astropy.Table : ListLines,
# array of galaxies list :ListGal, error bar list : spIvar, maximum iteration Int :iterr,
# resolution limit float :limResol):)
def finalFit(i, spec,lambd,ListLines,ListGal,spIvar,iterr, limResol):
lineP = lineProfile(i,spec,lambd,ListLines,ListGal, limResol) # Table of line profile indexes upper and lower
linePaux=lineP['lambda'].data
alslk = lineP['lambda'][0]
# it obtain the number of lines overlayed in a profile
for c in range(1,len(linePaux)):
if (linePaux[c]==alslk) & (alslk!=0):
linePaux[c-1] = 2
linePaux[c] = 2
else:
linePaux[c] = 1
alslk = lineP['lambda'][c]
f1 = fitting.LevMarLSQFitter()
lineStdDev = 3.5 # default standard deviation
condit = True # esto es mejor quitarlo
while condit: # esto es mejor quitarlo
Gaus=[] # array of models
for x in range(len(ListLines)): # for each line profile it is created a gaussian model
if lineP['inf'][x]==0:
Gaus.append(models.Gaussian1D(amplitude=0,mean=ListLines['LAMBDA VAC ANG'][x],stddev=0))
# it didn't model a profile for this line
else:
v = np.where((lambd >= ListLines['LAMBDA VAC ANG'][x]-limResol) & (lambd <= ListLines['LAMBDA VAC ANG'][x]+limResol))
# estimate the wavelenghts for the center of the line
ampMax= ListGal[i][v[0][0]]-spec[v[0][0]] # max amplitude of the profile
nLines = linePaux[x] # obtain the number of overlaying line profiles
f = FWHM(lambd[lineP['inf'][x]:lineP['sup'][x]], ListGal[i][lineP['inf'][x]:lineP['sup'][x]])
# calculate the FWHM of the profile
coasi = 0
while len(f)==0:
coasi = coasi + 1
f = FWHM(lambd[(lineP['inf'][x]-coasi):(lineP['sup'][x]+coasi)], ListGal[i][(lineP['inf'][x]-coasi):(lineP['sup'][x]+coasi)])
# calculate FWHM so often as it is found a non profile spectrum
stdLine = (1/2.3548)*f[0]/nLines # estimate the observed standard deviation
lineWidthBase = (lambd[lineP['sup'][x]] - lambd[lineP['inf'][x]])/(2*nLines)
## calculate the observed line width at the base of the profile
if (ampMax <=0) or (ListGal[i][lineP['sup'][x]] < spec[lineP['sup'][x]]) or (ListGal[i][lineP['inf'][x]] < spec[lineP['inf'][x]]):
spec = lineContCorrection(lambd, spec, x, lineP, ListGal, i)
v = np.where((lambd >= ListLines['LAMBDA VAC ANG'][x]-limResol) & (lambd <= ListLines['LAMBDA VAC ANG'][x]+limResol))
# estimate the wavelenghts for the center of the line
ampMax= ListGal[i][v[0][0]]-spec[v[0][0]] # max amplitude of the profile
nLines = linePaux[x] # obtain the number of overlaying line profiles
f = FWHM(lambd[lineP['inf'][x]:lineP['sup'][x]], ListGal[i][lineP['inf'][x]:lineP['sup'][x]])
# calculate the FWHM of the profile
coasi = 0
while len(f)==0:
coasi = coasi + 1
f = FWHM(lambd[(lineP['inf'][x]-coasi):(lineP['sup'][x]+coasi)], ListGal[i][(lineP['inf'][x]-coasi):(lineP['sup'][x]+coasi)])
# calculate FWHM so often as it is found a non profile spectrum
stdLine = (1/2.3548)*f[0]/nLines # estimate the observed standard deviation
lineWidthBase = (lambd[lineP['sup'][x]] - lambd[lineP['inf'][x]])/(2*nLines)
## calculate the observed line width at the base of the profile
if ampMax <=0:
print "It has not been possible to fit the line: ", ListLines['LAMBDA VAC ANG'][x]
Gaus.append(models.Gaussian1D(amplitude=0,mean=ListLines['LAMBDA VAC ANG'][x],stddev=0))
#elif ampMax<=3*spIvar[lineP['lambda'][x]]:
## no model for the amplitud less tah three times the error
# Gaus.append(models.Gaussian1D(amplitude=0,mean=ListLines['LAMBDA VAC ANG'][x],stddev=0))
else:
Gaus.append(models.Gaussian1D(amplitude=ampMax,mean=ListLines['LAMBDA VAC ANG'][x],stddev=stdLine, bounds={'mean':(ListLines['LAMBDA VAC ANG'][x]-lineWidthBase/(2*nLines), ListLines['LAMBDA VAC ANG'][x]+lineWidthBase/(2*nLines)),'stddev':(0.8*stdLine, 1.5*stdLine)}))
#elif ampMax<=3*spIvar[lineP['lambda'][x]]:
## no model for the amplitud less tah three times the error
# Gaus.append(models.Gaussian1D(amplitude=0,mean=ListLines['LAMBDA VAC ANG'][x],stddev=0))
else: # ampMax>0: # & (ampMax>3*spIvar[lineP['lambda'][x]]): # & (coasi < 2):
# a model for the observed amplitude (emission line) is greater than three times
# the error of the intensity in the spectrum and the bounds depend on the number of lines
# in the profile and the width of his base
Gaus.append(models.Gaussian1D(amplitude=ampMax,mean=ListLines['LAMBDA VAC ANG'][x],stddev=stdLine, bounds={'mean':(ListLines['LAMBDA VAC ANG'][x]-lineWidthBase/(2*nLines), ListLines['LAMBDA VAC ANG'][x]+lineWidthBase/(2*nLines)),'stddev':(0.8*stdLine, 1.5*stdLine)}))
condit = False # esto es mejor quitarlo
sum_Gaussian=Gaus[0]
for ooasia in range(1,len(Gaus)):
# sum of all the models respective to all the wavelenghts
sum_Gaussian=sum_Gaussian + Gaus[ooasia]
sum_Ga2=Gaus[0]+Gaus[1] # only two models of the [o II]3726-29 lines
#sum_Gaussian=Gaus[0]+Gaus[1]+Gaus[2]+Gaus[3]+Gaus[4]+Gaus[5]+Gaus[6]+Gaus[7]+Gaus[8]+Gaus[9]+Gaus[10]+Gaus[11]+Gaus[12]+Gaus[13]+Gaus[14]
gaussian_fit = f1(sum_Gaussian, lambd, ListGal[i]-spec, maxiter=iterr)
gaussian_fit2 = f1(sum_Ga2, lambd, ListGal[i]-spec, maxiter=iterr)
Graph=[]
Graph.append(gaussian_fit)
Graph.append(gaussian_fit2)
return Graph, lineP, spec ## return the fitting to both models and line profiles wavelenghts
# In[2]:
# ----------------------------------------------------------------------------------------------------------------------
#----> http://www.stecf.org/software/ASTROsoft/DER_SNR/der_snr.py
# =====================================================================================
def DER_SNR(flux):
# =====================================================================================
"""
DESCRIPTION This function computes the signal to noise ratio DER_SNR following the
definition set forth by the Spectr2al Container Working Group of ST-ECF,
MAST and CADC.
signal = median(flux)
noise = 1.482602 / sqrt(6) median(abs(2 flux_i - flux_i-2 - flux_i+2))
snr = signal / noise
values with padded zeros are skipped
USAGE snr = DER_SNR(flux)
PARAMETERS none
INPUT flux (the computation is unit independent)
OUTPUT the estimated signal-to-noise ratio [dimensionless]
USES numpy
NOTES The DER_SNR algorithm is an unbiased estimator describing the spectrum
as a whole as long as
* the noise is uncorrelated in wavelength bins spaced two pixels apart
* the noise is Normal distributed
* for large wavelength regions, the signal over the scale of 5 or
more pixels can be approximated by a straight line
For most spectr2a, these conditions are met.
REFERENCES * ST-ECF Newsletter, Issue #42:
www.spacetelescope.org/about/further_information/newsletters/html/newsletter_42.html
* Software:
www.stecf.org/software/ASTROsoft/DER_SNR/
AUTHOR Felix Stoehr, ST-ECF
24.05.2007, fst, initial import
01.01.2007, fst, added more help text
28.04.2010, fst, return value is a float now instead of a numpy.float64
"""
from numpy import array, where, median, abs
flux = array(flux)
# Values that are exactly zero (padded) are skipped
flux = array(flux[where(flux != 0.0)])
n = len(flux)
# For spectr2a shorter than this, no value can be returned
if (n>4):
signal = median(flux)
noise = 0.6052697 * median(abs(2.0 * flux[2:n-2] - flux[0:n-4] - flux[4:n]))
return float(signal / noise)
else:
return 0.0
# end DER_SNR -------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------
def split_list(alist, wanted_parts=1):
length = len(alist)
return [ alist[i*length // wanted_parts: (i+1)*length // wanted_parts]
for i in range(wanted_parts) ]
rc = pn.RedCorr()
rc.law = 'G03 LMC'
IntrinsicHB=np.linspace(2.8,3.1,31)