objref mylist, f, myfile
strdef filename

mylist = new List()

proc getpaths() {
        mylist.remove_all()
        mylist = new List()

        avg = 0
        elp = 0
        telp = 0
        Rm = 1/g_pas

        elp = sqrt(diam*Rm / (4.0*Ra)) / L  //Inverse of electrotonic length constant
        telp = 0

        myfile.printf ("%s,%f,%f,%f", secname(), diam, L, elp)
        print secname()

        tot =1

	totlength=0

        for i=1,100 {

                f = new SectionRef()
                if (!f.has_parent) {
                        break
                }

                avg += diam
                elp = sqrt(diam*Rm / (4.0*Ra)) / L  //(Impact of Dendritic Size and Dendritic Topology on BurstFiring in Pyramidal Cells, 2010, Elburg and Ooyen)
                telp += elp
		totlength +=  L
                mylist.append(f)
                access f.parent 
                tot += 1
        }
        print secname()

        myfile.printf (",%f,%f,%f,%d", telp/tot, avg/tot, totlength, mylist.count() )

	/*if (mylist.count() > 0) { 
		for (i = mylist.count()-1; i >=0; i-=1) {
			access mylist.o(i).sec
			myfile.printf("%s;", secname())
		}
	}*/
        print "end"
	myfile.printf("\n")
}

//========================================================== 

proc exportinfo() {
	//filename = $s1
	myfile = new File()
	myfile.wopen("MEP.csv")
	/*  Commented code causes infinite loop for i==3 (first for loop) for some reason!!!!
                        for i=0, basal.count()-1 {
                                access basal.o(i).sec
                                print i
                                getpaths()
	}
                        if (!ablated) {
                                for i=0, apical.count()-1 {
                                        access apical.o(i).sec
                                        print i
                                        getpaths()
                                }
                        }
                        */
                        forall {
                                print secname()
                                getpaths()
                        }       
	myfile.close()
}


//============================================================= MEP for terminal branches

proc MEPc() {
 
        mylist.remove_all()
        mylist = new List()

        avg = 0
        elp = 0
        telp = 0
        Rm = 1/g_pas

        elp = L / sqrt(diam*Rm / (4.0*Ra))
        telp = 0

        myfile.printf ("%s\t%f\t%f\t%f", secname(), diam, L, elp)
        //print secname()

        tot =1

        totlength=0

        for i=1,100 {

                f = new SectionRef()
                if (!f.has_parent) {
                        break
                }

                avg += diam
                elp = L / sqrt(diam*Rm / (4.0*Ra))  //(Impact of Dendritic Size and Dendritic Topology on BurstFiring in Pyramidal Cells, 2010, Elburg and Ooyen)
                telp += elp
                totlength +=  L
                mylist.append(f)
                dchild=f.nchild
                access f.parent 
                tot += 1
        }
        //print secname()

        myfile.printf ("\t%f\t%f\t%f\t%d\t%d\t%d", telp, avg/tot, totlength, mylist.count(),tot,dchild)

        //print "end"
        myfile.printf("\n")

}


proc exportinfo2() {
        //filename = $s1
        myfile = new File()
        myfile.wopen("MEPc.tsv")
        /*  Commented code causes infinite loop for i==3 (first for loop) for some reason!!!!
                        for i=0, basal.count()-1 {
                                access basal.o(i).sec
                                print i
                                getpaths()
        }
                        if (!ablated) {
                                for i=0, apical.count()-1 {
                                        access apical.o(i).sec
                                        print i
                                        getpaths()
                                }
                        }
                        */
                        myfile.printf ("%s\t%s\t%s\t%s", "secname", "diam", "L", "elp")
                        myfile.printf ("\t%s\t%s\t%s\t%s\t%s", "telp", "avg/n", "totlength", "nc","nt","dchild")
                        myfile.printf("\n")

                        forall {
                                print secname()
                                MEPc()
                        }       
        myfile.close()
}