//First, run nrnivmodl *.mod file to compile all the channels.

load_file("nrngui.hoc")

load_file("baseline_values.txt")
load_file("all_tau_vecs.hoc")


load_file("../Elec/Nov9IR2a_spine.hoc")

load_file("ranstream.hoc")
xopen("electro_procs.hoc")


// Using SEClamp, as recommended on the NEURON User Forum.  See init_PFC.hoc for details.
INITDUR = 80	

steps_per_ms = 40
dt = 0.025



load_file("basic_procs.hoc")

set_pas(G_PAS)

set_naf(G_NAF)
set_nap(G_NAP)

set_kir(G_KIR)
set_kas(G_KAS)
set_kaf(G_KAF)
set_kdr(G_KDR)
set_bkkca(G_BKKCA)
set_skkca(G_SKKCA)


set_caL(P_CAL)
set_caL13(P_CAL13)
set_can(P_CAN)
set_caq(P_CAQ)
set_car(P_CAR)
set_cat(P_CAT)

set_cainf(CAINF)
set_taur(TAUR)
set_cadrive(CA_DRIVE)
set_pump(CA_PUMP)

print "setup complete"

/**** This is HS's new codes: ***/

objref nslist, rslist, rs
nslist = new List()
rslist = new List()
objref tvec, idvec, nil
tvec = new Vector()
idvec = new Vector()

random_stream_offset_ = 500 // Adjacent streams will be correlated by this offset.
// Seeds for network architecture and stimulus trains.
connect_random_low_start_ = 1  
run_random_low_start_ = 2  


proc restart() { local i
  for i = 0, rslist.count()-1 rslist.o(i).start()
}



print "Addsynapese good?"

distance()

objref synBranches, synLoc

/*** new parameter settings as of 23 Jan 2012 ***/
VO = -80
V0 = -80	//avoid ambiguity of "Capital O" vs "zero 0"



/**** 
    set up a Voltage Clamp
****/

objref seClamp
    soma seClamp = new SEClamp(0.5)
    seClamp.dur1 = 1e9
    seClamp.amp1 = -80
    seClamp.dur2 = 0




/***** end voltage clamp ****/






/**********************  copied from synTweak.hoc in ~/LuebkeAmatrudo_forCluster  *********/



strdef synFilename
objref synFout, tv, iv
objref rs

/**********************

    Written originally in main_PFC_simEPSC_all.hoc; now copied here.

    $1  number of synapses
    $2  value of tau1
    $3  value of tau2
    $4  value of gAMPA
    $s5 file basename

**********************/
proc synTweak() { local i, vecsz, btyp

    adjust_tau1($2,$1)
    adjust_tau2($3,$1)
    adjust_gAMPA($4,$1)

    sprint(synFilename,"%s_tR%.4f_tF%.4f_gAMP%.7f.Ibin",$s5,$2,$3,$4)

    synFout = new File()
    synFout.wopen(synFilename)

    tv = new Vector()
    tv.record(&t)
    iv = new Vector()
    iv.record(&seClamp.i)

    init()
    run()

    vecsz = tv.size()
    synFout.vwrite(&vecsz)
    tv.fwrite(synFout)
    iv.fwrite(synFout)
    synFout.close()

    sprint(synFilename,"%s_tR%.4f_tF%.4f_gAMP%.7f_dist.txt",$s5,$2,$3,$4)


	synFout = new File()
    synFout.wopen(synFilename)

    i = 0
    forsec synBranches {
        btyp = 0
	ifsec proximal { btyp = 1 }
	ifsec middend  { btyp = 2 }
	ifsec distal  {btyp = 3}
        synFout.printf("%d\t%g\t%g\t%d\n",i,distance(synLoc.x[i]),distance(synLoc.x[i])-soma.diam,btyp)
         i+=1
    }
    synFout.close()
}




/**********************  end from synTweak.hoc in ~/LuebkeAmatrudo_forCluster  *********/





xopen("plot_seClamp_i.ses")
xopen("PFC-V1_AddSynapses_neg.hoc")
nBr = 0

//WTD1
    AddExcSynapses_byNumber(5,15,10,200,200,synBranches,synLoc)
    //This is to specify how many synapses to insert in proximal, medial and distal dendrites. 
//WTD2
//    AddExcSynapses_byNumber(5,14,10,200,200,synBranches,synLoc)
//HETD1
//    AddExcSynapses_byNumber(4,13,10,200,200,synBranches,synLoc)
//HETD2
//   AddExcSynapses_byNumber(3,14,11,200,200,synBranches,synLoc)
//    nSynapse = AddExcSynapses_byNumber(10,10,10,200,200,synBranches,synLoc)
	nSynapse = nASyn + nBSyn + nCSyn
//    endSyn = 200 + (nSynapse + 2)*200

	endSyn = 30000
    distance()
    cnt = 0

    
    tstop=endSyn
 //WTD1_Nov9IR2a
 synTweak(nSynapse,2.0053,1.5669,.000268,"fig11_PFCapic")
//This is to specify the rise/decay time constants and max AMPAR conductance. 


connEsyndend.record(tvec,idvec)
sprint(synFilename,"test_raster.txt")
//The test_raster file contains all the time for EPSC and corresponding activated synapses. 
synFout = new File()
synFout.wopen(synFilename)

for j=0, tvec.size-1 synFout.printf("%7.3f \t%d\n", tvec.x(j), idvec.x(j))
synFout.close()

//printf("\n\n**************************\n\n")
//printf("Output has been written to a file ending in .Ibin, a customized binary file format.\n")
//printf("These files can be read with MATLAB.  See the .m files contained in this directory.\n")
//printf("MATLAB's Statistics Toolbox is required to analyze EPSC shapes.\n")
//printf("\tSample usage:  read_EPSCsims_mdb('fig11_PFCapic',0.00054)\n")
//printf("\n\n**************************\n\n")