// For each band of gaba synapses, this function makes a file with the
// stimulation train and shifts the activation time (by temporal_offset)  
// written by Yiota Poirazi, July 2001, poirazi@LNC.usc.edu

double iary[1000]
objref vary[1000], s_file, stimtmp, rpid
stimtmp = new Vector()
strdef shiftsyn_filename, syscmd, estr

proc gaba_shift() { local synapses, temporal_offset, hertz, gmax_default, PID, lo, hi

synapses = $1        // number of synapses in the band
tstop = $2           // stimulation end time
dt = $3   
hertz = $4           // stimulation train frequency
synch = $5           // synchronous or a synchronous stimulation of synapses
perio = $6           // periodic or not periodic stimulation
PID = $7
PID = abs(PID)       // only positive random seeds used for spike train generation 
temporal_offset = $8 // shift stimulation time by temporal_offset

// Make the desired activation pattern file and
// Set GABA pointers to activation pattern

   sprint(shiftsyn_filename,"gaba_shiftsyn-%d-%.2f-%.2f-%.2f-%.2f-%.2f-%d-%.2f",\
         synapses,tstop,dt,hertz,synch,perio,PID,temporal_offset)   // filename
   
   sprint(syscmd,"newshiftsyn gaba_shiftsyn %d %g %g %g %g %g %d %g",\
         synapses,tstop,dt,hertz,synch,perio,PID,temporal_offset)    // use newshiftsyn to make
								     // stimulation train	
   system(syscmd)  // Same as typing syscmd in DOS, execute above command
   print  syscmd

  s_file = new File()
  s_file.ropen(shiftsyn_filename) // open the stimulation train file

  stimtmp.scanf(s_file)           // read temporal stimulation pattern in a 1-d vector
  s_file.close()

  items = int(tstop/dt)           // number of stimuli (firing patterns) for each synapse

  for i=0,synapses-1 {
  //  printf(" -- synapse %d\n", i)

    vary[i] = new Vector(items)  // for each synapse, save firing pattern in this vector

  // Put the firing patterns from stimtmp (1-d vector) to a 2-d vector for each synapse i
 
    for j=0,items-1 {   
        vary[i].x[j]=stimtmp.x[i + j*synapses] 
      
     }
  }

//  print "setpointer to each synapse"

    for s=0, synapses-1 {
        sprint(estr,"vary[%d].play(&iary[%d])", s,s)
        execute1(estr)
//        print estr

      	sprint(estr,"setpointer gabaa[%d].pre, iary[%d]", s,s)
        execute1(estr)
//        print estr

        sprint(estr,"setpointer gabab[%d].pre, iary[%d]", s,s)
        execute1(estr)
//        print estr
       
    }

sprint(econ.syscmd,  "rm %s", shiftsyn_filename) // remove firing pattern file
print  econ.syscmd
system(econ.syscmd)

sprint(econ.syscmd,  "rm ERROR*")                // if an error file is generated, remove it as well
print  econ.syscmd                               // this is because newshiftsyn gives error messages
system(econ.syscmd)                              // that don't affect the firing patterns generated
 						 // if errors do happen, NEURON crashes	
}