// write weight file, read weight file, initialize weights
// weight file format is
//nweight
//t
//srcgid targid s w   : nweight of these
//any number of above two

////////create a weight file
weight_start_ = 1e9
weight_interval_ = 1e9
objref fih_weight_, weight_file_
proc weight_snapshots() {local i, nw   localobj f
	weight_file_ = new File($s1)
	f = weight_file_
	weight_start_ = $2
	weight_interval_ = $3
	fih_weight_ = new FInitializeHandler(2, "w1_()")
	nw = 0
	for i=0, mgrs_list.count-1 {
		if (mgrs_list.object(i).mexist) { nw += 1 }
		if (mgrs_list.object(i).gexist) { nw += 1 }
	}
	nw = pc.allreduce(nw,1)
	if (pc.id == 0) {
		f.wopen()
		f.printf("%d\n", nw)
		f.close()
	}
}

proc w1_() { cvode.event(weight_start_, "w2_()") }

proc w2_() {local i  localobj f
	f = weight_file_
	for pnm.serialize() {
		f.aopen()
		if (pc.id == 0) {
			f.printf("%g\n", t)
		}
		for i=0, mgrs_list.count-1 {
			f.printf("%s\n", mgrs_list.object(i).ws_str().s)
		}
		f.close()
	}
	cvode.event(t + weight_interval_, "w2_()")
}
//////////// end of create a weight file

/////////// initialize weight from first group of a weight file
objref fih_weight_init_, weight_init_file_
proc weight_initialize() {
	weight_init_file_ = new File($s1)
	// after NET_RECEIVE INITIAL
	fih_weight_init_ = new FInitializeHandler(1, "w3_()")
}

proc w3_() {local i, nw, srcgid, targid, s, w, sgid \
    localobj f, map, newmap, mgrs, p, nc, syn, ncl, str
	str = new String()
	f = weight_init_file_
	f.ropen()
	nw = f.scanvar()
	f.scanvar() // discard t
	p = new PythonObject()
	nrnpython("newmap = lambda key, value : {key:value}")
	map = p.newmap(-1,0)
	for i=0, nw - 1 {
		srcgid = f.scanvar
		targid = f.scanvar
		s = f.scanvar
		w = f.scanvar
		sgid = syn_gid(srcgid, targid)
//		if (pc.gid_exists(sgid)) {// correct even if multisplit
			// no way at present to efficiently derive the
			// MGRS from the srcgid, targid but we know it
			// exists. So save data in a Python map and
			// retrieve next loop
			map.update(p.newmap(sgid, s))
//printf("map %d %d %d %d\n", srcgid, targid, ncell, s)
//		}
	}
	f.close()
	// sweep mgrs_list
    if (1 || mgrs_list.count == 0) { // must be a synapse movie
	ncl = new List("NetCon")
	for i=0, ncl.count-1 {
		nc = ncl.object(i)
		syn = nc.syn
		if (object_id(syn) == 0) { continue }
		classname(syn, str.s)
		if (strcmp(str.s, "FastInhib") != 0 && strcmp(str.s, "AmpaNmda") != 0) {
			continue
		}
		sgid = syn_gid(syn.ggid, syn.mgid)
		if (sgid == syn.srcgid) {
			sgid = syn_gid(syn.mgid, syn.ggid)
		}
		s = map.__getitem__(sgid)
//printf("%d %d %d %d %d %d\n", i, syn.ggid, syn.mgid, syn.srcgid, sgid, s)
		nc.weight[1] = s
		nc.weight[2] = syn.plast(s)*nc.weight[0]
	}
    }else{ // a normal run with real cells
	for i=0, mgrs_list.count-1 {
		mgrs = mgrs_list.object(i)
		if (pc.gid_exists(mgrs.md_gid)) { // mitral detector means fi exists
			s = map.__getitem__(mgrs.md_gid)
//printf("%d %d %g\n", mgrs.granule_gid, mgrs.mitral_gid, s)
			mgrs.set_sm(s)
		}
		if (pc.gid_exists(mgrs.gd_gid)) {
			s = map.__getitem__(mgrs.gd_gid)
//printf("%d %d %g\n", mgrs.mitral_gid, mgrs.granule_gid, s)
			mgrs.set_sg(s)
		}
	}
    }
}