# -*- coding: utf-8 -*-
"""
Created on Fri Dec 16 01:15:24 2022

@author: wicki
"""

# -*- coding: utf-8 -*-
"""
Created on Fri Oct 21 15:46:32 2022

@author: wicki
"""

tempscale=1 # K
#tempscale=10 # C

tempregion =1.0 # all
#tempregion =10.0 # low
#tempregion =100.0 # mid
#tempregion =1000.0 # high
# -*- coding: utf-8 -*-

### CHECK TOP AXIS HASN'T MAXED OUT!
###PLT gives thr right values!
import numpy as np
import matplotlib.pyplot as plt
from math import exp, ceil, sqrt
import matplotlib.patches as patches
fs=14

data2=np.genfromtxt('GaussianIRSE_split.csv', dtype=float, delimiter=',', names=['t1','tc1','gIR','gSE'])
t1=data2['t1']
tc1=data2['tc1']
gIR=data2['gIR']
gSE=data2['gSE']



data1=np.genfromtxt('M2M4_SE_split.csv', dtype=float, delimiter=',', names=['f1','f2','f3','f4','f5','f6','f7','f8'])  

f2=data1['f2']
f7=data1['f7']
f8=data1['f8']



if tempregion < 5:
    plt.figure(figsize=(5.5,3.7))
else:
    plt.figure(figsize=(5.5/2,3.7/1.8))
    

pp=-85.5
qq=678

ax1 = plt.gca()
if tempregion >500:
    ax1.set_title('High T', fontsize = fs)
elif tempregion > 50:
    ax1.set_title('Mid T', fontsize = fs)
elif tempregion > 5:
    ax1.set_title('Low T', fontsize = fs)

#plt.scatter(pp,qq)
K=273.15
if tempregion < 5:
    df = 1
else:
    df=0.5
if tempscale <5:   
    #plt.scatter(tc-df+K,s98, color='r', marker = "+", linewidth = 1.3,label='Integration (IR)' )
    #ax1.errorbar(tc-df+K,s98,e, alpha = 0.5,color='r', capsize = 3 ,linestyle="None")
    plt.scatter(tc1+K,gSE, color='k', marker = "+" , label='Gaussian, spec',  alpha = 1)
    plt.scatter(f2+df+K,f7, color='r', marker = "+" , label='M2M4')
    ax1.errorbar(f2+df+K,f7,f8, alpha = 0.5,color='r', capsize = 3 ,linestyle="None")  
else:
    if tempregion < 5:
        plt.scatter(tc1,gSE, color='k', marker = "o" , label='Gaussian (SE) ', s=10, alpha = 0.8)
    else:
        plt.scatter(tc1,gSE, color='k', marker = "o" , label='Gaussian (SE)', s=20, alpha = 0.8) 
    #plt.scatter(tc-df,s98, color='r', marker = "+", linewidth = 1.3,label='Integration (IR)' )
    #ax1.errorbar(tc-df,s98,e, alpha = 0.5,color='r', capsize = 3 ,linestyle="None")
    plt.scatter(f2+df,f7, color='b', marker = "+" , label='M2M4 (SE)')
    ax1.errorbar(f2+df,f7,f8, alpha = 0.5,color='b', capsize = 3 ,linestyle="None")

#plt.scatter(t0,a0, color='b', marker = "+", linewidth = 1.3,label='M2')
#ax1.errorbar(t0,a0,ae, alpha = 0.5,color='b', capsize = 3 ,linestyle="None")



#plt.scatter(tc,s99, color='r', marker = "+", linewidth = 1.3,label='$M_2$ (99%)' )
#plt.scatter(tc,s995, color='g', marker = "+", linewidth = 1.3,label='$M_2$ (99.5%)' )
#plt.scatter(tc,s999, color='k', marker = "+", linewidth = 1.3,label='$M_2$ (99.9%)' )

#plt.scatter(tc-df,s98, color='r', marker = "+", linewidth = 1.3,label='Integration (98%) \n err = (99% - 97%) / 2' )
#ax1.errorbar(tc-df,s98,e, alpha = 0.5,color='r', capsize = 3 ,linestyle="None")
#plt.scatter(f2+df,f7, color='b', marker = "+" , label='M2M4 ')
#ax1.errorbar(f2+df,f7,f8, alpha = 0.5,color='b', capsize = 3 ,linestyle="None")


xxx1=np.array([-70])#,-70, -60])
yyy1=np.array([13945])#,20505, 16900])
xxx2=-70
yyy2=20505
#plt.plot(xxx1,yyy1/18.128, 'bo')
#plt.plot(xxx2,yyy2/18.128, 'bo')
#ax1.errorbar(xxx1,yyy1/18.4,1050/18.4, alpha = 0.5,color='r', capsize = 3 ,linestyle="None")
#ax1.errorbar(xxx2,yyy2/18.4,857/18.4, alpha = 0.5,color='r', capsize = 3 ,linestyle="None")
#ax1.scatter(tc,a, color='b', marker = "+", linewidth = 1.3,label='deuterium T1')
#ax1.errorbar(tc,a,b, alpha = 0.5,color='b', capsize = 3 ,linestyle="None")

