// The spike viewer colors spikes using the theme of red if
// a spike is potentiating, blue if depressing, and black if unchanged.
//Spikes are shown as raster triples. Each triple is
// mitral soma spike, the mitral dendrite spike at the selected location,
// and the granule spike at the selected location.
// The selected location is some granule cell location.
// The lower graph is a zoom view of the indicated postion in the
// full raster upper graph

begintemplate SpikeViewer
external ngranule, nmitral, ncell
external gid2mg
objref tpat, idpat, vb, g, gz, this
objref dtpat, mat[2], xsel, ysel, csel
proc init() {
	zv_width = 1000
	tpat = $o1 // pattern_tvec
	idpat = $o2 // pattern_idvec
	color_spikes()
	xgran = int(ngranule/2)
	xsel = tpat.c.resize(0)
	ysel = tpat.c.resize(0)
	csel = tpat.c.resize(0)
	build()
}

proc build() {
	vb = new VBox()
	vb.ref(this)
	vb.save("")
	vb.intercept(1)
	xpanel("",1)
	xmenu("Z View Width")
	xbutton("200", "zvw(200)")
	xbutton("500", "zvw(500)")
	xbutton("1000", "zvw(1000)")
	xmenu()
	xpvalue("Granule x", &xgran, 1, "x_change()")
	//xslider(&xgran, 0, ngranule, "x_change()", 0, 1)
	xpanel()
	g = new Graph()
	g.menu_tool("zoomed view", "zview")
	g.size(0, 1000*int(tpat.x[tpat.size-1]/1000 + .5), 0, nmitral)
	g.view(0,0,zv_width,nmitral,0,0,300,200)
	vb.intercept(0)
	vb.map("Spike Viewer", 200, 200, 500, 600)
	g.exec_menu("zoomed view")
	x_change()
}

proc zvw() {local x
	zv_width = $1
	x = g.view_info(1, 5)
	g.view_size(1, x, x+zv_width, -1, nmitral+1)
}
proc zview() {local i
	i = g.view_info()
	if (i == 0) {
		g.view_size(1, $2-zv_width/2, $2+zv_width/2, -1, nmitral+1)
	}else{
		translate($1, $2, $3)
	}
}

i=0
x=0
width=0
proc translate() {local x0,y0
        if ($1 == 2) {
                i = g.view_info()
                x = g.view_info(i, 5)
                xrel=g.view_info(i, 11, $2)
                width=g.view_info(i,1)
        }
        if ($1 == 1) {
                x1 = g.view_info(i, 11, $2)
                x0 = x - width*(x1 - xrel)
                g.view_size(i, x0, x0 + width, -1, nmitral+1)
        }
}


proc x_change() {local i, c, f, gid, m, g, is_m2g
	if (xgran < 0) {
		xgran = 0
	}else if (xgran > ngranule-1) {
		xgran = ngranule-1
	}else{
		xgran = int(xgran)
	}
	xsel.resize(0)
	ysel.resize(0)
	csel.resize(0)
	for i=0, tpat.size-1 {
		gid = idpat.x[i]
		f = 0
		if (gid < nmitral) {
			xsel.append(tpat.x[i])
			ysel.append(gid+1)
			f = 1
		}else if (gid > ncell){
			gid2mg(gid, &m, &g, &is_m2g)
			if (g == xgran) {
				f = 1
				xsel.append(tpat.x[i])
				ysel.append(m+.7 - .3*is_m2g)
			}
		}
		if (f) {
			dt = dtpat.x[i]
			if (dt < ltpinvl_AmpaNmda) {
				c = 2
			}else if (dt > ltdinvl_AmpaNmda) {
				c = 1
			}else{
				c = 3
			}
			csel.append(c)
		}
	}
	gplt()
}
proc color_spikes() {local i, gid, m, g, is_m2g
	dtpat = tpat.c
	mat[0] = new Matrix(nmitral+1, ngranule+1)
	for m=0, nmitral for g = 0, ngranule { mat[0].x[m][g] = -1000 }
	mat[1] = mat[0].c
	for i=0, tpat.size-1 {
		gid = idpat.x[i]
		if (gid < nmitral) {
			m = gid + 1  g = 0  is_m2g = 0
		}else if (gid < nmitral + ngranule) {
			m = 0  g = gid - nmitral + 1
		}else{
			gid2mg(gid, &m, &g, &is_m2g)
			m += 1
			g += 1
		}
		
		dtpat.x[i] = tpat.x[i] - mat[is_m2g].x[m][g]
		mat[is_m2g].x[m][g] = tpat.x[i]
	}
}
proc gplt() {local i
	g.erase()
	for i=0, xsel.size-1 {
		g.mark(xsel.x[i], ysel.x[i], "|", 10, csel.x[i], 1)
	}
}
endtemplate SpikeViewer