objref map, x

obfunc synaptic_map_construct (){ localobj f, s, tmpmap
	/* $1 thisno,
	$2 num_presynaptic_cells,
	$3 num_postsynaptic_cells,
	// no more $o4 map(i,j), Matrix object = cell # of ith presyn input to jth postsyn cell
	$4 num_presyninputs_perpostsyn_cell, 
	$5 display
	$s6 map file base name
	*/

	// Construct a map of presynaptic cells of one type to postsyn.
	//  cells of some type. 
	// display is an integer flag.  If display = 1, print gjtable

	//        INTEGER thisno, num_presynaptic_cells,
	//     &   num_postsynaptic_cells,
	//     &   num_presyninputs_perpostsyn_cell,
	//     &   map (num_presyninputs_perpostsyn_cell,
	//     &          num_postsynaptic_cells) 
	//        INTEGER i,j,k,l,m,n,o,p
	//       INTEGER display

	//        double precision seed, x(1)
	thisno = $1
	num_presynaptic_cells = $2
	num_postsynaptic_cells = $3
	objref map
	//map = $o4 // dosen't matter
	num_presyninputs_perpostsyn_cell = $4
	display = $5
	// objref seed
	seed = new Vector()
	seed.append(297.e0)
	map = new Matrix( num_presyninputs_perpostsyn_cell+1, num_postsynaptic_cells+1 )

  if (!use_p2c_net_connections) {
	k = 1

	for ii = 1, num_postsynaptic_cells {
	//	print " Constructing map for cell #",ii
		for j = 1, num_presyninputs_perpostsyn_cell {
	        	x = durand (seed, k, x)
			// This defines a presynaptic cell
		
		        LL = int ( x.x(0) * num_presynaptic_cells ) + 1
		        if (LL > num_presynaptic_cells) {
				print " unnexpected boundary issue in synaptic_map_construct()"
				LL = num_presynaptic_cells
			}

		        map.x[j][ii] = LL
	
		}
	}

	// Possibly print out map when done.
	if ((display == 1) && (thisno == 0)) {
		print "SYNAPTIC MAP"
	        for i = 1, num_postsynaptic_cells {
         		printf("%6d %6d %6d \n",map.x(1,i), map.x(2,i), \
			map.x(num_presyninputs_perpostsyn_cell,i))
		}
	}
  }else{
	// read from file created by port2colossus
	s = new String()
	sprint(s.s, "../../p2c/map/%s.dat", $s6)
//printf("%s %d %d\n", s.s, map.nrow-1, map.ncol-1)
	f = new File()
	f.ropen(s.s)
	tmpmap = new Matrix(map.nrow-1, map.ncol-1)
	tmpmap.scanf(f, map.nrow-1, map.ncol-1)
	tmpmap.bcopy(0,0,map.nrow-1, map.ncol-1, 1, 1, map)
  }
	return map
}