strdef fitFile,timesFile,paramsFile,stimFile,outFile,modelFile,outFPre,base
objref st,st_dend,stims,fin,fout,pmat,matOut,stimtime,somaref,secref,extVec,strFunc,most,root,transvec

ntimestep = 5000 
nparams = 14 
psize = 1 
ntraces = 1 
v_init = -70 
calc_eca = 1 
calc_eca = 1 

   stLoc = 0.5 
base = "E:/GitHub/NeuroGPU/NeuroGPU_Base" 
paramsFile = "./params/orig.csv" 
stimFile = "./Stims/Stim_raw.csv" 
modelFile = "./mosinit.hoc" 
timesFile = "./Stims/times.csv" 


transvec = new Vector(nparams)

fin = new File()
fout = new File()
load_file(modelFile)


root = new SectionRef()
if (root.has_parent()){
	print secname()
	root = root.root()
	}
access cADpyr232_L5_TTPC1_0fb1ca4724[0].soma


objref recSites
recSites = new SectionList()
outFPre = "./volts/orig_step"
access root.sec
print "**********************"
print secname()
root.sec recSites.append()


 
secondorder=0
proc runMatrix(){

	//access dend[1]
	sprint(outFile,"%s_%s.dat",outFPre,secname())

	st.del=0 
	st.dur=1e9
	//printf ("$o1.nrow,$o2.nrow,$o2.ncol,%d,%d,%d",$o1.nrow,$o2.nrow,$o2.ncol)
	matOut = new Matrix($o1.nrow,($o2.nrow*$o2.ncol))
	for(sim=0;sim<$o1.nrow;sim+=1){
	//printf("running sim %d\n",sim)
		counter=0
		transvec = $o1.getrow(sim)
		tfunc()
		for(i=0;i<stims.nrow;i+=1){
			finitialize(v_init)
			for(tt =0;tt<stimtime.ncol;tt+=1){
			dt  =stimtime.x(0,tt)

				st.amp = 1*stims.x(i,tt)

				matOut.x(sim,counter)=v(0.5)
				counter+=1
				fadvance()
				
			}
		}
	}
	//printf("finshed run matrix %s\n",outFile)
}
strFunc = new StringFunctions()
proc readMatrix(){localobj temp
	csv_ind = strFunc.substr($s1,"csv")
	if (csv_ind==-1){
		temp = new Vector()
		fin.ropen($s1)
		for (i=0;i<$o2.nrow;i+=1){
			temp.vread(fin)
			$o2.setrow(i,temp)
			//print i
		}
		fin.close()
	} else {
	readCSVMatrix($s1,$o2)
	}
}

proc readCSVMatrix(){
	fin = new File($s1)
	fin.ropen()
	print $o2.nrow()
	$o2.scanf(fin,$o2.nrow(),$o2.ncol())
	fin.close()
}
proc writeMatrix(){localobj temp
	fout.wopen($s1)
	temp = new Vector()
	for (i=0;i<$o2.nrow;i+=1){
		temp = $o2.getrow(i)
		temp.vwrite(fout,3)
	}
	fout.close()
}
proc mul32(){localobj thisone
	countSegs()
	segsMat = segs+comps+1 
	//printf("1we have a matrix in the size of %d \n",segsMat )
	segsToAdd = 32-segsMat%32
	forall {
		thisone = new SectionRef()   
		if (thisone.nchild == 0 ) {
			break
			}
	}
	access thisone.sec
	nseg = nseg+segsToAdd
	fcurrent()
	countSegs()
	//printf("we have a matrix in the size of %d \n",segs+comps+1 )
	
}
proc writeVector(){
	fout.aopen($s1)
	$o2.vwrite(fout,3)
	fout.close()
	
}
proc printChildren(){localobj sl
	sl = new SectionList()
	sl.children()
	u=0
	forsec sl{
	print secname()
	u+=1
	}
	print u
}	
proc printparent(){localobj sref
	sref = new SectionRef()
	sref.parent
	print secname()
}		




proc PrintRecSites(){localobj sref
	recSites.printnames()
}	
stimtime = new Matrix(1,ntimestep)
readMatrix(timesFile,stimtime)
pmat = new Matrix(psize,nparams)
readMatrix(paramsFile,pmat)
stims = new Matrix(ntraces,ntimestep)
readCSVMatrix(stimFile,stims)
forsec "dend" nseg=1
forsec "apic" nseg=1
	
//topology()
most = new SectionList()
forall most.append()
fcurrent()
//fmatrix()
//printf("Creating Exts %d\n",ext_num)
//printf("Creating Exts %d\n",ext_num)

//****************************************************
forall{nseg=1}
//******************************************************
working()
access root.sec
transvec = pmat.getrow(0)
tfunc()
st = new IClamp(0.5)

//Added this to stimulate at the dendrite
access cADpyr232_L5_TTPC1_0fb1ca4724[0].dend
st_dend = new IClamp(0.5)
access root.sec


 
finitialize(v_init)



//runMatrix(pmat,stims)
//writeMatrix(outFile,matOut)
finitialize(v_init)
//ADD STUFF HERE

//quit()