/* square array of cones */
/* connected by gap junctions */
/* square array of B-horizontal cells, connected by gap junctions */
/* square array of A-horizontal cells, connected by gap junctions */
/* inhibitory feedback from hz cells to cones */
/* 22 um blur */


timinc = 1e-4;
crit = 1e-8;
endexp = .05;
ploti = .001;
relax = .15;
drm = 20000;
conlam = .5;

arrsiz = 60;			/* size of cone array */

hzsiza = 14;			/* size of type A horizontal cells */
hzsizb = 10;			/* size of type B horizontal cells */
cov_a = 4;                      /* coverage factor type A hz cell to cone */
cov_b = 6;                      /* coverage factor type B hz cell to cone */
nhconn = 10;
nconn = 10;
soma = 0;

hza = 16000;					/* first type A hz cell */
hzb = 15000;					/* first type B hz cell */

stimtime = .04;
scal = 6;
scatter = 1;
drg = 2.5e6;
gjcond =  100e-9;
hagjcond = 100e-9;
hbgjcond = 5e-9;
hbrm = 20000;
conerm = 20000;
conrest = -.0377;
conrestt = -.0377;
hzrev = -0.07;
hresta = -.045;
hrestb = -.046;
hrestta = -.041;
hresttb = -.041;
hzthr = -.05;
expona = 2;
exponb = 2;
exponf = 2;
conda = 2e-10;
condb = 2e-10;
condf = 1e-10;
fbtau = 20;

x = setvar();

hzmaxa = int (arrsiz / hzsiza * sqrt(cov_a)) - 1;	/* size of HzA array */
hzmaxb = int (arrsiz / hzsizb * sqrt(cov_b)) - 1;	/* size of HzB array */

totsiz = arrsiz * arrsiz + 1;
hztot = hzmaxb * hzmaxb + 1;		/* make large enough for type A or B */

include "mid.m";

mida = hza + midrow(hzmaxa);		/* center node in array */
midb = hzb + midrow(hzmaxb);
midc =       mid(arrsiz);

rada = hzmaxa/2;			/* radius for plotting */
radb = hzmaxb/2;
radc = arrsiz/2;
cent = radc*scal;			/* location of stimulus in microns */

dim conarr[arrsiz][arrsiz][4];
dim gconnect[totsiz][nconn];

dim hzarr[2][hzmaxb][hzmaxb][2];	/* array to hold either type A or B */
dim hzconn[2][hztot][nhconn];

include "con4.m";
include "congj.m";
include "conhz5.m";


for (i=0; i=arrsiz) continue;
          if (h<0 || h>=arrsiz) continue;
          if (i==h && j==k) continue;
          congj (i,j,h,k);
      };

for (hztype=0; hztype<2; hztype++) {
 if (hztype == 0) {                             /* A-type Hz */
  hznum = hzmaxa;
  hzsiz = hzsiza;
  hznode = hza;
  hzoff = int (hzsiz/sqrt(cov_a));
  hzst = hzsiz/2;
  hrest = hresta;
  hrestt = hrestta;
 };
 if (hztype == 1) {                             /* B-type Hz */
  hznum = hzmaxb;
  hzsiz = hzsizb;
  hznode = hzb;
  hzoff = int (hzsiz/sqrt(cov_b));
  hzst = hzsiz/2;
  hrest = hrestb;
  hrestt = hresttb;
 };

 hzcount = 0;
 for (hzly=hzst,j=0; j=arrsiz) continue;
     if (y<0 || y>=arrsiz) continue;
     cnode = conarr[x][y][0];
     ped  = conarr[x][y][1];
     conarr[x][y][2+hztype]++;                /* increment synapse count */
     conn [cnode][ped] to [hznode][dend][1]
  synapse  open expon=exponf maxcond=condf thresh=-.045 vrev=-.01;

    if (hztype == 0) {
    conn [hznode][dend][1] to [cnode][ped]
  synapse open expon=expona maxcond=conda thresh=hzthr vrev=-.067
                 nfilt2 1 timec2=fbtau;
    }
    else if (hztype == 1) {
    conn [hznode][dend][1] to [cnode][ped]
  synapse open expon=exponb maxcond=condb thresh=hzthr vrev=-.067
                 nfilt2=1 timec2=fbtau;
    };

    conn [hznode][dend][1] to [hznode][dend][2]
         cable dia=.1 length=10 rm=hbrm vrev=hzrev vrest=hrestt;

/*  dx = hzlx - x;
    dy = hzly - y;
    cablen = sqrt (dx*dx + dy*dy) * scal;    /* */
    cablen = 20;
    conn [hznode][dend][2] to [hznode][soma]
         cable dia=2 length=cablen rm=hbrm vrev=hzrev vrest=hrest;
   };
  at [hznode][soma] sphere dia=15 rm=hbrm vrev=hzrev vrest=hrest;
  hzarr[hztype][i][j][0] = hzcount;
  hzarr[hztype][i][j][1] = hznode;
 };
 print "#  hzcount",hzcount;

}; /* for (hztype; ;) */

proto_hza = 26000;
proto_hzb = 25000;
at proto_hza sphere dia=1 vrest=hrestta;
at proto_hzb sphere dia=1 vrest=hresttb;

conn [hzb][20][1] to proto_hzb vbuf;          /* make prototype buffers */ 
conn [hza][20][1] to proto_hza vbuf;          /*  to eliminate edge effect */



 for (x=0; x=hzmax) continue;
          if (h<0 || h>=hzmax) continue;
          if (i==h && j==k) continue;
          conhz (i,j,h,k);
      };
};

stim file "hz284.t";
stim center (cent,cent); 
stim backgr 100 start 0;
stim spot 1 loc (cent,cent) blur=22 inten=2.5e9 start=stimtime dur=.001;

plmax = -.04;
plmin = -.05;
if (int(midc/2)*2 == midc) fpl = 10	/* plot cones */
else                       fpl = 9;
for (i=0; i