import matplotlib.pyplot as plt
import numpy as np

# global settings
ec=(0.35,0.35,0.35)
fc=(0.45,0.45,0.45)
xlim=(-100,300)
ylim=(-0.2,1.5)
hl=0.02*(xlim[1]-xlim[0])
hw=0.027*(ylim[1]-ylim[0])
dx_cut=4
dy_cut=0.02
w_cut=5 
dy=0.1
fs=12
fs_tax=10
fs_small=8
fw='bold'
oh=hl/30

# prepare figure
plt.figure(figsize=(10,5),dpi=300)
plt.xlim(xlim)
plt.ylim(ylim)
plt.axis(False)
plt.box(False)

# add labels
plt.text(xlim[0],0.3,'DECK',fontsize=15,horizontalalignment='left',rotation='vertical')
plt.text(xlim[0],1.0,'DCPP',fontsize=15,horizontalalignment='left',rotation='vertical')

# add time axis
yoff=-0.1
plt.arrow(-50,yoff,315,0,width=0.005,head_width=hw,head_length=hl,length_includes_head=True,ec='k',fc='k',overhang=oh)
#
x_cut=-27.5
plt.plot((x_cut-dx_cut,x_cut+dx_cut),(yoff+dy_cut,yoff-dy_cut),linewidth=3,c=(1,1,1))
plt.plot((x_cut-0.5*dx_cut,x_cut+1.5*dx_cut),(yoff+dy_cut,yoff-dy_cut),linewidth=1,c='k')
plt.plot((x_cut-1.5*dx_cut,x_cut+0.5*dx_cut),(yoff+dy_cut,yoff-dy_cut),linewidth=1,c='k')
x_cut=227.5
plt.plot((x_cut-dx_cut,x_cut+dx_cut),(yoff+dy_cut,yoff-dy_cut),linewidth=3,c=(1,1,1))
plt.plot((x_cut-0.5*dx_cut,x_cut+1.5*dx_cut),(yoff+dy_cut,yoff-dy_cut),linewidth=1,c='k')
plt.plot((x_cut-1.5*dx_cut,x_cut+0.5*dx_cut),(yoff+dy_cut,yoff-dy_cut),linewidth=1,c='k')
#
plt.plot((-50,-50),(yoff-dy/4,yoff),linewidth=1,c='k')
plt.plot((0,0),(yoff-dy/4,yoff+dy/4),linewidth=1,c='k')
plt.plot((50,50),(yoff-dy/4,yoff+dy/4),linewidth=1,c='k')
plt.plot((100,100),(yoff-dy/4,yoff+dy/4),linewidth=1,c='k')
plt.plot((150,150),(yoff-dy/4,yoff+dy/4),linewidth=1,c='k')
plt.plot((200,200),(yoff-dy/4,yoff+dy/4),linewidth=1,c='k')
plt.plot((250,250),(yoff-dy/4,yoff),linewidth=1,c='k')
#
plt.text(-50,yoff-dy/3,'-1250',fontsize=fs_tax,horizontalalignment='center',verticalalignment='top',fontweight=fw)
plt.text(0,yoff-dy/3,'0',fontsize=fs_tax,horizontalalignment='center',verticalalignment='top',fontweight=fw)
plt.text(100,yoff-dy/3,'100',fontsize=fs_tax,horizontalalignment='center',verticalalignment='top',fontweight=fw)
plt.text(200,yoff-dy/3,'200',fontsize=fs_tax,horizontalalignment='center',verticalalignment='top',fontweight=fw)
plt.text(250,yoff-dy/3,'500',fontsize=fs_tax,horizontalalignment='center',verticalalignment='top',fontweight=fw)
#
plt.text(0,yoff+dy/3,'1850',fontsize=fs_tax,horizontalalignment='center',verticalalignment='bottom',fontweight=fw)
plt.text(50,yoff+dy/3,'1900',fontsize=fs_tax,horizontalalignment='center',verticalalignment='bottom',fontweight=fw)
plt.text(100,yoff+dy/3,'1950',fontsize=fs_tax,horizontalalignment='center',verticalalignment='bottom',fontweight=fw)
plt.text(150,yoff+dy/3,'2000',fontsize=fs_tax,horizontalalignment='center',verticalalignment='bottom',fontweight=fw)
plt.text(200,yoff+dy/3,'2050',fontsize=fs_tax,horizontalalignment='center',verticalalignment='bottom',fontweight=fw)
# 
plt.text(270,yoff,'Time (yr)',fontsize=fs_tax,horizontalalignment='left',verticalalignment='center',fontweight=fw)
plt.text(250,yoff-dy,'(from start of piControl)',fontsize=fs_small,horizontalalignment='right',verticalalignment='top')

