/****************************************************************/
/* au_qcpmg 01.02.2003                      */
/****************************************************************/
/* Short Description : */
/* Program to store 0th. fid and echos from */
/* qcpmgall.av experiment */
/* calculating everything from a pivot point */
/****************************************************************/
/* Keywords : */
/* quadrupolar echo, CPMG */
/****************************************************************/
/* Description/Usage: */
/* After data acquisition using qcpmg_all.av */
/* check first data point where sampled intensity grows */
/* out of noise. Everything else is calculated from  */
/* this pivot point. */
/* Program takes out data points which have been sampled */
/* during pulses and ring down delays. */
/* After first execution of the program the value of the */
/* pivot point is stored and suggested on subsequent */
/* executions so that the question for the pivot point */
/* can be answered with a simple 'return'. */
/* Program stores result as time domain data into 1r */
/* and 1i files for further processing, */
/* raw data are kept! */
/****************************************************************/
/* Author(s) : */
/* Name : Stefan Steuernagel */
/* Organisation : Bruker Analytik */
/* Email : stefan.steuernagel@bruker.de */
/****************************************************************/
/* Name Date Modification: */
/* ste 030201 created */
/****************************************************************/


int size, sizeout, si2, tdout, td2, ireal, sizeofint, parmod, zeroth;
int wdw2,pk2,bc2,ft2,mem2;
int pivot;
char binfile[255], pulprog[255];
int *in1, *in2;
int order_a_BIT, first, echopoints, echos, l22, dead;
float del3, del6, pul3, pul4, dwell;
double swh;
char s1[255];

GETCURDATA;

/* ---------------------------------------------*/
/* make sure program works on proper data set */

FETCHPARS("PARMODE",&parmod);
if (parmod != 0) STOPMSG(" Program only works on 1D data");
FETCHPARS("PULPROG",pulprog);
if (strcmp(pulprog,"qcpmgall.av")!=0) { Proc_err(0,"Wrong AU program for %s pulse program",pulprog);
return(AUERR);}

/* ---------------------------------------------*/
/* make sure that pivot point can be entered */

(void) sprintf(s1,"%s %s","au_qcpmg AU program","- Note \n\
- If you don't know the pivot point yet,\n\
  press Cancel and check with\n\
  'calibrate'\n\
- otherwise press OK and continue");
FETCHPARS("FL1",&pivot);
if (pivot>80) {
 AUERR=Proc_err(ERROPT_AK_CAN|ERROPT_BEEP_DEF,"%s",s1);
 if (AUERR == ERR_CANCEL) ABORT
}

/* ---------------------------------------------*/
/* get relevant acquisition parameters */

FETCHPARS("TD",&td2);
FETCHPARS("SI",&si2);
FETCHPARS("SW_h",&swh);
FETCHPARS("D 6",&del6);
FETCHPARS("D 3",&del3);
FETCHPARS("P 3",&pul3);
FETCHPARS("P 4",&pul4);
FETCHPARS("L 22",&l22);
FETCHPARS("DW",&dwell);
FETCHPARS("BYTORDA",&order_a_BIT);

/* ---------------------------------------------*/
/* check for starting point */

if (pivot>80) pivot=(int)((10/dwell));
if (strlen(cmd) == 0) 
{
    GETINT("Enter pivot point : ",pivot);
}
else
{
    if (1 != sscanf(cmd, "%d", &pivot))
    {
STOPMSG("illegal input");
    }
}

/* ---------------------------------------------*/
/* calculate required loop parameters */

zeroth=(int)((pul3+del3*2000000.0+pul4)/dwell+0.5)+pivot;
zeroth=(zeroth/2)*2;
first=(int)((del6*1000000.0+del3*2000000.0+pul4)/dwell+0.5)+zeroth;
first=(first/2)*2;
echopoints=(int)(del6*2000000.0/dwell+0.5);
echopoints=(echopoints/2)*2;
dead=(int)((del3*2000000.0+pul4)/dwell+0.5)+2;
dead=(dead/2)*2;
echos=l22;
tdout=echopoints*echos+echopoints/2;
sizeout=tdout*sizeof(int);

/* ---------------------------------------------*/
/* open file and allocate array  */

(void)sprintf(binfile,"%s/data/%s/nmr/%s/%d/fid",disk,user,name,expno);
sizeofint=sizeof(int);
size=td2*sizeofint;
in1=calloc(td2,sizeof(int));
in2=calloc(td2,sizeof(int));

if ( (ireal=open(binfile, O_RDWR)) == -1) {
Perror(DEF_ERR_OPT,binfile);
ABORT;
}
if ( read(ireal,in1,size) == -1 ) {
Perror(DEF_ERR_OPT,binfile);
ABORT;
}

/* ---------------------------------------------*/
/* create final output array   */

i1=zeroth;
i2=0;

TIMES2(echopoints/2);
in2[i2]=in1[i1];
i2++;
i1++;
END;

i1=first;

TIMES(echos);
TIMES2(echopoints);
in2[i2]=in1[i1];
i2++;
i1++;
END;
i1+=dead-2;
END;

if ( lseek(ireal, 0, SEEK_SET) == -1 ) {
Perror(DEF_ERR_OPT,binfile);
ABORT;
}
if ( write(ireal,in2,sizeout) == -1 ) {
Perror(DEF_ERR_OPT,binfile);
ABORT;
}

/* ---------------------------------------------*/
/* correct relevant acquisition parameters */

STOREPARS("TD",tdout);
STOREPAR("TDeff",tdout);
STOREPAR("SI",tdout*2);

/* ---------------------------------------------*/
/* prepare for processing */

FETCHPAR("WDW",&wdw2)
FETCHPAR("PH_mod",&pk2)
FETCHPAR("BC_mod",&bc2)
FETCHPAR("FT_mod",&ft2)
FETCHPAR("ME_mod",&mem2)

/* ---------------------------------------------*/
/* set required parameters to NO */

STOREPAR("WDW",0)
STOREPAR("PH_mod",0)
STOREPAR("BC_mod",0)
STOREPAR("FT_mod",0)
STOREPAR("ME_mod",0)

/* ---------------------------------------------*/
/* store new FID into 1r and 1i */

TRF;

/* ---------------------------------------------*/
/* restore original FID */

if ( lseek(ireal, 0, SEEK_SET) == -1 ) {
Perror(DEF_ERR_OPT,binfile);
ABORT;
}
if ( write(ireal,in1,size) == -1 ) {
Perror(DEF_ERR_OPT,binfile);
ABORT;
}

/* ---------------------------------------------*/
/* restore original parameters */

STOREPAR("WDW",wdw2)
STOREPAR("PH_mod",pk2)
STOREPAR("BC_mod",bc2)
STOREPAR("FT_mod",ft2)
STOREPAR("ME_mod",mem2)
STOREPARS("TD",td2);
STOREPAR("TD",td2);
STOREPAR("TDeff",td2);
STOREPARS("FL1",pivot);

free(in1);
free(in2);
(void)close(ireal);
(void)sprintf(text,"QCPMG transformation done");
Show_status(text);
QUIT