"""Matched linewidth profiles, selection criteria and absolute lifetimes."""
import numpy as np
from plate import Plate, INK, MUTED
from make_figures import series, result, numeric, ACC, MARK, NAMES, salts
from structures import COL

p = Plate(100)
p.panel(1,3,'a','Iodine and chlorine exchange rank')
ax=p.axes(15,14,68,64)
ax.axhline(1.2,color='#8792a9',ls=(0,(1.6,2.3)),lw=.7,zorder=0)
ax.text(29.9,1.228,'20% longer',fontsize=7.0,color=MUTED,ha='right')
curves={k:(ACC[k],MARK[k]) for k in ['SEED','R3_F','R4_pCl','R5_pI']}
curves['R4_pBr']=(COL['Br'],'v')
for k,(colour,marker) in curves.items():
    r=series(k)
    x=numeric(r,'sigma_cm1')
    y=numeric(r,'T1_over_seed')
    ax.plot(x,y,c=colour,marker=marker,lw=.85 if k=='SEED' else 1.15,
            ls=(0,(3,2)) if k=='SEED' else '-',ms=3.9,mec='white',mew=.3,label=NAMES[k])
    ax.annotate(NAMES[k],(x[-1],y[-1]),xytext=(3.5,0),textcoords='offset points',
                color=colour,va='center',fontsize=7.65,annotation_clip=False)
ax.set(xlim=(1.7,30.3),ylim=(.40,1.61),xticks=[2,5,10,15,20,30],
       yticks=[.5,.75,1,1.25,1.5],yticklabels=['0.50×','0.75×','1.00×','1.25×','1.50×'],
       xlabel='Vibrational linewidth, σ (cm⁻¹)',ylabel='Spin lifetime relative to parent')

p.panel(99,3,'b','Lifetime gains at narrow linewidths')
rows=[('R3_F',29),('R5_pI',36)]
for k,y in rows:
    p.line(103.2,y,103.2,y,marker=MARK[k],ms=4.9,mfc=ACC[k],mec='none')
    p.text(106,y,NAMES[k],fontsize=7.75,color=ACC[k],weight='bold')
p.line(102,24.3,177,24.3,color='#d5d9e5',lw=.55)
p.line(102,40.1,177,40.1,color='#d5d9e5',lw=.55)
for x, label, aggregate in [(138.2,'Minimum',min),(165.4,'Maximum',max)]:
    p.text(x,16.3,label,fontsize=7.7,ha='center',weight='bold')
    p.text(x,21,'3, 5, 7 cm⁻¹',ha='center',fontsize=7.1,color=MUTED)
    for k,y in rows:
        ratio=aggregate(result(k,w) for w in (3,5,7))
        p.text(x,y,f'{100*(ratio-1):+.0f}%',ha='center',fontsize=8.3,color=ACC[k])

p.panel(99,54,'c','Predicted spin lifetimes at narrow linewidths')
bars=p.axes(117,68,57,20)
for j,k in enumerate(['SEED','R3_F','R5_pI']):
    x=np.arange(3)+(j-1)*.235
    bars.bar(x,[result(k,v,'T1_us') for v in [3,5,7]],width=.21,color=ACC[k],label=NAMES[k])
bars.set(xlim=(-.55,2.55),ylim=(0,4.22),yticks=[0,2,4],xticks=range(3),xticklabels=['3','5','7'],
         xlabel='Linewidth (cm⁻¹)',ylabel='Spin lifetime (µs)')
bars.legend(loc='lower center',bbox_to_anchor=(.5,1.01),ncol=3,columnspacing=.85,
            handlelength=1.05,handletextpad=.4,borderaxespad=0,fontsize=7.3)
p.save('05_linewidth_and_candidates')
