if (name_declared("pkgversions") != 4 ) {  execute("strdef pkgversions") }
sprint(pkgversions,"%sVCaGraph = $Revision: 1.5 $, ",pkgversions)

load_file("RPlot.hoc")
load_file("FormatFile.hoc")
load_file("GraphUtils.hoc")

begintemplate VCaGraph
public addvar, glist, rprintfile, save

strdef cmd
objref vb
objref hblist
objref glist
objref this
objref file, rp
objref gu
objref xvl, yvl
objref areas

// VCaGraph(double rows, double cols)
// This takes four arguments: left, top, width and height
proc init() { local i, j
    rows=2
    vb = new VBox()
    hblist = new List()
    glist = new List()
    vb.intercept(1)
    glist.append(new Graph(0))
    glist.object(0).view(0, -80, 100, 40, 930, 304, 300.48, 200.32)
    glist.append(new Graph(0))
    glist.object(1).view(0, 0, 100, 0.01, 930, 304, 300.48, 200.32)
    glist.append(new Graph(0))
    glist.object(2).view(0, 0, 100, 1, 930, 304, 300.48, 200.32)
    glist.append(new Graph(0))
    glist.object(3).view(0, 0, 100, 0.1 , 930, 304, 300.48, 200.32)
    vb.intercept(0)
    vb.map("Spine variables", $1, $2, $3, $4)
    sprint(cmd,"graphList[0].append(%s.glist.object(0))", this)
    execute1(cmd)
    sprint(cmd,"graphList[2].append(%s.glist.object(1))", this)
    execute1(cmd)
    sprint(cmd,"graphList[2].append(%s.glist.object(2))", this)
    execute1(cmd)
    sprint(cmd,"graphList[2].append(%s.glist.object(3))", this)
    execute1(cmd)
    rp = new RPlot()
    gu = new GraphUtils()
    areas = new Vector()
}

//
// addvar(NmdaAmpaSpineSynStim input, double col, double linewidth)
// Add voltage, calcium concntration, block value and calcium current from INPUT with particular
// colour COL and linewidth LINEWIDTH
//
proc addvar() {
    sprint(cmd,"%s.glist.object(0).addvar(\"%s.spine.head.v( 0.5 )\",%d,%d)", this, $o1, $2, $3)
    execute1(cmd)
    sprint(cmd,"%s.glist.object(1).addvar(\"%s.spine.head.cai( 0.5 )\",%d,%d)", this, $o1, $2, $3)
    execute1(cmd)
    sprint(cmd,"%s.glist.object(2).addvar(\"%s.nmdasyn.b\",%d,%d)", this, $o1, $2, $3)
    execute1(cmd)
    sprint(cmd,"%s.glist.object(3).addvar(\"%s.nmdasyn.ica\",%d,%d)", this, $o1, $2, $3)
    execute1(cmd)
    sprint(cmd,"%s.glist.object(3).addvar(\"%s.ampasyn.ica\",%d,%d)", this, $o1, $2, $3)
    execute1(cmd)
    sprint(cmd,"%s.glist.object(3).addvar(\"%s.spine.head.ica( 0.5 )\",%d,%d)", this, $o1, $2, $3)
    execute1(cmd)
    $o1.spine.head areas.append(area(0.5))
}


// rprintfile(String epsfilename, Double SaveFile)
proc rprintfile() { local i, j
    if (numarg() == 1) { rp.create_rfile() }
    if (numarg() == 2) {
        if ($2 != 0) { rp.create_rfile($s1) }
    }
    rp.start_plot($s1)
    rp.set_mfrow(rows,1)
    for i=0,rows-1 {
        rp.add_graph(glist.object(i))
    }
    rp.end_plot()
    rp.run()
}

//
// save(File FILE) 
// savie recordings from all panels to file handle FILE
// 
proc save() { localobj file
    file = $o1
    xvl = new List()
    yvl = new List()
    file.printvec("areas",areas)
    gu.getlines_list(glist.object(0),xvl,yvl) 
    file.printlistasmat("tvsyn",xvl)
    file.printlistasmat("vsyn",yvl)
    xvl.remove_all()
    yvl.remove_all()
    gu.getlines_list(glist.object(1),xvl,yvl) 
    file.printlistasmat("tcasyn",xvl)
    file.printlistasmat("casyn",yvl)
    xvl.remove_all()
    yvl.remove_all()
    gu.getlines_list(glist.object(2),xvl,yvl) 
    file.printlistasmat("tbsyn",xvl)
    file.printlistasmat("bsyn",yvl)
    xvl.remove_all()
    yvl.remove_all()
    gu.getlines_list(glist.object(3),xvl,yvl) 
    file.printlistasmat("ticasyn",xvl)
    file.printlistasmat("icasyn",yvl)
    xvl.remove_all()
    yvl.remove_all()
}

endtemplate VCaGraph