//Control crated by Oscar Javier Avella from modifications on Ronald vn Elburg 
//Neuron-files
                               
                                

// --------------------------------------------------------------
// Loading Standard Menus
// --------------------------------------------------------------

	UseGui=1   
	if(UseGui==1){
    	load_file("nrngui.hoc")	
		nrnmainmenu()
		runStopAt=tstop
		runStopIn = 100
		nrncontrolmenu()
	}

// --------------------------------------------------------------
// --------------------------------------------------------------
if(UseGui==1){
		load_file("../Instrum_Visualize/spikeplot2.hoc")
	}else{
		load_file("emptyGUIClasses.hoc")      
	}

        create cvode_init_dummy			// CVode needs an accessed section for some of the settings to take effect
	access cvode_init_dummy
	Ra=150	
	load_file("../MRC/cvode_init.hoc")
	
	// Set a few Global Flags
	useprtmymes=0			// Setting this flag to one will make the template cells produce basic debugging info
	screen_update_invl=0.05	// Screen Update Interval of GUI
 
// General simulation related settings
	dt = 0.025           //time step
	//tstop = 1200         //1 second simulation
	steps_per_ms = 40    

  	
//load_file("./in_process/functions_net_bgk_multitest_sparse_onboard.hoc")
load_file("functions_net_multipop.hoc")

//load_file("functions_net_bgk_multitest_sparse.hoc")
//load_file("functions_net_bgk_multitest.hoc") //Load the network's  builder-administrator
//load_file("functions_net_bgk_testING.hoc") //Load the network's  builder-administrator	
	strdef win_title,win_title1
	objref SP_FS[n_layerFS],SP_pyr[n_layerP] //,SP_NS[fs_popul+pyram_popul] references to spikeplots
	objref SP_NS[n_layerP+n_layerFS] //references to spikeplots
	
	       
//creating layers  
//==================================================================================
//==================================================================================
        if(n_layerP && n_P>0){
        for i=0, n_layerP-1{
        if(layerP[i].count()>0){
        sprint(win_title1, "Pyramidal Cells Population # %d", i)
        SP_pyr[i]= new SpikePlot2(layerP[i],0,win_title1)
          }
        } 
      }     
        if(n_layerFS>0 && n_FS>0){
        for i=0, n_layerFS-1{
        if(layerFS[i].count()>0){
        sprint(win_title, "Fast Spiking Cells Population # %d", i)
        SP_FS[i] = new SpikePlot2(layerFS[i],0,win_title)
        }
    }
  }
//==================================================================================
//==================================================================================
        
//creating vectors to save the data
	
	objref yvec1[n_layerFS], tvec1[n_layerFS], yvec3[n_layerP], tvec3[n_layerP]
	objref yvec2[n_layerFS], tvec2[n_layerFS], yvec4[n_layerP], tvec4[n_layerP]
	objref TracePlot
	strdef buttontext, buttoncomand
	
	if(UseGui==1){                                   
		newPlot(0,tstop,-100,60)
		TracePlot = graphItem
		if(n_layerFS>0 && n_FS>0){
		for i=0,n_layerFS-1{
		  	yvec1[i]= new Vector()
			tvec1[i]= new Vector()
			if(layerFS[i].count()>0){
              layerFS[i].object(int(n_FS/2)).soma cvode.record(&v(0.5), yvec1[i], tvec1[i])
		      yvec2[i] = new Vector()
	          tvec2[i] = new Vector()
       		  layerFS[i].object(0).soma cvode.record(&v(0.5), yvec2[i], tvec2[i])
		      }
        }
      }
if(n_layerP>0 && n_P>0){
		for i=0,n_layerP-1{	
		    yvec3[i] = new Vector()
			tvec3[i] = new Vector()
			if(layerP[i].count()>0){
			layerP[i].object(int(n_P/2)).soma cvode.record(&v(0.5), yvec3[i], tvec3[i])
			//layerP[i].object(87).soma cvode.record(&v(0.5), yvec3[i], tvec3[i])
            yvec4[i] = new Vector()
			tvec4[i] = new Vector()
			layerP[i].object(0).soma cvode.record(&v(0.5), yvec4[i], tvec4[i])
           }
        }
    }
  
		xpanel("Plot Traces")
			if(n_layerFS>0 && n_FS>0){
      for i=0,n_layerFS-1{
			 if(layerFS[i].count()>0){
				sprint(buttontext,"Center FS Cell from Population %d",i)
				sprint(buttoncomand,"yvec1[%d].plot(TracePlot,tvec1[%d],3.3,2.5)",i,i)
                xbutton(buttontext,buttoncomand)
                sprint(buttontext,"Boundary FS Cell from Population %d",i)			
			 	sprint(buttoncomand,"yvec2[%d].plot(TracePlot,tvec2[%d],3.3,2.8)",i,i)		 	
                xbutton(buttontext,buttoncomand)
                }
			 }
		}
    if(n_layerP>0 && n_P>0){
	   for i=0,n_layerP-1{
	    if(layerP[i].count()>0){
			    sprint(buttontext,"Boundary Pyramidal from Population %d",i)
			    sprint(buttoncomand,"yvec4[%d].plot(TracePlot,tvec4[%d],2,0.9)",i,i)
                xbutton(buttontext,buttoncomand)	
				sprint(buttontext,"Center Pyramidal from Population %d",i)               
			 	sprint(buttoncomand,"yvec3[%d].plot(TracePlot,tvec3[%d],2,0.7)",i,i)                
                xbutton(buttontext,buttoncomand)
                }
			 }
		}
		xpanel()
	}
             

if(n_P>0&&n_FS>0&&n_layerP==1&&n_layerFS==1){
    load_file("sessions/DrivePower_run_mono_no_spk.ses")
    flag_p=1    
    }else{
        if(n_layerP>=2||n_layerFS>=2){
       load_file("sessions/DrivePower_run_multi.ses")	
	   flag_p=2
       }
	}
	
    load_file("./sessions/parameters.hoc")
    print_parameters(flag_p,nme)	
	
//==================================================================================================================
//==================================================================================================================
/*
objref u, u2
u=new Random(874) 
u2=new Random(647)
u.uniform(-85,40) 
u2.uniform(-85,40)


proc init1() { local i j
for i = 0, layerP[0].count()-1 {layerP[0].o(i).soma.v(0.5) = u.repick }
for j = 0, layerFS[0].count()-1 {layerFS[0].o(j).soma.v(0.5) = u.repick }
for ii = 0, layerP[1].count()-1 {layerP[1].o(ii).soma.v(0.5) = u.repick }
for jj = 0, layerFS[1].count()-1 {layerFS[1].o(jj).soma.v(0.5) = u.repick }
finitialize()
}

proc init() {
finitialize(-85)
init1()
if (cvode.active()) {
cvode.re_init()
} else {
fcurrent()
}
frecord_init()
}
*/
//==================================================================================================================
//==================================================================================================================
        //run()
        //prt2file(word,wrd2)