-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxmgrace_plot.py
2068 lines (1577 loc) · 69.1 KB
/
xmgrace_plot.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
import subprocess # for calling xmgrace to plot.
import copy
import math
import mytool
class PRE_Separate_levels( object ):
def __init__(self, list_lvl, dim):
''' It take a list of levels
each element is a dic, and we have keys:
'lvlE', and 'bandN', we will use these two to do level separation.
'''
self.__list_lvl = list_lvl
self.__length = dim[0]
self.__font_size = dim[1]
self.__lvl_split = dim[2] # to help further separate lables.
self.__bandL_list = []
self.__bandU_list = [] # I won't use it.
#
# to examine the overlap of a text, we have to make sure
# both x and y location. x location will be determined by the bandL
for lvl in self.__list_lvl:
bandN = lvl['bandN']
band = bandN.split("_")
n = len( band )
if( n>1 ):
bandL = int ( band[0] )
bandU = int ( band[1] )
lvl['bandL'] = bandL
lvl['bandU'] = bandU
self.__bandL_list.append( bandL ) # not used, but save it.
else:
lvl['bandL'] = int( bandN )
lvl['bandU'] = int( bandN )
self.__bandL_list.append( int(bandN) ) # not used, but save it.
lvl['textY'] = float(lvl['lvlE']) # assign the initial value.
# to set the text height value
# the formula is from my empirical test
self.__text_height = self.__font_size/1000 * 250 * self.__length/1000 \
+ self.__lvl_split
self.Check_level_overlap()
self.modify_level()
#===================================================
def Get_textY_min_max(self):
""" to get the min/max text y position"""
self.__textY_MAX = 0
self.__textY_MIN = 9999999
for lvl in self.__list_lvl:
if( lvl['textY'] >self.__textY_MAX ): self.__textY_MAX = lvl['textY']
if( lvl['textY'] <self.__textY_MIN ): self.__textY_MIN = lvl['textY']
pass
return self.__textY_MIN, self.__textY_MAX
#print( "TEST: textY max = ", self.__textY_MAX, "textY min = ", self.__textY_MIN )
#print( "TEST: lvlE max = ", self.__list_lvl[-1]['lvlE'], \
# "lvlE min = ", self.__list_lvl[0]['lvlE'] )
def set_text_separate_value(self, text_height):
''' Let user to input text_height.
'''
self.__text_height = text_height
def Check_level_overlap(self ):
""" adding 'overlap' key to each element of list_lvl
and check what the levels are overlapping a given level.
"""
idx = 0
for lvl1 in self.__list_lvl :
textY = float( lvl1['textY'] )
# create a sub list to second loop. here [:] is a new one.
# We exclude the lvl1 in the sub_list, in order to compare
# lvl2 level ( to avoid self-confilct )
sub_list = self.__list_lvl [:]
del sub_list[idx]
idx +=1
# set initial values: no overlap
isOverLap = False
lvl1['olap_lvl'] = []
for lvl2 in sub_list:
# conditions for overalping:
# (1) the y separation of two text label less than text height
# (2) the two text labels are not from the two states with
# exact the same energy. They cannot be separated.
# (3) the states have the same starting band index.
check1 = abs( textY - float( lvl2['textY'] )) <= self.__text_height
check2 = ( lvl1['lvlE'] != lvl2['lvlE'] )
check3 = ( lvl1['bandL'] == lvl2['bandL'] )
checkALL = ( check1 and check2 and check3 )
if ( checkALL ) :
lvl1['overlap'] = 'Yes'
lvl1['olap_lvl'].append( lvl2['textY'] )
isOverLap = True
pass # end of the second for, for sub_list
# after check the elemnts now, we can set the non-overlap situation.
if isOverLap == False:
lvl1['overlap']="none"
lvl1['olap_lvl'] = []
pass # end of the first for, for self.__list_lvl
def isAllgood( self):
""" to check is there is no more overlapped levels. """
for lvl in self.__list_lvl:
if ( lvl['overlap'] == "Yes" ): return False
return True
def modify_level(self):
""" to modify the text spacing. """
go_on = True
step = 0 # for protecting purpose.
while( go_on ):
for i in range( len(self.__list_lvl) ):
lvl = self.__list_lvl[i] # short-handed notation.
#-------------------------------------------------------------------|
# note: if ['olap_lvl'] is empty, then min() will casue ValueError.
#
if len( lvl['olap_lvl'] ) > 0:
#print( 'step=',step,
# 'overlap_lvls =',lvl['olap_lvl'],
# 'lvlE=' , lvl['lvlE'],
# 'textY=', lvl['textY'])
if min( lvl['olap_lvl'] ) > lvl['textY'] :
# this block only applies to the lowest level
# among the of the group of the overlapped states.
# move the lowest text label even lower
lvl['textY'] -= 5
if max( lvl['olap_lvl'] ) < lvl['textY'] :
# this block only applies to the highest level
# among the of the group of the overlapped states.
# move the highest text label even higher
lvl['textY'] += 5
#-------- re-check the overlap situation.
self.Check_level_overlap()
#--------- exit if there no more overlapping.
if ( self.isAllgood( ) ):
go_on = False # to exit the while loop.
break # to break the for loop.
pass # end of "for"
#-------------------------------- for proventing infinite loop.
step += 1
if (step+1) > 5000: go_on = False
#--------------------------------
pass # end of while
class PRE_Parse_gamma_level( object ):
'''This class is used to parse the gamma ray (no plot part yet)
We need both gamma and level list to do the calculations.
variable explanation:
self.__lvl_list is a list, each element is a dic
-- key: 'bandN' (ex. 0_5 ) is for the band setting.
-- key: 'lvlE' (ex. 100) is for the energy of a level.
-- key: 'color' (ex. red) is for the color of a level.
-- key: 'spin' (ex. 3+ ) is for the spin of a level.
-- key: 'xi' and 'xf' are for the x coordinates.
'''
def __init__(self, lvl_list, gam_list, bandL_list, bandU_list, par ):
self.__lvl_list = lvl_list
self.__gam_list = gam_list
self.__bandL_list = bandL_list
self.__bandU_list = bandU_list
self.__par = par
self.__bandspacing = par.bandspacing
# to assign idx and gammaE to our gammas,
# idx will help us to find the gamma easier.
gam_idx = 0
for gam in self.__gam_list:
gam['idx'] = gam_idx
gam['gammaE'] = float( gam['Ei'] ) - float( gam['Ef'] )
gam['crossBand'] = False
gam_idx += 1
self.Parse()
def Parse(self):
''' the main core of parsing. It prepares the xi and xf info '''
lvl_list = self.__lvl_list
gam_list = self.__gam_list
# assign the the E_i and E_f lvl info into each gam
# ex. gam['Ei_xi'] = 1000, gam['Ei_xf'] = 2000, .. for both Ei and Ef.
self.SetEiEf()
# calculate the overlap x range
# ie. calculate which x range of Ei and Ef would overlap for a given gamma ray.
# if Ei level spans (10-20 ) and Ef level(10-30), then the overlap range is (20-30)
overlap_range_list = self.Cal_overlap_range()
if(0):
for temp in overlap_range_list: print( temp )
# initialization for xi and xf of a gamma ray
# xi and xf are very important keys.
for gam in gam_list:
if ( gam['overlap'] ):
#
# if Ei and Ef levels are overlapped, then set the gamma
# at it middle of its overlapped range
# . Note, it is just the initial setting.
#
gam['xi'] = ( gam['overlap_range_xi'] + gam['overlap_range_xf'] )/ 2.0
gam['xf'] = ( gam['overlap_range_xi'] + gam['overlap_range_xf'] )/ 2.0
else:
#
# This part is for the gammas for cross band case.
self.Cal_cross_band( gam )
# to group gammas within the same x overlap range.
# ie. we group the gammas with the same overlap xi-xf range
# into a subgroup
# gam1's Ei and Ef has overlapped range at 20-30
# gam2's Ei and Ef has overlapped range at 20-30
# then gam1 and gam2 are in the same subgroup of the same x range.
subgroups = self.Cal_subgroups( overlap_range_list )
if( 0 ): print ( subgroups )
# now the subgroup look like
# [ [0, 1, 2, 3, 10],
# [4, 5, 6, 7, 8, 9, 11] ]
# the numbers are the gammas' idx, and we have two X groups.
# even within a subgroup of the same xi-xf range,
# some gamma would overlap with the same yi-yf range, but some don't
# so we need grouping in Y (vertical grouping) in addition to
# horizontal grouping. ( this process is rather complicated. )
if(0): print( "-----------test vertical grouping------")
subgroups_new = []
for subgroup in subgroups:
subgroup_temp = self.Cal_sugroupV( subgroup )
for item in subgroup_temp:
subgroups_new.append(item)
subgroups = copy.deepcopy( subgroups_new )
# now the subgroups will look like:
# [[0, 1, 2], [3], [10]] <-- have 3 Y groups, in X group1
# [[4], [5], [6, 7, 8, 9, 11]] <-- have 3 Y groups, in X group2
# to separate lines in a subgroup according to X and Y grouping.
# this method will also update the self.__gam_list
self.Separate_gamma_line( subgroups)
#____________________done with parsing____________________
pass
def Update(self):
return self.__lvl_list, self.__gam_list
pass
def isInRange(self, xia, xfa, xib, xfb):
''' Return true when two lines overlap,
index i, f for two lines,
index a, b for the two ends.
'''
result = True
if( xia >= xfb) or ( xfa <= xib): result = False
return result
def isInRange2(self, xia, xfa, xib, xfb):
''' Return true when two lines overlap,
index i, f for two lines,
index a, b for the two ends.
'''
result = True
if( xia > xfb) or ( xfa < xib): result = False
return result
def isInRangeY(self, yia, yfa, yib, yfb):
''' Return true when two yi-yf interval overlap'''
result = True
if( yia <= yfb ) or ( yfa >= yib ): result = False
return result
def GetBandL_U( self, lvlE ):
""" serach a lvl's bandL and bandU by its lvl E
"""
bandL = -999
bandU = -999
for lvl in self.__lvl_list:
if lvlE == lvl['lvlE']:
bandL = lvl['bandL']
bandU = lvl['bandU']
return bandL, bandU
return bandL, bandU
def Getgam(self, idx):
''' From idx to get corresponding gam dictionary'''
temp = {}
for gam in self.__gam_list:
if gam['idx'] == idx: temp = gam
return temp
def GetEiEf(self, idx):
''' From gamma's idx to get its Ei and Ef '''
Ei =0
Ef =0
for gam in self.__gam_list:
if gam['idx'] == idx:
Ei = gam['Ei']
Ef = gam['Ef']
#note: now Ei and Ef are in Str
return float(Ei), float(Ef)
def SetEiEf(self):
''' ex. gam['Ei_xi'] = 1000, gam['Ei_xf'] = 2000, ..
for both Ei and Ef.
'''
lvl_list = self.__lvl_list
gam_list = self.__gam_list
# assign the the E_i and E_f lvl info into gam
for lvl in lvl_list:
for gam in gam_list:
if lvl['lvlE'] == gam['Ei']:
gam['Ei_xi'] =lvl['xi']
gam['Ei_xf'] =lvl['xf']
if lvl['lvlE'] == gam['Ef']:
gam['Ef_xi'] =lvl['xi']
gam['Ef_xf'] =lvl['xf']
# here, since we pass by reference, and so we also update the
# self.__lvl_list and self.__gam_list, it is equal to
#self.__lvl_list = lvl_list
#self.__gam_list = gam_list
pass
def Cal_overlap_range(self):
'''to cal which x range Ei and Ef would overlap for a given gamma ray.'''
gam_list = self.__gam_list # for short-handed notation.
overlap_range_list = []
# we check the common x range between initial and final state for
# each gamma ray, we also add new keys.
# instead of checking xi-xf range, we should check the
# bandL and bandU
# since the wedge shape will shink the level.
for gam in gam_list:
Ei_bandL, Ei_bandU = self.GetBandL_U( gam['Ei'] )
Ef_bandL, Ef_bandU = self.GetBandL_U( gam['Ef'] )
if not self.isInRange2( Ei_bandL, Ei_bandU, \
Ef_bandL, Ef_bandU ):
gam['overlap'] = False
gam['overlap_range'] = 0
else:
gam['overlap'] = True
########################
overlap_bandL = max( Ei_bandL, Ef_bandL )
overlap_bandU = min( Ei_bandU, Ef_bandU )
gam['overlap_range_xi'] = self.__bandL_list[ overlap_bandL ]
gam['overlap_range_xf'] = self.__bandU_list[ overlap_bandU ]
#######################
temp_tuple = ( gam['idx'], \
gam['overlap_range_xi'], \
gam['overlap_range_xf'])
overlap_range_list.append( temp_tuple )
self.__gam_list = gam_list
return overlap_range_list
pass
def Cal_subgroups(self, overlap_range_list):
''' we group the gammas with the same overlap xi-xf range
into a subgroup'''
subgroups = []
for i in range( len( overlap_range_list ) ):
temp_subgroup = []
idxa = overlap_range_list[i][0]
x_ia = overlap_range_list[i][1]
x_fa = overlap_range_list[i][2]
temp_subgroup.append( idxa )
# test with oter members
for j in range( len( overlap_range_list ) ):
idxb = overlap_range_list[j][0]
x_ib = overlap_range_list[j][1]
x_fb = overlap_range_list[j][2]
# check both two end points i_______f
if i != j and ( x_ia == x_ib ) and (x_fa == x_fb) :
temp_subgroup.append( idxb )
pass
# check just start point i_______ or end point _____f
# if i != j and ( ( x_ia == x_ib ) or ( x_fa == x_fb ) ):
# temp_subgroup.append( idxb )
# pass
# to sort the list.
temp_subgroup.sort( key= lambda x: x ) # f(x) = x
# we only want the cases with 2 or more
if ( len(temp_subgroup) > 1 and temp_subgroup not in subgroups ):
subgroups.append( temp_subgroup )
return subgroups
pass
def Cal_sugroupV(self, subgroupX ):
''' given a subgroupX, further separate them according Y overlapping.'''
Vgroups = []
## testing y overlapping
for g_idxa in subgroupX:
Vgroup=[]
Vgroup.append(g_idxa)
yi_a, yf_a = self.GetEiEf( g_idxa )
overlap_yi_a = yi_a
overlap_yf_a = yf_a
# we want to compare g_idxa with the other members.
for g_idxb in subgroupX:
if(g_idxa != g_idxb):
yi_b ,yf_b = self.GetEiEf(g_idxb )
if self.isInRangeY( overlap_yi_a, overlap_yf_a, yi_b ,yf_b ):
# update the overlapping range:
overlap_yi_a = max( overlap_yi_a, yi_b)
overlap_yf_a = min( overlap_yi_a, yf_b)
Vgroup.append( g_idxb)
pass
pass
pass
# to sort the list according its gammaE
Vgroup.sort( key= lambda x:x )
if( Vgroup not in Vgroups): Vgroups.append( Vgroup )
pass
# sometimes, the order of the gamma ray will fool the overlapping
# detection so I need to check it again.
Vgroups_new=[]
#
# to detect whether we have common items.
#
for list1 in Vgroups:
for list2 in Vgroups:
having_same_item = False
# the follwoing is just to compare each items
if ( list1 != list2 ):
for item1 in list1:
for item2 in list2:
if ( item1 == item2 ):
having_same_item = True
# after comparing, we put the common item into list1
if ( having_same_item ):
for item2 in list2:
if (item2 not in list1 ):
list1.append( item2 )
list1.sort( key= lambda x:x )
if( list1 not in Vgroups_new): Vgroups_new.append( list1 )
# now the vertical grouping is good.
return Vgroups_new
def Separate_gamma_line(self, subgroups):
''' accoring to X and Y grouping, we separate the lines equally.
Note: initially, we just put the line in the middle of
overlapped range of X. That will cause several line overlap
each other.'''
for subgroup in subgroups:
if(0):print("============================")
if(0):print( "TEST subgroupV = ", subgroup)
gamma_to_adust_list = []
# a temp container for the idx
# for the gammas in a subgroupV we want to adjust
#
# we only need to adjust when we have two more gammas
# in a subgroupV
if ( len(subgroup) > 1 ):
for idx in subgroup:
# retrieve the gam{} dictionary, from self.__gam_list
gam_temp = self.Getgam( idx )
gamma_to_adust_list.append( gam_temp )
pass
# to sort the gamma list according its Ei
gamma_to_adust_list.sort( key= lambda x:x['Ei'], reverse=True )
# calculations for saving space
# we will update 'gamma_to_adust_list'
# it is a subgroup of list_gam
N_of_sections, gamma_to_adust_list = \
self.sort_gamma_to_save_space(gamma_to_adust_list)
#
# preparation for the adjustment
#
x_start = float( gamma_to_adust_list[0]['overlap_range_xi'] )
x_end = float( gamma_to_adust_list[0]['overlap_range_xf'] )
interval = (x_end - x_start) / N_of_sections
times = -1
# we use the element in 'gamma_to_adjust_list'
# to help us assign the xi and xf,
# in this way, we would not add the keys that are only used for the
# calculations into the gam dictionary.
#
for item in gamma_to_adust_list:
idx = item['idx']
gam = self.Getgam(idx)
gam['xi'] = \
gam['xf'] = x_start + interval*( item['section']+1 ) \
+ self.__par.minorShift * item['shift']
if(0 ):
print( "section = ", item['section'], "gamE =", gam['gammaE'] )
pass # end of Separate_gamma_line()
###########################################################################
# @detail
# this function will update the gam (which is a dictionary)in the
# "gamma_to_adjust" list, in which all the gammas are in the same
# X-subgroup and Y-subroup.
#
# we add two new keys 'section' and 'shift' to the gam.
# These two keys are used to separated the gammas.
# 'section' is for the major poisition.
# 'shift' is a minor adjustment.
#
#
def sort_gamma_to_save_space(self, gamma_to_adjust):
"""
to sort and group the gam objects to save space.
we add 'section' key for the major poisition mark.
"""
if(0):
for gam in gamma_to_adjust:
print( "Ei = %s, gammaE =%s " %( gam['Ei'], gam['gammaE']) )
#
# to get the overlap y range for each subgroupV
#
y_high = -1
y_low = 9999999
for gamma in gamma_to_adjust:
if( float(gamma['Ei']) > y_high ): y_high = float( gamma['Ei'] )
if( float(gamma['Ef']) < y_low ): y_low = float( gamma['Ef'] )
section = 0
doneList = []
gamma_to_adjust_new = copy.deepcopy( gamma_to_adjust )
while( len(gamma_to_adjust_new) >= 1 ):
# get the gamma from the highest level which has the largest energy.
# since it is the longest line, we should do its arrangement first.
gamHL = self.__get_longest_highest_gam(gamma_to_adjust_new)
if(0): print("============================================")
if(0): print( "gammaE = %4.f for gamHL" %gamHL['gammaE'] )
# remove the gamma in the original list, and put it into 'done' list.
gamma_to_adjust_new.remove( gamHL )
doneList.append( gamHL )
gamHL['section'] = section
# we could place other gammas under the gamma we just placed
# only if the other gammas are lower.
# we will update 'space' once we put a gam1 under the gamHL,
# and then we can search other possible gam2 ot put underneath.
space = float( gamHL['Ef'] )
while( space > 0 ):
# if gamHL directly decay to the ground state, definitely,
# we don't have enough space to put other gammas.
# use 'tmplist' to store possible gammas which are below the gamHL
tmplist = []
for gam in gamma_to_adjust_new:
if(0):print( "space = %.f gam Ei = %s" %(space, gam['Ei'] ) )
if( float( gam['Ei'] ) <= space):
tmplist.append( gam )
# to get the shortest gamma among all possible gammas in 'tmplist'
# shortest_gam will use to point to the gam obj ( it is a dictionary)
shortest_gamE = 999999
shortest_gam = {}
if( len(tmplist) > 0 ):
for gam in tmplist:
if ( float(gam['gammaE']) < shortest_gamE ):
shortest_gam = gam
shortest_gamE = gam['gammaE']
else:
# no suitable gammas
break
#
# to update space.
# for example, if we have 5000->2000 (so initally, we have space = 2000)
# and we get 2000->1800 as our shortest gam below 2000 level,
# we then update space to 1800.
#
if( len(tmplist) > 0 ):
shortest_gam['section'] = section
space = float( shortest_gam['Ef'] ) # update the space.
# put the shortest one to the 'done' list.
gamma_to_adjust_new.remove( shortest_gam )
doneList.append( shortest_gam )
if(0): print( "gammaE = %4.f for shortest_gam" %shortest_gam['gammaE'] )
pass #----- end of inner while loop.
section += 1
pass #------ end of outter while loop.
sectionN = section + 1 #total number of sections to make division.
#
# 'shift' is for minor adjusment for the gams with the same
# 'section' value. the gams in the 'done' list has an ascendant 'section' value.
# for a given section, when we just have 1 gam, then shift = 0.
# if we have 3, 5, 7 ... gams in a given section, then the shift will be -2, -1, 0, 1, 2
# if we have 2, 4, 6 ... gams in a given section, then the shift will be -2, -1, 1, 2
# check the number of gam in a given section
# we use a list 'num' to record the num of gams
# the index refer to the 'section' value.
#
num = [ 0 for _ in range(section ) ]
for gam in doneList:
num [ gam['section'] ] += 1
tmp_idx = 0
for gam in doneList:
gam['shift'] = 0 # initialization
# to skip just 1 gam case for a given 'section'
if( num[ gam['section'] ]== 1 ): continue
# when the idx < total number of gam in a given 'section'
if( ( tmp_idx + 1 ) <= num[ gam['section'] ] ):
gam['shift'] = self.Cal_shift( tmp_idx, num[ gam['section'] ] )
tmp_idx += 1
else:
# we done a 'section', then reset idx to 0.
tmp_idx = 0
if(0):
for gam in doneList:
print( "Ei = %s gammaE = %s, sect = %d, shift = %.2f" \
%(gam['Ei'], gam['gammaE'], gam['section'], gam['shift'] ) )
return sectionN, doneList
def __get_longest_highest_gam(self, gamList):
highest_Ei = -1
longest_gamE = -1
gammHi_longest = {}
tempList = []
# to get the highest Ei
# and append all the gammas from the highest Ei to a temp list
for gam in gamList:
if( float( gam['Ei'] ) > highest_Ei):
highest_Ei = float( gam['Ei'] )
for gam in gamList:
if( float( gam['Ei'] ) == highest_Ei):
tempList.append( gam)
# when we have multiple gamma from the highest level
# we select the longest gamma
if( len(tempList) > 1):
for gam in tempList:
if( float( gam['gammaE'] ) > longest_gamE ):
longest_gamE = float( gam['gammaE'] )
gammHi_longest = gam
else:
longest_gamE = float( tempList[0]['gammaE'] )
gammHi_longest = tempList[0]
del tempList
return gammHi_longest
def Cal_cross_band( self, gam ):
gam['crossBand'] = True
bandsize = self.__par.bandwidth + 2*self.__par.bandspacing
cross_limit = self.__par.auxBandLimit * bandsize
# it looks like
# /=======
# /
# ====== V
# if the gamma's crossing is too long, we will have a dashed line.
# note: I may need some corrections for the x position,
# since arrow will need to shift the x as well.. not only y
if( gam['Ei_xi'] > gam['Ef_xf'] ):
if( (gam['Ei_xi']- gam['Ef_xf'] ) > cross_limit ):
# long crossing
gam['xi'] = gam['Ei_xi']
gam['xf'] = gam['Ei_xi'] - self.__bandspacing * 2
else:
# nearby crossing
gam['xi'] = gam['Ei_xi']
gam['xf'] = gam['Ef_xf']
# It looks like:
# ========\
# \
# V ========
# if the gamma's crossing is too long, we will have a dashed line.
if( gam['Ei_xf'] < gam['Ef_xi'] ):
if( ( gam['Ef_xi'] -gam['Ei_xf'] ) > cross_limit ):
# long crossing
gam['xi'] = gam['Ei_xf']
gam['xf'] = gam['Ei_xf'] + self.__bandspacing * 2
else:
# nearby crossing
gam['xi'] = gam['Ei_xf']
gam['xf'] = gam['Ef_xi']
#====================================================================
#
# to calculate the minor adjustment 'shift'
# gamN = the n-th gam in a given 'section'
# gamNtotal = total gam number in a given 'section'
def Cal_shift(self, gamN, gamNtotal):
# just to make sure gamN is the positive number.
gamNtotal = int( abs(gamNtotal) )
if( gamNtotal == 1 or gamNtotal == 0):
return 0
elif( gamN < 0 ):
return 0
elif( gamNtotal%2 == 0 ):
# for even
# gamN = 0, 1, 2, 3
# -1.5, -0.5, 0.5, 1.5
return gamN-(gamNtotal/2) + 0.5
else:
# for odd
# gamN = 0, 1, 2, 3, 4
# -2, -1, 0, 1, 2
return gamN-(gamNtotal/2)
pass
class Gamma(object):
def __init__(self, gam, lvl_list, dim ):
self.__gam = gam
self.__lvl_list = lvl_list
self.__dim = dim
self.__angle = 0 # label's angle for cross-band gam
self.__Ei = "0"
self.__Ef = "0"
self.__xi = 0
self.__xf = 0
self.__I = 10
self.__color = "black"
self.__label = ""
self.__linestyle = "1"
self.__arrow = 1
# gam is a dic, here we make sure the keys are really in it.
if 'Ei' in gam: self.__Ei = gam['Ei']
if 'Ef' in gam: self.__Ef = gam['Ef']
if 'xi' in gam: self.__xi = gam['xi']
if 'xf' in gam: self.__xf = gam['xf']
if 'I' in gam: self.__I = gam['I']
if 'linewidth' in gam: self.__I = float( gam['linewidth'] )
if 'label' in gam: self.__label = gam['label']
if 'color' in gam: self.__color = gam['color']
if 'linestyle' in gam: self.__linestyle = gam['linestyle']
if 'arrow' in gam: self.__arrow = int( gam['arrow'] )
pass
def Parse_color( self ):
color = self.__color
result =""
if color.lower() == 'black': result = 'color 1'
elif color.lower() == 'red': result = 'color 2'
elif color.lower() == 'green': result = 'color 3'
elif color.lower() == 'blue': result = 'color 4'
elif color.lower() == 'yellow':result = 'color 5'
elif color.lower() == 'brown': result = 'color 6'
elif color.lower() == 'grey': result = 'color 7'
elif color.lower() == 'violet':result = 'color 8'
elif color.lower() == 'cyan': result = 'color 9'
elif color.lower() == 'magenta': result = 'color 10'
elif color.lower() == 'orange': result = 'color 11'
elif color.lower() == 'indigo': result = 'color 12'
elif color.lower() == 'maroon': result = 'color 13'
elif color.lower() == 'turquoise': result = 'color 14'
elif color.lower() == 'grey4': result = 'color 15'
else: result = 'color 1'
self.__color = result
def Process(self):
self.Parse_color()
outStr = ""
outStr += self.Get_gamma_line()
outStr += self.Get_gamma_label()
return outStr
pass