/****************************************************************
 * FILE ana_vc_synss.hoc
 *
 * init_w_synss.hoc loads Randy's motoneuron and voltage ramp code
 * To execute, execute runvc("<modelname>")
 * This will save the current and voltage data, for the trace, one point per 
 * millisecond, with the model name incorporated in the file name
 ****************************************************************/

objref vecV, vecI, ff, str0
strdef fn, ldfl, myf

// The mult_synss variable controls the presence of synaptic current
mult_synss=0	// set to 0 to disable synaptic input

/**************************************************
 * PROC ana_IV() begin
 *
 * Generate a voltage ramp and record the current
 * Argument is the cell file name
 **************************************************/
proc ana_IV() {
    print "Entering ana_IV\n", "mult_synss = ", mult_synss
    if (numarg() < 1) {
	print "ana_FI(): call with FR cell for argument"
	return
    }
    
    fn = $s1
    
    if (! xopen(fn)) {
	printf("Fail to open requested cell file %s\n", fn)
	return
    }
    
    // The following code generates synaptic input
    // The boolean mult_synss turns it on and off
    // Settings are established in the call to 
    access dendrite
    insert synss
    onset_synss=0
    offset_synss=11000
    gss_synss=2e-5
    access soma
    insert synss
    onset_synss=0
    offset_synss=11000
    gss_synss=2e-5
    
    sprint(myf, "%s/vrampdel.hoc", codeloc)
    load_file(myf)
    
    tstop = 12000
    sprint(myf, "%s/FRmnrampvc_synss.ses", codeloc)
    load_file(myf)
     
    // allow for an extra second before and after the ramp
    tstop = 12000
    
    //    simple()
    vrampdelon()
    runvc(fn)
    
}
// PROC ana_FI end

/*******************************************
 * PROC runvc() begin
 * To execute, execute runvc("<modelname>")
 * This will save the current and voltage data, for the trace, one point 
 * per second, with the model name incorporated in the file name
 ********************************************/
proc runvc() {local i
    print "entering runvc()\n"
    ldfl = $s1
    fo=load_file(ldfl)
    if (!fo) {
	printf("bad file %s\n", ldfl)
	return
    }
    
    objref vecV, vecI, ff, str0
    vecI=new Vector()
    vecI.record(&VClamp[0].i)
    vecV = new Vector()
    vecV.record(&v(0.5))
    init()
    run()
    ff = new File()
    str0 = new StringFunctions()
    // remove the ".hoc" at end of cell filename
    str0.head(ldfl, "\.hoc*", fn)
    sprint(fn,"%s-vc_V0=%d_SLOPE=%.3f_TR=%d_SYN=%d.csv",fn,V0,SLOPE,TR, mult_synss)
    fo=ff.wopen(fn)
    if (!fo) {
	printf("Fail to open file %s, code=%d\n", fn, fo)
	return
    }
    ff.printf("label:%s\n", ldfl)
    ff.printf("%d\n", (vecI.size-1)/40)
    // We only need to save one point per millicsecond, i.e. every 40th point
    for (i=0; i<vecI.size; i=i+40) {
	ff.printf("%.4f,%.5f\n", vecV.x[i], vecI.x[i])
    }
    ff.close()
    
    printf("Created output file %s\nUse Excel to analyze IV curve and PIC parameters\n", fn)

}