/******************************************************************* splitadd.c (Topspin >= 3.5) AU program for splitting an echo train into a series of 1D files, each containing a single full echo. Each echo will then be coadded into a single 1D experiments. The temporary 1D files will be deleted after coaddition. The final spectra can be found at procno start_procno+l22+1. Changes: 06.12.2019 - Max Busskamp - Initial commit 02.03.2021 - Max Busskamp - Added comments and cleanup 01.04.2021 - Max Busskamp - Input lengths are now floats - Moved automatic read-in of blanking time to before manual input - otherwise manual input will always be overwritten - Added an echocheck variable to ensure correct ratio between dwelltime and echo/blanking length - Restructured safety checks ********************************************************************/ int size, sizeout, si2, tdout, td2, sizeofint, parmod; int ireal, irealecho, startExpno; int order_a_BIT, echotop, echopoints, echos, l22, startpoint, td2_double, blankpoints, points, point_multi, echo_cutoff, mode; int *in1, *in2; char binfile[255], boutfile[255], pulprog[255]; char s1[255]; float echo_len, pulse_len, dead_time, dwell, mas, blank_len, test_var, echocheck, blankcheck; double swh; GETCURDATA; startExpno = expno; /* ---------------------------------------------*/ /* make sure program works on proper data set */ FETCHPARS("PARMODE",&parmod); if (parmod != 0) STOPMSG(" Program only works on 1D data"); /* ---------------------------------------------*/ /* get relevant acquisition parameters */ FETCHPAR("TD",&td2); FETCHPARS("SI",&si2); FETCHPARS("SW_h",&swh); FETCHPARS("L 22",&l22); FETCHPARS("DW",&dwell); FETCHPARS("BYTORDA",&order_a_BIT); FETCHPARS("D 7",&echo_len); FETCHPARS("P 1",&pulse_len); FETCHPARS("DE",&dead_time); /* ---------------------------------------------*/ /* Debugging Output */ /*sprintf(text,"echo_len: %f\npulse_len: %f\ndead_time: %f", echo_len, pulse_len, dead_time); STOPMSG(text);*/ /* ---------------------------------------------*/ /* standard values */ point_multi=2; echo_cutoff=0; mode=1; /* ---------------------------------------------*/ /* check for starting point */ GETINT("Point Multiplier (Neo=2; Avance=1): ",point_multi); GETINT("Use automatic input of timings (yes=1, no=0): ",mode); GETINT("Enter Position of Echotop: ",echotop); if(point_multi==2) blank_len=pulse_len+dead_time+dead_time; if(point_multi==1) blank_len=pulse_len+dead_time+dead_time+2.0; if (mode==0) GETFLOAT("WARNING! Dividing echo and blanking length by \n'2 * Dwell Time'\nhas to result in even numbers for both!\nEnter echo length in us: ",echo_len); if (mode==0) GETFLOAT("WARNING! Dividing echo and blanking length by \n'2 * Dwell Time'\nhas to result in even numbers.\nEnter blanking time in us: ",blank_len); GETINT("Echo-Cutoff (l22 - Echo-Cutoff = Coadded Echos): ",echo_cutoff); /* ---------------------------------------------*/ /* calculate required loop parameters */ if (mode!=0) echo_len=echo_len*1000000; echopoints=echo_len/dwell*point_multi; blankpoints=blank_len/dwell*point_multi; echocheck=echo_len/(dwell*point_multi); blankcheck=blank_len/(dwell*point_multi); points=echopoints+blankpoints; startpoint=(echotop*point_multi)-(echopoints/2); echos=l22+1-echo_cutoff; tdout=td2*point_multi; td2_double=td2*2*point_multi; sizeout=tdout*sizeof(int); if ( (fmod( echocheck, 2.0 ) != 0.0) || (fmod( blankcheck, 2.0 ) != 0.0) ) STOPMSG("WARNING! Divison of echo and/or blanking length by: \n'Point Multiplier * Dwell Time' \nhas not resulted in even numbers!\nUse manual timing mode!"); /* ---------------------------------------------*/ /* open file and allocate array */ (void)sprintf(binfile,"%s/%s/%d/fid",disk,name,expno); sizeofint=sizeof(int); size=td2_double*sizeofint; in1=calloc(td2_double,sizeof(int)); in2=calloc(td2_double,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 output arrays */ i2 = startExpno + 1; TIMES(echos); WRA(i2) /* no spectrum saved, FID only */ i2++; END; i1 = startpoint; i3 = startExpno + 1; TIMES(echos); (void)sprintf(boutfile,"%s/%s/%s/%d/fid",disk,user,name,i3); if ( (irealecho=open(boutfile, O_RDWR)) == -1) { Perror(DEF_ERR_OPT,boutfile); ABORT; } i2=0; TIMES2(td2_double); in2[i2]=0; /* clear data */ i2++; END; i2=0; TIMES2(points); if (i2 < echopoints) { in2[i2]=in1[i1]; /* copy echo data */ }else { in2[i2]=0; /* write zeros if receiver is blanked */ } i2++; i1++; END; if ( lseek(irealecho, 0, SEEK_SET) == -1 ) { Perror(DEF_ERR_OPT,boutfile); ABORT; } if ( write(irealecho,in2,sizeout) == -1 ) { Perror(DEF_ERR_OPT,boutfile); ABORT; } (void)close(irealecho); i3++; /* next file */ END; /* ---------------------------------------------*/ /* close file and clear memory */ (void)close(ireal); free(in2); free(in1); /* ---------------------------------------------*/ /* Start FIDADD */ int oexpno, newexpno, scans, totscans=0, startexpno, endexpno; GETCURDATA; expno = expno+1; startexpno = expno+1; oexpno = expno; while (access(ACQUPATH("fid"), F_OK) == 0) expno++; endexpno=expno; i1 = expno - oexpno; newexpno = expno; expno = oexpno; WRA(newexpno) ERRORABORT TIMES(i1) SETCURDATA FETCHPARS("NS", &scans) totscans += scans; IEXPNO END DATASET(name, newexpno, procno, disk, user) DATASET2(name, newexpno, procno, disk, user) STOREPAR("DC", 1.0) STOREPARS("NS", totscans) STOREPAR("TI", "result of fidadd") STOREPARS("TI", "result of fidadd") /* ---------------------------------------------*/ /* Remove temporary files */ /* Comment this part, if you want to check each echo alone */ TIMES(i1-1) DATASET3(name, ++oexpno, procno, disk, user) ADDFID DELETEEXPNO(name, oexpno, disk, user) ERRORABORT END DELETEEXPNO(name, startexpno-1, disk, user) /* ---------------------------------------------*/ /* set TD and SI values to exact echo length */ STOREPAR("TD",echopoints/point_multi); STOREPARS("TD",echopoints/point_multi); STOREPAR("SI",echopoints*8); STOREPARS("SI",echopoints*8); /* ---------------------------------------------*/ /* show final dataset */ VIEWDATA /* ---------------------------------------------*/ /* show final number of scans */ Proc_err(DEF_ERR_OPT, "--- %d FID's added with\n" " total of %d scans ---", i1, totscans); QUIT