/*
SPEN180 @ Zhiyong @ 20140630
   slice alfa -- delay -- prephasing/crushers -- 180 chirp -- prephasing/crushers -- acquisition loop --180 hard 
*/      
        
#include <standard.h>
#include <string.h>
#include "group.h"
#include <math.h>
#include <Pbox_psg.h>
#include "sgl.c"
        
        
pulsesequence()
{
  	get_parameters();
  	euler_test();
	
	if (tep < 0) { 		// adjust by reducing gpropdelay by that amount
          gpropdelay += tep;
          tep = 0;
        }

	double roff1,roff2;
	init_readout(&ro_grad,"SPEN180_ro",lro,np,sw);
	init_readout_refocus(&ror_grad,"SPEN180_ror");
	init_phase(&pe_grad,"SPEN180_pe",lpe,nv);
	init_phase(&per_grad,"SPEN180_per",lpe,nv);
	init_readout(&nav_grad,"nav",lro,np,sw);
	init_epi(&epi_grad);
	fract_ky=nv/2;
	etl=nv;
	strcpy(rampsamp,"n");
	strcpy(ky_order,"l");
	calc_epi(&epi_grad,&ro_grad,&pe_grad,&ror_grad,&per_grad,&nav_grad,NOWRITE); 

	double Ta,Tp,BW,Rvol=getval("Rvol"),Npe=(int) nv/2/nseg,lpecor=getval("lpecor"),zzppe=getval("zzppe");
	char cmd[MAXSTR];
	shape SPENchirp180;
	

	SLICE_SELECT_GRADIENT_T chirp180_grad;
	double gamma,Gchirp,tchirp,chirptof;
	double tpwr3=getval("tpwr3"),tpwr3f=getval("tpwr3f"),gcrush3=getval("gcrush3"),tcrush3=getval("tcrush3");

	Ta=ro_grad.duration;
	Tp=Ta/2;
	BW=Rvol/Tp;

	gamma=nuc_gamma();
	Gchirp=BW/gamma/lpe;
	chirptof=poffset(ppe,Gchirp);
	
	putCmd("aqtm= %f",at);
	putCmd("fract_ky= %f",nv/2);
	putCmd("etl= %f",nv);
	putCmd("rampsamp= \'n\'");
	putCmd("ky_order= \'l\'");
	putCmd("BW= %f",BW);
	putCmd("Ta= %f",Ta);
	putCmd("Tp= %f",Tp);
	putCmd("Gchirp= %f",Gchirp);

	sprintf(cmd, "Pbox SPENchirp180.RF -w \"WURST180 %f/%f %f\" -s 4.0 -p %f -l %f -header i -0", 0.64*lpecor*BW,Tp,chirptof,tpwr,pw*1e6);
	system(cmd);
	SPENchirp180=getRsh("SPENchirp180");

  	init_rf(&p2_rf,SPENchirp180.name,Tp,180,rof1,rof2 );    
	init_slice_butterfly(&chirp180_grad,"SPEN180_grad",lpe*10,gcrush,tcrush);                  
	p2_rf.bandwidth=BW;
  	calc_slice(&chirp180_grad,&p2_rf,WRITE,"gss");




	init_readout(&ro_grad,"SPEN180_ro",lro,np,sw);
	init_readout_refocus(&ror_grad,"SPEN180_ror");
	init_phase(&pe_grad,"SPEN180_pe",nv/gamma/Gchirp/Tp/2,nv);
	init_phase(&per_grad,"SPEN180_per",nv/gamma/Gchirp/Tp/2,nv);
	init_readout(&nav_grad,"nav",lro,np,sw);
	init_epi(&epi_grad);
	calc_epi(&epi_grad,&ro_grad,&pe_grad,&ror_grad,&per_grad,&nav_grad,NOWRITE); 
	


	
	double  freq90[MAXNSLICE];
	int     shapelist90;

  	init_rf(&p1_rf,p1pat,p1,flip1,rof1,rof2);
  	init_slice(&ss_grad,"ss",thk);
  	init_slice_refocus(&ssr_grad,"ssr");  
	calc_rf(&p1_rf,"tpwr1","tpwr1f"); 
  	calc_slice(&ss_grad,&p1_rf,WRITE,"");
	calc_slice_refocus(&ssr_grad,&ss_grad,NOWRITE,"gssr");
  	offsetlist(pss,ss_grad.ssamp,0,freq90,ns,seqcon[1]);
  	shapelist90 = shapelist(p1_rf.pulseName,ss_grad.rfDuration,freq90,ns,ss_grad.rfFraction,seqcon[1]);
      
	calc_sim_gradient(&ssr_grad, &per_grad, &ror_grad, 0, WRITE);
	

	int i;

	int vpe          = v21;   
	int vpe_ctr      = v22;
	int vgro         = v23;   
  	int vms_slices   = v25;   // Number of slices
  	int vms_ctr      = v26;   // Slice loop counter
  	int vnseg        = v27;   // Number of segments
  	int vnseg_ctr    = v28;   // Segment loop counter

	F_initval(nseg, vnseg);

	trampfixed=0;
	if (gspoil*tspoil!=0){
	   init_generic(&spoil_grad,"spoil",gspoil,tspoil);
	   calc_generic(&spoil_grad,WRITE,"","");
	}
	
	if (gcrush3*tcrush3!=0)
	   trapezoid(&crush_grad,"gcrush",gcrush3,tcrush3,0,WRITE);
	else 
	   trapezoid(&crush_grad,"gcrush",5,0.002,0,WRITE);

  	roff1 = -poffset(pro,ro_grad.roamp);
  	roff2 = -poffset(pro,-ro_grad.roamp);

	if(te<Ta+p1/2+tcrush*2+tcrush3*2+per_grad.duration){
	   text_message("te is set to minte: %f",Ta+p1/2+tcrush*2+tcrush3*2+per_grad.duration); 
	   te=Ta+p1/2+tcrush*2+tcrush3*2+per_grad.duration; 
	}

	if (minte[0]=='y') 
 	  te=Ta+p1/2+tcrush*2+tcrush3*2+per_grad.duration;

	putCmd("te= %f",te); 
	double te_delay;
	te_delay=te-(Ta+p1/2+tcrush*2+tcrush3*2+per_grad.duration);


	if(tr<(p1/2+te+Ta+p3+tspoil)*ns){
	   text_message("tr is set to minte: %f",(p1/2+te+Ta+p3+tspoil)*ns); 
	   tr=(p1/2+te+Ta+p3+tspoil)*ns;
	}

	if (mintr[0]=='y') 
 	  tr=(p1/2+te+Ta+p3+tspoil)*ns;
	putCmd("tr= %f",tr); 

	sgl_error_check(sglerror);


	assign(ct,oph);
	assign(oph,v1);

	
 	get_wsparameters();
	if (ws[0] == 'y') create_watersuppress();

	/*   =======================================
	     ==    Beginning of Pulse Sequence    ==
	     =======================================   */
        status(A);
  	rotate();

	if (ws[0]   == 'y') watersuppress();


	F_initval(Npe,vpe);
        setacqmode(WACQ|NZ);
 	ifzero(rtonce); grad_advance(gpropdelay); endif(rtonce);


        loop(vnseg,vnseg_ctr); 
             msloop(seqcon[1],ns,vms_slices,vms_ctr); 


      	if (ticks) {
           xgate(ticks);
           grad_advance(gpropdelay);
           delay(4e-6);
        }

	rcvroff();
	txphase(0);


	/* -----------------------------------------
	      Excitation
	   ----------------------------------------- */


	obsoffset(resto);
	obspower(p1_rf.powerCoarse);
      	obspwrf(p1_rf.powerFine);
      	delay(4e-6);
      	obl_shapedgradient(ss_grad.name,ss_grad.duration,0,0,ss_grad.amp,NOWAIT);
      	delay(ss_grad.rfDelayFront);     
	shapedpulselist(shapelist90,p1_rf.rfDuration,v1,rof1,rof2,seqcon[1],vms_ctr);
      	delay(ss_grad.rfDelayBack);


	pe3_shaped3gradient(ror_grad.name,per_grad.name,ssr_grad.name,ssr_grad.duration,-ror_grad.amp,-per_grad.amp,-ssr_grad.amp,0,per_grad.amp*pe_grad.m0/per_grad.m0/nseg,0,zero,vnseg_ctr,zero,WAIT);


	delay(te_delay/2);
	obsoffset(resto);
	obspower(SPENchirp180.pwr);
	obspwrf(SPENchirp180.pwrf);					
      	delay(4e-6);
	obl_shaped3gradient("",chirp180_grad.name,"",chirp180_grad.duration,0,chirp180_grad.amp,0,NOWAIT); 
      	delay(chirp180_grad.rfDelayFront);
	shaped_pulse(SPENchirp180.name,SPENchirp180.pw,zero,rof1,rof2);
      	delay(chirp180_grad.rfDelayBack);
	delay(te_delay/2+Ta/2+ssr_grad.duration+p1_rf.rfDuration/2);


	delay(GDELAY);
	obl_shaped3gradient(crush_grad.name,crush_grad.name,crush_grad.name,crush_grad.duration,crush_grad.amp,crush_grad.amp,crush_grad.amp,WAIT);
	obspower(tpwr3);
      	obspwrf(tpwr3f);
	shaped_pulse(p3pat,p3,zero,rof1,rof2);
	obl_shaped3gradient(crush_grad.name,crush_grad.name,crush_grad.name,crush_grad.duration,crush_grad.amp,crush_grad.amp,crush_grad.amp,WAIT);


	delay(GDELAY);
	obl_shaped3gradient(ro_grad.name,pe_grad.name,"",ro_grad.duration,ro_grad.roamp,pe_grad.amp,0.0,NOWAIT);
	delay(tep);

	
	nowait_loop(Npe,vpe,vpe_ctr);

    	roff = roff1;  
	delay(epi_grad.skip-alfa); 
	startacq(alfa);
	acquire(np,1/sw);
	delay((epi_grad.dwell[(int) (np/2-1)]-1/sw));
	if (aqtm > at) sample(aqtm-at);
	endacq();
	delay(epi_grad.skip - 1/sw - (aqtm-at));

    	roff = roff2;   
	delay(epi_grad.skip-alfa);
	startacq(alfa);
	acquire(np,1/sw);
	delay((epi_grad.dwell[(int) (np/2-1)]-1/sw));
	if (aqtm > at) sample(aqtm-at);
	endacq();
	delay(epi_grad.skip - 1/sw - (aqtm-at));

	nowait_endloop(vpe_ctr);

	if (gspoil && tspoil) {
		delay(GDELAY);
		obl_shaped3gradient(spoil_grad.name,spoil_grad.name,spoil_grad.name,spoil_grad.duration,spoil_grad.amp, spoil_grad.amp,spoil_grad.amp,WAIT);
		delay(GDELAY);
	}

	delay(tr/ns-p1/2-te-p3-tspoil-Ta);

    	   	endmsloop(seqcon[1],vms_ctr);   		 /* end multislice loop */
  	endloop(vnseg_ctr); 
	loop_check();               			 	/* end segments loop */
	g_setExpTime(tr*nt*nseg*arraydim);
}
