-------------------------------------------------------------------------------
--                                                                           --
--                          Experiment Source Code                           --
--              Delta NMR Experiment & Machine Control Interface             --
--                                                                           --
--                     Copyright (c) 2021 JEOL Ltd                           --
--                            All Rights Reserved                            --
--                                                                           --
-------------------------------------------------------------------------------
-- HELP.eng: GEMSTONE COSY
-- Category: 1D, COSY, GEMSTONE,liquid_advanced
-- File name : GEMSTONE_COSY
--
-- Sequence name : GEMSTONE COSY
--
-- References :
-- [1] P. Kiraly et al, Angew. Chem. Int. Ed., 2021, 60, 666-669.
-- [2] P. Kiraly et al, Chem. Commun., 2021, 57, 2368-2371.
-- [3] D. Taylor et al, Chem. Commun., 2023, in press. 
--
-- Parameters
-- x_pulse          : 90[deg] pulse width
-- x_atn            : attenuator of x_pulse
--
-- obs_sel_180      : 180[deg] selective pulse
-- obs_sel_offset   : offset for obs_sel_180
-- obs_sel_atn      : attenuator of obs_sel_180
-- obs_sel_shape    : pulse shape of obs_sel_180
--
--
-- relaxation_delay : inter-pulse delay
-- repetition_time  : pulse repetition_time (= relaxation_delay+x_acq_time)
--
--
-- dante_presat     : select TRUE or FALSE for obs_dante_presaturation.
-- irr_mode         : select irr_mode, "OFF","Presaturation" or "Homo Decouple".
-- tri_mode         : select tri_mode, "OFF","Presaturation" or "Homo Decouple".
-- presat_time      : duration of dante presaturation.
--
-- dante parameters
--
-- dante_pulse      : pulse width of dante presaturation
-- dante_interval   : pulse interval of dante presaturation
-- dante_attenuator : attenuator of dante presaturation
-- dante_loop       : real loop number/100
--
-- irr/tri decoupling parameters
--
-- irr_domain       : nucleus of irr presaturation or homo_decoupling
-- irr_offset       : offset of irr presaturation or homo_decoupling
-- irr_attenuator   : attenuator of irr presaturation or homo_decoupling
--
-- tri_domain       : nucleus of tri presaturation or homo_decoupling
-- tri_offset       : offset of tri presaturation or homo_decoupling
-- tri_attenuator   : attenuator of tri presaturation or homo_decoupling
--
-- Note :
-- scans = 16*n
--
-- x90-PFG1-sel180-PFG1-PFG2-sel180-PFG2-mix_time-acq
-- PFG1 not = PFG2
--
--
-- END HELP

header
    filename  => "GEMSTONE_COSY";
    sample_id => "";
    comment   => "GEMSTONE COSY";
    process   =  "proton_autophase.list";
    include "header";
end header;

instrument
    include "instrument";
    SPIN_STATE => "SPIN OFF";
end instrument;

acquisition
    x_domain         => "Proton";
    x_offset         => 5[ppm];
    x_sweep          => 18[ppm];
    include "x_points_default_1d_calc";
    x_points         => 16384;

    scans            => 32, help "scans = 4*n";
    x_prescans       => 4;
    mod_return       => 4;
    include "acquisition";
end acquisition;

pulse    
    collect COMPLEX,OBS;

    obs_domain = x_domain;
    obs_offset = x_offset;
    obsfreq = _get_freq( obs_domain );

comment_1 =? "*** Pulse ***";
    x_pulse => x90, help "observe 90[deg] pulse";
    x_atn   =? xatn, help "attenuator for x_pulse";

    obs_pulse  = x_pulse;
    obs_atn    = x_atn;
	
