//Modified by Jose Gomez jfcgomez@ull.es: (1-Ca_blocker)

/* ---------------------   PROCEEDURES USED IN CELL SETUP  -------------------------*/

/*  To make the distal membrane less conductive, vis-a-vis Stuart G.
and Spruston N., J. Neuroscience 18(10) 3501-3510, 1998, we deceay Rm
from proximal to distal sigmoidally
*/

proc Rm_sigmoid() { local rm
    $o1.defvar("channel:pas","Rm_soma", "Rm_default", "")
//     $o1.defvar("channel:pas","Rm_end",  "5e3", "")
    $o1.defvar("channel:pas","Rm_end",  "12e3", "")
    $o1.defvar("channel:pas","dhalf",   "200",   "")
    $o1.defvar("channel:pas","steep",   "50",   "")

    for (x) {
      xdist = find_vector_distance_precise(secname(),x)    // calc. perpedicular distance
      rm = Rm_soma + (Rm_end - Rm_soma)/(1.0 + exp((dhalf-xdist)/steep))
      g_pas(x) = 1.0/rm
    }

}

/* Changing Ra sigmoidally along the apical trunk (obsolete in this case)*/

proc Ra_sigmoid() {
    $o1.defvar("channel:pas","Ra_soma", "Ra_default", "")
//     $o1.defvar("channel:pas","Ra_end",  "Ra_default", "")
    $o1.defvar("channel:pas","Ra_end",  "35", "")
    $o1.defvar("channel:pas","dhalf",   "210",   "")
    $o1.defvar("channel:pas","steep",   "50",   "")

    for (x) {
      xdist = find_vector_distance_precise(secname(),x)  //calc. perpedicular distance
      Ra = Ra_soma + (Ra_end - Ra_soma)/(1.0 + exp((dhalf-xdist)/steep))
    }
}

/* To make the distal trunk h-current conductance, g_h, about 7
times higher (at 300 um) than the somatic value vis-a-vis Magee
J., J. of Neuroscience 18(19) 7613-7624, 1998, we vary I_h
conductance sigmoidally along the apical trunk.
*/

proc apical_h_insert_sig() {
    $o1.defvar("channel:h","gh_soma", "soma_hbar", "")
    $o1.defvar("channel:h","gh_end",  "soma_hbar*9", "")
    $o1.defvar("channel:h","dhalf",   "280",   "")
    $o1.defvar("channel:h","steep",   "50",   "")

    for (x) {
      xdist = find_vector_distance_precise(secname(),x)  //calc. perpedicular distance
      insert h
      gbar_h(x) = gh_soma + (gh_end - gh_soma)/(1.0 + exp((dhalf-xdist)/steep))

      }
}

/* Inserting proximal (kap) and distal(kad) A-type channels
along the apical trunk. Proximal I_A is distributed in a fixed
conductance over the first 100 um from the cell
body. Distal-type I_A is distributed in a linearly increasing
manner for distances 100 < xdist < 350. For xdist < 100,
g_A_distal(x) = 0 and for xdist > 350 g_A_distal(x) = constant =
g_A_distal(350)
*/

proc A_insert() {
    $o1.defvar("channel:kap","kap_distal_maxfactor", "1", "maximum cond. factor in dendrites")
//     $o1.defvar("channel:kap","kap_distal_distance", "100", "distance in dendrites for maximum cond.")
    $o1.defvar("channel:kap","kap_distal_distance", "200", "distance in dendrites for maximum cond.")
    $o1.defvar("channel:kad","kad_distal_maxfactor", "6.5", "maximum cond. factor in dendrites")
    $o1.defvar("channel:kad","kad_distal_distance", "350", "distance in dendrites for maximum cond.")


    for (x) {
      xdist=find_vector_distance_precise(secname(),x)
      fr= xdist/kad_distal_distance
      insert kap
      insert kad
      insert kdBG

      if (xdist < kap_distal_distance ) {
         gkabar_kad(x) = 0
         gkabar_kap(x) = soma_kap
         gbar_kdBG(x) = soma_kdBG

      } else if (xdist < kad_distal_distance ) {
         gkabar_kap(x) = 0
         gkabar_kad(x) = kad_distal_maxfactor*kad_init*fr
        gbar_kdBG(x) = kdBG_init

      } else {
         gkabar_kap(x) = 0
         gkabar_kad(x) = kad_distal_maxfactor*kad_init
         gbar_kdBG(x) = kdBG_init
      }
    }
}


