//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

func cellID() {	//$1 cell type, $2 id within type, returns global id
//return global id
	tmp = -1

	if ($1==ID_STN) {
		if ($2 < numSTN) {
			tmp = $2
		}
	} else {
	if ($1==ID_GPe) {
		if ($2 < numGPe) {
			tmp = numSTN + $2
		}
	} else {
	if ($1==ID_GPi) {
		if ($2 < numGPi) {
			tmp = numSTN + numGPe + $2
		}
	}
	}
	}

	return tmp
}


//----------------------------------------------------------------------------
func synNum() {	//$1 src cell type, $2 target cell type, $3 number within type, returns synapse number
//return synapse list index

	tmp = -1

//-----------------------------------------
	if ($2==ID_STN) {

		if ($1==ID_Ctx) {
			if ($3 < numCtxSTN) {
				tmp = $3
			}
		} else {
		if ($1==ID_GPe) {
			if ($3 < numGPeSTN) {
				tmp = $3 + numCtxSTN
			}
		}
		}

	} else {

//-----------------------------------------
	if ($2==ID_GPe) {

		if ($1==ID_Str) {
			if ($3 < numStrGPe) {
				tmp = $3
			}
		} else {
		if ($1==ID_STN) {
			if ($3 < numSTNGPe) {
				tmp = $3 + numStrGPe
			}
		} else {
		if ($1==ID_GPe) {
			if ($3 < numGPeGPe) {
				tmp = $3 + numStrGPe + numSTNGPe
			}
		}
		}
		}

	} else {

//-----------------------------------------
	if ($2==ID_GPi) {

		if ($1==ID_Str) {
			if ($3 < numStrGPi) {
				tmp = $3
			}
		} else {
		if ($1==ID_STN) {
			if ($3 < numSTNGPi) {
				tmp = $3 + numStrGPi
			}
		} else {
		if ($1==ID_GPe) {
			if ($3 < numGPeGPi) {
				tmp = $3 + numStrGPi + numSTNGPi
			}
		}

		}
		}

	} else {

//-----------------------------------------
//	if ($2==ID_Tha) {

//		if ($1==ID_GPi) {	// this is a fake Tha synapse located in its connected GPi output cell
//			if ($3 < numGPiTha) {
//				tmp = $3 + numStrGPi + numSTNGPi + numGPeGPi
//			}
//		}
//	}

	}
	}
	}

	return tmp
}






//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//Setup global parameters
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

objref probCtxSTN, probStrGPe, probStrGPi

//-----------------------------------------------------------------------------------------------------
// IDs

ID_STN = 1
ID_GPe = 2
ID_GPi = 3
ID_Ctx = 4
ID_Str = 5
ID_Tha = 6

// make space for variables
numSTN = 0
numGPe = 0
numGPi = 0
ncell = 0
numSTNGPe = 0
numGPeGPe = 0
numGPeSTN = 0
numGPeGPi = 0
numSTNGPi = 0
numCtxSTN = 0
numStrGPe = 0
numStrGPi = 0
numGPiTha = 0

divSTNGPe = 0
divGPeGPe = 0
divGPeSTN = 0
divGPeGPi = 0
divSTNGPi = 0
divGPiTha = 0

iBiasSTN = 0
iBiasGPe = 0
iBiasGPi = 0

gmaxSTNGPe = 0
gmaxGPeGPe = 0
gmaxGPeSTN = 0
gmaxGPeGPi = 0
gmaxSTNGPi = 0
gmaxGPiTha = 0

gDA_STNGPe = 0
gDA_GPeGPe = 0
gDA_GPeSTN = 0
gDA_GPeGPi = 0
gDA_STNGPi = 0
gDA_GPiTha = 0

delSTNGPe = 0
delGPeGPe = 0
delGPeSTN = 0
delGPeGPi = 0
delSTNGPi = 0
delGPiTha = 0

imnCtxSTN = 0
isdCtxSTN = 0
bmnCtxSTN = 0
bsdCtxSTN = 0
bdmnCtxSTN = 0
bdsdCtxSTN = 0

