load_file("initactivesdRichy.hoc")
cvode_active(1) // use variable time step

// close the fig 9 button window
if (PWManager[0].count>1) { // if this file run directly do nothing
  PWManager[0].close(1)
}

objref OuterVBox, InnerHBox[3],A1, B1, C1
objref A2, B2, C2
objref A3, B3, C3
objref vvec, tvec
vvec = new Vector()
tvec = new Vector()

vvec.record(&soma[0].v(0.5))
tvec.record(&t)

proc fig9() {
OuterVBox = new VBox()
OuterVBox.intercept(1)
  InnerHBox[0] = new HBox()
  InnerHBox[0].intercept(1)

    fig9A1()

    fig9B1()

    fig9C1()

  InnerHBox[0].intercept(0)
  InnerHBox[0].map()
  InnerHBox[1] = new HBox()
  InnerHBox[1].intercept(1)

    fig9A2()

    fig9B2()

    fig9C2()

  InnerHBox[1].intercept(0)
  InnerHBox[1].map()
  InnerHBox[2] = new HBox()
  InnerHBox[2].intercept(1)

    fig9A3()

    fig9B3()

    fig9C3()

  InnerHBox[2].intercept(0)
  InnerHBox[2].map()
OuterVBox.intercept(0)
OuterVBox.map()
}

// sine wave current_step_on/_off

objref sin_current_clamp_vec, sin_current_clamp_tvec

sin_current_clamp_vec = new Vector()
sin_current_clamp_tvec = new Vector()
sin_current_clamp_tvec.indgen(0,1999,5)
sin_current_clamp_vec.indgen(0,PI*20-PI/20,PI/20)
sin_current_clamp_vec.apply("sin").mul(0.010) // -10 to 10 pA
// to see on a graph: sin_current_clamp_vec.line(Graph[2],sin_current_clamp_tvec)

objref current_clamp

proc sin_current_step_on() {
    current_step_on()
    current_clamp.del = 0
    current_clamp.dur=2000
    tstop = 2000
    sin_current_clamp_vec.play(&current_clamp.amp,sin_current_clamp_tvec)
}

proc sin_current_step_off() {
    sin_current_clamp_vec.play_remove()
    current_clamp.amp=0
}

// current_step_on/_off

proc current_step_on() {
    soma[0] current_clamp = new IClamp(0.5)
    current_clamp.del=466
    current_clamp.dur=1000
    current_clamp.amp=0.015 // 15 pA in nA
}

proc current_step_off() {
    current_clamp.amp=0.0 // nA
}

// the following two functions switches between using IM and IMminret

proc xe991_on() {
    forall {
        if (ismembrane("IM")) {
	    gbar_IM=0
            gbar_IMminret = 0.0
	}
    }
}
proc xe991_off() {
    forall {
        if (ismembrane("IM")) {
	    gbar_IM=0.000075
            gbar_IMminret = 0
	}
    }
}
proc switch_to_IMminret() {
    forall {
        if (ismembrane("IM")) {
	    gbar_IM=0
            gbar_IMminret = 0.000075
	}
    }
}

proc switch_to_IM() {
    forall {
        if (ismembrane("IM")) {
	    gbar_IMminret=0
            gbar_IM = 0.000075
	}
    }
}

proc fig9A1() {
    print "calculating fig 9 A1"
    tstop=4000
    init()
    run()
    A1 = new Graph()
    vvec.line(A1,tvec)
    // A1.exec_menu("View = plot")
    A1.view_size(0,-tstop*0.10,tstop*1.1,-90-15,60+15)
}
proc fig9B1() {
    print "calculating fig 9 B1"
    current_step_on()
    tstop=4000
    init()
    run()
    B1 = new Graph()
    vvec.line(B1,tvec)
    // B1.exec_menu("View = plot")
    B1.view_size(0,-tstop*0.10,tstop*1.1,-90-15,60+15)
    current_step_off()
}
proc fig9C1() {
    print "calculating fig 9 C1"
    sin_current_step_on()
    tstop=2000
    init()
    run()
    C1 = new Graph()
    vvec.line(C1,tvec)
    // C1.exec_menu("View = plot")
    C1.view_size(0,-tstop*0.10,tstop*1.1,-90-15,60+15)
    sin_current_step_off()
}

proc fig9A2() {
    print "calculating fig 9A2"
    xe991_on()
    tstop=4000
    init()
    run()
    A2 = new Graph()
    vvec.line(A2,tvec)
    // A2.exec_menu("View = plot")
    A2.view_size(0,-tstop*0.10,tstop*1.1,-90-15,60+15)
    xe991_off()
}

proc fig9B2() {
    print "calculating fig 9B2"
    xe991_on()
    current_step_on()
    tstop=4000
    init()
    run()
    B2 = new Graph()
    vvec.line(B2,tvec)
    // B2.exec_menu("View = plot")
    B2.view_size(0,-tstop*0.10,tstop*1.1,-90-15,60+15)
    xe991_off()
    current_step_off()
}

proc fig9C2() {
    print "calculating fig 9C2"
    xe991_on()
    sin_current_step_on()
    tstop=2000
    init()
    run()
    C2 = new Graph()
    vvec.line(C2,tvec)
    // C2.exec_menu("View = plot")
    C2.view_size(0,-tstop*0.10,tstop*1.1,-90-15,60+15)
    xe991_off()
    sin_current_step_off()
}
proc fig9A3() {
    print "calculating fig 9 A3"
    switch_to_IMminret()
    tstop=4000
    init()
    run()
    A3 = new Graph()
    vvec.line(A3,tvec)
    // A3.exec_menu("View = plot")
    A3.view_size(0,-tstop*0.10,tstop*1.1,-90-15,60+15)
    switch_to_IM()
}

proc fig9B3() {
    print "calculating fig 9 B3"
    switch_to_IMminret()
    current_step_on()
    tstop=4000
    init()
    run()
    B3 = new Graph()
    vvec.line(B3,tvec)
    // B3.exec_menu("View = plot")
    B3.view_size(0,-tstop*0.10,tstop*1.1,-90-15,60+15)
    switch_to_IM()
    current_step_off()
}

proc fig9C3() {
    print "calculating fig 9 C3"
    switch_to_IMminret()
    sin_current_step_on()
    tstop=2000
    init()
    run()
    C3 = new Graph()
    vvec.line(C3,tvec)
    // C3.exec_menu("View = plot")
    C3.view_size(0,-tstop*0.10,tstop*1.1,-90-15,60+15)
    switch_to_IM()
    sin_current_step_off()
}

// CntrlBox has fig buttons and run cntrl in it
objref CntrlBox
CntrlBox=new HBox()
CntrlBox.intercept(1)
// bring up a run control
nrncontrolmenu()

tstop=4000

xpanel("Lawrence et al. 2006")
xlabel("                                       ")
xbutton("Create Fig 9 A1","fig9A1()")
xbutton("Create Fig 9 B1","fig9B1()")
xbutton("Create Fig 9 C1","fig9C1()")
xlabel("")
xbutton("Create Fig 9 A2","fig9A2()")
xbutton("Create Fig 9 B2","fig9B2()")
xbutton("Create Fig 9 C2","fig9C2()")
xlabel("")
xbutton("Create Fig 9 A3","fig9A3()")
xbutton("Create Fig 9 B3","fig9B3()")
xbutton("Create Fig 9 C3","fig9C3()")
xlabel("")
xbutton("Create all of Fig 9","fig9()")
xlabel("")
xpanel()

CntrlBox.intercept(0)
CntrlBox.map()