comment_56 =? "*** GEMSTONE ***";
    obs_chpGS_shape      => "CHIRP", ad_shape_names, help "shape pulse";
    obs_chpGS_m_pulse   => 50[ms], help "ZQfilter pulse width";
    obs_chpGS_m_fsweep  => 2.5[kHz];
    obs_chpGS_sweep_ppm = (obs_chpGS_m_fsweep / obsfreq) * 1000000[ppm];
    obs_chpGS_chirp_smooth =  10[%];
    obs_chpGS_m_q0      => 11, help "q0 >= 11";
    obs_chpGS_m_ph_rev  = 1;
    obs_chpGS_b1_calc   = sqrt( obs_chpGS_m_q0 * obs_chpGS_m_fsweep / (2 * pi * obs_chpGS_m_pulse) );
    obs_chpGS_m_b1max   => obs_chpGS_b1_calc;
    obs_chpGS_pw90      =  1 / (4 * obs_chpGS_m_b1max);
    obs_chpGS_atn_calc  = obs_atn + 20[dB] * log( obs_chpGS_pw90 / obs_pulse );
    obs_chpGS_atn       => obs_chpGS_atn_calc;
	
	obs_chpGS2_m_fsweep     =  obs_chpGS_m_fsweep; 
    obs_chpGS2_m_pulse      =  obs_chpGS_m_pulse;
    obs_chpGS2_chirp_smooth =  obs_chpGS_chirp_smooth;
    obs_chpGS2_m_q0        =  obs_chpGS_m_q0;
    obs_chpGS2_m_ph_rev     =  -1, (1,-1), help "phase reverse";
    obs_chpGS2_atn          =  obs_chpGS_atn_calc;
	
	grad_sl1           =? obs_chpGS_m_pulse;
    grad_sl1_amp       => 2.5[mT/m], help "Amplitude of gradient during 1st chirp";
    grad_sl2           = obs_chpGS_m_pulse;
    grad_sl2_amp       =? -1.0*grad_sl1_amp, help "Amplitude of gradient during 2nd chirp";
	grad_shape_type    => "SQUARE", fg_shape_names, help "gradient shape";
				

comment_32 =? "*** Selective Pulse 180deg  ***";
    obs_sel180_shape       => "RSNOB_180",std_shape_names, help "shape of obs_sel_180";
    soft_shape_input    =  obs_sel180_shape;
    obs_sel_offset      =? x_offset, help "offset of obs_sel_180";
    soft_bw_input       => 100[Hz], help "bandwidth in [ppm] or [Hz] of obs_sel_180";
    include "soft_pulse_calc";    -- input : soft_shape_input, soft_bw_input, output : soft_angle, soft_pw_calc, soft_atn_calc, soft_bw_hz
    obs_sel_180         => soft_pw_calc, help "selective 180[deg] pulse";
    obs_sel180_atn         => soft_atn_calc, help "attenuator of obs_sel_180";
    obs_sel180_slp         =  obs_sel_offset;


comment_7 =? "*** Pulse Delay ***";
	initial_wait      =  1[s];
    j_hh => 10[Hz], help "1H-1H J constant ~ 8 - 12 [Hz]";
    include "relaxation_delay_1d_calc";
    relaxation_delay => relaxation_delay_default, help "inter-pulse delay";
    repetition_time  =? relaxation_delay + x_acq_time, help "relaxation_delay+x_acq_time";