/* Inserting m-type potassium current with a fixed conductance along the apical trunk */

proc apical_km_insert() {
    $o1.defvar("channel:km","km_distal_maxfactor", "1", "maximum cond. factor in dendrites")
    $o1.defvar("channel:km","km_distal_distance", "350", "distance in dendrites for maximum cond.")

    for (x) {
      xdist = find_vector_distance_precise(secname(),x)
      fr = xdist/km_distal_distance
      insert km
      gbar_km(x)=soma_km
    }
}


/* Inserting K(Ca++)-type channels and calsium pumps along the
apical trunk with maximum conductances in 50 < xdist < 200
*/

proc apical_kca_insert() {
    $o1.defvar("channel:kca","kca_distal_maxfactor", "1", "maximum cond. factor in dendrites")
    $o1.defvar("channel:kca","kca_distal_distance", "100", "distance in dendrites for maximum cond.")
    nseg = 5
    for (x) {
      xdist = find_vector_distance_precise(secname(),x)
      fr = xdist/kca_distal_distance
      insert cad    // calsium pump/buffering mechanism
      insert kca    // slow AHP K++ current
      insert mykca  // medium AHP K++ current
     // insert kca2

       if (xdist < kca_distal_distance) {

       gkbar_mykca(x) = mykca_init/30
       gbar_kca(x) = soma_kca/30
       //gbar_kca2(x) = 0

       if (x == 0.3) {
             gbar_kca(x) = 5*soma_kca
             gkbar_mykca(x) = mykca_init/30
        }
       if (x == 0.7) {
             gkbar_mykca(x) = 5*mykca_init*(kca_distal_distance - xdist)/kca_distal_distance
             gbar_kca(x) = soma_kca/30
        }


//          gkbar_mykca(x) = 5*mykca_init*(kca_distal_distance - xdist)/kca_distal_distance
//          gbar_kca(x) = 5*soma_kca
//          gbar_kca2(x) = 0

       } else {
         //  gbar_kca2(x) = 0*soma_kca
           gbar_kca(x) = 0*soma_kca/30
           gkbar_mykca(x) = mykca_init
      }

    }
}


/* Inserting LVA Ca++ T-type channels along the apical trunk in
a linearly increasing manner, for xdist > 100 um
*/

proc apical_caT_insert() {
    $o1.defvar("channel:cat","caT_distal_maxfactor", "4", "maximum cond. factor in dendrites")
    $o1.defvar("channel:cat","caT_distal_distance", "350", "distance in dendrites for maximum cond.")

    for (x) {
       xdist = find_vector_distance_precise(secname(),x)
       fr = xdist/caT_distal_distance
       insert cat
       if (xdist < 100) {
          gcatbar_cat(x) = 0
       } else {
          gcatbar_cat(x) = caT_distal_maxfactor*soma_caT*fr
       }
   }
}


/* Inserting HVAm Ca++ R-type and HVA L-type channesls along
the apical trunk. The R-type current is distributed in a fixed
conductance while the L-type current is distributed in a
maximum fixed conductance for distances xdist > 50 um and in a very
small conductance for xdist < 50 um
*/

proc apical_caR_caLH_insert() {
    for (x) {
        xdist = find_vector_distance_precise(secname(),x)
        insert car
        gcabar_car(x) = soma_car

        insert calH
        if (xdist > 50) {
           gcalbar_calH(x) = 4.6*soma_caLH
        } else {
           gcalbar_calH(x) = 0.1*soma_caLH
        }
    }
}

proc apical_caL_caN_insert() {
    $o1.defvar("channel:cal","caL_distal_distance", "150", "distance in dendrites for maximum cond.")
    nseg = 5
    for (x) {
        xdist = find_vector_distance_precise(secname(),x)
        fr = xdist/caL_distal_distance
        insert cal
        insert can
        insert cad

        if (xdist < caL_distal_distance) {

           gcalbar_can(x) = soma_caN/30
           gcalbar_cal(x) = soma_caL/30

        if (x == 0.3) {
             gcalbar_cal(x) = soma_caL//*(1-2.*fr/3.)
             gcalbar_can(x) = soma_caN/30
        }
        if (x ==0.7) {
           gcalbar_can(x) = soma_caN//*(1-2.*fr/3.)
           gcalbar_cal(x) = soma_caL/30
       }

       //  gcalbar_can(x) = soma_caN*(1-2.*fr/3.)
       //  gcalbar_cal(x) = soma_caL*(1-2.*fr/3.)

       } else {
//         gcalbar_cal(x) = soma_caL/6.
 //        gcalbar_can(x) = soma_caN/4.
          gcalbar_can(x) = soma_caN
          gcalbar_cal(x) = soma_caL/10

        }
    }
}

