/****************************************************************/
/*	shr			03.05.2006			*/
/****************************************************************/
/*	Short Description :					*/
/*	Program to shift 1D FID right to a given no. of points 	*/
/*	New points are filled with previous FID[0] complex value*/
/****************************************************************/
/*	Keywords :						*/
/*	FFT, FID, SHIFT						*/
/****************************************************************/
/*	Description/Usage :					*/
/*	Program to shift 1D FID right to a given no. of points 	*/
/*	New points are filled with previous FID[0] complex value*/
/*	Usage: shr parm where parm is integer (number of points */
/*	or float (time in usecs). shr 0 restores original FID.  */
/*	Use it when the solid-state spectra has two distinct	*/
/*	signals with a gap between them which has <0 ordinates  */
/*	because of deadtime.					*/
/*	Original fid is stored in the file 'fid.org'		*/
/****************************************************************/
/*	Author(s) :						*/
/*	Name		: Arseny Slobodyuk			*/
/*	Organisation	: Institute of Chemistry, FEBAS 	*/
/*	Email		: ampy at ich.dvo.ru       		*/
/****************************************************************/
/*	Name		Date	Modification:			*/
/****************************************************************/

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utime.h>

int	ftmod, td, shft, parmod, mc2, sizeofint, size;
char	imag[4], filename[255], ofilename[255];
struct  stat fidstat, ofidstat;
struct  utimbuf timbuf;
const int bufsz = 1024;
float   dw;
int     i, rdsz, beg = 1, fin, fout, fidexists=0, ofidexists=0, buf[bufsz];

GETCURDATA
FETCHPARS("TD",&td)
FETCHPARS("DW",&dw)
if (strrchr(cmd,'.')) /* Float parameter: shift by a given time (usecs) */
  shft = (int) (0.5 + atof(cmd) / dw);
else /* Shift by a number of data points */
  shft=atoi(cmd);
sizeofint=sizeof(int);
size=td*2*sizeofint;

(void)sprintf(filename,"%s/data/%s/nmr/%s/%d/fid",disk,user,name,expno);
(void)sprintf(ofilename,"%s/data/%s/nmr/%s/%d/fid.org",disk,user,name,expno);

if (_stat(filename,&fidstat) == 0) fidexists = 1;
if (_stat(ofilename,&ofidstat) == 0) ofidexists = 1;

/* time to be set of corrected fid is the same as of the 'fid' file */
timbuf.modtime = timbuf.actime = fidstat.st_mtime;

if (fidexists && (!ofidexists || fidstat.st_mtime > ofidstat.st_mtime)) {
  /* A new spectrum was acquired, save a copy of it */
  if (ofidexists) unlink(ofilename);
  if (rename(filename,ofilename)) {
    Show_status("Unable to backup the FID");
    goto L_end;
  }
}

fin = open(ofilename, O_RDONLY|O_BINARY);
if (fin==-1) {
    Show_status("Unable to open the original FID");
    goto L_end;
}

fout=open(filename, O_WRONLY|O_BINARY|O_CREAT|O_TRUNC,
                         S_IREAD|S_IWRITE);
if (fout == -1) 
{
    Show_status("Unable to open the FID");
    close(fin);
    goto L_end;
}
while (rdsz=read(fin,buf,bufsz*sizeofint)) {
  if (beg) {
    for (i=0;i<shft;i++)
      if (write(fout,buf,2*sizeofint)!=2*sizeofint) {
        Show_status("Write problem");
        close(fout);close(fin);
        goto L_end;
      }
    beg = 0;
  }
  if (write(fout,buf,rdsz)!=rdsz) {
    Show_status("Copy problem");
    close(fout);close(fin);
    goto L_end;
  }
}
close(fout);close(fin);
utime(filename,&timbuf);

/* No point to store the same file */
if (!shft) unlink(ofilename);

sprintf((char *)buf,"Shifted by %d = %3.1fus / %3.1fus points",shft,shft * dw,dw);
Show_status((char *)buf);
L_end:;
QUIT
