-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathstellab.py
1309 lines (998 loc) · 46.4 KB
/
stellab.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, print_function, absolute_import,
unicode_literals)
'''
Stellab (Stellar Abundances)
Fonctionality
=============
This class plots observational stellar abundances data for different galaxies.
Made by
=======
JUNE2015: B. Cote
Usage
=====
Import the module:
>>> import stellab
Load the data:
>>> s = stellab.stellab()
Plot [Mg/Fe] vs [Fe/H] for the Fornax dwarf spheoridal galaxy:
>>> s.plot_spectro(xaxis='[Fe/H]', yaxis='[Mg/Fe]', galaxy='fornax')
See the Sphinx documentation for more options
'''
# Import standard packages
import matplotlib
import matplotlib.pyplot as plt
import os
# Define where is the working directory
# This is where the NuPyCEE code will be extracted
nupy_path = os.path.dirname(os.path.realpath(__file__))
# Import NuPyCEE codes
import NuPyCEE.read_yields as ry
class stellab():
'''
See the plot_spectro() function to plot the data.
'''
# Input paramters
def __init__(self):
# read abundance data library via
# index file abundance_data_library.txt
# creates self.paths, self.paths_s, self.cs, self.leg
self.read_abundance_data_library(os.path.join(nupy_path, "stellab_data",\
"abundance_data_library.txt"))
# Declaration of the name of available galaxies
#self.galaxy_name = []
#self.galaxy_name.append('milky_way')
#self.galaxy_name.append('sculptor')
#self.galaxy_name.append('fornax')
#self.galaxy_name.append('carina')
#self.galaxy_name.append('lmc')
# Declaration of arrays containing information on every data set
self.elem_list = [] # Available elements
self.elem = [] # Nominator and denominator of each abundance ratio
self.ab = [] # Stellar abundances
#self.paths = [] # Path to the stellar abundances file
#self.paths_s = [] # Path to the solar values
self.solar = [] # Solar values used
#self.cs = [] # Color and symbol
#self.leg = [] # Legend
## List of all the solar values used in the data sets
#for i_path in range(0,len(self.paths)):
# self.paths_s.append(self.paths[i_path]+'_s')
# For every data set ...
for i_entry in range(0,len(self.paths)):
# Copy the stellar abundaces
self.__read_data_file(self.paths[i_entry], i_entry)
# Copy the solar values used for the normalization
self.__read_solar_values(self.paths_s[i_entry], i_entry)
# Copy the list of available elements
self.elem_list.append([])
for i_elem in range(0,len(self.solar[i_entry])):
self.elem_list[i_entry].append(self.solar[i_entry][i_elem][0])
# Declaration of arrays containing information for every solar reference
self.paths_norm = [] # Path of the files
self.sol_norm = [] # Solar abundance "eps_x = log(n_x/n_H) + 12"
# List of all the reference solar normalization (path to file)
self.paths_norm.append('Anders_Grevesse_1989')
self.paths_norm.append('Grevesse_Noels_1993')
self.paths_norm.append('Grevesse_Sauval_1998')
self.paths_norm.append('Asplund_et_al_2009')
self.paths_norm.append('Asplund_et_al_2005')
self.paths_norm.append('Lodders_et_al_2009')
# For every solar reference ...
for i_sol in range(0,len(self.paths_norm)):
# Copy the solar values
self.__read_solar_norm(self.paths_norm[i_sol], i_sol)
##############################################
# Read abundance data library #
##############################################
def read_abundance_data_library(self,filename):
# open index file
f=open(filename)
lines=f.readlines()
f.close()
# Declaration of the name of available galaxies
# name of galaxy
self.galaxy_name = []
# color + symbol
self.cs=[]
# legend
self.leg=[]
# path to the stellar abundances file
self.paths = []
for k in range(len(lines)):
line = lines[k]
#ignore header
if k == 0:
continue
#if commented line
if line[0] == '#':
continue
# Declaration of the name of available galaxies
if line[0] == 'H':
galaxy=line[1:].strip()
self.galaxy_name.append(galaxy)
#print ('found galaxy ',galaxy)
# read data set
else:
#print (line)
path = 'stellab_data/'+line.split('&&')[0].strip()
leg = line.split('&&')[1].strip()
cs = line.split('&&')[2].strip()
self.paths.append(path)
self.leg.append(leg)
self.cs.append(cs)
#print ('path : ',path)
#print ('leg : ',leg)
self.paths_s = [] # Path to the solar values
# List of all the solar values used in the data sets
for i_path in range(0,len(self.paths)):
self.paths_s.append(self.paths[i_path]+'_s')
##############################################
# Read Data File #
##############################################
def __read_data_file(self, file_path, i_entry):
# Create an entry
self.elem_list.append([])
self.elem.append([])
self.ab.append([])
# Open the data file
with open(os.path.join(nupy_path, file_path + ".txt"), "r") as data_file:
# Read and split the first line (header)
line_1_str = data_file.readline()
ratios_f = [str(x) for x in line_1_str.split()]
# Extract the X and Y in the [X/Y] ratio
err_available = self.__extract_x_y(ratios_f, i_entry)
# Get the number of ratios
nb_ratios = len(self.elem[i_entry])
# For every remaining line (for each star) ...
i_star = 0
for line_2_str in data_file:
# Split the line
ab_f = [float(x) for x in line_2_str.split()]
# Create a star entry
self.ab[i_entry].append([])
# For every abundance ratio ...
i_ab = 0
for i_ratio in range(0,nb_ratios):
# Copy the ratio (stellar abundance)
self.ab[i_entry][i_star].append([ab_f[i_ab],0.0])
i_ab += 1
# Add the error bars if any
if err_available:
if ab_f[i_ab] == -30.0:
self.ab[i_entry][i_star][i_ratio][1] = 0.0
else:
self.ab[i_entry][i_star][i_ratio][1] = ab_f[i_ab]
i_ab += 1
# Move to the next line (next star)
i_star += 1
# Close the file
data_file.close()
##############################################
# Read Solar Values #
##############################################
def __read_solar_values(self, file_path, i_entry):
# Create an entry
self.solar.append([])
# Open the data file
with open(os.path.join(nupy_path, file_path + ".txt"), "r") as data_file:
# For every line (for each element) ...
i_elem = 0
for line_1_str in data_file:
# Create an entry
self.solar[i_entry].append([])
# Split the line
split = [str(x) for x in line_1_str.split()]
# Copy the element and the solar value
self.solar[i_entry][i_elem].append(str(split[0]))
self.solar[i_entry][i_elem].append(float(split[1]))
# Go to the next element
i_elem += 1
# Close the file
data_file.close()
##############################################
# Read Solar Norm #
##############################################
def __read_solar_norm(self, file_path, i_sol):
# Create an entry
self.sol_norm.append([])
# Open the data file
with open(os.path.join(nupy_path, "stellab_data", "solar_normalization",\
file_path + ".txt"), "r") as data_file:
# For every line (for each element) ...
i_elem = 0
for line_1_str in data_file:
# Create an entry
self.sol_norm[i_sol].append([])
# Split the line
split = [str(x) for x in line_1_str.split()]
# Copy the element and the solar value
self.sol_norm[i_sol][i_elem].append(str(split[1]))
self.sol_norm[i_sol][i_elem].append(float(split[2]))
# Go to the next element
i_elem += 1
# Close the file
data_file.close()
##############################################
# Extract X Y #
##############################################
def __extract_x_y(self, ratios_f, i_entry):
# Variable that indicates if the file contains error bars
err_available = False
# For each available ratios [X/Y] ...
for i_ratio in range(0,len(ratios_f)):
# Look if the ratio is actually an error bar
if ratios_f[i_ratio] == 'err':
err_available = True
# If it is not an error bar ...
else:
# Get X and Y
x_extxt, y_extxt = self.__get_x_y(ratios_f[i_ratio])
# Copy the extracted X and Y strings
self.elem[i_entry].append([x_extxt,y_extxt])
# Return wether or not error bars were present in the input string
return err_available
##############################################
# Get X Y #
##############################################
def __get_x_y(self, str_ratio):
# Initialisation of the numerator X and the denominator Y
x_in = ''
y_in = ''
is_num = True
for i_str in range(0,len(str_ratio)):
if str_ratio[i_str] == '/':
is_num = False
elif (not str_ratio[i_str] == '[') and \
(not str_ratio[i_str] == ']'):
if is_num:
x_in += str_ratio[i_str]
else:
y_in += str_ratio[i_str]
# Return the X and Y of the [X/Y] ratio
return x_in, y_in
##############################################
# Plot Spectro #
##############################################
def plot_spectro(self,fig=-1, galaxy='', xaxis='[Fe/H]', yaxis='[Mg/Fe]', \
fsize=[10,4.5], fontsize=14, rspace=0.6, bspace=0.15,\
labelsize=15, legend_fontsize=14, ms=6.0, norm='', obs='',\
overplot=False, return_xy=False, show_err=False, \
show_mean_err=False, stat=False, flat=False,abundistr=False,show_legend=True, \
sub=1, sub_plot=False, alpha=1.0, lw=1.0):
'''
This function plots observational data with the spectroscopic notation:
[X/Y] = log10(n_X/n_Y) - log10(n_X/n_Y)_solar where 'n_X' and 'n_Y' are
the number densities of elements X and Y.
Parameters
---------
galaxy : string
Name of the target galaxy. The code then automatically selects
the corresponding data sets (when available).
Choices : 'milky_way', 'sculptor', 'carina', 'fornax'
Default value : 'milky_way'
xaxis : string
Elements on the x axis in the form of '[X/Y]'.
Default value : '[Fe/H]'
yaxis : string
Elements on the y axis in the form of '[X/Y]'.
Default value : '[Mg/Fe]'
norm : string
Common solar normalization used to scale all the data. Use the
list_solar_norm() function for a list of available normalizations.
When not specified, each data uses the solar normalization of the
reference paper.
Example : norm='Anders_Grevesse_1989'
obs : string array
Personal selection of observational data. Use the list_ref_papers()
function for a list of availble data sets. When not specified, all
the available data for the selected galaxy will be plotted.
Example : obs=['milky_way_data/Venn_et_al_2004_stellab',
'milky_way_data/Hinkel_et_al_2014_stellab']
show_err : boolean
If True, show error bars when available in the code.
Default value : False
show_mean_err : boolean
If True, print the mean X and Y errors when error bars are available
in the code.
Default value : False
return_xy : boolean
If True, return the X and Y axis arrays instead of plotting the data.
Default value = False
Example : x, y = stellab.plot_spectro(return_xy=True)
fsize : 2D float array
Figure dimension/size.
fontsize : integer
Font size of the numbers on the X and Y axis.
rspace : float
Extra space on the right for the legend.
bspace : float
Extra space at the bottom for the Y axis label.
labelsize : integer
Font size of the X and Y axis labels.
legend_fontsize : integer
Font size of the legend.
Examples
----------
>>> stellab.plot_spectro(yaxis='[Ti/H]',xaxis='[Mg/H]',galaxy='sculptor',norm='Anders_Grevesse_1989',show_err=True)
'''
# Copy the marker size
ms_copy = ms
lw_copy = lw
# Extract the X and Y of the input [X/Y]
xx, yx = self.__get_x_y(xaxis)
xy, yy = self.__get_x_y(yaxis)
elem_in = ([xx,yx], [xy,yy])
# Initialization of the variables used to calculate the average error
sum_x = 0.0
sum_y = 0.0
sum_count = 0
# Initialization of the variables used for statistical plot
if stat:
xy_plot_all = []
xy_plot_all.append([])
xy_plot_all.append([])
# Show the frame of the plot
if not overplot and not return_xy and not sub_plot:
if fig>=0:
plt.figure(fig,figsize=(fsize[0],fsize[1]))
else:
plt.figure(fig,figsize=(fsize[0],fsize[1]))
# If data need to be re-normalized ...
re_norm = False
if len(norm) > 0:
# Get the array index associated with the solar normalization
i_re_norm = self.__get_i_re_norm(norm)
# Look if the normalization reference is valid ...
if not i_re_norm == -1:
re_norm = True
# Warning message if the reference is not valid
else:
print ('!! Warning - The solar normalization is not valid !!')
# Return the list of index of the wanted data set
# If a specif set of references is choosen ...
if len(obs) > 0:
# Get the indexes for the wanted references
i_obs = self.__get_i_data_set(obs)
# If a specific galaxy is choosen ...
elif len(galaxy) > 0:
# Get the indexes of the wanted galaxy
i_obs = self.__get_i_data_galaxy(galaxy)
# If the default mode is used ...
else:
# Use the Milky Way
i_obs = self.__get_i_data_galaxy('milky_way')
# Keep the number of indexes in memory
len_i_obs = len(i_obs)
# Prepare the returning array if the option is choosen
if return_xy:
ret_x = []
ret_y = []
ret_x_err = []
ret_y_err = []
#store index of stars of all data sets
ret_star_i = []
# For every data set ...
for i_obs_index in range(0,len_i_obs):
# Copy the data set index
i_ds = i_obs[i_obs_index]
#store temporary index of stars for current data set
ret_star_i_tmp = []
# If the current data set contains the input elements ...
if elem_in[0][0] in self.elem_list[i_ds] and \
elem_in[0][1] in self.elem_list[i_ds] and \
elem_in[1][0] in self.elem_list[i_ds] and \
elem_in[1][1] in self.elem_list[i_ds]:
# Variable that tells if the ratio can be plotted
ok_for_plot = True
# Local variables
xy_plot = [[0.0 for ii in range(len(self.ab[i_ds]))] \
for ijk in range(2)]
err_plot = [[0.0 for ii in range(len(self.ab[i_ds]))] \
for ijk in range(2)]
# Common loop for the x-axis and the y-axis ...
for i_x_y in range(0,2):
# Find the corresponding entry in the data set for the numinator
i_num, den_ok = self.__find_num(i_ds, i_x_y, elem_in)
# If a perfect match was found ...
if den_ok:
# Copy the current data set for the current input element ratio
for i_star in range(0,len(self.ab[i_ds])):
xy_plot[i_x_y][i_star] = self.ab[i_ds][i_star][i_num][0]
err_plot[i_x_y][i_star] = self.ab[i_ds][i_star][i_num][1]
ret_star_i_tmp.append(i_star)
# If the data need to be manipulated to recover the wanted ratio ...
else:
# Find the corresponding entry in the data for the denominator
i_denom, div = self.__find_denom(i_ds, i_x_y, i_num, elem_in)
# Verify if the ratio can be plotted
if i_denom == -1:
ok_for_plot = False
else:
# If a division is needed to recover the wanted ratio ...
# Want [A/C] with nominator [A/B] and denominator [C/B]
if div:
# Calculate the ratio for each star in the data set ...
for i_star in range(0,len(self.ab[i_ds])):
if self.ab[i_ds][i_star][i_num][0] == -30.0 or \
self.ab[i_ds][i_star][i_denom][0] == -30.0 :
xy_plot[i_x_y][i_star] = -30.0
ret_star_i_tmp.append(i_star)
else:
xy_plot[i_x_y][i_star] = \
self.ab[i_ds][i_star][i_num][0] - \
self.ab[i_ds][i_star][i_denom][0]
ret_star_i_tmp.append(i_star)
# If a multiplication is needed to recover the input ratio ..
# Want [A/C] with nominator [A/B] and denominator [B/C]
else:
# Calculate the ratio for each star in the data set ...
for i_star in range(0,len(self.ab[i_ds])):
xy_plot[i_x_y][i_star] = \
self.ab[i_ds][i_star][i_num][0] + \
self.ab[i_ds][i_star][i_denom][0]
ret_star_i_tmp.append(i_star)
# If a re-normalization is needed ...
if re_norm and ok_for_plot:
# Calculate the wanted solar normalization
# eps = log(n_X/n_Y) + 12
eps_x = self.__get_eps(i_re_norm,elem_in[i_x_y][0],True)
eps_y = self.__get_eps(i_re_norm,elem_in[i_x_y][1],True)
sol_wanted = eps_x - eps_y
# Calculate the solar normalization used in data
eps_x = self.__get_eps(i_ds,elem_in[i_x_y][0],False)
eps_y = self.__get_eps(i_ds,elem_in[i_x_y][1],False)
sol_data = eps_x - eps_y
# If the solar values where available in the ref. paper ...
if not eps_x == -30.0 and not eps_y == -30.0:
# Indication of the success of re-normalization
sol_ab_found = True
# For every star ...
for i_star in range(0,len(self.ab[i_ds])):
# Correct the normalization
xy_plot[i_x_y][i_star] = \
xy_plot[i_x_y][i_star] + sol_data - sol_wanted
# Warning message if the re-normalization cannot be made
else:
sol_ab_found = False
if i_x_y == 0:
warn_ratio = xaxis
else:
warn_ratio = yaxis
if eps_x == -30.0 and eps_y == -30.0:
warn_el = elem_in[i_x_y][0]+' and '+elem_in[i_x_y][1]
print ('Solar values for '+warn_el+' not found in ' + \
self.leg[i_ds]+'. '+warn_ratio+ ' was not modified.')
else:
if eps_x == -30.0:
warn_el = elem_in[i_x_y][0]
else:
warn_el = elem_in[i_x_y][1]
print ('Solar value for '+warn_el+' not found in ' + \
self.leg[i_ds]+'. '+warn_ratio + ' was not modified.')
self.leg[i_ds]
#####################################################
# If ratio is available
if ok_for_plot:
# If a plot is generated
if not return_xy:
# Reduce the size of APOGEE data
if self.paths[i_ds] == 'milky_way_data/APOGEE_stellab':
ms = 1
lw = 1
else:
ms = ms_copy
lw = lw_copy
if re_norm and not sol_ab_found:
leg_temp = self.leg[i_ds]+' **'
else:
leg_temp = self.leg[i_ds]
if stat:
xy_plot_all[0].append(xy_plot[0])
xy_plot_all[1].append(xy_plot[1])
else:
if show_err:
if show_legend:
if sub_plot:
sub.errorbar(xy_plot[0], xy_plot[1], \
xerr=err_plot[0], yerr=err_plot[1],
fmt=self.cs[i_ds][0], ecolor=self.cs[i_ds][1], \
color=self.cs[i_ds][1], label=leg_temp, markersize=ms, \
alpha=alpha, linewidth=lw)
else:
plt.errorbar(xy_plot[0], xy_plot[1], \
xerr=err_plot[0], yerr=err_plot[1],
fmt=self.cs[i_ds][0], ecolor=self.cs[i_ds][1], \
color=self.cs[i_ds][1], label=leg_temp, markersize=ms, \
alpha=alpha, linewidth=lw)
else:
if sub_plot:
sub.errorbar(xy_plot[0], xy_plot[1], \
xerr=err_plot[0], yerr=err_plot[1],
fmt=self.cs[i_ds][0], ecolor=self.cs[i_ds][1], \
color=self.cs[i_ds][1], markersize=ms, alpha=alpha, \
linewidth=lw)
else:
plt.errorbar(xy_plot[0], xy_plot[1], \
xerr=err_plot[0], yerr=err_plot[1],\
fmt=self.cs[i_ds][0], ecolor=self.cs[i_ds][1], \
color=self.cs[i_ds][1], markersize=ms, alpha=alpha, \
linewidth=lw)
else:
if show_legend:
if sub_plot:
sub.plot(xy_plot[0],xy_plot[1],self.cs[i_ds],\
label=leg_temp, markersize=ms, alpha=alpha, \
linewidth=lw)
else:
plt.plot(xy_plot[0],xy_plot[1],self.cs[i_ds],\
label=leg_temp, markersize=ms, alpha=alpha, \
linewidth=lw)
else:
if sub_plot:
sub.plot(xy_plot[0],xy_plot[1],self.cs[i_ds], \
markersize=ms, alpha=alpha, linewidth=lw)
else:
plt.plot(xy_plot[0],xy_plot[1],self.cs[i_ds], \
markersize=ms, alpha=alpha, linewidth=lw)
if show_legend:
if sub_plot:
sub.legend()
else:
plt.legend()
# If the data need to be returned ...
else:
# Add the data set to the returning arrays
for i_ret in range(0,len(xy_plot[0])):
if xy_plot[0][i_ret] < 5 and xy_plot[0][i_ret] > -5 and \
xy_plot[1][i_ret] < 5 and xy_plot[1][i_ret] > -5:
ret_x.append(xy_plot[0][i_ret])
ret_y.append(xy_plot[1][i_ret])
ret_x_err.append(err_plot[0][i_ret])
ret_y_err.append(err_plot[1][i_ret])
ret_star_i.append(ret_star_i_tmp[i_ret])
# If the average error need to be calculated...
if show_mean_err:
# For every data point ...
for i_dp in range(0,len(self.ab[i_ds])):
# Add the error to the sum
if not xy_plot[0][i_dp] == -30.0 and \
not xy_plot[1][i_dp] == -30.0 and \
not err_plot[0][i_dp] == 0.0 and \
not err_plot[1][i_dp] == 0.0:
sum_x += err_plot[0][i_dp]
sum_y += err_plot[1][i_dp]
sum_count += 1
# Calculate the average error
if show_mean_err and not return_xy:
if sum_count > 0:
print ('Mean',xaxis,'error =',sum_x/sum_count)
print ('Mean',yaxis,'error =',sum_y/sum_count)
# Provide a standard plot
if not return_xy:
# If plot median, 68%, 95%,
if stat:
# Build the x_bin
x_bin = []
y_bin = []
dx_bin = 0.3
x_max = 1.0
xx = -5.0
while xx <= x_max:
x_bin.append(xx)
y_bin.append([])
xx += dx_bin
# Put the y data in the right bin
for xi in range(0,len(xy_plot_all[0])):
for xxi in range(0,len(xy_plot_all[0][xi])):
for xb in range(len(x_bin)-1):
if x_bin[xb] <= xy_plot_all[0][xi][xxi] < x_bin[xb+1]:
if xy_plot_all[1][xi][xxi] > -10.0:
y_bin[xb].append(xy_plot_all[1][xi][xxi])
y_stat = []
for i in range(0,7):
y_stat.append([])
for xb in range(len(x_bin)-1):
x_bin[xb] += 0.5 * dx_bin
y_temp = sorted(y_bin[xb])
if len(y_temp) > 0:
i_med = int(len(y_temp)/2)
y_stat[0].append(y_temp[0])
y_stat[1].append(y_temp[i_med - int(len(y_temp)*0.475)])
y_stat[2].append(y_temp[i_med - int(len(y_temp)*0.340)])
y_stat[3].append(y_temp[i_med])
y_stat[4].append(y_temp[i_med + int(len(y_temp)*0.340)])
y_stat[5].append(y_temp[i_med + int(len(y_temp)*0.475)])
y_stat[6].append(y_temp[-1])
else:
y_stat[0].append(-30.0)
y_stat[1].append(-30.0)
y_stat[2].append(-30.0)
y_stat[3].append(-30.0)
y_stat[4].append(-30.0)
y_stat[5].append(-30.0)
y_stat[6].append(-30.0)
if flat:
for i in range(0,len(y_stat[0])):
y_stat[0][i] -= y_stat[3][i]
y_stat[1][i] -= y_stat[3][i]
y_stat[2][i] -= y_stat[3][i]
y_stat[4][i] -= y_stat[3][i]
y_stat[5][i] -= y_stat[3][i]
y_stat[6][i] -= y_stat[3][i]
y_stat[3][i] = 0.0
del x_bin[-1]
plt.fill_between(x_bin,y_stat[0],y_stat[-1],color='0.95')
plt.plot(x_bin,y_stat[1],linestyle='-', color='w',linewidth=3)
plt.plot(x_bin,y_stat[1],linestyle=':', color='k',linewidth=1.5)
plt.plot(x_bin,y_stat[2],linestyle='-', color='w',linewidth=3)
plt.plot(x_bin,y_stat[2],linestyle='--',color='k',linewidth=1)
plt.plot(x_bin,y_stat[3], color='w',linewidth=3)
plt.plot(x_bin,y_stat[3], color='k',linewidth=1.5)
plt.plot(x_bin,y_stat[4],linestyle='-' ,color='w',linewidth=3)
plt.plot(x_bin,y_stat[4],linestyle='--',color='k',linewidth=1)
plt.plot(x_bin,y_stat[5],linestyle='-', color='w',linewidth=3)
plt.plot(x_bin,y_stat[5],linestyle=':', color='k',linewidth=1.5)
if not sub_plot:
ax = plt.gca()
plt.xlabel(xaxis)
plt.ylabel(yaxis)
matplotlib.rcParams.update({'font.size': 14})
self.__fig_standard(ax=ax, fontsize=fontsize, labelsize=labelsize, \
rspace=rspace, bspace=bspace, legend_fontsize=legend_fontsize)
# Return the data if the option is choosen
else:
if show_mean_err and show_err:
if sum_count > 0:
return ret_x, ret_y, ret_x_err, ret_y_err, \
sum_x/sum_count, sum_y/sum_count
else:
return ret_x, ret_y, ret_x_err, ret_y_err, sum_count, sum_count
if abundistr:
return ret_x, ret_y, ret_x_err, ret_y_err,ret_star_i
return ret_x, ret_y
##############################################
# Get i Data Set #
##############################################
def __get_i_data_set(self, obs):
# Declaration of the list of index to be returned
i_return = []
# For every wanted data set ...
for i_gids in range(0,len(obs)):
# Initialization of the index
i_search = -1
# Look every available data set
for i_look in range(0,len(self.paths)):
# Add the index if the data set is found
if self.paths[i_look] == obs[i_gids]:
i_search = i_look
i_return.append(i_look)
# Warning message if the data set is not available
if i_search == -1:
print ('!! Warning - '+obs[i_gids]+' not available !!')
# Return a bad index if the wanted normalization is not available
return i_return
##############################################
# Get i Data Galaxy #
##############################################
def __get_i_data_galaxy(self, galaxy):
# Declaration of the list of index to be returned
i_return = []
# Verify is the galaxy is available
if not galaxy in self.galaxy_name:
print ('!! Warning - '+galaxy+' not available !!')
else:
# Keep the number of characters in the name of the galaxy
nb_char = len(galaxy)
# Look every available data set
for i_look in range(0,len(self.paths)):
# Extract the nb_char first characters of the data set name
ds_name = ''
if len(self.paths[i_look]) > nb_char:
for i_extr in range(0,nb_char):
ds_name += self.paths[i_look][i_extr+13]
# Add the index if it's for the right galaxy
if ds_name == galaxy:
i_return.append(i_look)
# Return a bad index if the wanted normalization is not available
return i_return
##############################################
# Get i Re-Norm #
##############################################
def __get_i_re_norm(self, norm):
# For every available standard solar normalization ...
for i_irn in range(0,len(self.paths_norm)):
# Return the index if the file is found
if self.paths_norm[i_irn] == norm:
return i_irn
# Return a bad index if the wanted normalization is not available
return -1
##############################################
# Find Num #
##############################################
def __find_num(self, i_ds, i_x_y, elem_in):
# Arbitraty value for the index, just in case nothing is found
i_num = -1
# For each element ratio in the data set ...
for i_elem in range(0,len(self.elem[i_ds])):
# Copy the index if the right numinator is found
if self.elem[i_ds][i_elem][0] == elem_in[i_x_y][0]:
i_num = i_elem
# Look for a perfect match
if self.elem[i_ds][i_elem][1]==elem_in[i_x_y][1]:
# Return the entry index (the exact wanted ratio is found)
return i_num, True
# Return the entry index (the exact wanted ratio is not found)
return i_num, False
##############################################
# Get Eps #
##############################################
def __get_eps(self, i_eps, elem_eps, is_solar_ref):
# Select the right array
if is_solar_ref:
temp_solar = self.sol_norm[i_eps]
else:
temp_solar = self.solar[i_eps]
# Find the index associated with the wanted element
for i_get_eps in range(0,len(temp_solar)):
if temp_solar[i_get_eps][0] == elem_eps:
return temp_solar[i_get_eps][1]
# Print a warning message if the element is not found
print ('Error - Element not found in __get_eps function.')
return -30.0
##############################################
# Find Denom #
##############################################
def __find_denom(self, i_ds, i_x_y, i_num, elem_in):
# For each element ratio in the data set ...
for i_elem in range(0,len(self.elem[i_ds])):
# If the denominator is found (in the division form) ...
if self.elem[i_ds][i_elem][0] == elem_in[i_x_y][1] and \
self.elem[i_ds][i_elem][1] == self.elem[i_ds][i_num][1]:
# Return the entry index (a division is needed)
return i_elem, True
# If the denominator is found (in the multiplication form) ...
elif self.elem[i_ds][i_elem][1] == elem_in[i_x_y][1] and \
self.elem[i_ds][i_elem][0] == self.elem[i_ds][i_num][1]: