/* 
Uses the makeCell*.hoc files in parallel. Also the new ball and stick template.
*/

/************* Loading necessary files *********************/

xopen("makeSavestates.hoc")
load_file("Threshold_Template.hoc")
// Uses 50 um range for threshold test, and 50 ms width Gaussian.
// Assumes savestates are already made and in the folder. Use 'makeSavestates.hoc' for each machine

/************* The procs ****************/

strdef mcVS,ThName,fName,filename
objref savestate
proc mcVolt() { local voltInput,length,startS,AMPAc localobj cell
	// Inputs:
	// $1 is v_init
	// $2 is length
	// $3 is starting seed
	// $4 is AMPA conductance, normal .0005 umho

	voltInput = $1
	length = $2
	startS = $3
	AMPAc = $4

	// Makes cell with desired voltage and length
	cell = new Cell(length,voltInput)
	v_init = voltInput

	savestate = new SaveState()
	savestate = loadSv(length,voltInput*-1)
		// Loads the savestate to a variable 'savestate'

	// Running the test
	runTh(length,cell,startS,AMPAc)
}

proc runTh() { local nSpines,startS,AMPAc
	// $1 is the number of spines (dendrite length)
	// $o2 is the cell
	// $3 is the starting seed for the trials
	// $4 is the AMPA conductance

	nSpines = $1

	startS = $3
	AMPAc = $4

	// Inputs:
	// $1: locRange is the range to be uniformly sampled in space
	// $2: gaussTime is the width of the Gaussian to be sampled for timing
	// $3: repStat is the number of repetitions for each location, to collect statistics in variation
	// $4: incrBy is the distance in microns each trial is separated by.
	// $5: branchLength is the number of spines on the tested branch.
	// $6: toggle is the kind of synapse: 0 BOTH, 1 AMPA, 2 NMDA
	// $s7: filename.
	// $8: the starting seed
	// $o9: the cell itself
	// $10: AMPA conductance, with normal .0005 umho

	sprint(ThName,"A%dTr%dThLen%dV%dB.dat",AMPAc*10000,startS,nSpines,v_init*-1)
	ThreshSpace(50,50,10,10,nSpines,0,ThName,startS,$o2,AMPAc)
	sprint(ThName,"A%dTr%dThLen%dV%dA.dat",AMPAc*10000,startS,nSpines,v_init*-1)
	ThreshSpace(50,50,10,10,nSpines,1,ThName,startS,$o2,AMPAc)
}

proc init() { 
	// The right savestate must be loaded

  	finitialize(v_init)
    savestate.restore(1)
    t=0
    fcurrent()
    frecord_init()
}

obfunc loadSv() { localobj f,svstate
	// Loads the right savestate
	// $1 is the dendrite length
	// $2 is the voltage*-1

	sprint(filename,"BLen%dstdstt%d.dat",$1,$2)
	svstate = new SaveState()
	f = new File(filename)
	svstate.fread(f)
	return svstate
}

/************************************************************/
/**************** The actual script *************************/
/************************************************************/


	for vo_ind = 1,7 {	// sets the RMP from -55 (1) to -85 (7) mV in 5 mV steps
		for le_ind = 1,5 {	// sets the dendritic length to 200, 400, 600, 800, or 1,000 um (1-5)
			for ac_ind = 1,10 {	// modifies the AMPA conductance; 1 = 100 pS, 10 = 1 nS
				for tr_ind = 0,0 {	// redundant
					mcVolt(((vo_ind*5)+50)*-1,le_ind*200,tr_ind,ac_ind*.0001)
				}
			}
		}
	}