#plt.scatter(Xc,d, color='b', marker = "+", linewidth = 1.3,label='deuterium T1')

#ax1.scatter(tc1,a1, color='r', marker = "+", linewidth = 1.3,label='deuterium T1')
#ax1.errorbar(tc1,a1,b1, alpha = 0.5,color='r', capsize = 3 ,linestyle="None")
#ax1.scatter(tc2,a2, color='r', marker = "+", linewidth = 1.3,label='deuterium T1')
#ax1.errorbar(tc2,a2,b2, alpha = 0.5,color='r', capsize = 3 ,linestyle="None")
#plt.legend(frameon=False, prop={'size':14})


#ax1.scatter(Xcq+0.05,bq, color='b', marker = "+", linewidth = 1.3,label='deuterium T1')
if tempregion < 5:
    ax1.set_xlabel('10$^3$ K / $T$', fontsize=fs)
    ax1.set_ylabel('M2 / kHz2',  fontsize=fs)
    if tempscale <  5:
        ax1.set_xlabel('T / K', fontsize=fs)  
    else:
        ax1.set_xlabel('T / $^\circ$C', fontsize=fs) 
else:
    ax1.set_xlabel('10$^3$ K / $T$', fontsize=fs-2)
    ax1.set_ylabel('M2 / kHz2',  fontsize=fs-2)
    if tempscale <  5:
        ax1.set_xlabel('T / K', fontsize=fs-2)  
    else:
        ax1.set_xlabel('T / $^\circ$C', fontsize=fs-2) 


#fig, ax = plt.subplots()
if tempregion < 5:
    plt.legend(frameon = False, fontsize=fs,handletextpad=0)
    ax1.tick_params(labelsize = fs)
else:
    ax1.tick_params(labelsize = fs-2)
#plt.legend(frameon = False, fontsize=fs,handletextpad=0)
#ax1.invert_yaxis()

#ax1.set_ylim(top=30000)
#ax1.set_ylim(bottom=0.006)

  # adjust the top leaving bottom unchanged
#ylim(bottom=1)  # adjust the bottom leaving top unchanged
#txlim=-100  
#txlim2=30
#txlimc=1000/(273.15+txlim)
#txlimc2=1000/(273.15+txlim2)
#plt.xlim(5.78,3.3)
re=5.2
#ax1.set_xlim(-18)
#ax1.set_xlim()


#ax1.set_yticks([ 0.01, 0.1, 1.0,10.0,100.0,1000.0,10000.0])
#ax1.set_yticklabels(['0.05',  '0.1', '0.2', '0.4'])
#ax1.grid(b=True, which='major', color='black', linestyle='-')
#'bh;;nplt.grid(b=True, which='major', color='blue', linestyle='-')         
#plt.axvline(x=1000.0/(273-29),color='k')
#plt.axvline(x=1000.0/(273-21),color='k')
#plt.axvline(x=1000.0/(273-39),color='k')
#plt.axvline(x=4.2735
ypp=-80.0
yppc=1000/(273.15+ypp)
wpp=40.0
wppc=1000/(273.15+wpp)
#ax1.plot([3.5,5.5],[0.01,0.01], 'red') #correct
#plt.plot([3.5,5.5],[0.05,0.05], 'blue')
#ax2.plot([3.5,5.5],[0.03,0.03], 'green')

#ax1.plot([5.5,5.5],[0.1,1000], 'red')
#plt.plot([5.4,5.4],[0.1,1000], 'blue')
#ax2.plot([5.3,5.3],[0.1,1000], 'green')

#ax1.plot([yppc,wppc],[1000,1000], 'red')#incorrect
#plt.plot([yppc,wppc],[3000,3000], 'blue')
#ax2.plot([yppc,wppc],[2000,2000], 'green')





#x,y = 4.0,80

if tempscale <5:
    if tempregion   < 5:
        ax1.set_xlim(-100+K,210+K)
        ax1.set_ylim(-50,750)
    elif tempregion   < 50:
        ax1.set_xlim(-95+K,17+K)
        ax1.set_ylim(440,820)
    elif tempregion   < 500:
        ax1.set_xlim(79+K,150+K)
        ax1.set_ylim(85,162)
    else:
        ax1.set_xlim(170+K,201+K)
        ax1.set_ylim(-1,8)
else:  
    if tempregion   < 5:
        ax1.set_xlim(-100,210)
        ax1.set_ylim(-50,850)
    elif tempregion   < 50:
        ax1.set_xlim(-95,17)
        ax1.set_ylim(440,820)
    elif tempregion   < 500:
        ax1.set_xlim(79,150)
        ax1.set_ylim(85,162)
    else:
        ax1.set_xlim(170,201)
        ax1.set_ylim(-1,8)
