/*  gNcpmgex_X.c

This is to replace gNcpmgex_NH.c because the latter doesn't give signal.
Use gNcpmgex_NH to setup the experiment, and set

seqfil='gNcpmgex_X' f1coef='1 0 -1 0 0 -1 0 -1' gzlvl6=5000 gzlvl5=16000 gt8=0.002 gzlvl8=18000 gzlvl11=17940 dpwr2_comp=48 ssfilter=200

or use macro gNcpmgex_x to setup the experiment.

Youlin Xia at University of Minnesota on Jan 8, 2014


 

 If comp_flg='y' there is a N15 pulse applied after d1 to compensate for RF/coil
 heating from the cpmg pulse train. This pulse be longest for the ncyc small and
 increases with ncyc to maintain a constant heating as a function of ncyc. Cryogenic
 probe tuning and shimming can be sensitive to power dissapation, so this compensates
 for the heating. Room temperature probes are not as sensitive to coil heating or
 deshimming, but may experience sample heating.

 The pulse is applied at dpwr2_comp power and the duration is calculated within the
 pulse sequence. The power should be determined by setting up a cpmg pulse train
 for the maximum ncyc to be used, starting the sequence, and then determining the
 heating power remaining (for a cold probe) after attaining steady-state. This should
 be done with comp_flg='n'. Then, set the N15 power levels other that dpwr2_comp to
 zero,ncyc=0 and set comp_flg='y'. Set ncyc_max to the largest ncyc value to be used
 in the queue of experiments. Try various compensation power levels until the heating
 power value achieves the same level as above. This represents a power level that
 produces the same heating as the cpmg pulse train.

 Now re-set ncyc and the cpmg pulse train power level to their proper values and leave
 comp_flg='y'. Use sufficient steady-state pulses to get to a stable heater value and
 then run the series of 2D experiments as a function of ncyc, one after the other.

 Once this is done the heating characteristics should be very similar from sample to
 sample so that the same settings could be used.

*/



#include <standard.h>
#include "bionmr.h"
  

 char        comp_flg[MAXSTR];
	     
static int   phx[1]={0},   phy[1]={1},   ph_x[1]={2},

	     phi2[2]  = {0,2},	
	     phi3[4]  = {0,0,2,2},	
             phi5[4] =  {0,0,2,2},
             phi6[4] =  {2,2,0,0},
             phi9[8]  = {0,0,0,0,1,1,1,1},	
             rec[8]   = {0,2,2,0,2,0,0,2};


static double   d2_init=0.0;


