//genesis (R.M. 4/9/2007)
// copied from Golgi cell in granular network

include defaults 

/* 
The function "make_Purk_cell_array" creates {length} Purkinje cells (PCs),
named /Purkinje_layer/Purkinje_cell [0] 
   to /Purkinje_layer/Purkinje_cell [{length} - 1].
Each PC is a copy of the PC described in psmall.p.
A spikegen object is added to the soma.

*/


include ../Purkinje_cell/Purk_const
include ../Purkinje_cell/Purk_chanload   // chansave // chanload
// shifted NaF
// include ../Purkinje_cell/Purk_chanloadNaFshift.g
include ../Purkinje_cell/Purk_cicomp
include ../Purkinje_cell/Purk_syn37dC



function make_Purkinje_cell_array (length_PFaxis, length_sagitt, separation)

   int length_PFaxis, length_sagitt

   float separation
   str cellpath = "/Purkinje"

   int i, j

   if (! {exists /library})
      create neutral /library
   end
   ce /library

// Make the prototypes of channels and compartments that can be invoked in .p files
   make_Purkinje_chans
   make_Purkinje_syns
   make_Purkinje_comps
   make_Purkinje_spines

   if (!{exists /Purkinje_layer})
          create neutral /Purkinje_layer
   end

// read cell data from .p file
//   readcell ../Purkinje_cell/psmall.p {cellpath}
   readcell ../Purkinje_cell/psmall+axon.p {cellpath}

// rotate the cell so that PF-axis becomes x-axis
//   rotcoord   {cellpath}  {-3.1415927/2} -z
// There is a bug in rotcoord: compartments and their children are rotated,
// spines are rotated too, but spike heads and par channels are NOT,
// so let us do it brute force (each element separately).

// rotcoord /Purkinje/##[CLASS=segment] {-3.1415927/2} -z  -fixkids
 rotcoord /Purkinje/##[CLASS=segment] {3.1415927/2} -z  -fixkids

// add a spikegen object
   create spikegen {cellpath}/soma/spike
   setfield {cellpath}/soma/spike thresh -0.02 \
                                  abs_refract 0.001 \
                                  output_amp 1
   addmsg {cellpath}/soma {cellpath}/soma/spike INPUT Vm

// add a spikegen object to axon:  not allowed by hines solver ??????
/*
   int index
   for (index=0; index < 20; index = index+1)

   create spikegen {cellpath}/axon[{index}]/spike
   setfield {cellpath}/axon[{index}]/spike thresh -0.02 \
                                  abs_refract 0.001 \
                                  output_amp 1
   addmsg {cellpath}/axon[{index}] {cellpath}/axon[{index}]/spike INPUT Vm
   end
*/

// add a diffamp for gap junction
//   create diffamp /library/diffamp_dend
//   createmap /library/diffamp_dend  {cellpath} 5 1


//   createmap {cellpath} /Purkinje_layer \
//             {length} 1 -delta {Purkinje_cell_separation} 0.0 -origin 0.0 0.0

  
   createmap {cellpath} /Purkinje_layer \
             {length_PFaxis} {length_sagitt} \ 
             -delta {separation} {separation * {sqrt {3}} * 0.5} \
             -origin 0.0 0.0

   for (i = 0; i < {length_sagitt}; i = {i + 1})
   for (j = 0; j < {length_PFaxis}; j = {j + 1})
   position /Purkinje_layer/Purkinje[{i*length_PFaxis + j}] \
      {{getfield /Purkinje_layer/Purkinje[{i*length_PFaxis + j}] x} + {separation * 0.5}} \
      {getfield /Purkinje_layer/Purkinje[{i*length_PFaxis + j}] y} \
      {getfield /Purkinje_layer/Purkinje[{i*length_PFaxis + j}] z}
   end
   end

   disable {cellpath}


end