imnStrGPe = 0
isdStrGPe = 0
bmnStrGPe = 0
bsdStrGPe = 0
bdmnStrGPe = 0
bdsdStrGPe = 0

imnStrGPi = 0
isdStrGPi = 0
bmnStrGPi = 0
bsdStrGPi = 0
bdmnStrGPi = 0
bdsdStrGPi = 0

probCtxSTN = new Vector(numCtxSTN)
probStrGPe = new Vector(numStrGPe)
probStrGPi = new Vector(numStrGPi)

gmaxCtxSTN = 0
gmaxStrGPe = 0
gmaxStrGPi = 0

gDA_CtxSTN = 0
gDA_StrGPe = 0
gDA_StrGPi = 0

delCtxSTN = 0
delStrGPe = 0
delStrGPi = 0

dbs_del = 0
dbs_dur = 0
dbs_direct_amp = 0
dbs_direct_pw = 0
dbs_ad_amp = 0
dbs_ad_pw = 0
dbs_period = 0

dbs_odSTNGPe_weight = 0
dbs_odSTNGPi_weight = 0
dbs_odGPeSTN_weight = 0
dbs_odGPiTha_weight = 0

dbs_odSTNGPe_delay = 0
dbs_odSTNGPi_delay = 0
dbs_odGPeSTN_delay = 0
dbs_odGPiTha_delay = 0





//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

proc allocCells() {local i


pnm.ncell = ncell

pnm.round_robin()  // may want to change this due to patterned gpe-gpe connections

for i=cellID(ID_STN,0),cellID(ID_STN, numSTN-1) {
	pnm.create_cell(i, "new pSTN(numCtxSTN, numGPeSTN)")
}

for i=cellID(ID_GPe,0),cellID(ID_GPe, numGPe-1) {
	pnm.create_cell(i, "new pGPe(numStrGPe, numSTNGPe, numGPeGPe)")
}

for i=cellID(ID_GPi,0),cellID(ID_GPi, numGPi-1) {
	pnm.create_cell(i, "new pGPi(numStrGPi, numSTNGPi, numGPeGPi)")
}


}



//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