comment_41 =? "*** Zero-Quantum Dephasing ***";
    obs_chp_shape         =? "CHIRP", ad_shape_names, help "shape pulse";

    obs_chp1_m_pulse      => 50[ms], (30[ms],40[ms],50[ms]), help "ZQfilter pulse width";
    obs_chp1_m_fsweep     =? if obs_chp1_m_pulse = 30[ms] then x_sweep * 2
                             else if obs_chp1_m_pulse = 40[ms] then x_sweep * 2
                             else x_sweep * 2;
    obs_chp1_sweep_ppm    =? round( obs_chp1_m_fsweep * 1000000[ppm] / obsfreq ), help "obs_chp1_ad_sweep_ppm";
    obs_chp1_chirp_smooth =  10[%];
    obs_chp1_m_q0         =  5, help "q >= 5 On resonance adiabatic Quality factor";
    obs_chp1_m_ph_rev     =  1, (1,-1), help "phase reverse";
    obs_chp1_b1_calc      =  sqrt( (obs_chp1_m_q0 * obs_chp1_m_fsweep) / (2 * pi * obs_chp1_m_pulse) );
    obs_chp1_m_b1max      =? obs_chp1_b1_calc;
    obs_chp1_pw90         =  1 / (4 * obs_chp1_m_b1max);
    obs_chp1_atn_calc     =  if obs_atn + 20[dB] * log( obs_chp1_pw90 / obs_pulse ) < obs_atn + 6[dB] then obs_atn + 6[dB]
                             else obs_atn + 20[dB] * log( obs_chp1_pw90 / obs_pulse );
    obs_chp1_atn          =? obs_chp1_atn_calc;

    obs_chp2_m_pulse      => 30[ms], (30[ms],40[ms],50[ms]), help "ZQfilter pulse width";
    obs_chp2_m_fsweep     =? if obs_chp2_m_pulse = 30[ms] then x_sweep * 2
                             else if obs_chp2_m_pulse = 40[ms] then x_sweep * 2
                             else x_sweep * 2;
    obs_chp2_sweep_ppm    =? round( obs_chp2_m_fsweep * 1000000[ppm] / obsfreq ), help "obs_chirp2_ad_sweep_ppm";
    obs_chp2_chirp_smooth =  10[%];
    obs_chp2_m_q0         =  5, help "q >= 5 On resonance adiabatic Quality factor";
    obs_chp2_m_ph_rev     =  1, (1,-1), help "phase reverse" ;
    obs_chp2_b1_calc      =  sqrt( (obs_chp2_m_q0 * obs_chp2_m_fsweep) / (2 * pi * obs_chp2_m_pulse) );
    obs_chp2_m_b1max      =? obs_chp2_b1_calc;
    obs_chp2_pw90         =  1 / (4 * obs_chp2_m_b1max);
    obs_chp2_atn_calc     =  if obs_atn + 20[dB]*log(obs_chp2_pw90/obs_pulse) < obs_atn+6[dB] then obs_atn+6[dB] else obs_atn + 20[dB]*log(obs_chp2_pw90/obs_pulse);
    obs_chp2_atn          =? obs_chp2_atn_calc;

    g_factor              =  1, help "obs_gamma/1H_gamma"; 
    coil_factor           =  1, help "coil_length/20mm";
    f_factor_zqf1         =  obs_chp1_m_fsweep / 40[kHz], help "ref 10ppm*8/500MHz=40kHz";
    grad_zqf1_amp_calc    =  47[mT/m] * f_factor_zqf1 / coil_factor /g_factor, help "gradient amplitude of grad_sl_ps";
    grad_zqf1_amp         =? grad_zqf1_amp_calc, help "Enter amplitude in Tesla/meter units";
    f_factor_zqf2         =  obs_chp2_m_fsweep / 40[kHz], help "ref 10ppm*8/500MHz=40kHz";
    grad_zqf2_amp_calc    =  47[mT/m] * f_factor_zqf2 / coil_factor / g_factor, help "gradient amplitude of grad_sl_ps";
    grad_zqf2_amp         =? grad_zqf2_amp_calc, help "Enter amplitude in Tesla/meter units";