#ax1.set_ylim(-100,1050)
#ax1.set_xlim(-100,210)
#ax1.set_ylim(440,820)
#ax1.set_xlim(-95,10)
#ax1.set_ylim(50,1050/5)
#ax1.set_xlim(60,160)
#ax1.set_ylim(-5,10)
#ax1.set_xlim(160,210)

# use the axis scale tform to figure out how far to translate 
#circ_offset = ScaledTranslation(x,y,ax1.transScale)

# construct the composite tform
#circ_tform = circ_offset + ax1.transLimits + ax1.transAxes

# create the circle centred on the origin, apply the composite tform
#circ = plt.Circle((4.1,8.0),x,color='k',alpha=0.5,transform=circ_tform)
#ax1.add_artist(circ)

#ax1.axvline(x=273, color = 'k', linestyle = '--', alpha = 0.9, linewidth = 1,zorder=1)
#ax1.axvline(x=303, color = 'k', linestyle = '--', alpha = 0.9, linewidth = 1,zorder=1)
#ax1.axvline(x=155+273, color = 'k', linestyle = '--', alpha = 0.9, linewidth = 1,zorder=1)

#plt.plot([155+273, 155+273], [-100, 440], color='k', linestyle='--', linewidth=1)
#plt.plot([20.5+273, 20.5+273], [-100, 900], color='k', linestyle='--', linewidth=1)
#ax1.yaxis.grid()
#plt.grid(b=True, which='major', color='b', linestyle='-')
#plt.grid(b=True, which='minor', color='r', linestyle='--')
#plt.show()
#ax2.invert_xaxis()
ax1.tick_params('y')
tp = [  -80.0, -60.01, -40.1, -20.2, 0., 20., 40.0, ]
ctp = [(1000/(i+273.15)) for i in tp]
new_tick_positions = np.array(ctp)
#new_tick_positions = np.array((3.9526, 3.663, 3.412, 3.1948, 3.003, 2.832, 2.6809, 2.544, 2.421))

if tempscale < 5:
    #plt.plot([-81.35+K, 6.31+K], [610.4, 610.4], color='k', linestyle='-', linewidth=2)
    #plt.plot([86.665+K, 145.105+K], [108.7, 108.7], color='k', linestyle='-', linewidth=2)
    plt.plot([174.325+K, 196.24+K], [0.814, 0.814], color='k', linestyle='-', linewidth=4, alpha=0.3)
    plt.plot([-150+K, 550+K], [0, 0], color='k', linestyle='-', linewidth=1)
    rect1 = patches.Rectangle((-81.35+K, 582), 87.66, 51.1, linewidth=1, facecolor='k', alpha=0.3)
    rect2 = patches.Rectangle((86.665+K, 101.4844), 58.44, 16.79, linewidth=1, facecolor='k', alpha=0.3)
    rect3 = patches.Rectangle((174.325+K, 0.5927), 21.915,0.4442 , linewidth=1, facecolor='k', alpha=0.3)
    
else:
    plt.plot([-81.35, 6.31], [610.4, 610.4], color='k', linestyle='-', linewidth=2)
    plt.plot([86.665, 145.105], [108.7, 108.7], color='k', linestyle='-', linewidth=2)
    plt.plot([174.325, 196.24], [0.814, 0.814], color='k', linestyle='-', linewidth=2)
    plt.plot([-150, 550], [0, 0], color='k', linestyle='-', linewidth=1)
    rect1 = patches.Rectangle((-81.35, 589.3), 87.66, 42.2, linewidth=1, facecolor='k', alpha=0.2)
    rect2 = patches.Rectangle((86.665, 102.89), 58.44, 11.62, linewidth=1, facecolor='k', alpha=0.2)
    rect3 = patches.Rectangle((174.325, 0.632), 21.915,0.3627 , linewidth=1, facecolor='k', alpha=0.2)


#ax1.add_patch(rect1)
ax1.add_patch(rect2)
ax1.add_patch(rect3)
if tempscale < 5:
    if tempregion   < 5:
        plt.savefig('m2vssigma2_K_all_50_s.svg', bbox_inches='tight')
    elif tempregion   < 50:
        plt.savefig('m2vssigma2_K_low_50_S.svg', bbox_inches='tight')
    elif tempregion   < 500:
        plt.savefig('m2vssigma2_K_mid_50_S.svg', bbox_inches='tight')
    else:
        plt.savefig('m2vssigma2_K_high_50_S.svg', bbox_inches='tight')
else:    
    if tempregion   < 5:
        plt.savefig('m2vssigma2_C_all_40.svg', bbox_inches='tight')
    elif tempregion   < 50:
        plt.savefig('m2vssigma2_C_low_40.svg', bbox_inches='tight')
    elif tempregion   < 500:
        plt.savefig('m2vssigma2_C_mid_40.svg', bbox_inches='tight')
    else:
        plt.savefig('m2vssigma2_C_high_40.svg', bbox_inches='tight')        

plt.show()
 #or