/* Seting conductances in all apical oblique dendrites so that the values of all dedrites after an initial section
are the same (or a multiple) as the values in apical_dendrite[46]. The values in the initial section of 50 um from
the parent trunk are set equal to the parent trunk conductances. For dendrites located beyond  300 (or/and 350) um,
we increase the Na+-persistent current, the A current, the Ca++ and K(Ca++) conductances and reduce the spike
attenuation coefficent. */

strdef trunk_temp
strdef khsection
proc khoblique_peri_decay() { local i,x,d



forsec "trunk"{	
     
       xdist = find_vector_distance_precise(secname(),0)
       if ((xdist>100)&&(xdist<=150)) {
       	  trunk_temp=secname()
	  print trunk_temp, "xdist=", xdist
          break
       }

}

//$o1.defvar("channel:obliques", "khsection", "\"trunk[6]\"", "Trunk section used for oblique conductance values")

$o1.defvar("channel:obliques", "khsection", "trunk_temp", "Trunk section used for oblique conductance values")

$o1.defvar("morphology:apical-non-trunk", "peri_trunkl", "50.0", "Length of the peri-trunk region")


sprint(trunk_temp,"access %s",khsection)
execute1(trunk_temp)
//psection()


// Holding the conductance values from apical_dendrite[46]

sprint($o1.tmp_str,"%s { hold_cat=gcatbar_cat(1) hold_car=soma_car hold_calH=soma_caLH hold_nap=0.0004*gnabar_hha_old }", khsection)
execute1($o1.tmp_str)
sprint($o1.tmp_str,"%s { hold_h=gbar_h(1) hold_ar2_hha_old=ar2_hha_old(1) hold_kdr=gkbar_hha_old(1) }", khsection)
execute1($o1.tmp_str)
sprint($o1.tmp_str,"%s { hold_g_pas=g_pas(1) hold_Ra=Ra hold_kap=gkabar_kap(1) hold_kad=gkabar_kad(1) }", khsection)
execute1($o1.tmp_str)
//sprint($o1.tmp_str,"%s { hold_mykca=gkbar_mykca(1) hold_kca2=gbar_kca2(1) hold_km=gbar_km(1) }", khsection)
sprint($o1.tmp_str,"%s { hold_kdBG = gbar_kdBG(1)  hold_mykca=gkbar_mykca(1)  hold_km=gbar_km(1) }", khsection)
execute1($o1.tmp_str)

    for i=0,plcount {
   //print "jo"
       // set the origin to the currently accessed section
       access opl[i].trunk_section.sec
       xdist = find_vector_distance_precise(secname(),0)
       distance(0,1)

       trunk_kap = gkabar_kap(1)  // holding the parent trunk values
       trunk_kad = gkabar_kad(1)
       trunk_kdBG =  gbar_kdBG(1)
       trunk_h  = gbar_h(1)
       trunk_pas = g_pas(1)
       trunk_Ra = Ra
       trunk_car  = gcabar_car(1)
       trunk_calH  = gcalbar_calH(1)
       trunk_cat  = gcatbar_cat(1)
       //trunk_kca2  = gbar_kca2(1)
       trunk_mykca  = gkbar_mykca(1)
       trunk_km  = gbar_km(1)
       trunk_nap  = 0.2*hold_nap // No persistent I_Na at the trunk => hold a small persent of hold_nap value
       trunk_ar2_hha_old  = ar2_hha_old(1) // spike attenuation variable

       sec_count=0

      forsec pl[i] {
        //  printf("\t-- %s --\n", secname()) //access all oblique paths from parent trunk to root oblique

          if (!sec_count) {              // skip all trunk sections
              sec_count=sec_count+1
              continue
           }
               insert kap
               insert kad
               insert kdBG
               insert h
               insert pas
               insert car
               insert calH
               insert cat
               //insert kca2
               insert mykca
               insert km
               insert nap
               insert cad

               e_pas = v_init

               for (x) {
                 if (x > 0 && x < 1) {
                    d = distance(1,x)
                   if (d < peri_trunkl) {   // for distances close to the parent trunk section keep trunk values
                       Ra = Ra_default
                       gkabar_kap(x) = trunk_kap
                       gkabar_kad(x) = trunk_kad
                       gbar_kdBG(x) = trunk_kdBG
                       gbar_h(x) = trunk_h
                       g_pas(x) = trunk_pas
                       gcabar_car(x) = trunk_car
                       gcalbar_calH(x) = trunk_calH
                       gcatbar_cat(x) = trunk_cat
                       //gbar_kca2(x) = trunk_kca2
                       gkbar_mykca = mykca_init
                       gbar_km(x) = trunk_km
                       gnabar_nap(x) = trunk_nap
                       ar2_hha_old(x) = trunk_ar2_hha_old

                   } else {          // for further distances set conductances to apical_dendrite[46] values (or a multiple)
                       gkabar_kap(x) = hold_kap
                       gkabar_kad(x) = 1.25*hold_kad
                       gbar_kdBG(x) = hold_kdBG
                       gbar_h(x) = hold_h
                       g_pas(x) = hold_g_pas
                       //g_pas(x) = 10*hold_g_pas

                       Ra = Ra_default
                       gcabar_car(x) = hold_car
                       gcalbar_calH(x) = hold_calH
                       gcatbar_cat(x) = hold_cat
//******                        ar2_hha_old(x) = 0.8*hold_ar2_hha_old  // set to 80% of dend. 46 value
                       ar2_hha_old(x) =  1 //hold_ar2_hha_old  // set to 80% of dend. 46 value
                       //gbar_kca2(x) = hold_kca2
                       gkbar_mykca = mykca_init
                       gnabar_nap(x) = hold_nap
                       gbar_km(x) = hold_km             // set to 2 times the dend 46 value

                       if (xdist > 300 ) {                // for xdist > 300 um increase:
                          gkabar_kad(x) = 2.65*hold_kad      // A-current,
                          gcabar_car(x) = 13*hold_car       // Ca++-R current,  original
                          gcalbar_calH(x) = 13*hold_calH     // Ca++-L current,
                          gkbar_hha_old(x) = 1.23*hold_kdr // delayed rectifier 1.1
                          gkbar_mykca = 0.6*mykca_init
                          //gkbar_mykca = 0.35*mykca_init

                        }

                       if (xdist > 350) {               // for xdist > 350 um increase even more:
                          gcalbar_calH(x)=15*hold_calH  // Ca++-L current,
                          ar2_hha_old(x) = 0.9         // less spike attenuation
                          gnabar_nap(x)=3*hold_nap      // Na+ persistent

                       }
                    }
               }
           }

         sec_count=sec_count+1
       }
   }

}


