-
Notifications
You must be signed in to change notification settings - Fork 0
/
Atmos.py
1437 lines (1179 loc) · 53.6 KB
/
Atmos.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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import division
from builtins import range
import os
import math
import re
from dataclasses import dataclass
from past.utils import old_div
import numpy as np
from astropy.stats import sigma_clip
from astropy.io import ascii
from scipy.stats import sigmaclip, linregress
import scipy.odr as ODR
#from interpol_function import interpol
from uncertainties import unumpy
import matplotlib.pyplot as plt
from AtmosInterpol import interpol
@dataclass
class Tolerance:
ab: float
ep: float
dif: float
rw: float
@dataclass
class AtmosQuantity:
name: str
value: float
hold: bool
ranges: list
bounds: tuple
width: float
tol: float
change: float
class atmos:
def __init__(self, star, hold, init_vals, debug, file_debug, in_errors,
set_boundaries, vals_boundaries, tolerance=[0.001, 0.001, 0.001, 0.001],
alias='test', one_round=False, read_mode='linearregression'):
self.starname = star
self.alias = alias
self.ini = init_vals
if len(init_vals) < 4:
self.ini = [0.0, 5500., 4.36, 1.23]
self.debug = debug
self.file_debug = file_debug
self.change = 'metallicity'
self.parameters = ['metallicity', 'temperature', 'gravity', 'velocity']
#self.changepar = [200.0, 0.2, 0.2]
self.tol = Tolerance(*tolerance)
self.one_round = one_round
self.read_mode = read_mode
self.moog = [0.0, 0.0, 0.0, 0.0]
self.hold = [i in hold for i in self.parameters]
boundaries = [(-3.0, 1.0), (3500., 9000.), (0.5, 4.9), (0.0, 5.0)]
if set_boundaries and vals_boundaries is not None:
for i_p, p in enumerate(self.parameters):
if p in vals_boundaries:
imin, imax = boundaries[i_p]
boundaries[i_p] = (max(vals_boundaries[p][0], imin),
min(vals_boundaries[p][1], imax))
self.metallicity = AtmosQuantity('metallicity', self.ini[0], self.hold[0],
[-999.0, -999.0, -999.0], boundaries[0],
0.25, self.tol.ab, 0.0)
self.temperature = AtmosQuantity('temperature', self.ini[1], self.hold[1],
[-999.0, -999.0], boundaries[1], 50., self.tol.ep, 200.0)
self.gravity = AtmosQuantity('gravity', self.ini[2], self.hold[2],
[-999.0, -999.0], boundaries[2], 0.25, self.tol.dif, 0.2)
self.velocity = AtmosQuantity('velocity', self.ini[3], self.hold[3],
[-999.0, -999.0], boundaries[3], 0.25, self.tol.rw, 0.2)
self.nfailed = 0
self.exception = 1
self.change_antes = 'metallicity'
if in_errors:
self.n_repeat = 100
else:
self.n_repeat = 200
self.params = []
self.nit = 0
self.nbreak = 0
self.nit_total = 0
self.nout = 0
@property
def values(self):
met = self.metallicity.value
T = self.temperature.value
logg = self.gravity.value
micro = self.velocity.value
return (met, T, logg, micro)
@property
def boundaries(self):
met = self.metallicity.bounds
T = self.temperature.bounds
logg = self.gravity.bounds
micro = self.velocity.bounds
return (met, T, logg, micro)
def write_debug_moog(self):
if self.debug:
f = self.file_debug
f.info('Ran %s with: feh=%.2f, T=%.0f, logg=%.2f, micro=%.2f\n'\
'\t\t Obtained: ab=%.3f, ep=%.3f, dif=%.3f, rw=%.3f, nfailed=%d',\
self.change, *list(self.values), *self.moog, self.nfailed)
self.file_debug = f
del f
def write_log(self, message):
if self.debug:
f = self.file_debug
f.info(message)
self.file_debug = f
del f
def add_param(self):
self.params.append(list(self.values))
def check_correct_vals(self):
output = self.moog
if abs(output[0] - list(self.values)[0]) <= self.tol.ab and \
abs(output[1]) <= self.tol.ep and \
abs(output[2]) <= self.tol.dif and \
abs(output[3]) <= self.tol.rw:
self.write_log('Found right parameters')
del output
return -1
del output
return 0
def check_nout(self):
nout = self.nout
vals = list(self.values)
boundaries = list(self.boundaries)
for v, b in zip(vals[:-1], boundaries[:-1]):
if v < b[0] or v > b[1]:
nout += 1
if nout >= 3:
self.write_log('[Fe/H], T and log g are out of the possible ranges. '\
'Cannot find final parameters.')
self.exception = 2
del nout, vals, boundaries
return -1
del nout, vals, boundaries
return 0
@property
def call_params(self):
return self.metallicity, self.temperature, self.gravity, self.velocity
def check_nfailed(self):
if self.nfailed > 0:
for p in list(self.call_params):
if p.hold is False:
new_p = np.random.normal(p.value, p.width)
if new_p > p.bounds[1]:
new_p = p.bounds[1]
if new_p < p.bounds[0]:
new_p = p.bounds[1]
p.value = new_p
del new_p
p.ranges = [-999.0, -999.0]
if p.name == 'metallicity':
p.ranges = [-999.0, -999.0, p.value]
def check_nbreak(self):
if self.nbreak > 5:
self.exception = 2
self.write_log('Failed more than 5 times in the models.')
return -1
return 0
def check_params_rep(self):
params = self.params
vals = list(self.values)
if vals in params:
self.write_log('Parameters have already been used in another iteration.')
n = self.next_change(self.change_antes)
self.change = n
self.nit += 1
del params, vals
def check_nrepeat(self):
if self.nit >= self.n_repeat:
self.write_log('Parameters were repeated more than %d times.' % (self.n_repeat))
self.exception = 2
return -1
return 0
def check_nit_total(self):
if self.nit_total >= 500000:
self.write_log('More than 500000 iterations for the same star. Stopping.')
self.exception = 2
return -1
return 0
def check_hold(self, xmetal):
for i_p, p in enumerate(self.call_params):
if p.hold:
self.moog[i_p] = 0.0
if i_p == 0:
self.moog[i_p] = xmetal
@property
def show_hold(self):
return [p.hold for p in self.call_params]
def new_iteration(self, xmetal):
self.nit_total += 1
self.check_hold(xmetal)
self.nout = 0
self.add_param()
self.change_antes = self.change
def moog_output(self, output, nfail):
self.moog = output
self.nfailed = nfail
def new_values(self, new_vals):
for i_p, p in enumerate(self.call_params):
p.value = new_vals[i_p]
@staticmethod
def next_change(change_ini):
c = ['metallicity', 'temperature', 'pressure', 'velocity']
i = c.index(change_ini)
if i == 3:
i = -1
return c[i+1]
def new_change(self, change_ini=None):
if change_ini is None:
change_ini = self.change
self.change = self.next_change(change_ini)
def check_nfailed_it(self, change_ini):
if self.nfailed > 0:
self.new_change(change_ini)
self.write_log('Failed in metallicity. Change=%s' % (self.change))
self.nbreak += 1
def change_metallicity(self, new_val):
self.metallicity.value = new_val
def check_met(self):
met = self.metallicity.ranges
return (met[0] == met[1]) and (met[1] == met[2]) and (met[0] != self.metallicity.value)
def select_param(self, name):
for p in (self.metallicity, self.temperature, self.gravity, self.velocity):
if p.name == name:
break
return p
def change_parameter(self, name_par, moog_output, range_m, decimals):
m_val = self.moog[moog_output]
p = self.select_param(name_par)
ext = p.ranges
val = p.value
if m_val > p.tol:
p.ranges[0] = val
if val < ext[1]:
p.value = round(np.mean(ext), decimals)
else:
p.value = round(mult(val, range_m, 'upper'), decimals)
else:
p.ranges[1] = val
if ext[0] != -999. and val > ext[0]:
p.value = round(np.mean(ext), decimals)
else:
p.value = round(mult(val, range_m, 'floor'), decimals)
def runMOOG(self, atmos_values, fesun=7.50):
m, T, g, vt = atmos_values
interpol(self.starname, T, g, m, vt, self.alias, fesun=fesun)
cmd = str("MOOGSILENT > temp.log 2>&1 <<EOF\nMOOGFEB2017/ab_%s.par\n\nEOF" % self.alias)
os.system(cmd)
ab, ep, dif, rw, nfailed = compute_average_abundance(self.starname,\
w=False, alias=self.alias,\
mode=self.read_mode)
ab = ab - fesun
return ab, ep, dif, rw, nfailed
def check_boundaries(self, atmos_values):
boundaries = list(self.boundaries)
for v, b in zip(atmos_values, boundaries):
if v > b[1] or v < b[0]:
return False
return True
def check_if_hold(self, atmos_values):
#params = np.copy(np.array(atmos_values))
for i, p in enumerate(self.call_params):
if p.hold:
atmos_values[i] = self.ini[i]
return atmos_values[0], atmos_values[1:]
def update_moog(self, moog, nfailed):
for i_p, p in enumerate(self.call_params):
if p.hold:
self.moog[i_p] = 0.0
if i_p == 0:
self.moog[i_p] = self.ini[0]
else:
self.moog[i_p] = moog[i_p]
self.nfailed = nfailed
return self.moog
def objective_function_vec(self, X, met):
boundaries = self.check_boundaries([met, X[0], X[1], X[2]])
if boundaries:
ab, ep, dif, rw, nfailed = self.runMOOG([met, X[0], X[1], X[2]])
ab, ep, dif, rw = self.update_moog([ab, ep, dif, rw], nfailed)
return ep, rw, ab, ab-dif
return 10.**20., 10.**20., 10.**20., 10.**20.
def objective_function(self, X, met):
boundaries = self.check_boundaries([met, X[0], X[1], X[2]])
if boundaries:
ab, ep, dif, rw, nfailed = self.runMOOG([met, X[0], X[1], X[2]])
ab, ep, dif, rw = self.update_moog([ab, ep, dif, rw], nfailed)
return 5*((3.5* ep)**2.+(1.3*rw)**2.)+2*(dif)**2.
return 10.**20.
def simplex(self, S, met):
Xm = np.array([0, 0, 0], dtype=float)
Xr = np.array([0, 0, 0], dtype=float)
Xe = np.array([0, 0, 0], dtype=float)
Xc = np.array([0, 0, 0], dtype=float)
Xm = np.mean(S[:3, 1], axis=0)
Xr = 2*Xm - S[3][1]
met, Xr = self.check_if_hold(np.array([met, Xr[0], Xr[1], Xr[2]]))
fr = self.objective_function(Xr, met)
if S[0][0] <= fr < S[2][0]:
S[3][1] = Xr
S[3][0] = fr
elif fr < S[0][0]:
Xe = 3*Xm - 2*S[3][1]
met, Xe = self.check_if_hold(np.array([met, Xe[0], Xe[1], Xe[2]]))
fe = self.objective_function(Xe, met)
if fe < fr:
S[3][1] = Xe
S[3][0] = fe
else:
S[3][1] = Xr
S[3][0] = fr
else:
Xc = 0.5*(Xm + S[3][1])
met, Xc = self.check_if_hold(np.array([met, Xc[0], Xc[1], Xc[2]]))
fc = self.objective_function(Xc, met)
if fc <= S[3][0]:
S[3][1] = Xc
S[3][0] = fc
else:
for i in range(1, 4):
S[i][1] = 0.5*(S[0][1]+S[i][1])
met, S[i][1] = self.check_if_hold(np.array([met, S[i][1][0],\
S[i][1][1], S[i][1][2]]))
S[i][0] = self.objective_function(S[i][1], met)
S = S[np.argsort(S.T[0])]
del Xm, Xr, Xe, Xc
return S
def nelder_optimizer(self, it_simp, it_res_simp):
counter = 0
met, T, logg, vmic = self.ini
for i in range(it_res_simp):
log_string = '{:>2s} {:>8s} {:>8s} {:>5s} {:>5s} '\
'{:>8s} {:>8s} {:>8s} {:>8s}'.format('It', 'S', 'T', 'logg', 'vt',
'slp1', 'slp2',
'af1-af2', 'af1-met')
self.write_log(log_string)
self.write_log('{:-^68s}'.format('-'))
xin = np.array([T, logg, vmic])
metin = met
slp1, slp2, af1, af2 = self.objective_function_vec(xin, metin)
l1 = self.temperature.change if slp1 > 0 else -self.temperature.change
l2 = self.gravity.change if (af1-af2) > 0 else -self.gravity.change
l3 = self.velocity.change if slp2 > 0 else -self.velocity.change
met, X0 = self.check_if_hold(np.array([met, T, logg, vmic]))
met, X1 = self.check_if_hold(np.array([met, T+l1, logg, vmic]))
met, X2 = self.check_if_hold(np.array([met, T, logg+l2, vmic]))
met, X3 = self.check_if_hold(np.array([met, T, logg, vmic+l3]))
f0 = self.objective_function(X0, met)
f1 = self.objective_function(X1, met)
f2 = self.objective_function(X2, met)
f3 = self.objective_function(X3, met)
S = np.array([[f0, X0], [f1, X1], [f2, X2], [f3, X3]])
S = S[np.argsort(S.T[0])]
if np.any(np.isnan(S.T[0].astype(float))):
self.write_log('One of the values of S is nan. Stopping the computation.')
return S[0][1], met, 2
count_simp = 0
slp1, slp2, af1, af2 = self.objective_function_vec(S[0][1], met)
while (np.abs(slp1) > self.tol.ep or\
np.abs(slp2) > self.tol.rw or\
np.abs(af1 - af2) > self.tol.dif) and\
count_simp < it_simp:
Santes = np.copy(S)
S = self.simplex(S, met)
if Santes == S:
self.write_log('No change in S from previous value. Stopping cycle')
break
slp1, slp2, af1, af2 = self.objective_function_vec(S[0][1], met)
self.new_values([af1, S[0][1][0], S[0][1][1], S[0][1][2]])
count_simp += 1
for j in range(4):
if j == 0:
log_string = '{:2d} {: 7.5f} {:7.3f} {:4.3f} {:4.3f} {: 6.5f} {: 6.5f} '\
'{: 6.5f} {: 6.5f}'.format(count_simp, S[0][0], S[0][1][0],
S[0][1][1], S[0][1][2], slp1,
slp2, af1-af2, af1-met)
else:
log_string = '{:2d} {: 7.5f} {:7.3f} {:4.3f} {:4.3f}'.format(count_simp,
S[j][0],
S[j][1][0],
S[j][1][1],
S[j][1][2])
self.write_log(log_string)
if self.check_correct_vals() == -1:
counter += 1
T = S[0][1][0]
logg = S[0][1][1]
vmic = S[0][1][2]
met = af1
self.new_values([met, T, logg, vmic])
if counter > 1:
self.write_log('###############################################')
self.write_log('Coverged at:')
log_string = '{:>5s} {:>8s} {:>5s} {:>5s} {:>5s} {:>8s} {:>8s} '\
'{:>8s} {:>8s}'.format('Cycle', 'T', 'logg', 'vt', 'met', 'slp1',
'slp2', 'af1-af2', 'af1-met')
self.write_log(log_string)
log_string = '{:5d} {:7.3f} {:4.3f} {:4.3f} {:4.3f} {: 6.5f} {: 6.5f} '\
'{: 6.5f} {: 6.5f}'.format(i+1, T, logg, vmic, met,
slp1, slp2, af1-af2, af1-met)
self.write_log(log_string)
return S[0][1], met, 1
if (counter == 1) and self.one_round:
self.write_log('###############################################')
self.write_log('Coverged only one time at:')
log_string = '{:>5s} {:>8s} {:>5s} {:>5s} {:>5s} {:>8s} '\
'{:>8s} {:>8s} {:>8s}'.format('Cycle', 'T', 'logg', 'vt', 'met',
'slp1', 'slp2', 'af1-af2', 'af1-met')
self.write_log(log_string)
log_string = '{:5d} {:7.3f} {:4.3f} {:4.3f} {:4.3f} {: 6.5f} {: 6.5f} '\
'{: 6.5f} {: 6.5f}'.format(i+1, T, logg, vmic, met, slp1, slp2,
af1-af2, af1-met)
self.write_log(log_string)
return S[0][1], met, 1
self.write_log('###############################################')
self.write_log('New Cycle')
self.write_log('{:>8s} {:>5s} {:>5s} {:>6s}'.format('T', 'logg', 'vt', 'met'))
self.write_log('{:7.3f} {:4.3f} {:4.3f} {: 4.3f}'.format(T, logg, vmic, af1))
return S[0][1], met, 2
#******************************************************************************
def runODR(x, y, weights, deg=1):
isort = np.argsort(x)
func = ODR.polynomial(deg)
mydata = ODR.Data(x=x[isort], y=y[isort], we=weights[isort])
myodr = ODR.ODR(mydata, func)
myoutput = myodr.run()
beta = myoutput.beta[1]
del isort, func, mydata, myodr, myoutput
return beta
#@profile
def compute_average_abundance(starname, w=False, alias='test', mode='linearregression'):
nfailed = 0
flag = -1
ep = dif = rw = final_Fe = -99
abund = {'ab':{'FeI':-99, 'FeII': -999},\
'lines':{'FeI':[], 'FeII':[]}}
names = ['FeI', 'FeII']
names_file = ['Fe I', 'Fe II']
failed1 = re.compile(r'OH NO! ANOTHER FAILED ITERATION!')
failed2 = re.compile(r'CANNOT DECIDE ON A LINE WAVELENGTH STEP FOR')
line1 = re.compile(r'[a-z]')
line2 = re.compile(r'[\d]+ [\d]+.+')
epline = re.compile(r'E.P. correlation')
rwline = re.compile(r'R.W. correlation')
abline = re.compile(r'average abundance')
with open('./output/%s_out.test' % alias, 'r') as filemoog:
for line in filemoog:
line = line.strip()
if failed1.search(line) or failed2.search(line):
nfailed += 1
for p in range(2):
m = re.search(r'Abundance Results for Species (%s\s)\.*' % names_file[p], line)
if m:
flag = p
m = line1.search(line)
if m is None:
m = line2.search(line)
if m:
abund['lines'][names[flag]].append(line)
m = epline.search(line)
if m and flag == 0:
ep = float(line.split()[4])
m = rwline.search(line)
if m and flag == 0:
rw = float(line.split()[4])
m = abline.search(line)
if m:
abund['ab'][names[flag]] = float(line.split()[3])
del failed1, failed2, line1, line2, epline, rwline, abline, filemoog
if mode == 'linearregression':
for p in names:
ab = np.array([fe.split()[6] for fe in abund['lines'][p]], dtype=float)
if p == 'FeI':
iclip = sigma_clip(ab, maxiters=1)
a_list = np.array([list(map(fe.split().__getitem__, [2, 5]))\
for fe in abund['lines'][p]], dtype=float).T
ep_list = a_list[0]
rw_list = a_list[1]
isort_ep = np.argsort(ep_list[~iclip.mask])
isort_rw = np.argsort(rw_list[~iclip.mask])
ep, _, _, _, _ = linregress(ep_list[~iclip.mask][isort_ep],\
ab[~iclip.mask][isort_ep])
rw, _, _, _, _ = linregress(rw_list[~iclip.mask][isort_rw],\
ab[~iclip.mask][isort_rw])
abund['ab'][p] = np.mean(ab[~iclip.mask])
del iclip, ep_list, rw_list, isort_ep, isort_rw, a_list
else:
abund['ab'][p] = np.median(ab)
del ab
elif mode == 'odr':
filename = './EW/%s.txt' % starname
filelines = ascii.read(filename, include_names=('col1', 'col2', 'col4', 'col5'))
file_wave = filelines['col1']
file_ew = filelines['col2']
file_e_ew = np.maximum(filelines['col4'], filelines['col5'])
for p in names:
ab = np.array([fe.split()[6] for fe in abund['lines'][p]], dtype=float)
if p == 'FeI':
a_list = np.array([list(map(fe.split().__getitem__, [2, 5]))\
for fe in abund['lines'][p]], dtype=float).T
ep_list = a_list[0]
rw_list = a_list[1]
wave = np.array([fe.split()[0] for fe in abund['lines'][p]], dtype=float)
ew = np.array([file_ew[int(np.where(file_wave == wv)[0])] for wv in wave])
ew_err = np.array([file_e_ew[int(np.where(file_wave == wv)[0])] for wv in wave])
weights = 1./(ew_err/ew)
weights = weights/np.sum(weights)
ep = runODR(ep_list, ab, weights=weights)
rw = runODR(rw_list, ab, weights=weights)
abund['ab'][p] = np.mean(unumpy.uarray(ab, (ew_err/ew)/np.sum(ew_err/ew))).n
del wave, ew_err, a_list, ep_list, rw_list, weights, ew
else:
wave = np.array([fe.split()[0] for fe in abund['lines'][p]], dtype=float)
ew = np.array([file_ew[int(np.where(file_wave == wv)[0])] for wv in wave])
ew_err = np.array([file_e_ew[int(np.where(file_wave == wv)[0])] for wv in wave])
abund['ab'][p] = np.mean(unumpy.uarray(ab, (ew_err/ew)/np.sum(ew_err/ew))).n
del wave, ew_err, ew
del ab
del filelines, file_wave, file_ew, file_e_ew
if w:
filename = './EW/%s.txt' % starname
filelines = ascii.read(filename, include_names=('col1', 'col2', 'col3', 'col4', 'col5'))
file_wave = filelines['col1']
file_ew = filelines['col2']
file_e_ew = np.maximum(filelines['col4'], filelines['col5'])
for p in names:
a_list = np.array([list(map(fe.split().__getitem__, [0, 6]))\
for fe in abund['lines'][p]], dtype=float).T
wave = a_list[0]
ab = a_list[1]
w = np.array([1./file_e_ew[int(np.where(file_wave == wv)[0])] \
for wv in wave])
if sum(w) != 0.:
abund['ab'][p] = round(np.average(ab, weights=w), 3)
else:
abund['ab'][p] = np.mean(ab)
del filename, filelines, file_wave, file_ew, file_e_ew, wave, ab, w
dif = abund['ab']['FeI'] - abund['ab']['FeII']
final_Fe = abund['ab']['FeI']
del abund, names, names_file
return final_Fe, ep, dif, rw, nfailed
#******************************************************************************
def mult(x, base, level):
"""
Finds the multiple of 'base' closer to the number x.
Input: x: number
base: base for which we want to compute the closer value
level: 'upper' or 'floor'
the higher or lower multiple of 'base' of x.
Return: closer multiple of 'base' to the number x.
"""
num = math.floor(old_div(x, base))
if level == 'upper':
final = (num + 1)*base
else:
final = num*base
if final == x:
final = final - base
return final
def calc_params(star, hold, init_vals, debug, log_f=None,\
set_boundaries=False, vals_boundaries=None,\
in_errors=False, alias='test',
minimization='per_parameter',
one_round=False, read_mode='linearregression'):
"""
Computes the stellar parameters for a certain star.
Uses the methods: Metallicity, Temperature, Pressure and Velocity.
Input: star: name of the star.
hold : array with the values that are not to be changed.
When empty, all the values will be changed.
init_vals : initial parameters.
When empty, default values will be used.
debug : True/False,
if you want to turn on or off the debugging option.
file_debug : File where the output from the debugging
will be stored.
Returns: T: temperature of the model.
logg: surface gravity.
xmetal: metallicity.
micro: microturbulence velocity.
exception: 1 or 2. If 1, then it founds the correct parameters.
If 2, it encountered a problem and couldn't converge to a solution.
"""
# Creates the atmosphere object, where all the data concerning the
# computation of the atmospheric parameters will be stored.
a = atmos(star, hold, init_vals, debug, log_f, in_errors, \
set_boundaries, vals_boundaries, alias=alias,\
one_round=one_round, read_mode=read_mode)
h_array = a.show_hold
a.write_log('hold_m=%s, hold_t=%s, hold_p=%s, hold_v=%s' % \
(h_array[0], h_array[1], h_array[2], h_array[3]))
line_hold_param = ', '.join(['%s = (%.2f, %.2f)' % (n, b[0], b[1])\
for n, b in zip(a.parameters, a.boundaries)])
a.write_log('Boundaries are: %s' % line_hold_param)
del line_hold_param
a.write_log('Initial values are: feh=%.2f, T=%.0f, logg=%.2f, vt=%.2f' % \
(init_vals[0], init_vals[1], init_vals[2], init_vals[3]))
# Modifies the output from MOOG if hold metallicity is 'yes'
xmetal_i = a.metallicity.value
a.check_hold(xmetal_i)
if minimization == 'per_parameter':
# First iteration with MOOG.
ab, ep, dif, rw, nfailed = runMOOG(a.starname, a.values, a.alias, a.read_mode)
a.moog_output([ab, ep, dif, rw], nfailed)
a.write_log('ab=%.3f, ep=%.3f, dif=%.3f, rw=%.3f, nfailed=%d' % \
(ab, ep, dif, rw, nfailed))
a.metallicity.ranges[2] = ab
a.write_log('change=%s' % a.change)
i = 0
while True:
# Values that need to be reset each iteration
a.new_iteration(xmetal_i)
# Found the right values
if a.check_correct_vals() == -1:
break
# If all the parameters are out of range, break the calculation
if a.check_nout() == -1:
break
# Parameters still in the permitted ranges
change = a.change
if change == 'metallicity':
a = Metallicity(a)
ab = a.moog[0]
a.metallicity.ranges = [-999., -999., ab]
a.temperature.ranges = [-999., -999.]
a.gravity.ranges = [-999., -999.]
a.velocity.ranges = [-999., -999.]
i = 0
a.check_nfailed_it(change)
elif change == 'temperature':
a, i = Temperature(a, i)
a.check_nfailed_it(change)
elif change == 'pressure':
a, i = Pressure(a, i)
a.check_nfailed_it(change)
else:
a, i = Velocity(a, i)
a.check_nfailed_it(change)
# If an iteration failed, change the input parameters
# according to a normal distribution
a.check_nfailed()
# If the iteration has failed more than 5 times,
# break the calculation
if a.check_nbreak() == -1:
break
# If the parameters for an iteration are the same
# as a previous one, save them
a.check_params_rep()
# If the parameters are repeated more than
# 500 times, break the calculation
if a.check_nrepeat() == -1:
break
# If mnore than 1 million iterations have been performed
# and the values have not converge, stop the calculation
if a.check_nit_total() == -1:
break
a.write_log('change is %s' % change)
xmetal, T, logg, micro = list(a.values)
exception = a.exception
a.write_log('Final parameters for %s: feh=%.3f, T=%.0f, logg=%.3f, micro=%.3f' %\
(star, xmetal, T, logg, micro))
del a, i, xmetal_i, h_array
elif minimization == 'downhill_simplex':
# First iteration with MOOG.
ab, ep, dif, rw, nfailed = runMOOG(a.starname, a.values, a.alias, a.read_mode)
ab, ep, dif, rw = a.update_moog([ab, ep, dif, rw], nfailed)
if a.check_correct_vals() == -1:
vals = a.values
del a
return vals[1], vals[2], vals[0], vals[3], 1
[T, logg, micro], xmetal, exception = a.nelder_optimizer(60, 10)
del a
return T, logg, xmetal, micro, exception
def Metallicity(atm):
"""
Runs MOOG with a model atmosphere in which the
metallicity is the value that changes.
It stops running when the abundances derived by MOOG
are the same as the input value.
"""
c = 0
nfailed = 0
nit_total = 0
while True:
nit_total += 1
if nit_total > 100000:
atm.write_log('Iteration in Metallicity was completed '\
'more than 100000 times. '\
'Stopping the computation.')
atm.new_change()
break
xmetal = atm.metallicity.value
xmetal_antes = xmetal
ab, ep, dif, rw = atm.moog
if abs(ab - xmetal) <= atm.tol.ab:
atm.new_change()
break
else:
if c > 50:
if abs(ep) <= atm.tol.ep:
atm.new_change('temperature')
else:
atm.new_change()
break
atm.change_metallicity(ab)
xmetal = atm.metallicity.value
bound_min, bound_max = atm.metallicity.bounds
if xmetal < bound_min or xmetal > bound_max:
atm.write_log('Not possible to compute parameters '\
'for [Fe/H] < %.1f or [Fe/H] > %.1f. '\
'Check the boundaries of your parameter.' % \
(bound_min, bound_max))
atm.new_change()
atm.change_metallicity(xmetal_antes)
break
ab, ep, dif, rw, nfailed = runMOOG(atm.starname, atm.values,
atm.alias, atm.read_mode)
atm.moog_output([ab, ep, dif, rw], nfailed)
atm.write_debug_moog()
del ab, ep, dif, rw, xmetal, xmetal_antes
if nfailed > 0:
atm.new_change()
break
c += 1
del c, nit_total, nfailed
return atm
#******************************************************************************
def Temperature(atm, i):
"""
Runs MOOG with a model atmosphere in which the
temperature is the value that changes.
It stops running when the correlation between Ab(FeI)
and excitation potential is less than 0.002
"""
nfailed = 0
nit_total = 0
while True:
nit_total += 1
if nit_total > 100000:
atm.write_log('Iteration in Temperature was completed '\
'more than 100000 times. '\
'Stopping the computation.')
atm.new_change()
break
Tantes = atm.temperature.value
ab, ep, dif, rw = atm.moog
if abs(ep) <= atm.tol.ep:
if ab == atm.metallicity.value:
atm.new_change()
else:
atm.new_change('velocity')
break
else:
atm.change_parameter('temperature', 1, 250., 0)
T = atm.temperature.value
atm.write_log('T is %.0f, Tantes is %.0f' % (T, Tantes))
bound_min, bound_max = atm.temperature.bounds
if T > bound_max or T < bound_min:
atm.write_log('Not possible to compute parameters '\
'for T < %d or T > %d. '\
'Check the boundaries of your parameter.' % \
(int(bound_min), int(bound_max)))
atm.new_change()
atm.temperature.value = Tantes #???????????????????
if T < 3500.:
atm.temperature.value = 3500.
break
ab, ep, dif, rw, nfailed = runMOOG(atm.starname, atm.values,
atm.alias, atm.read_mode)
atm.moog_output([ab, ep, dif, rw], nfailed)
atm.write_debug_moog()
atm.metallicity.ranges[i] = ab
del ab, ep, dif, rw, T, Tantes
i += 1
if i == 3:
i = 0
if nfailed > 0:
atm.new_change()
break
if atm.check_met():
atm.new_change('velocity')
break
del nit_total, nfailed
return atm, i
#******************************************************************************
def Pressure(atm, i):
"""
Runs MOOG with a model atmosphere in which the
surface gravity is the value that changes.
It stops running when the difference between the abundances
derived for FeI and FeII is less than 0.002.
"""
nfailed = 0
nit_total = 0
while True:
nit_total += 1
if nit_total > 100000:
atm.write_log('Iteration in Pressure was completed '\
'more than 100000 times. '\
'Stopping the computation.')
atm.new_change()
break
logg_antes = atm.gravity.value
ab, ep, dif, rw = atm.moog
if abs(dif) <= atm.tol.dif:
if ab == atm.metallicity.value:
atm.new_change()
else:
atm.new_change('velocity')
break
else:
atm.change_parameter('gravity', 2, 0.25, 5)
logg = atm.gravity.value
bound_min, bound_max = atm.gravity.bounds
if logg < bound_min or logg > bound_max:
atm.write_log('Not possible to compute parameters '\