func readParms() {local i localobj f
//Setup global parameters from values stored in parameter file, name passed as $s1


f = new File()
f.ropen($s1)

// Counts

sav1 = numSTN
sav2 = numGPe
sav3 = numGPi

numSTN = f.scanvar()
numGPe = f.scanvar()
numGPi = f.scanvar()
ncell = numSTN + numGPe + numGPi

numSTNGPe = f.scanvar()		// each GPe contacted by numSTNGPe presyn STN cells, etc...
numGPeGPe = f.scanvar()
numGPeSTN = f.scanvar()
numGPeGPi = f.scanvar()
numSTNGPi = f.scanvar()
numCtxSTN = f.scanvar()
numStrGPe = f.scanvar()
numStrGPi = f.scanvar()
numGPiTha = f.scanvar()

divSTNGPe = f.scanvar()		// each STN contacts divSTNGPe postsyn GPe cells, etc...
divGPeGPe = f.scanvar()
divGPeSTN = f.scanvar()
divGPeGPi = f.scanvar()
divSTNGPi = f.scanvar()
divGPiTha = f.scanvar()


//-----------------------------------------------------------------------------------------------------
// bias currents

iBiasSTN = f.scanvar()
iBiasGPe = f.scanvar()
iBiasGPi = f.scanvar()



//-----------------------------------------------------------------------------------------------------
// cell to cell connection parameters

gmaxSTNGPe = f.scanvar()
gmaxGPeGPe = f.scanvar()
gmaxGPeSTN = f.scanvar()
gmaxGPeGPi = f.scanvar()
gmaxSTNGPi = f.scanvar()
gmaxGPiTha = f.scanvar()

gDA_STNGPe = f.scanvar()
gDA_GPeGPe = f.scanvar()
gDA_GPeSTN = f.scanvar()
gDA_GPeGPi = f.scanvar()
gDA_STNGPi = f.scanvar()
gDA_GPiTha = f.scanvar()

// THESE NEED TO BE SELECTED BASED ON EXPERIMENTAL/ANATOMICAL DATA ***********************************
delSTNGPe = f.scanvar()
delGPeGPe = f.scanvar()
delGPeSTN = f.scanvar()
delGPeGPi = f.scanvar()
delSTNGPi = f.scanvar()
delGPiTha = f.scanvar()



//-----------------------------------------------------------------------------------------------------
// higher level input connection parameters

imnCtxSTN = f.scanvar()
isdCtxSTN = f.scanvar()
bmnCtxSTN = f.scanvar()
bsdCtxSTN = f.scanvar()
bdmnCtxSTN = f.scanvar()
bdsdCtxSTN = f.scanvar()

imnStrGPe = f.scanvar()
isdStrGPe = f.scanvar()
bmnStrGPe = f.scanvar()
bsdStrGPe = f.scanvar()
bdmnStrGPe = f.scanvar()
bdsdStrGPe = f.scanvar()

imnStrGPi = f.scanvar()
isdStrGPi = f.scanvar()
bmnStrGPi = f.scanvar()
bsdStrGPi = f.scanvar()
bdmnStrGPi = f.scanvar()
bdsdStrGPi = f.scanvar()

probCtxSTN.resize(numCtxSTN)
for i=0,6 {
	probCtxSTN.x(i) = f.scanvar()
}

probStrGPe.resize(numStrGPe)
for i=0,6 {
	probStrGPe.x(i) = f.scanvar()
}

probStrGPi.resize(numStrGPi)
for i=0,6 {
	probStrGPi.x(i) = f.scanvar()
}

gmaxCtxSTN = f.scanvar()
gmaxStrGPe = f.scanvar()
gmaxStrGPi = f.scanvar()

gDA_CtxSTN = f.scanvar()
gDA_StrGPe = f.scanvar()
gDA_StrGPi = f.scanvar()

delCtxSTN = f.scanvar()
delStrGPe = f.scanvar()
delStrGPi = f.scanvar()




//-----------------------------------------------------------------------------------------------------
// DBS parameters

dbs_del = f.scanvar()
dbs_dur = f.scanvar()
dbs_direct_amp = f.scanvar()
dbs_direct_pw = f.scanvar()
dbs_ad_amp = f.scanvar()
dbs_ad_pw = f.scanvar()
dbs_period = f.scanvar()

dbs_odSTNGPe_weight = gmaxSTNGPe
dbs_odSTNGPi_weight = gmaxSTNGPi
dbs_odGPeSTN_weight = gmaxGPeSTN
dbs_odGPiTha_weight = 1

dbs_odSTNGPe_delay = delSTNGPe/2
dbs_odSTNGPi_delay = delSTNGPi/2
dbs_odGPeSTN_delay = delGPeSTN/2
dbs_odGPiTha_delay = delGPiTha/2



f.close()



if (numSTN != sav1 || numGPe != sav2 || numGPi != sav3) {
	return 1	//number of cells (and cellIds) have changed
} else {
	return 0
}


}




