// The goal of this experiment is to insure that our cell responses
// are reasonably similar to the data on back propagating action potentials
// where the site of current injection is either at the soma or the apical trunk


load_proc("nrnmainmenu")             // load main NEURON library
cvode.active(1)
load_template("ExperimentControl")   // load a custom made library function that centralizes parameters so as to 

strdef accstr                                                // not confuse experimental variable bindings with neurophysiological variable bindings
objref econ                          // Create an experiment object
show_errs=1
debug_lev=1
econ=new ExperimentControl(show_errs,debug_lev)
econ.self_define(econ) // points the object at itself
econ.morphology_dir = "../../morphology/n123"                             // Setup morphology directory
econ.generic_dir = "../../experiment"                                    // Setup directory with cell-setup file
econ.add_lib_dir("Terrence","../../lib")                                  // Setup directory with library functions 

econ.data_dir       = "data"                       // Define directory to save produced data 
sprint(econ.syscmd, "mkdir -p %s", econ.data_dir)  // make the data directory
system(econ.syscmd)

// Setup cell

econ.xopen_geometry_dependent("cell")              // load the raw cell morphology
econ.xopen_geometry_dependent("cell-analysis")     // load user-defined semantics on morphology 
cell_analysis(econ)

printf("Opening cell setup\n")
econ.xopen_generic("cell-setup")                   // load the cell-setup file (define specific 
printf("Opened. Setting up cell\n")                // channels, membrane properties etc)
cell_setup(econ)

// Set simulation parameters for the experiment 

econ.defvar("Simulation Control", "tstop", "1400", "Defines when the simulation stops.")
econ.defvar("Simulation Control", "dt", "0.1", "Timestep")
econ.defvar("Simulation Control", "steps_per_ms", "10", "How many points are plotted per ms")
setdt()

proc CaL_increase(){  
f = 1.15
  forall if(ismembrane("cal")) { 
     for (x) { gcalbar_cal(x) = f*gcalbar_cal(x) }
  }
 }



// Define directories for different cases tested
strdef data_dir

//For the young cell  model (don't call the CaL_increase() function)
  
//data_dir       = "data/Young/AHP"             // Define directory to save produced data for somatic stimulation, AHP traces 
//data_dir       = "data/Young/Train"             // Train of APs


//For the old model (call the CaL_increase function)
CaL_increase()  
//data_dir       = "data/Aging/AHP"             // Define directory to save produced data for somatic stimulation, AHP traces 
data_dir       = "data/Aging/Train"             // Train of APs




sprint(econ.syscmd,  "mkdir -p %s", data_dir)                               // make directory
system(econ.syscmd) 


// Insert current clamp into soma or dendritic section

access soma
objref ic,ic2
ic = new IClamp(0.5)

// Configure current clamp 
econ.defvar("Current Clamp Control", "ic.del", "100", "Determines the delay before onset of the current clamp.")
//econ.defvar("Current Clamp Control", "ic.dur", "100", "Determines the duration of the current clamp.") //for AHP traces
econ.defvar("Current Clamp Control", "ic.dur", "800", "Determines the duration of the current clamp.") //for Train

// current used with somatic stimulation
econ.defvar("Current Clamp Control", "ic.amp", ".21", "Determines the amplitude of the current clamp at the soma.")  //for both young and aged AHP
econ.defvar("Current Clamp Control", "ic.amp", ".19", "Determines the amplitude of the current clamp at the soma.")  //for both young and aged Train



// Create basic graphics

econ.xopen_library("Terrence","basic-graphics")                            // open graphics library file   
//addgraph_2("soma.v(0.5)",              0,tstop,-76,-56) //for AHP traces
addgraph_2("soma.v(0.5)",              0,tstop,-72,20)    // for Train
addgraph_2("apical_dendrite[46].v(0.5)",0,tstop,-72,20)    // 156.96 um from soma
addgraph_2("apical_dendrite[71].v(0.5)", 0,tstop,-72,20)   // 324.53 um from soma


// Initialize and run the experiment

finitialize(v_init)
fcurrent()
cvode.re_init()
xpanel("Run the aging model")
  xlabel("click run to start the simulation")
  xbutton("run","run()")
xpanel()

// Dump the graphical output to .eps files

//econ.xopen_library("Terrence","verbose-system")
for i=0,windex {

  sprint(econ.tmp_str, "%s/graph-%d.eps",data_dir, i)    // define the name of the .eps file  
  win[i].printfile(econ.tmp_str)

}