/* Seting conductance values in all basal dendrites to be the
same as the values in apical_dendrite[14], except for the A
current conductance which is 0.6 times higher.
*/

proc khbasal_fixed() { local i,x,d
$o1.defvar("channel:basal", "khsection", "\"trunk[3]\"", "Trunk section used for basal conductance values")
sprint($o1.tmp_str,"%s { hold_g_pas=g_pas(1) hold_kap=gkabar_kap(1)  hold_kad=gkabar_kad(1) hold_h=gbar_h(1)}", khsection)
execute1($o1.tmp_str)
access trunk[3]

forsec basal_tree_list {

     insert kap
     insert kad
     insert h
     insert pas

     for (x) {
        gkabar_kap(x) = 1.6*hold_kap
        gkabar_kad(x) = 1.6*hold_kad
        gbar_h(x) = soma_hbar
        g_pas(x) =  hold_g_pas
        Ra = Ra_default
        e_pas = v_init
             }
 }
}

/* The Na channels developed Mel and modified by Brannon,
Poirazi (hha2 and hha_old) both reduce activation as function
of voltage. In other words, they show actvity-dependent
attenuation of conductance.  Within both of these mechanisms,
ar2 ([0..1]) is used to inversely describe the intensity of
voltage-dependent attenuation. 0 is maximum attenuation, 1 is
no attenuation.

Within the cell model, we vary the amount of attenuation along
the apical trunk as a function of distance from the cell body
such that proximal sections show little attenuation and distal
sections show comparably more (with the exception of distal
obliques).

We typically decay ar2 linearly from proximal to distal with
the maximum and minimum values of decay as
parameters. Initialize these parameters:
*/

