/
temperatures.py
636 lines (578 loc) · 28.8 KB
/
temperatures.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
"""
TEST FILE
"""
from astropy.table import Table
from fitting import *
from functions import *
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import csv
from scipy.optimize import curve_fit
from nobelt import *
from onebelt import *
from twobelt import *
from scipy.stats import f
from scipy.stats import norm as Norm
#takes a datafile and returns a list of the instruments.
def intlist(fill):
if fill == 'tests/.DS_Store':
pass
else:
data = Table.read(fill, format='ipac')
instlist = []
for line in data:
inst = line['instrument']
if inst not in instlist:
instlist.append(inst)
else:
pass
return instlist
Singles= 0
Doubles= 0
starfiles = getfiles('GoodStarfiles')
DataTable = [['Star Name', 'Spectral Type', 'TStar', 'LStar', 'T Single', 'T Warm', 'T Cold', 'ReChi2', 'Age']]
TempList1BB = []
TempList2BB = []
GoodStars =[]
BadStars = []
count = 0 #set = to (first-1) in following for loop
for i in starfiles[5:50]:
count += 1
print count
try:
if i == 'stars/.DS_Store':
pass
else:
#FILE INPUT DATA + NEXTGEN
DataList = []
Source = tname(i)
print Source
DataList.append(Source)
SpecTpe = SpcType(i)
DataList.append(SpecTpe)
insts = intlist(i)
filein = Table.read('%s' % (i), format='ipac')
DataWave = filein['wavelength']
DataFlux = filein['flux']
DataUncert = filein['error']
Source = tname(i)
Tstar = tstar(i)
DataList.append(Tstar)
NextGen = ngfind(i)
NGwave, NGflux = data_ng('nextgen/%s' % (NextGen))
data = Table.read(i, format='ipac') #READS INDIVIDUAL STARFILE
Her = []
HerWave = []
HerUn = []
toMA = []
toMAWave = []
toMAUn = []
WIS = []
WISWave = []
WISun = []
SLone = []
SLoneWave = []
SLoneUN = []
SLto = []
SLtoWave = []
SLtoUN = []
LLone = []
LLoneWave = []
LLoneUN = []
LLto = []
LLtoWave = []
LLtoUN = []
AllFlux = []
AllWave = []
AllEr = []
for n in insts:
for i in data:
if n[len(n)-3:] == i['instrument'][len(i['instrument'])-3:]:
wave = i['wavelength']
flux = i['flux']
error = i['error']
if n[0:3] == 'Her':
HerWave.append(wave)
Her.append(flux)
HerUn.append(error)
if n[0:3] == '2MA':
toMAWave.append(wave)
toMA.append(flux)
toMAUn.append(error)
if n[0:3] == 'WIS':
WISWave.append(wave)
WIS.append(flux)
WISun.append(error)
if n == 'SpitzerIRS-SL1':
SLoneWave.append(wave)
SLone.append(flux)
SLoneUN.append(error)
if n == 'SpitzerIRS-SL2':
SLtoWave.append(wave)
SLto.append(flux)
SLtoUN.append(error)
if n == 'SpitzerIRS-LL1':
LLoneWave.append(wave)
LLone.append(flux)
LLoneUN.append(error)
if n == 'SpitzerIRS-LL2':
LLtoWave.append(wave)
LLto.append(flux)
LLtoUN.append(error)
if len(SLone) > 0 or len(SLto) > 0 or len(LLone) > 0 or len(LLto) > 0:
SLone = np.array(SLone)
SLoneWave = np.array(SLoneWave)
SLto = np.array(SLto)
SLtoWave = np.array(SLtoWave)
LLone = np.array(LLone)
LLoneWave = np.array(LLoneWave)
LLto = np.array(LLto)
LLtoWave = np.array(LLtoWave)
###########STITCHING FOR IRS MODULES###############
try:
point1 = (LLoneWave[0] + LLtoWave[len(LLto)-1])/2
ll_one_beg = np.interp(point1, LLoneWave, LLone)
ll_to_end = np.interp(point1, LLtoWave, LLto)
LLto *= ll_one_beg/ll_to_end
except:
pass
try:
point2 = (LLtoWave[0] + SLoneWave[len(SLone)-1])/2
ll_to_beg = np.interp(point2, LLtoWave, LLto)
sl_one_end = np.interp(point2, SLoneWave, SLone)
SLone *= ll_to_beg/sl_one_end
except:
pass
try:
point3 = (SLoneWave[0] + SLtoWave[len(SLto)-1])/2
sl_one_beg = np.interp(point3, SLoneWave, SLone)
sl_to_end = np.interp(point3, SLtoWave, SLto)
SLto *= sl_one_beg/sl_to_end
except:
pass
#############RECOMBINATION OF DATA################
if len(SLone) > 0:
for i in SLone:
AllFlux.append(i)
for i in SLoneWave:
AllWave.append(i)
for i in SLoneUN:
AllEr.append(i)
if len(SLto) > 0:
for i in SLto:
AllFlux.append(i)
for i in SLtoWave:
AllWave.append(i)
for i in SLtoUN:
AllEr.append(i)
if len(LLone) > 0:
for i in LLone:
AllFlux.append(i)
for i in LLoneWave:
AllWave.append(i)
for i in LLoneUN:
AllEr.append(i)
if len(LLto) > 0:
for i in LLto:
AllFlux.append(i)
for i in LLtoWave:
AllWave.append(i)
for i in LLtoUN:
AllEr.append(i)
if len(Her) > 0:
HerSaturationLimits = [220, 510, 1125]
HerschelWave = [70, 100,160]
for i in HerWave:
if i == 70:
a = HerWave.index(70)
b = Her[a]
c = HerWave[a]
d = HerUn[a]
e = HerSaturationLimits[a]
if b <= e:
AllFlux.append(b)
AllWave.append(c)
AllEr.append(d)
elif i == 100:
a = HerWave.index(100)
b = Her[a]
c = HerWave[a]
d = HerUn[a]
e = HerSaturationLimits[a]
if b <= e:
AllFlux.append(b)
AllWave.append(c)
AllEr.append(d)
elif i == 160:
a = HerWave.index(160)
b = Her[a]
c = HerWave[a]
d = HerUn[a]
e = HerSaturationLimits[a]
if b <= e:
AllFlux.append(b)
AllWave.append(c)
AllEr.append(d)
else:
pass
#for i in Her:
# AllFlux.append(i)
#for i in HerWave:
# AllWave.append(i)
#for i in HerUn:
# AllEr.append(i)
if len(toMA) > 0:
toMASaturationLimits = [10.057, 10.24, 10.566]
toMASSWave = [1.235, 1.662, 2.159]
for i in toMAWave:
if i == 1.235:
a = toMAWave.index(1.235)
b = toMA[a]
c = toMAWave[a]
d = toMAUn[a]
e = toMASaturationLimits[a]
if b <= e:
AllFlux.append(b)
AllWave.append(c)
AllEr.append(d)
elif i == 1.662:
a = toMAWave.index(1.662)
b = toMA[a]
c = toMAWave[a]
d = toMAUn[a]
e = toMASaturationLimits[a]
if b <= e:
AllFlux.append(b)
AllWave.append(c)
AllEr.append(d)
elif i == 2.159:
a = toMAWave.index(2.159)
b = toMA[a]
c = toMAWave[a]
d = toMAUn[a]
e = toMASaturationLimits[a]
if b <= e:
AllFlux.append(b)
AllWave.append(c)
AllEr.append(d)
else:
pass
#for i in toMA:
# AllFlux.append(i)
#for i in toMAWave:
# AllWave.append(i)
#for i in toMAUn:
# AllEr.append(i)
if len(WIS) > 0:
WiseSaturationLimits = [0.18, 0.36, 0.88, 12]
WiseWave = [3.368, 4.618, 12.082, 22.194]
for i in WISWave:
if i == 3.368:
a = WISWave.index(3.368)
b = WIS[a]
c = WISWave[a]
d = WISun[a]
e = WiseSaturationLimits[a]
if b <= e:
AllFlux.append(b)
AllWave.append(c)
AllEr.append(d)
elif i == 4.618:
a = WISWave.index(4.618)
b = WIS[a]
c = WISWave[a]
d = WISun[a]
e = WiseSaturationLimits[a]
if b <= e:
AllFlux.append(b)
AllWave.append(c)
AllEr.append(d)
elif i == 12.082:
a = WISWave.index(12.082)
b = WIS[a]
c = WISWave[a]
d = WISun[a]
e = WiseSaturationLimits[a]
if b <= e:
AllFlux.append(b)
AllWave.append(c)
AllEr.append(d)
elif i == 22.194:
a = WISWave.index(22.194)
b = WIS[a]
c = WISWave[a]
d = WISun[a]
e = WiseSaturationLimits[a]
if b <= e:
AllFlux.append(b)
AllWave.append(c)
AllEr.append(d)
else:
pass
#for i in WIS:
# AllFlux.append(i)
#for i in WISWave:
# AllWave.append(i)
#for i in WISun:
# AllEr.append(i)
#print 'Luminousity'
############### INSERT CODE FOR STAR LUMINOUSITY HERE ############
############### INSERT CODE FOR STAR LUMINOUSITY HERE ############
############### INSERT CODE FOR STAR LUMINOUSITY HERE ############
############### INSERT CODE FOR STAR LUMINOUSITY HERE ############
print 'Lists done'
print '1B test'
##################### ONE BELT DATA ##############################
#DataWave1 = AllWave
#DataFlux1 = AllFlux
#DataUncert1 = AllEr
#Variables
wave = np.arange(1e-6, 500e-6, 1e-6) #wave range in meters
wavew = np.arange(1, 500, 1) #wave range in microns
NGfluxDataWave = np.interp(AllWave,NGwave,NGflux) #interpolation of NextGen in data wave range
Yw = np.interp(wavew,NGwave,NGflux) #interpolation of NextGen data in BlackBody wave range
T1 = 100 #initlal temperature for inner belt
num = 1
#normalizer functions
FluxNorm = norm_ng(AllWave,AllFlux,NGwave,NGflux) #initial normalizer for NextGen values
norm = normilize(AllWave,AllFlux) #initial normalizers for blackdodies
Yn = FluxNorm
#this function needs access to NGfluxDataWave but not as an argument so has to be in this code.
def forwardmodel(AllWave, T1, N1, Ns): #function used by curve_fit
from functions import bbl
return N1*bbl(AllWave,T1) + Ns*NGfluxDataWave
try:
popt, pcov = curve_fit(forwardmodel, AllWave, AllFlux, p0=(100,norm,FluxNorm), sigma=AllEr) #find values for the fitting
uncert = np.sqrt(np.diag(pcov)) #the uncertainties for each value
except:
popt = [100, norm, FluxNorm]
uncert = [1]
print 'Error with 1BB Pre-Ftest'
Source = Source + ' with unfit parameters'
NGflux *= popt[2] #normalizes the NextGen values
#Main SED info
inner = planck(wave, popt[0]) * popt[1] #calls function for inner blackbody
some = Yw*popt[2] + planck(wave, popt[0]) * popt[1] #creates the summation value array
#Excess info
excess = AllFlux - NGfluxDataWave*popt[2]
#inner see above
#outer see above
exsum = inner
#Residual info
Yr = np.interp(AllWave, wavew, some)
res = (AllFlux - Yr) / AllEr
zero = wavew * 0
#Chi Calculator
DoF1 = len(AllWave)-3
chi1 = (AllFlux - Yr)/AllEr
chisq1 = np.dot(chi1, chi1)
rechisq1 = chisq1 / DoF1
###################### END OF ONE BELT #######################
print '2B test'
###################### TWO BELT DATA ########################
DataWave2 = AllWave
DataFlux2 = AllFlux
DataUncert2 = AllEr
#Variables
wave = np.arange(1e-6, 500e-6, 1e-6) #wave range in meters
wavew = np.arange(1, 500, 1) #wave range in microns
NGfluxDataWave = np.interp(DataWave2,NGwave,NGflux) #interpolation of NextGen in data wave range
Yw = np.interp(wavew,NGwave,NGflux) #interpolation of NextGen data in BlackBody wave range
T1 = 100 #initlal temperature for inner belt
T2 = 100 #initial temperature for outer belt
num = 2
#normalizer functions
FluxNorm = norm_ng(DataWave2,DataFlux2,NGwave,NGflux) #initial normalizer for NextGen values
norm = normilize(DataWave2,DataFlux2) #initial normalizers for blackdodies
#this function needs access to NGfluxDataWave but not as an argument so has to be in this code.
def forwardmodelto(DataWave2, T1, T2, N1, N2, Ns): #function used by curve_fit
from functions import bbl
return N1*bbl(DataWave2,T1) + N2*bbl(DataWave2,T2) + Ns*NGfluxDataWave
try:
popt2, pcov = curve_fit(forwardmodelto, AllWave, AllFlux, p0=(100,100,norm,norm,FluxNorm), sigma=AllEr) #find values for the fitting
uncert2 = np.sqrt(np.diag(pcov)) #the uncertainties for each value
except:
popt = [150, 60, norm, norm, FluxNorm]
uncert = [1,1]
print 'Error with 2BB pre-Ftest'
Source = Source + ' with unfit parameters and'
NGflux *= popt2[4] #normalizes the NextGen values
#Main SED info
inner = planck(wave, popt2[0]) * popt2[2] #calls function for inner blackbody
outer = planck(wave, popt2[1]) * popt2[3] #calls function for outer blackbody
some = Yw*popt2[4] + planck(wave,popt2[0],popt2[2]) + planck(wave,popt2[1],popt2[3])#creates the summation value array
#Excess info
excess = DataFlux2 - NGfluxDataWave*popt2[4]
#inner see above
#outer see above
exsum = inner + outer
#Residual info
Yr = np.interp(DataWave2, wavew, some)
res = (DataFlux2 - Yr) / DataUncert2
zero = wavew * 0
#Chi Calculator
DoF2 = len(DataWave2)-5
chi2 = (DataFlux2 - Yr)/DataUncert2
chisq2 = np.dot(chi2, chi2)
rechisq2 = chisq2 / DoF2
##################### END OF TWO BELT DATA ########################
print 'Ftest'
########################## F-TEST ###############################
bigN = len(AllWave) # Number of data points
Nparam1 = 3.
Nparam2 = 5.
dof1 = bigN - Nparam1
dof2 = bigN - Nparam2
#chi1 = 300. #428.65 #383.6 #really bad
#chi2 = 300. #results in chisqr close to 1
ftest = (chisq1/(dof1)) / (chisq2/(dof2))
#RJ+BB vs RJ+BB+BB
#ftest = ( (chi1-chi2)/(dof1-dof2) ) / (chi2/dof2)
proba_at_f_pdf = f.pdf(ftest, dof1, dof2)
proba_at_f_cdf = f.cdf(ftest, dof1, dof2) # P(F(1,30) < 3)
f_at_proba_98 = f.ppf(.98, dof1, dof2) # q such P(F(1,30) < .95)
proba_at_norm_idf = Norm.isf(proba_at_f_cdf) # P(F(1,30) < 3)
proba_at_norm_ppf = Norm.ppf(proba_at_f_cdf) # P(F(1,30) < 3)
print ''
print '-----------'
print 'Source: ', Source
print 'ftest: ', ftest
print 'proba_at_f_pdf: ', proba_at_f_pdf
print 'proba_at_f_cdf: ', proba_at_f_cdf
print 'f_at_proba_98: ', f_at_proba_98
print 'proba_at_norm_isf: ', proba_at_norm_idf # inverse survival function
print 'proba_at_norm_ppf: ', proba_at_norm_ppf, ' sigma' # Number of sigma away
print '-----------'
print ''
#####################END OF FTEST SECTION##############################
print 'fitting'
################BEGINNING FIT FOR PLOTS AND STORAGE####################
if f_at_proba_98 > 1 and len(popt) == 3:
DataList.append('nan')
Doubles += 1
"""
DataWave2 = AllWave
DataFlux2 = AllFlux
DataUncert2 = AllEr
#Variables
wave = np.arange(1e-6, 500e-6, 1e-6) #wave range in meters
wavew = np.arange(1, 500, 1) #wave range in microns
NGfluxDataWave = np.interp(DataWave2,NGwave,NGflux) #interpolation of NextGen in data wave range
Yw = np.interp(wavew,NGwave,NGflux) #interpolation of NextGen data in BlackBody wave range
T1 = 100 #initlal temperature for inner belt
T2 = 100 #initial temperature for outer belt
num = 2
#normalizer functions
Yn = norm_ng(DataWave2,DataFlux2,NGwave,NGflux) #initial normalizer for NextGen values
norm = normilize(DataWave2,DataFlux2) #initial normalizers for blackdodies
#this function needs access to NGfluxDataWave but not as an argument so has to be in this code.
def forwardmodel(DataWave2, T1, T2, N1, N2, Ns): #function used by curve_fit
from functions import bbl
return N1*bbl(DataWave2,T1) + N2*bbl(DataWave2,T2) + Ns*NGfluxDataWave
try:
popt, pcov = curve_fit(forwardmodel, DataWave2, DataFlux2, p0=(100,100,norm,norm,Yn), sigma=DataUncert2) #find values for the fitting
uncert = np.sqrt(np.diag(pcov)) #the uncertainties for each value
except:
popt = [150, 60, norm, norm, Yn]
uncert = [1,1]
print 'Could not fit star %s' % (Source)
Source = Source + ' with unfit parameters and'
"""
TempList2BB.append(popt2[0])
TempList2BB.append(popt2[1])
DataList.append(popt2[0])
DataList.append(popt2[1])
NGflux *= popt2[4] #normalizes the NextGen values
#Main SED info
inner = planck(wave, popt2[0]) * popt2[2] #calls function for inner blackbody
outer = planck(wave, popt2[1]) * popt2[3] #calls function for outer blackbody
some = Yw*popt2[4] + planck(wave,popt2[0],popt2[2]) + planck(wave,popt2[1],popt2[3])#creates the summation value array
#Excess info
excess = AllFlux - NGfluxDataWave*popt2[4]
#inner see above
#outer see above
exsum = inner + outer
#Residual info
Yr = np.interp(DataWave2, wavew, some)
res = (DataFlux2 - Yr) / DataUncert2
zero = wavew * 0
#Chi Calculator
DoF2 = len(DataWave2)-5
chi2 = (DataFlux2 - Yr)/DataUncert2
chisq2 = np.dot(chi2, chi2)
rechisq2 = chisq2 / DoF2
DataList.append(rechisq2)
elif f_at_proba_98 <= 1 and len(popt2) == 5:
Singles += 1
"""
DataWave1 = AllWave
DataFlux1 = AllFlux
DataUncert1 = AllEr
#Variables
wave = np.arange(1e-6, 500e-6, 1e-6) #wave range in meters
wavew = np.arange(1, 500, 1) #wave range in microns
NGfluxDataWave = np.interp(DataWave1,NGwave,NGflux) #interpolation of NextGen in data wave range
Yw = np.interp(wavew,NGwave,NGflux) #interpolation of NextGen data in BlackBody wave range
T1 = 100 #initlal temperature for inner belt
T2 = 100 #initial temperature for outer belt
num = 1
#normalizer functions
Yn = norm_ng(DataWave1,DataFlux1,NGwave,NGflux) #initial normalizer for NextGen values
norm = normilize(DataWave1,DataFlux1) #initial normalizers for blackdodies
#this function needs access to NGfluxDataWave but not as an argument so has to be in this code.
def forwardmodel(DataWave1, T1, N1, Ns): #function used by curve_fit
from functions import bbl
return N1*bbl(DataWave1,T1) + Ns*NGfluxDataWave
try:
popt, pcov = curve_fit(forwardmodel, DataWave1, DataFlux1, p0=(100,norm,Yn), sigma=DataUncert1) #find values for the fitting
uncert = np.sqrt(np.diag(pcov)) #the uncertainties for each value
except:
popt = [100, norm, Yn]
uncert = [1]
print 'could not complete fitting'
Source = Source + ' with unfit parameters and'
"""
TempList1BB.append(popt[0])
DataList.append(popt[0])
DataList.append('nan')
DataList.append('nan')
NGflux *= popt[2] #normalizes the NextGen values
#Main SED info
inner = planck(wave, popt[0]) * popt[1] #calls function for inner blackbody
some = Yw*popt[2] + planck(wave, popt[0]) * popt[1] #creates the summation value array
#Excess info
excess = DataFlux1 - NGfluxDataWave*popt[2]
#inner see above
#outer see above
exsum = inner
#Residual info
Yr = np.interp(DataWave1, wavew, some)
res = (DataFlux1 - Yr) / DataUncert1
zero = wavew * 0
#Chi Calculator
DoF1 = len(DataWave1)-3
chi1 = (DataFlux1 - Yr)/DataUncert1
chisq1 = np.dot(chi1, chi1)
rechisq1 = chisq1 / DoF1
else:
print 'Problem witih ftest on star', Source
if f_at_proba_98 > 1:
print f_at_proba_98
print '2BB fitting error'
print len(popt2), + 'numer of fitting values'
elif f_at_proba_98 <= 1:
print f_at_proba_98
print '1BB fitting error'
print len(popt), + 'numer of fitting values'
else:
print 'unknown error with fitting portion'
######################RUNNING BB FIT PER FTEST END#####################
DataTable.append(DataList)
except:
pass
import os
with open('Temperatures.csv', 'w') as file:
for i in DataTable:
file.write('%s' % (i) + os.linesep)