# plot stippled lines indicating the initialisation relations 
xoff=-50
yoff=dy
plt.plot((xoff+50,xoff+50),(yoff,yoff+6*dy),linewidth=1,c=ec,linestyle='dashed')
plt.plot((xoff+50+129,xoff+50+129),(yoff+4.5*dy,yoff+6*dy),linewidth=1,c=ec,linestyle='dashed')
plt.plot((xoff+50+100,xoff+50+100),(yoff+6*dy,yoff+11*dy),linewidth=1,c=ec,linestyle='dashed')

#plt.plot((0,0),yoff-dy,yoff+dy),linewidth=1,c='k') spin-up
label='spinup'
xoff=-50
yoff=dy
plt.arrow(xoff,yoff,50,0,width=0.02,head_width=hw,head_length=hl,length_includes_head=True,ec=ec,fc=fc,overhang=oh)
x_cut=-27.5
plt.plot((x_cut-dx_cut,x_cut+dx_cut),(yoff+dy_cut,yoff-dy_cut),linewidth=3,c=(1,1,1))
plt.plot((x_cut-0.5*dx_cut,x_cut+1.5*dx_cut),(yoff+dy_cut,yoff-dy_cut),linewidth=1,c=ec)
plt.plot((x_cut-1.5*dx_cut,x_cut+0.5*dx_cut),(yoff+dy_cut,yoff-dy_cut),linewidth=1,c=ec)
plt.text(xoff-5,yoff,label,fontsize=fs,horizontalalignment='right',verticalalignment='center',fontweight=fw)

# piControl
label='piControl'
xoff=0
yoff=yoff+dy*1.5
plt.arrow(0,yoff,250,0,width=0.02,head_width=hw,head_length=hl,length_includes_head=True,ec=ec,fc=fc,overhang=oh)
x_cut=227.5
plt.plot((x_cut-dx_cut,x_cut+dx_cut),(yoff+dy_cut,yoff-dy_cut),linewidth=3,c=(1,1,1))
plt.plot((x_cut-0.5*dx_cut,x_cut+1.5*dx_cut),(yoff+dy_cut,yoff-dy_cut),linewidth=1,c=ec)
plt.plot((x_cut-1.5*dx_cut,x_cut+0.5*dx_cut),(yoff+dy_cut,yoff-dy_cut),linewidth=1,c=ec)
plt.text(xoff-5,yoff,label,fontsize=fs,horizontalalignment='right',verticalalignment='center',fontweight=fw)

# 1pctCO2 (should be 150 years, actual length is 164 years)
label='1pctCO2'
xoff=1850-1850
yoff=yoff+dy
plt.arrow(xoff,yoff,150,0,width=0.02,head_width=hw,head_length=hl,length_includes_head=True,ec=ec,fc=fc,overhang=oh)
plt.text(xoff-5,yoff,label,fontsize=fs,horizontalalignment='right',verticalalignment='center',fontweight=fw)

# abrupt4XCO2 (should be 150 years)
label='abrupt4XCO2'
xoff=1850-1850
yoff=yoff+dy
plt.arrow(xoff,yoff,150,0,width=0.02,head_width=hw,head_length=hl,length_includes_head=True,ec=ec,fc=fc,overhang=oh)
plt.text(xoff-5,yoff,label,fontsize=fs,horizontalalignment='right',verticalalignment='center',fontweight=fw)

# amip 1979-2014
label='amip'
xoff=1979-1850
yoff=yoff+dy
plt.arrow(xoff,yoff,36,0,width=0.02,head_width=hw,head_length=hl,length_includes_head=True,ec=ec,fc=fc,overhang=oh)
plt.text(xoff-5,yoff,label,fontsize=fs,horizontalalignment='right',verticalalignment='center',fontweight=fw)
plt.text(xoff,yoff-dy/5,'1979',fontsize=fs_small,horizontalalignment='left',verticalalignment='top')
plt.text(xoff+36+1,yoff,'2014',fontsize=fs_small,horizontalalignment='left',verticalalignment='center')

