// This piece of code was generated for the most part by Michael Hines. It reads the
// NINDS file format and creates the appropriate NEURON morphology and connection.
// The only thing that may vary for other motoneurons is the soma size and the name
// of the anatomy file.
//
// AUTHOR: Kelvin Jones
// DATE: 980729

create soma, hillock, is
soma	{ 		// From Culheim et al.
	L=48.8		// (um) length
	diam=48.8	// (um) diameter
}
hillock	{ 		 // linear taper from Kellerth et al. J Comp Neurol 184:755, 1979
	nseg=10
	L=20	
	diam(0:1)=3:13
}
is	{ 		// morphology from Kellerth
	L=30	
	diam=3.3
}	  

connect hillock(1), soma(0)
connect is(1), hillock(0)


objref den, ord, br, seg, type
strdef tstr, tstr2, tstr3
objref f
proc r() { local x1, y1, z1, x2, y2, z2, diam, i
        den = new Vector()
        ord = den.c  br = den.c  seg = den.c  type = den.c
        f = new File()
        f.ropen("M43c5.anat")
        for (i = 0; !f.eof ; i += 1) {
                den.append(f.scanvar())
                ord.append(f.scanvar())
                br.append(f.scanvar())
                seg.append(f.scanvar())
                type.append(f.scanvar())
                x1 = f.scanvar()
                y1 = f.scanvar()
                z1 = f.scanvar()
                x2 = f.scanvar()
                y2 = f.scanvar()
                z2 = f.scanvar()
                f.scanvar() // skip seg length. it is inconsistent with 3-d
                diam = f.scanvar()

                sname(tstr, i)
                sprint(tstr2, "create %s", tstr)
                execute(tstr2)
                sprint(tstr2, "access %s", tstr)
                execute(tstr2)
                pt3dadd(x1, y1, z1, diam)
                pt3dadd(x2, y2, z2, diam)
                con(i)
        }
}

proc sname() {
        if (numarg() > 2) {
                sprint($s1, "den%do%db%ds%dt%d", $2, $3, $4, $5, $6)
        }else{
                sprint($s1, "den%do%db%ds%dt%d", den.x[$2], ord.x[$2], br.x[$2], seg.x[$2], type.x[$2])
        }
}

proc  con() {local i, j, x
        i = $1
        if (seg.x[i] > 1) {
                j = i - 1
        }else if (ord.x[i] > 0) {
                for (j=i-1; j >= 0; j -= 1) {
                        if (ord.x[i] - 1 == ord.x[j]) {
                                break
                        }
                }
                if (j == -1) {
                        sname(tstr2, i)
                        printf("couldn't find parent of %s\n", tstr2)
                }
        }else{
                j = -1
        }
        if (j == -1) {
                tstr2 = "soma"
                x = .5
        }else{
                sname(tstr2, j)
                x = 1
        }
        sname(tstr3, $1)
        sprint(tstr3, "%s connect %s(0), %g", tstr2, tstr3, x)
        execute(tstr3)
}

r()