max_ar2=0
min_ar2=0
decay_start=0 /* The distance at which decay starts.
             The distance at which decay ends.
             */
decay_end=0

strdef ar24_tmp_str
objref  strobj, ar24_f
strobj=new StringFunctions()
ar2_firsttime=1

proc ar2_log() {

 if (!ar2_firsttime) { return }

 ar24_f=new File()
 sprint($o3.tmp_str3, "%s/ar2_log", $o3.generic_dir)

 ar24_f.wopen($o3.tmp_str3)
 print "joseeeeeeee"
 ar24_f.printf("%s:",$s1)

 while (strobj.substr($s2, "*") > -1) {
   //printf("substr:%d\n", strobj.substr($s2, "*"))

     index=strobj.head($s2, "\\*", ar24_tmp_str)
   //printf("index:%d\n", index)

     strobj.right($s2, 1+index)
    //printf("%s ... %s \n", ar24_tmp_str, $s2)

     $o3.create_variable("ar24_val", ar24_tmp_str)
     ar24_f.printf("%s:%g:", ar24_tmp_str, ar24_val)
    //printf("%s:%g", ar24_tmp_str, ar24_val)
 }

 $o3.create_variable("ar24_val", $s2)
 ar24_f.printf("%s:%g\n", $s2, ar24_val)
 //printf("%s:%g\n", $s2, ar24_val)

 ar24_f.close()
 ar2_firsttime=0
}


/*_______ END OF PROCEEDURES ROUTINELY USED IN CELL SETUP______*/

//__________________________________________________________________________________________________________


/* ____________      CELL SET-UP PROCEEDURE      _____________ */

maximum_segment_length=75
strdef sectype
objref CAN_temp, CAL_temp, CAT_temp, KAD_temp, KAP_temp, NA_temp