pulsesequence()
{

/* DECLARE AND LOAD VARIABLES */

char        f1180[MAXSTR],   		      /* Flag to start t1 @ halfdwell */
	    c_flg[MAXSTR];			    /* do TROSY on N15 and H1 */
 
int         icosel,          			  /* used to get n and p type */
            t1_counter;		    /* to obtain maximum relaxT, ie relaxTmax */

double      tau1,         				         /*  t1 delay */
             taua,                 /* < 1 / 4J(NH) 2.25 ms      */
             taub,                 /*   1 / 4J(NH) in NH : 2.68 ms  */
        
/* the sech/tanh pulse is automatically calculated by the macro "proteincal", */
/* and is called directly from your shapelib.                  		      */

   pwClvl = getval("pwClvl"), 	  	        /* coarse power for C13 pulse */
   pwC = getval("pwC"),     	      /* C13 90 degree pulse length at pwClvl */
   rf0,            	          /* maximum fine power when using pwC pulses */
   rfst,	                           /* fine power for the stCall pulse */

   compH = getval("compH"),        /* adjustment for H1 amplifier compression */
   compN = getval("compN"),       /* adjustment for N15 amplifier compression */
   compC = getval("compC"),       /* adjustment for C13 amplifier compression */


  tpwrsf_t = getval("tpwrsf_t"), /* fine power adustment for first soft pulse(TROSY=n)*/
  tpwrsf_n = getval("tpwrsf_n"), /* fine power adustment for first soft pulse(TROSY=y)*/
  tpwrsf_d = getval("tpwrsf_d"), /* fine power adustment for second soft pulse(TROSY=y)*/
   	pwHs = getval("pwHs"),	        /* H1 90 degree pulse length at tpwrs */
   	tpwrs,	  	              /* power for the pwHs ("H2Osinc") pulse */

	pwNlvl = getval("pwNlvl"),	              /* power for N15 pulses */
        pwN = getval("pwN"),          /* N15 90 degree pulse length at pwNlvl */

	sw1 = getval("sw1"),

            dpwr2_comp,           /* power level for CPMG compensation       */
             dpwr2_cp,             /* power level for N CPMG        */
             pwn_cp,               /* PW90 for N CPMG           */
             tauCPMG,              /* CPMG delay */
             ncyc,                 /* number of times to loop    */
             ncyc_max,              /* max number of times to loop    */
             time_T2,              /* total time for T2 measuring     */
             timeC,                /* compensation time */

             gt1,
             gt2,
             gt3,
             gt4,
             gt5,
             gt6,
             gt7,
             gt8,
             gt9,
             gt10,
             gt11,
             gstab,
             gzlvl0,
             gzlvl1,
             gzlvl2,
             gzlvl5,
             gzlvl6,
             gzlvl7,
             gzlvl8,
             gzlvl9,
             gzlvl10,
             gzlvl11;







/*   LOAD PHASE TABLE    */
  taua = getval("taua");
  taub = getval("taub");

  dpwr2_comp = getval("dpwr2_comp"); 
  dpwr2_cp = getval("dpwr2_cp"); 
  pwn_cp = getval("pwn_cp");
  ncyc = getval("ncyc");
  ncyc_max = getval("ncyc_max");
  time_T2 = getval("time_T2");


    getstr("f1180",f1180);
    getstr("c_flg",c_flg);
    
  gt1 = getval("gt1");
  gt2 = getval("gt2");
  gt3 = getval("gt3");
  gt4 = getval("gt4");
  gt5 = getval("gt5");
  gt6 = getval("gt6");
  gt7 = getval("gt7");
  gt8 = getval("gt8");
  gt9 = getval("gt9");
  gt10 = getval("gt10");
  gt11 = getval("gt11");
  gstab  = getval("gstab");
  gzlvl0 = getval("gzlvl0");
  gzlvl1 = getval("gzlvl1");
  gzlvl2 = getval("gzlvl2");
  gzlvl5 = getval("gzlvl5");
  gzlvl6 = getval("gzlvl6");
  gzlvl7 = getval("gzlvl7");
  gzlvl8 = getval("gzlvl8");
  gzlvl9 = getval("gzlvl9");
  gzlvl10 = getval("gzlvl10");
  gzlvl11 = getval("gzlvl11");

  getstr("comp_flg",comp_flg);
	
        settable(t1,1,phx);
	settable(t2,2,phi2);
        settable(t3,4,phi3);
	settable(t4,1,phx);
	settable(t5,4,phi5);
	settable(t6,4,phi6);
	settable(t9,8,phi9);
 	settable(t10,1,phx);
	settable(t11,1,phy);
	settable(t12,8,rec);



/*   INITIALIZE VARIABLES   */

/* maximum fine power for pwC pulses (and initialize rfst) */
	rf0 = 4095.0;    rfst=0.0;

/* 180 degree adiabatic C13 pulse from 0 to 200 ppm */
     if (c_flg[A]=='y')
       {rfst = (compC*4095.0*pwC*4000.0*sqrt((30.0*sfrq/600.0+7.0)/0.35));   
	rfst = (int) (rfst + 0.5);
	if ( 1.0/(4000.0*sqrt((30.0*sfrq/600.0+7.0)/0.35)) < pwC )
           { text_error( " Not enough C13 RF. pwC must be %f usec or less.\n", 
	    (1.0e6/(4000.0*sqrt((30.0*sfrq/600.0+7.0)/0.35))) );    psg_abort(1); }}

/* selective H20 one-lobe sinc pulse */
    tpwrs = tpwr - 20.0*log10(pwHs/(compH*pw*1.69));   /*needs 1.69 times more*/
    tpwrs = (int) (tpwrs);                   	  /*power than a square pulse */




/* CHECK VALIDITY OF PARAMETER RANGES */

  if((dm[A] == 'y' || dm[B] == 'y' || dm[C] == 'y' ))
  { text_error("incorrect dec1 decoupler flags! Should be 'nnn' "); psg_abort(1); }

  if((dm2[A] == 'y' || dm2[B] == 'y'))
  { text_error("incorrect dec2 decoupler flags! Should be 'nny' "); psg_abort(1); }

  if( dpwr2 > 50 )
  { text_error("don't fry the probe, DPWR2 too large!  ");   	    psg_abort(1); }

  if( pw > 50.0e-6 )
  { text_error("dont fry the probe, pw too high ! ");               psg_abort(1); } 
  
  if( pwN > 100.0e-6 )
  { text_error("dont fry the probe, pwN too high ! ");              psg_abort(1); }

   if( ncyc > 100)
    {
       printf("ncyc exceeds 100. May be too much \n");
       psg_abort(1);
    }  

   if( time_T2 > 0.090 )
    {
       printf("total T2 recovery time exceeds 90 msec. May be too long \n");
       psg_abort(1);
    }  

   if( ncyc > 0)
    {
      tauCPMG = time_T2/(4*ncyc) - pwn_cp;
      if( ix == 1 )
      printf("nuCPMG for current experiment is (Hz): %5.3f \n",1/(4*(tauCPMG+pwn_cp)) );
    }
   else
    {
      tauCPMG = time_T2/4 - pwn_cp;
      if( ix == 1 )
      printf("nuCPMG for current experiment is (Hz): not applicable \n");
    }

   ncyc_max = time_T2/1e-3;
   if( tauCPMG + pwn_cp < 0.000250)
   {
      printf("WARNING: value of tauCPMG must be larger than or equal to 250 us\n");
      printf("maximum value of ncyc allowed for current time_T2 is: %5.2f \n",ncyc_max);
      psg_abort(1);
   }


/* PHASES AND INCREMENTED TIMES */

/*  Phase incrementation for hypercomplex 2D data, States-Haberkorn element */

    if (phase1 == 1)  {tsadd(t10,2,4); icosel = +1;}
    else              {tsadd(t3,2,4);  icosel = -1;}  


/*  Set up f1180  */
   
    tau1 = d2;
    if((f1180[A] == 'y') && (ni > 1.0)) 
	{ tau1 += ( 1.0 / (2.0*sw1) ); if(tau1 < 0.2e-6) tau1 = 0.0; }
    tau1 = tau1/2.0;



/* Calculate modifications to phases for States-TPPI acquisition          */

   if( ix == 1) d2_init = d2;
   t1_counter = (int) ( (d2-d2_init)*sw1 + 0.5 );
   if(t1_counter % 2) 
	{ tsadd(t3,2,4); tsadd(t12,2,4); }





/* BEGIN PULSE SEQUENCE */

status(A);

	obspower(tpwr);
	decpower(pwClvl);
	decpwrf(rf0);
 	dec2power(pwNlvl);
	txphase(zero);
        decphase(zero);
        dec2phase(zero);

	delay(d1);

     obspower(tpwr);
     obsoffset(tof);
     delay(0.000001);
 
/*  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  */
        rcvroff();
	dec2rgpulse(pwN, zero, 0.0, 0.0);   /*destroy N15 magnetization*/
	zgradpulse(gzlvl1, gt1);
	delay(1.0e-4);
	dec2rgpulse(pwN, one, 0.0, 0.0);
	zgradpulse(0.7*gzlvl1, gt1);
	decpwrf(rfst);
	txphase(t1);
	delay(5.0e-4);

/* apply the compensation 15N pulses if desired */
 if(comp_flg[A] == 'y') {

  dec2power(dpwr2_comp);            /* Set decoupler2 compensation power */

  timeC = time_T2*(ncyc_max-ncyc)/ncyc_max; 

  dec2rgpulse(timeC,zero,0.0,0.0);
  dec2power(pwNlvl);
  delay(1e-3);
}

   	rgpulse(pw,t1,0.0,0.0);                 /* 1H pulse excitation */

	txphase(zero);
   	dec2phase(zero);
	zgradpulse(gzlvl2, gt2);
	delay(taua - gt2);

   	sim3pulse(2.0*pw, 0.0, 2.0*pwN, zero, zero, zero, 0.0, 0.0);

   	txphase(one);
	zgradpulse(gzlvl2, gt2);
	delay(taua - gt2);

 	rgpulse(pw, one, 0.0, 0.0);
	txphase(two);
        if (tpwrsf_t<4095.0)
        {
         obspower(tpwrs+6.0);
           obspwrf(tpwrsf_t);
   	   shaped_pulse("H2Osinc_t", pwHs, zero, 5.0e-5, 0.0);
	 obspower(tpwr); obspwrf(4095.0);
        }
        else
        {   
         obspower(tpwrs);
   	 shaped_pulse("H2Osinc_n", pwHs, zero, 5.0e-5, 0.0);
	 obspower(tpwr);
        }
	zgradpulse(gzlvl5, gt5);
	dec2phase(t3);
	delay(gstab);

/*
  	dec2rgpulse(pwN, t2, 0.0, 0.0);
	delay(taub);

	sim3pulse(2.0*pw, 0.0, 2.0*pwN, zero, zero, zero, 0.0, 0.0);

	dec2phase(one);
	delay(taub);
   	dec2rgpulse(pwN, one, 0.0, 0.0);
	zgradpulse(gzlvl7, gt7);
	delay(gstab);
*/





  dec2power(dpwr2_cp);            /* Set decoupler2 power to dpwr2_cp for CPMG period */

  dec2rgpulse(pwn_cp,t2,4.0e-6,2.0e-6);

  dec2phase(zero);

  /* start of the CPMG train for first period time_T2/2 on 2NyHz */
  if(ncyc > 0) 
  {
    delay(tauCPMG - (2/PI)*pwn_cp - 2.0e-6);
    dec2rgpulse(2*pwn_cp,one,0.0,0.0);
    delay(tauCPMG);   
  }
 
  if(ncyc > 1) 
  {
  initval(ncyc-1,v4);
  loop(v4,v5);
 
    delay(tauCPMG);
    dec2rgpulse(2*pwn_cp,one,0.0,0.0);
    delay(tauCPMG);   
 
  endloop(v5);
  }
 
  /* change 2NyHz to Nx */
  delay(2.0e-6);
  zgradpulse(gzlvl6,gt6);
  delay(gstab);

  delay(taub - gt6 - gstab -2.0e-6 - pwn_cp - 10.0e-6  - pwHs -2.0*POWER_DELAY
        - WFG_START_DELAY - WFG_STOP_DELAY );

        if (tpwrsf_t<4095.0)
        {
         obspower(tpwrs+6.0);
           obspwrf(tpwrsf_t);
   	   shaped_pulse("H2Osinc_t", pwHs, t5, 5.0e-5, 0.0);
	 obspower(tpwr); obspwrf(4095.0);
        }
        else
        {   
         obspower(tpwrs);
   	 shaped_pulse("H2Osinc_n", pwHs, t5, 5.0e-5, 0.0);
	 obspower(tpwr);
        }

  /* composite 1H 90y-180x-90y on top of 15N 180x */
  dec2rgpulse(pwn_cp-2*pw,zero,2.0e-6,0.0);
  sim3pulse(pw,0.0e-6,pw,one,zero,zero,0.0,0.0);
  sim3pulse(2*pw,0.0e-6,2*pw,t6,zero,zero,0.0,0.0);
  sim3pulse(pw,0.0e-6,pw,one,zero,zero,0.0,0.0);
  dec2rgpulse(pwn_cp-2*pw,zero,0.0,2.0e-6);
  /* composite 1H 90y-180x-90y on top of 15N 180x */

        if (tpwrsf_t<4095.0)
        {
         obspower(tpwrs+6.0);
           obspwrf(tpwrsf_t);
   	   shaped_pulse("H2Osinc_t", pwHs, t5, 5.0e-5, 0.0);
	 obspower(tpwr); obspwrf(4095.0);
        }
        else
        {   
         obspower(tpwrs);
   	 shaped_pulse("H2Osinc_n", pwHs, t5, 5.0e-5, 0.0);
	 obspower(tpwr);
        }

  delay(taub - gt6 - gstab -2.0e-6 - pwn_cp - 10.0e-6 - pwHs -2.0*POWER_DELAY
        - WFG_START_DELAY - WFG_STOP_DELAY);

  delay(2.0e-6);
  zgradpulse(gzlvl6,gt6);
  delay(gstab);

  /* start of the CPMG train for second period time_T2/2 on Nx */
  if(ncyc > 1) 
  {
  initval(ncyc-1,v4);
  loop(v4,v5);
 
    delay(tauCPMG);
    dec2rgpulse(2*pwn_cp,zero,0.0,0.0);
    delay(tauCPMG);   
 
  endloop(v5);
  }
 
  if(ncyc > 0) 
  {
    delay(tauCPMG);
    dec2rgpulse(2*pwn_cp,zero,0.0,0.0);
    delay(tauCPMG - (2/PI)*pwn_cp - 2.0e-6);   
  }
 
  dec2phase(one);

  dec2rgpulse(pwn_cp,one,2.0e-6,0.0);

  delay(4.0e-6);
  dec2power(pwNlvl);            /* Set decoupler2 power back to pwNlvl */

  dec2phase(t3);

  delay(2.0e-6);
  zgradpulse(gzlvl7,gt7);
  delay(gstab);




/*  xxxxxxxxxxxxxxxxxx     N15 EVOLUTION    xxxxxxxxxxxxxxxxxxxxx  */
   	dec2rgpulse(pwN, t3, 0.0, 0.0);
	txphase(zero);
	decphase(zero);


	txphase(zero);
	dec2phase(t9);


  	if ( (c_flg[A]=='y') && (tau1 > 0.5e-3 + WFG2_START_DELAY) )
           {delay(tau1 - 0.5e-3 - WFG2_START_DELAY);     /* WFG2_START_DELAY */
            simshaped_pulse("", "stC200", 2.0*pw, 1.0e-3, zero, zero, 0.0, 0.0);
            delay(tau1 - 0.5e-3);}
	else
           {delay(tau1);
            rgpulse(2.0*pw, zero, 0.0, 0.0);
            delay(tau1);} 
 
	delay(taub);

	sim3pulse(2.0*pw, 0.0, 2.0*pwN, zero, zero, t9, 0.0, 0.0);

        zgradpulse(gzlvl8, gt8);   	/* 2.0*GRADIENT_DELAY */
	txphase(t4);
	dec2phase(t10);
	delay(taub -gt8 - 2.0*GRADIENT_DELAY +2.0*pw);



/*  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  */
	sim3pulse(pw, 0.0, pwN, t4, zero, t10, 0.0, 0.0);

	txphase(zero);
	dec2phase(zero);
	zgradpulse(gzlvl9, gt9); /*modified amp according to Frans Mulder and LEK suggestion */
	delay(taub - 1.3*pwN - gt9);

	sim3pulse(2.0*pw, 0.0, 2.0*pwN, zero, zero, zero, 0.0, 0.0);

	zgradpulse(gzlvl9, gt9);
	txphase(one);
	dec2phase(t11);
	delay(taub - 1.3*pwN - gt9);

	sim3pulse(pw, 0.0, pwN, one, zero, t11, 0.0, 0.0);

	txphase(zero);
	dec2phase(zero);
	zgradpulse(gzlvl10, gt10);
	delay(taua - 1.3*pwN - gt10);

	sim3pulse(2.0*pw, 0.0, 2.0*pwN, zero, zero, zero, 0.0, 0.0);

	dec2phase(t10);
	zgradpulse(gzlvl10, gt10);
	delay(taua - 0.65*pwN - gt10);

	rgpulse(pw, zero, 0.0, 0.0); 

	delay(0.1*gt8 + 1.0e-4 +gstab - 0.65*pw + 2.0*GRADIENT_DELAY + POWER_DELAY);

	rgpulse(2.0*pw, zero, 0.0, 0.0);

	dec2power(dpwr2);				       /* POWER_DELAY */
        zgradpulse(icosel*gzlvl11, 0.1*gt8);		/* 2.0*GRADIENT_DELAY */
        delay(gstab);
        rcvron();
statusdelay(C,1.0e-4);		


	setreceiver(t12);
}		 