//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
proc writeParms() {local i localobj f
//Save current global parameter values in parameter file, name passed as $s1

f = new File()
f.wopen($s1)

// Counts

f.printf("%f\n", numSTN)
f.printf("%f\n", numGPe)
f.printf("%f\n", numGPi)
 
f.printf("%f\n", numSTNGPe)		// each GPe contacted by numSTNGPe presyn STN cells, etc...
f.printf("%f\n", numGPeGPe)
f.printf("%f\n", numGPeSTN)
f.printf("%f\n", numGPeGPi)
f.printf("%f\n", numSTNGPi)
f.printf("%f\n", numCtxSTN)
f.printf("%f\n", numStrGPe)
f.printf("%f\n", numStrGPi)
f.printf("%f\n", numGPiTha)

f.printf("%f\n", divSTNGPe)		// each STN contacts divSTNGPe postsyn GPe cells, etc...
f.printf("%f\n", divGPeGPe)
f.printf("%f\n", divGPeSTN)
f.printf("%f\n", divGPeGPi)
f.printf("%f\n", divSTNGPi)
f.printf("%f\n", divGPiTha)


//-----------------------------------------------------------------------------------------------------
// bias currents

f.printf("%f\n", iBiasSTN)
f.printf("%f\n", iBiasGPe)
f.printf("%f\n", iBiasGPi)



//-----------------------------------------------------------------------------------------------------
// cell to cell connection parameters

f.printf("%f\n", gmaxSTNGPe)
f.printf("%f\n", gmaxGPeGPe)
f.printf("%f\n", gmaxGPeSTN)
f.printf("%f\n", gmaxGPeGPi)
f.printf("%f\n", gmaxSTNGPi)
f.printf("%f\n", gmaxGPiTha)

f.printf("%f\n", gDA_STNGPe)
f.printf("%f\n", gDA_GPeGPe)
f.printf("%f\n", gDA_GPeSTN)
f.printf("%f\n", gDA_GPeGPi)
f.printf("%f\n", gDA_STNGPi)
f.printf("%f\n", gDA_GPiTha)

// THESE NEED TO BE SELECTED BASED ON EXPERIMENTAL/ANATOMICAL DATA ***********************************
f.printf("%f\n", delSTNGPe)
f.printf("%f\n", delGPeGPe)
f.printf("%f\n", delGPeSTN)
f.printf("%f\n", delGPeGPi)
f.printf("%f\n", delSTNGPi)
f.printf("%f\n", delGPiTha)



//-----------------------------------------------------------------------------------------------------
// higher level input connection parameters

f.printf("%f\n", imnCtxSTN)
f.printf("%f\n", isdCtxSTN)
f.printf("%f\n", bmnCtxSTN)
f.printf("%f\n", bsdCtxSTN)
f.printf("%f\n", bdmnCtxSTN)
f.printf("%f\n", bdsdCtxSTN)

f.printf("%f\n", imnStrGPe)
f.printf("%f\n", isdStrGPe)
f.printf("%f\n", bmnStrGPe)
f.printf("%f\n", bsdStrGPe)
f.printf("%f\n", bdmnStrGPe)
f.printf("%f\n", bdsdStrGPe)

f.printf("%f\n", imnStrGPi)
f.printf("%f\n", isdStrGPi)
f.printf("%f\n", bmnStrGPi)
f.printf("%f\n", bsdStrGPi)
f.printf("%f\n", bdmnStrGPi)
f.printf("%f\n", bdsdStrGPi)

for i=0,6 {
	f.printf("%f\n", probCtxSTN.x(i))
}

for i=0,6 {
	f.printf("%f\n", probStrGPe.x(i))
}

for i=0,6 {
	f.printf("%f\n", probStrGPi.x(i))
}

f.printf("%f\n", gmaxCtxSTN)
f.printf("%f\n", gmaxStrGPe)
f.printf("%f\n", gmaxStrGPi)

f.printf("%f\n", gDA_CtxSTN)
f.printf("%f\n", gDA_StrGPe)
f.printf("%f\n", gDA_StrGPi)

// THESE NEED TO BE SELECTED BASED ON EXPERIMENTAL/ANATOMICAL DATA ***********************************
f.printf("%f\n", delCtxSTN)
f.printf("%f\n", delStrGPe)
f.printf("%f\n", delStrGPi)




//-----------------------------------------------------------------------------------------------------
// DBS parameters

f.printf("%f\n", dbs_del)
f.printf("%f\n", dbs_dur)
f.printf("%f\n", dbs_direct_amp)
f.printf("%f\n", dbs_direct_pw)
f.printf("%f\n", dbs_ad_amp)
f.printf("%f\n", dbs_ad_pw)
f.printf("%f\n", dbs_period)



f.close()

}