// create single EPSP at each synapse and record the current measured at the soma with voltage clamp

proc EPSC_parameters() {
	tstop=50
	sprint(str_tstop,"tstop = %d ms",tstop)
	NUM=1
	STRT=5    	 // input start time
	NOISE=0   	 // noise level
	INTRVL=10*tstop	 // inter-spike average interval (big number)
	VChold=v_init//-70.3		 // voltage clamp holding potential
	WEIGHT = 0.001 // by default synapse has a 1 nS conductance
}

proc EPSC_stim() {
	sprint(cmd,"access %s",ORIGIN_NAME)
	execute(cmd) // all NetStims attached logically to soma (it doesn't matter where you put them)
	objref source,trvl_syn
	source=new NetStim(0.5)
	source.number=NUM
	source.start=STRT
	source.interval=INTRVL
	source.noise=NOISE
//	For some reason this only works when the global scaling is delted from the ScalExp2Syn.mod file...
	trvl_syn=new Exp2Syn(0.5)
	trvl_syn.tau1=SYNTAU1
	trvl_syn.tau2=SYNTAU2
	trvl_syn.e=SYNE
	syn=new NetCon(source,trvl_syn,0.9,0, WEIGHT)	
	objref vclmp
	vclmp=new VClamp(0.5)
	vclmp.dur[0]=tstop
	vclmp.amp[0]=VChold
}

proc EPSC_recvec_initial() {
	EPSC_time=new Vector()
	EPSC_time.indgen(STRT,tstop+dt,dt) // record only from start of EPSP
}

proc EPSC_recvec() {
	sEPSCrec=new Vector(EPSC_time.size)
	sEPSCrec.record(&vclmp.i,EPSC_time)
}

proc EPSC_undo(){
	objref sEPSCrec
	objref EPSC_time
	objref syn
	objref logsyn
	objref source
	objref vclmp
	objref trvl_syn
}

proc EPSC_begin_simulation() { local i, enable, variant, weight, gsyn
	// variant: 1=all SF=1; 2=current SF profile
	variant=$1
	enable = SCALE_ENABLE
	SCALE_ENABLE = 0
	set_SCALE_ENABLE(1)
	EPSC_stim()
	EPSC_recvec_initial()
objref g
g=new Graph()
	for i = 0 ,NLOGSYNS-1 {
		logsyn=logsynlist.object(i)
		logsyn.sec.sec trvl_syn.loc(logsyn.loc)
		EPSC_recvec()
		if (variant==2) {
			reclist.object(1).read_record() // read SFs (which get reset at beginning of simulation)
			syn.weight = logsyn.syn.Egmax * WEIGHT
//			trvl_syn.Esf0=logsyn.syn.Esf
//			trvl_syn.Esf=logsyn.syn.Esf
		}
		gsyn = syn.weight * 1000 	// nS
	 	sprint(str_status,"Running EPSC syn %d/%d gsyn = %g nS",i+1,NLOGSYNS, gsyn)
	 	shape_plot.point_mark(trvl_syn, 7)
		my_run()
//		sEPSCrec.add(-sEPSCrec.x(0)) // subtract baseline
		sEPSCrec.mul(1000)			 // transform to pico Amps
		if (variant==1) {
			logsyn.sEPSCrec=new Vector()
		 	logsyn.sEPSCrec.copy(sEPSCrec)
		} else if (variant==2) {
			logsyn.post_sEPSCrec=new Vector()
			logsyn.post_sEPSCrec.copy(sEPSCrec)
		}
sEPSCrec.line(g, EPSC_time)
g.exec_menu("View = plot")
	}
	SCALE_ENABLE = enable
	set_SCALE_ENABLE(1)
	if (variant==1) {
		write_EPSC_records(sEPSCrec.size(),dt,STRT)
	} else if (variant==2) {
		write_EPSC_after_records(sEPSCrec.size(),dt,STRT)
	}
	reset_records()
	str_status="Ready"
}