//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//
// NOTICE OF COPYRIGHT AND OWNERSHIP OF SOFTWARE
//
// Copyright 2010, The University Of Michigan
// 	
//   All rights reserved.
//   For research use only; commercial use prohibited.
//   No Distribution without permission of William Stacey
//   wstacey@umich.edu
//
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

// Definition of the synaptic properties in the network

begintemplate SynParam

public preCell, postCell, synName, tao1, tao2, Erev, synLocSec, synLoc, Npre, gmax, gmaxUnits, delay, synID, globalID, modFileName, r

strdef preCell, postCell, synName, modFileName

proc init() {
	
	preCell     = $s1 // Name of the presynaptic cell, e.g. Pyr
	postCell    = $s2 // Name of the postsynaptic cell, e.g Pyr
	synName     = $s3 // Usually the type of the synamse eg. AMPA
	tao1        = $4 
	tao2        = $5
	Erev        = $6
	modFileName = $s7 // name of the used mod file, it is used in the syn.tmp
	synLocSec   = $8  // ID 0->soma, 1->dendrite
	synLoc      = $9  // [0-1]
	Npre        = $10 // Number of the presynaptic connections from Pyr
	gmax        = $11 // gmax for connections from Pyr
	gmaxUnits   = $12 // gmax for connections from Pyr
	delay       = $13 // delay for connections from Pyr
	r           = $14 // NMDA/AMPA ratio
	synID       = -2 //$14 // synaptic ID used to define the connections in the conn.dat files
	globalID    = $15  // Global ID to identify the entity in SynParamSet

}

endtemplate SynParam


// This object keeps the description of synapses in the public list "synSet"


begintemplate SynParamSet

public synSet
objref synSet
strdef preCell, postCell, synName, tmpstr, tmpstr2, modFileName

proc init() { local i localobj fo, strFun
	tao1      = 0
	tao2      = 0
	Erev      = 0
	synLocSec = 0
	synLoc    = 0
	Npre      = 0
	gmax      = 0
	gmaxUnits = 0
	delay     = 0
	r         = 0
	
	synSet   = new List()
	strFun   = new StringFunctions()
	
	// READ synapses
	fo = new File("parameters/synapses.par")
	fo.ropen()
	while(!fo.eof()) {
	
		fo.gets(tmpstr)
		
		// Find in tmpstr all that follows non-blank character, and store it in tmpstr2
		strFun.tail(tmpstr, "[^\t]", tmpstr2)
		
		// Remove end of the line
		strFun.head(tmpstr2, "\n", tmpstr2)
		
		// Process data if nonepty line
		if (strFun.len(tmpstr2)>0 && strFun.substr(tmpstr, "//")==-1) {
		
			sscanf(tmpstr, "%[^,], %[^,], %[^,], %lf, %lf, %lf, %[^,], %d, %lf, %d, %lf, %d, %*lf, %*lf\n", preCell, postCell, synName, &tao1, &tao2, &Erev, modFileName, &synLocSec, &synLoc, &Npre, &gmax, &gmaxUnits)
			sscanf(tmpstr, "%*[^,], %*[^,], %*[^,], %*lf, %*lf, %*lf, %*[^,], %*d, %*lf, %*d, %*lf, %*d, %lf\n, %lf", &delay, &r)
			
			synSet.append(new SynParam( preCell, postCell, synName, tao1, tao2, Erev, modFileName, synLocSec, synLoc, Npre, gmax, gmaxUnits, delay, r, synSet.count()))
			//printf("%s, %s, %s, %lf, %lf, %lf, %s, %d, %lf, %d, %lf, %d, %lf\n", preCell, postCell, synName, tao1, tao2, Erev, modFileName, synLocSec, synLoc, Npre, gmax, gmaxUnits, delay, synSet.count())
		}
	}
	
	fo.close()
}

endtemplate SynParamSet