# -*- coding: utf-8 -*-
"""
Created on Wed Oct 26 19:09:14 2022

@author: wicki
"""

# -*- coding: utf-8 -*-
"""
Created on Tue May 11 14:24:35 2021

@author: wicki
"""


tempscale=1 # K
#tempscale=10 # C
# -*- 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

K=273.15


data0=np.genfromtxt('diamantane_sigma.csv', dtype=float, delimiter=',', names=['t0','s0','a0']) 
t0=data0['t0']
s0=data0['s0']
a0=data0['a0']



plt.figure(figsize=(5.5,3.7))
ax1 = plt.gca()

fs=14
#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(t0,b0*22, color='r', marker = "+", linewidth = 1.3,label='scaled $M_2$' )
#ax1.errorbar(t0,b0*22,be*22, alpha = 0.5,color='r', capsize = 3 ,linestyle="None")
if tempscale < 5:
    plt.scatter(s0+K,a0**2, color='k', marker = "+", linewidth = 1.3,label=' $\sigma^2 $ ')
else:#=
    plt.scatter(s0,a0**2, color='k', marker = "+", linewidth = 1.3,label=' $\sigma^2 $ ')

#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')
#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) 

#ax1.tick_params(labelsize = fs)
#fig, ax = plt.subplots()
#plt.legend(loc='upper right',frameon = False)

#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_ylim(-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')


temp1=-31.0
temp2=-51.0
J1=0.7
J2=0.3
s1=1000/(273.15+temp1)
s2=1000/(273.15+temp2)
s = [s1, s1, s2, s2]
d = [J1, J2, J2, J1]

#plt.fill(s, d, alpha = 0.1, color = 'k')


temp3=-80.0 #CH2 13C lineshape
temp4=-90.0
J3=0.2  #200kHz
J4=0.01 #10 kHZ
s3=1000/(273.15+temp3)
s4=1000/(273.15+temp4)
s = [s3, s3, s4, s4]
d = [J3, J4, J4, J3]

#plt.fill(s, d, alpha = 0.1, color = 'b')

x=1000.0/(273-85)
y=0.04
#ax1.plot(x,y,marker='o',ms=x*10, color='g',mec='None', alpha=0.5)
'''
x1=1000/(273-40.0)
y1=0.5
plt.scatter(x1,y1)

x1=1000/(273-40.0)
y1=1
plt.scatter(x1,y1)
x1=1000/(273-40.0)
y1=0.2
plt.scatter(x1,y1)
'''
from matplotlib.transforms import ScaledTranslation

#ax = plt.axes(xscale='log')
#x,y = 4.0,80
if tempscale < 5:
    ax1.set_xlim(-70+K,240+K)
else:
   ax1.set_xlim(-70,240) 
ax1.set_ylim(-5,119)
#ax.set_xlim(1E-11,1E11)

# 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.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))
ax1.tick_params(labelsize = fs)
if tempscale < 5: 
    #plt.plot([-52.13+K, 64.75+K], [92.43, 92.43], color='k', linestyle='-', linewidth=2)
    #plt.plot([108.58+K, 137.8+K], [56.61, 56.61], color='k', linestyle='-', linewidth=2)
    plt.plot([196.24+K, 225.45+K], [0.2673, 0.2673], color='k', linestyle='-', linewidth=2, alpha=0.2)
    plt.plot([-150, 550], [0, 0], color='k', linestyle='-', linewidth=1)
    rect1 = patches.Rectangle((-52.13+K, 88.3), 116.88, 8.8, linewidth=1, facecolor='k', alpha=0.2)
    rect2 = patches.Rectangle((108.58+K, 55.525), 29.22, 2.1676, linewidth=1, facecolor='k', alpha=0.2)
    rect3 = patches.Rectangle((196.24+K, 0.1605), 29.22,0.417 , linewidth=1, facecolor='k', alpha=0.2)
else:
    plt.plot([-52.13, 64.75], [92.43, 92.43], color='k', linestyle='-', linewidth=2)
    plt.plot([108.58, 137.8], [56.61, 56.61], color='k', linestyle='-', linewidth=2)
    plt.plot([196.24, 225.45], [0.2673, 0.2673], color='k', linestyle='-', linewidth=2)
    plt.plot([-150, 250], [0, 0], color='k', linestyle='-', linewidth=1)
    rect1 = patches.Rectangle((-52.13, 89.508), 116.88, 5.853, linewidth=1, facecolor='k', alpha=0.4)
    rect2 = patches.Rectangle((108.58, 55.525), 29.22, 2.1676, linewidth=1, facecolor='k', alpha=0.4)
    rect3 = patches.Rectangle((196.24, 0.1007), 29.22,0.2013 , linewidth=1, facecolor='k', alpha=0.4)

ax1.axvline(x=1000/2.393, color = 'k', linestyle = '--', alpha = 0.9, linewidth = 1,zorder=1)
ax1.axvline(x=1000/2.22, color = 'k', linestyle = '--', alpha = 0.9, linewidth = 1,zorder=1)
#plt.plot([169.5, 169.5], [-10, 110], color='k', linestyle='-', linewidth=2)
#plt.plot([136, 136], [-10, 110], color='k', linestyle='-', linewidth=2)
#plt.plot([77, 77], [-10, 110], color='k', linestyle='-', linewidth=2)

ax1.add_patch(rect1)
ax1.add_patch(rect2)
ax1.add_patch(rect3)

yyy=105
plt.text(1000/4.5,yyy, 'Phase', color = 'k', fontsize=fs)
plt.text(1000/3.2,yyy, 'III',color = 'k', fontsize=fs)
plt.text(1000/2.32,yyy, 'II', color = 'k',fontsize=fs)
plt.text(1000/2.05,yyy, 'I',color = 'k', fontsize=fs)

plt.savefig('Di_M2_temp.svg', bbox_inches='tight')
plt.show()
 #or

