begintemplate SpikePlot1
public cells, vecs, g, update
public map, save_data, b, unmap
public flush, begin, plot, size, view_count, fastflush, simgraph
external addplot, tstop
objref cells, vecs[1], nc, g, this, y, tobj, b, outlist, nil
strdef tstr, modestr
proc init() {
	fwindow = 100
	binwidth = .1
	modestr = "Spikes   "
	mode=1
	y = new Vector(1000)
	cells = $o1
	if (numarg() == 1) {
		build()
		map()
	}
}
proc map() {
	sprint(tstr, "%s for %s", this, cells)
	if (numarg() > 1) {
		b.map(tstr, $2,$3,$4,$5)
	}else{
		b.map(tstr)
	}
	update()
	flush()
}
proc unmap() {
	b.unmap()
	g = nil
}
proc build() {
	b = new VBox(3)
	b.priority(600)
	b.save("")
	b.ref(this)
	b.dismiss_action("unmap()")
	b.intercept(1)
	xpanel("")
	xmenu("Plot")
	xbutton("Update", "update() flush()")
	xradiobutton("Spikes", "pmode(1)", mode==1)
	xradiobutton("Frequency", "pmode(2)", mode==2)
	xradiobutton("Histogram", "pmode(3)", mode==3)
	xmenu()
	xvarlabel(modestr)
	xpvalue("Freq Window (ms)", &fwindow, 1, "flush()")
	xpvalue("Hist Bin (ms)", &binwidth, 1, "flush()")
	xpanel()
	g = new Graph()
	b.intercept(0)
	addplot(this, 1)
	begin()
	pmode(mode)
}
proc pmode() {
	mode = $1
	if (mode == 1) {
		modestr = "Spikes   "
	}else if (mode == 2) {
		modestr = "Frequency"
	}else if (mode == 3) {
		modestr = "Histogram"
	}
	flush()
}


proc update() {local i
	n = cells.count
	if (n == 0) return
	objref vecs[n]
	for i=0, n-1 {
		vecs[i] = new Vector(0)
		tobj = cells.object(i)
		tobj.connect2target(nil, nc)
		sprint(tstr, "%s", tobj)
		vecs[i].label(tstr)
		nc.record(vecs[i])
	}
	objref nc, tobj
}

proc begin() {
}

func view_count() {
	if (g == nil) {
		return 0
	}
	return g.view_count()
}
proc simgraph() {
}
proc plot() {
}
func size() {
	if (numarg() == 4) {
		g.size($1,$2,0,cells.count+1)
		return 1.
	}else{
		return g.size($1)
	}
}
proc fastflush() {}

proc flush() {local i
	if (object_id(g) == 0) return
	g.erase_all
	g.vfixed(1)
	g.label(.9,1)
if (mode == 1) {
	for (i=n-1; i >= 0; i -= 1) {
		y.resize(vecs[i].size).fill(i+1)
		y.label(vecs[i].label)
		y.mark(g, vecs[i], "|", 8, 1, 1)
		y.line(g, vecs[i], 1, 0)
	}
}else if (mode == 2) {
	for (i=n-1; i >= 0; i -= 1) {
		y = vecs[i].sumgauss(0, tstop, tstop/100, fwindow)
		y.label(vecs[i].label)
		y.mul(1000).line(g, y.c.indgen(0, tstop/100), 1,1)
	}
}else if (mode == 3) {
	for (i=n-1; i >= 0; i -= 1) if (vecs[i].size > 1){
		y = vecs[i].c.deriv(1,1)
		high = y.max
		y = y.sumgauss(0, high, high/50, binwidth)
		y.label(vecs[i].label)
		y.line(g, y.c.indgen(0, high/50), 1,1)
	}
}
	g.flush()
}

endtemplate SpikePlot1