proc cell_setup() {


 // Set passive membrane properties

   $o1.defvar("passive", "Rm_default", "200000","Specific membrane resistance. ")
   $o1.defvar("passive", "Rm_trunk", "Rm_default","Non-oblique dendritic specific membrane resistance.")
   $o1.defvar("passive", "Rm_non_trunk", "Rm_default","Apical oblique specific membrane resistance.")
   $o1.defvar("passive", "Rm_basal", "Rm_default","Basal specific membrane resistance.")
   $o1.defvar("passive", "Rm_tip", "Rm_default","Tip specific membrane resistance.")
   $o1.defvar("passive", "Rm_soma", "Rm_default", "Somatic specific membrane resistance.")
   $o1.defvar("passive", "Rm_axon", "Rm_default", "Axonal specific membrane resistance. ")
   $o1.defvar("passive", "Ra_default", "50","Specific axial resistance. ")
//    $o1.defvar("passive", "Ra_default", "35","Specific axial resistance. ")
   $o1.defvar("passive", "Ra_basal", "Ra_default","Basal specific axial resistance.")
   $o1.defvar("passive", "Ra_trunk", "Ra_default","Somatic specific axial resistance.")
   $o1.defvar("passive", "Ra_non_trunk","Ra_default","Somatic specific axial resistance.")
   $o1.defvar("passive", "Ra_soma", "Ra_default","Somatic specific axial resistance.")
   $o1.defvar("passive", "Ra_tip", "Ra_default","Apical tip specific axial resistance.")
   $o1.defvar("passive", "Ra_axon", "Ra_default","Axonal specific axial resistance. ")
   $o1.defvar("passive", "Cm_default", "1","Default specific capacitance.")
   $o1.defvar("passive", "Cm_axon", "Cm_default","Axonal specific capacitance. ")
   $o1.defvar("passive", "Cm_soma", "Cm_default","Somatic specific capacitance. ")
   $o1.defvar("passive", "Cm_trunk", "Cm_default","Trunk specific capacitance.")
   $o1.defvar("passive","Cm_non_trunk", "Cm_default","Oblique specific capacitance.")
   $o1.defvar("passive", "Cm_basal", "Cm_default","Basal specific capacitance.")
   $o1.defvar("passive", "Cm_tip", "Cm_default","Apical tip specific capacitance.")

   $o1.defvar("general", "v_init", "-66","Initial voltage of sections.")
   //$o1.defvar("general", "v_init", "-66","Initial voltage of sections.")


 // SEVERELY affects experiment results
 $o1.defvar("general", "celsius", "34","Temperature of slice.")

 // Set HH Sodium - Potassium properties

   $o1.defvar("channel:na", "gna_default", "0.007", "Default Na conductance.") //0.007
   $o1.defvar("channel:na", "gna_trunk", "gna_default", "Trunk Na conductance. ")
   $o1.defvar("channel:na", "trunk_non_trunk_ratio", "1.0", "Non-trunk Na conductance ratio")  //1.0
   $o1.defvar("channel:na", "gna_non_trunk", "trunk_non_trunk_ratio*gna_default", "Non-trunk Na conductance")
   $o1.defvar("channel:na", "gna_tip", "gna_non_trunk", "Oblique Tip Na conductance.")
   $o1.defvar("channel:na", "gna_basal", "gna_default", "Basal Na conductance.")
   $o1.defvar("channel:na", "gna_axon", "0.1", "Axonal Na conductance. ")
   $o1.defvar("channel:na", "gna_soma", "gna_default", "Somatic Na conductance. ")

// Set delayed rectifier properties as a percent of Na conductances

   //**** $o1.defvar("channel:na-kdr", "kdr_div", "10.0/1.24", "Default ratio of Na to kdr conductances")
  $o1.defvar("channel:na-kdr", "kdr_div", "10.0/2.0", "Default ratio of Na to kdr conductances") //1.4

//*****    $o1.defvar("channel:na-k", "kdr_div_soma", "10.0/2.0", " Soma Na-kdr divider")
   $o1.defvar("channel:na-k", "kdr_div_soma", "10.0/2.0", " Soma Na-kdr divider")
   $o1.defvar("channel:na-k", "kdr_div_axon", "kdr_div_soma", " Axon Na-kdr divider")
   $o1.defvar("channel:na-k", "kdr_div_trunk", "kdr_div"," Trunk Na-kdr divider")
   $o1.defvar("channel:na-k", "kdr_div_non_trunk", "kdr_div", "Non-trunk Na-kdr divider")
   $o1.defvar("channel:na-k", "kdr_div_tip", "kdr_div", "Oblique Tips Na-kdr divider")
   $o1.defvar("channel:na-k", "kdr_div_basal", "kdr_div", "Basal Na-kdr divider")

// Set delayed rectifie conductances

   $o1.defvar("channel:kdr", "gkdrbar_default", "gna_default/kdr_div", "Default KDR conductance. ")
   $o1.defvar("channel:kdr", "gkdrbar_axon", "gna_axon/kdr_div_axon",  "Axonal  KDR conductance. ")
   $o1.defvar("channel:kdr", "gkdrbar_basal", "gna_basal/kdr_div_basal","Basal  KDR conductance. ")
   $o1.defvar("channel:kdr", "gkdrbar_soma", "gna_soma/kdr_div_soma", "Somatic  KDR conductance. ")
   $o1.defvar("channel:kdr", "gkdrbar_trunk", "gna_trunk/kdr_div_trunk", "Trunk KDR conductance. ")
   $o1.defvar("channel:kdr", "gkdrbar_non_trunk", "gna_non_trunk/kdr_div_non_trunk", "Non-Trunk KDR conductance. ")
   $o1.defvar("channel:kdr", "gkdrbar_tip", "gna_tip/kdr_div_tip", "Oblique Tip KDR conductance. ")
   $o1.defvar("channel:kdr", "gkdrbar_basal", "gna_basal/kdr_div_basal", "Basal KDR conductance. ")

   $o1.xopen_library("Terrence","cut-sections")
   cut_sections(maximum_segment_length)

// make 3-d mapping of cell sections

   $o1.xopen_library("Terrence","map-segments-to-3d")
   map_segments_to_3d()

// prepare to make a graph with cell configuration
   $o1.tmpo2=new Shape()

// Set initial conductance values

   soma_caN = 0.5*0.0002    //*(1-Ca_blocker)          //Jose
   soma_caL =  1.2825*0.0003 //*(1-Ca_blocker)         //Jose
   soma_kca =  2.3*0.005
   soma_km = 10*0.00022
   mykca_init =  0.0003

   //soma_caL = 0.014
   soma_car = 0.88*0.0003  //*(1-Ca_blocker)   //Jose  //0.8* for -66 (also see kdr_hold from 1.1 -> 1.23)
   gsomacar = 0*0.003  //*(1-Ca_blocker)               //Jose
   soma_caLH =1.45*0.000333 //*(1-Ca_blocker)  //Jose
   soma_caT = 0.5*0.0001  //*(1-Ca_blocker)    //Jose
  // soma_kca = 0.0001
  // soma_km = 0.06
   //mykca_init = 1.1*0.015
   soma_hbar = 1.872e-5
   soma_kap = 0.0075
   kad_init = 1.06*0.0072 //1.06* for -66
   soma_kdBG = 3*0.00012 //3* for -66
   kdBG_init = 5.76*0.00035 //4.5* for -66

// Start inserting mechanisms in cell


     sectype ="soma"
     forsec "soma" {
           nseg = 5
           insert hha2    // HH mechanism with low threshold for Na spikes (-57 mV)
                   gnabar_hha2 = gna_soma    // JOSE put *0
                   gkbar_hha2  = gkdrbar_soma   //JOSE put *0


           insert pas    // leak conductance
                   g_pas =  1/Rm_soma
                   e_pas = v_init
                   Ra    = Ra_soma

           insert h     // h current
                  gbar_h  = soma_hbar
                  K_h     = 8.8
                  vhalf_h = -82

           insert kap  // proximal A current
                  gkabar_kap = soma_kap

           insert km  // m-type potassium current
                  gbar_km    = soma_km


           insert cal // HVAl Ca++-L type current
           insert can
           insert kca
           insert mykca
           insert cad


           //  gcalbar_cal(x) = soma_caL
           //  gbar_kca(x) = 5*soma_kca
           //  gcalbar_can(x) = soma_caN
           //  gkbar_mykca(x) = 4*mykca_init


           for (x) {

           gcalbar_can = soma_caN/30
           gkbar_mykca = mykca_init/30
           gcalbar_cal = soma_caL/30
           gbar_kca = soma_kca/30


             if (x ==0.3) {

                  gcalbar_cal(x) = soma_caL
                  gbar_kca(x) = 5*soma_kca
                  gcalbar_can(x) = soma_caN/30
                  gkbar_mykca(x) = mykca_init/30

             }

             if (x ==0.7) {

                  gcalbar_can(x) = soma_caN
                  gkbar_mykca(x) = 4*mykca_init
                  gcalbar_cal(x) = soma_caL/30
                  gbar_kca(x) = soma_kca/30
             }

          }


           insert cat // LVA Ca++-T type current
                  gcatbar_cat = soma_caT/2

           insert somacar // HVAm Ca++-R type current
                  gcabar_somacar = 0.1*gsomacar


           //work in progress!!!

           insert ican
                  gbar_ican  = 0*0.5

           $o1.tmpo2.color(2)
     }

//  Configure Axon

     sectype="axon"
     forsec axon_sec_list {

             insert hha2  // HH mechanism with low threshold for Na spikes (-57 mV)
               //    gnabar_hha2 = gna_axon
               //    gkbar_hha2  = gkdrbar_axon

               gnabar_hha2 = gna_axon*0        // JOSE
               gkbar_hha2  = gkdrbar_axon*0    // JOSE



             insert pas  // leak conductance
                   g_pas       = 1/Rm_axon
                   e_pas       = v_init
                   Ra          = Ra_axon
                   cm          = Cm_axon

              insert km  // m-type potassium current
                   gbar_km     = 0.5*soma_km

              $o1.tmpo2.color(1)
     }

//  Configure apical trunk

     forsec apical_trunk_list {

         apical_h_insert_sig($o1)    // Inserting h-current
         apical_caR_caLH_insert($o1) // Inserting Ca++ R-type and Ca++ L-type currents
         apical_caT_insert($o1)      // Inserting Ca++ T-type current
          apical_caL_caN_insert($o1) // Inserting HVAl Ca++  L-type current
         apical_kca_insert($o1)      // Inserting K(Ca) sAHP and mAHP potassium currents
         apical_km_insert($o1)       // Inserting m-type potassium current
         A_insert($o1)               // Inserting A-current

         insert hha_old // HH mechanism with high threshold for Na spikes (-50 mV)
                   gnabar_hha_old = gna_trunk
                   gkbar_hha_old  = gkdrbar_trunk


         insert pas // leak conductance
                   e_pas          = v_init
                   Ra             = Ra_trunk
                   cm             = Cm_trunk

         Rm_sigmoid($o1)   // configure Rm along apical trunk
         Ra_sigmoid($o1)   // configure Ra along apical trunk
         $o1.tmpo2.color(4)

// Set the Na+ spike attenuation variable (linearly decreasing from soma to 300 um)

        $o1.defvar("channel:na", "max_ar2", "0.95", "Somatic value of ar2")
        $o1.defvar("channel:na", "min_ar2", "0.30", "Minimum value of ar2")
        $o1.defvar("channel:na", "decay_end", "300.0", "Distance beyond which all values are min_ar2")
        $o1.defvar("channel:na", "decay_start", "50.0", "Distance at which ar2 starts to decrease")
        m_ar2 = (max_ar2 - min_ar2)/(decay_start - decay_end)
           for (x) {
               xdist = find_vector_distance_precise(secname(),x)
               if (xdist < decay_start) {
                 ar2_hha_old(x) = max_ar2
               } else if (xdist > decay_end) {
                 ar2_hha_old(x) = min_ar2
               } else {
              ar2_hha_old(x) = max_ar2 + m_ar2*xdist
               }
           }
           ar2_log("linear", "min_ar2*max_ar2*m_ar2*decay_start*decay_end",$o1)
     }


// Configure the apical-non-trunk section: insert basic mechanisms

   sectype = "apical non-trunk"

   forsec apical_non_trunk_list {

             insert hha_old // HH mechanism with high threshold for Na spikes (-50 mV)
                    gnabar_hha_old = gna_non_trunk
                    gkbar_hha_old  = gkdrbar_non_trunk


              insert pas // passive properties
                    g_pas     = 1/Rm_non_trunk
                    e_pas     = v_init
                    Ra        = Ra_non_trunk
                    cm        = Cm_non_trunk

               $o1.tmpo2.color(3)
   }

   khoblique_peri_decay($o1)  // Configure the apical oblique dendrites

// Configure the basal dendrites

  sectype = "basal tree"
  forsec basal_tree_list {
           insert hha_old // HH mechanism with high threshold for Na spikes (-50 mV)
                   gnabar_hha_old = gna_basal
                   gkbar_hha_old  = gkdrbar_basal


           insert pas // passive properties
                   g_pas          = 1/Rm_basal
                   e_pas          = v_init
                   Ra             = Ra_basal
                   cm             = Cm_basal

           insert kap // Insert proximal A current
                 gkabar_kap       = 2.5*soma_kap

        Ra_sigmoid($o1) // configure Ra
       $o1.tmpo2.color(5)
     }
  khbasal_fixed($o1) // Configure basal dendrites

   forsec "soma" { g_pas=1/Rm_soma } // force Rm at all soma sections


forall {
   if (ismembrane("hha_old")) {  // zero out gl (leak conductance) since g_pass is taking its place
      gl_hha_old = 0
      el_hha_old     = v_init
      ena            = 50
           }
    if (ismembrane("hha2")) {  // zero out gl (leak conductance) since g_pass is taking its place
      gl_hha2 = 0
      el_hha2 = v_init
      ena =  50
           }

    if (ismembrane("k_ion")) {
            ek         = -77
    }

    if(ismembrane("ca_ion")) {
     eca = 140
    // cai0_ca_ion =  2.4e-4 // for pump
    // cao = 2
    //ion_style("ca_ion",0,1,0,0,0)
    // ion_style("ca_ion",3,2,1,1,1)
     vshift_ca = 0

    }

 }

 // Account for spines (obsolete in this case)

  forall {
       for (x) {
         if (x > 0 && x < 1) {
               diam(x)=diam(x)*1.0
               }
       }
  }



forsec apical_non_trunk_list{
xdist = find_vector_distance_precise(secname(),0)

if (xdist < 200) {
for (x) {

gkabar_kad=0
gkabar_kap=0.0078

}
} else {
for (x) {
gkabar_kap=0
gkabar_kad=0.055
}
}
}

  // Print a postcript file in the generic directory with the cell configuration

  sprint($o1.tmp_str2, "%s/configure_sections.eps", $o1.generic_dir)
  $o1.tmpo2.printfile($o1.tmp_str2)
  $o1.xopen_library("Terrence","current-balance") // balance current to -70 mV
  current_balance(v_init)





}

proc init() {
finitialize(v_init)
fcurrent()
}