# historical
label='historical'
xoff=1850-1850
yoff=yoff+dy*1.5
plt.arrow(xoff,yoff,164,0,width=0.02,head_width=hw,head_length=hl,length_includes_head=True,ec=ec,fc=fc,overhang=oh)
plt.arrow(xoff+165,yoff,16,0,width=0.02,head_width=hw,head_length=hl,length_includes_head=True,ec=ec,fc=fc,overhang=oh)
plt.text(xoff-5,yoff,label,fontsize=fs,horizontalalignment='right',verticalalignment='center',fontweight=fw)
plt.text(xoff+165+16+1,yoff,'2029',fontsize=fs_small,horizontalalignment='left',verticalalignment='center')

# dcppA-assim i1 
label='dcppA-assim (i1)'
xoff=1950-1850
yoff=yoff+dy*1.5
plt.arrow(xoff,yoff,69,0,width=0.02,head_width=hw,head_length=hl,length_includes_head=True,ec=ec,fc=fc,overhang=oh)
plt.text(xoff-5,yoff,label,fontsize=fs,horizontalalignment='right',verticalalignment='center',fontweight=fw)
plt.text(xoff+10,yoff-dy/5,'1960',fontsize=fs_small,horizontalalignment='left',verticalalignment='top')
plt.text(xoff+69+1,yoff,'2018',fontsize=fs_small,horizontalalignment='left',verticalalignment='center')

# dccpA-hindcast i1 
label='dcppA-hindcast (i1)'
xoff=1960-1850
yoff=yoff+dy*0.5
for m in range(60):
    ecnew=(ec[0]*(60-m*0.6)/30,ec[1]*(60-m*0.6)/30,ec[2]*(60-m*0.6)/30)
    fcnew=(fc[0]*(60-m*0.6)/30,fc[1]*(60-m*0.6)/30,fc[2]*(60-m*0.6)/30)
    plt.arrow(xoff+m,yoff+m/60/6,11,0,width=0.02,head_width=0.04,head_length=5,length_includes_head=True,ec=ecnew,fc=fcnew,overhang=oh)
    plt.plot((xoff+m,xoff+m),(yoff-dy*0.5,yoff+m/60/6),linewidth=0.25,c=ec,linestyle='dashed')
plt.text(xoff-5-10,yoff+29/60/6,label,fontsize=fs,horizontalalignment='right',verticalalignment='center',fontweight=fw)
plt.text(xoff+(60-1)+11+1,yoff+59/60/6,'2028',fontsize=fs_small,horizontalalignment='left',verticalalignment='center')

# dcppA-assim i2 
label='dcppA-assim (i2)'
xoff=1950-1850
yoff=yoff+dy*0.5+0.25
plt.arrow(xoff,yoff,69,0,width=0.02,head_width=hw,head_length=hl,length_includes_head=True,ec=ec,fc=fc,overhang=oh)
plt.text(xoff-5,yoff,label,fontsize=fs,horizontalalignment='right',verticalalignment='center',fontweight=fw)

# dccpA-hindcast i2 
label='dcppA-hindcast (i2)'
xoff=1960-1850
yoff=yoff+dy*0.5
for m in range(60):
    ecnew=(ec[0]*(60-m*0.6)/30,ec[1]*(60-m*0.6)/30,ec[2]*(60-m*0.6)/30)
    fcnew=(fc[0]*(60-m*0.6)/30,fc[1]*(60-m*0.6)/30,fc[2]*(60-m*0.6)/30)
    plt.arrow(xoff+m,yoff+m/60/6,11,0,width=0.02,head_width=0.04,head_length=5,length_includes_head=True,ec=ecnew,fc=fcnew,overhang=oh)
    plt.plot((xoff+m,xoff+m),(yoff-dy*0.5,yoff+m/60/6),linewidth=0.25,c=ec,linestyle='dashed')
plt.text(xoff-5-10,yoff+29/60/6,label,fontsize=fs,horizontalalignment='right',verticalalignment='center',fontweight=fw)

plt.savefig("fig1b.png",dpi=300)