// For each band of gaba background 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_bg[1000]
objref vary_bg[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_bg[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_bg[i].x[j]=stimtmp.x[i + j*synapses] 
      
     }
  }

//  print "setpointer to each synapse"

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

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

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

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

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