comment_8 =? "*** Pulse Field Gradient ***";
    gradient_max       =? z_gradient_max, help "Maximum amplitude for a given probe as defined in the probe file";

    grad_1       => 1[ms], help "pulse width of PFG1";
    grad_1_amp   => 0.06[T/m], help "amplitude of PFG1";

    grad_2       =? grad_1, help "pulse width of PFG2";
    grad_2_amp   => 0.09[T/m], help "amplitude of PFG2 (not = PFG1)";

    grad_shape   => "SINE", fg_shape_names, help "shape type of PFG pulse PFG1,PFG2";
    grad_recover => 0.1[ms], help "gradient recovery time";

    grad_spoil       => 1[ms], help "duration of grad_1 during mixing";
    grad_spoil_amp   => 90[mT/m], help "Enter amplitude in Tesla/meter units";

    include "lockhold";
    include "obs_dante_presat";
    include "obs_presat_time";
    -- I think homo-decoupling should be removed, we can't use it with antiphase signals observed
    include "irr_presat_homo_dec";
    include "tri_presat_homo_dec";
    include "presat_relaxation_delay_calc";
    include "pulse";

   	phase_x      = {0,90};      -- Paul,what is this one
    phase_1      = {0,180};     --hard90 excitation
	phase_shape1 = {0};         --1st adiabaic pulse
	phase_shape2 = {0,0,90,90}; --rsnob
	phase_shape3 = {0};         --2nd adiabatic pulse
	phase_2      = {0};         --hard180 in COSY transfer
    phase_3      = {90};         --COSY read pulse
	phase_acq    = {0,180,180,0};  
	
    
    module_config = if irr_mode = "Homo Decouple" then
                        if tri_mode = "Homo Decouple" then
                             "irr.time_share tri.time_share obs.blank_inhibit"
                        else "irr.time_share obs.blank_inhibit"
                    else if tri_mode = "Homo Decouple" then
                         "tri.time_share obs.blank_inhibit"
                    else "";
begin
    initial_wait;

    relaxation_delay_calc;

    --irr_presaturation block on
    when irr_mode /= "Off" do
        on (irr.gate, irr.phs.0, irr.atn.irr_attenuator);
    end when;
    --tri_presaturation block on
    when tri_mode /= "Off" do
        on (tri.gate, tri.phs.0, tri.atn.tri_attenuator);
    end when;

    obs_presat_time(dante_presat, dante_loop, dante_pulse, dante_interval,
                    phase_dante, phs_shft, dante_attenuator, presat_time);
    --irr presaturation block off
    when irr_mode /= "Off" do
        off (irr.gate);
    end when;
    --tri presaturation block off
    when tri_mode /= "Off" do
        off (tri.gate);
    end when;

    --lock_hold on
    when lock_hold do
        on(lockhold);
    end when;

    1[us];
		
	x_pulse, (obs.gate, obs.phs.phase_1, obs.atn.x_atn);
    
	3[ms];
	
    parallel
    --begin
	justify center
	     grad_sl1, (fgz.gate, fgz.amp.grad_sl1_amp, fgz.shape."SQUARE");
    justify center
         obs_chpGS_m_pulse, (obs.gate, obs.phs.phase_shape1, obs.atn.obs_chpGS_atn, obs.shape.{obs_chpGS_shape,"obs_chpGS"});
    end parallel;
	
	3[ms];
	
    grad_1, (fgz.gate, fgz.amp.grad_1_amp, fgz.shape.grad_shape_type);
    grad_recover;


    obs_sel_180, (obs.gate, obs.phs.phase_shape2, obs.atn.obs_sel180_atn,
                  obs.laminar.obs_sel180_slp, obs.shape.obs_sel180_shape);

      
    grad_1, (fgz.gate, fgz.amp.grad_1_amp, fgz.shape.grad_shape_type);
    grad_recover;   
   
    3[ms];
    parallel
    --begin
	justify center
	    grad_sl2, (fgz.gate, fgz.amp.grad_sl2_amp, fgz.shape."SQUARE");
    justify center
	    obs_chpGS_m_pulse, (obs.gate, obs.phs.phase_shape3, obs.atn.obs_chpGS2_atn, obs.shape.{obs_chpGS_shape,"obs_chpGS2"});
    end parallel;
    3[ms];


	1/(4*j_hh);	
    x_pulse*2, (obs.gate, obs.phs.phase_2, obs.atn.x_atn);    
    1/(4*j_hh);
    x_pulse, (obs.gate, obs.phs.phase_3, obs.atn.x_atn);
        
    acq( dead_time, delay, phase_acq );

    --lock_hold off
    when lock_hold do
        off(lockhold);
    end when;
end pulse;
