
/***/
/* Align vectors in interfergram with first vector: 
/*
/* | nmrPipe -fn MAC -macro findShift.M 
/*           -var firstShift -10 lastShift 10 shiftStep 1 
/*           -str shiftX1 0.5ppm shiftXN -0.4ppm
/***/

ix1 = spec2pnt( fdata, CUR_XDIM, shiftX1 );
ixn = spec2pnt( fdata, CUR_XDIM, shiftXN );

if (ix1 < 1)    {ix1 = 1;};
if (ix1 > size) {ix1 = size;};
if (ixn < 1)    {ixn = 1;};
if (ixn > size) {ixn = size;};

if (ix1 > ixn) { i = ix1; ix1 = ixn; ixn = i; };

n = 1 + ixn - ix1;

if (yLoc == 1)
   {
    float firstR[size], firstI[size];

    (void) vvCopyOff( firstR, rdata, n, 0, ix1-1 );
    (void) vvCopyOff( firstI, idata, n, 0, ix1-1 );
    (void) modulus( firstR, firstI, n );
    refMax = vMax( firstR, n );
    exit( 0 );
   };

float sR[size], sI[size], qR[size], qI[size];

if (lastShift < firstShift) { s = firstShift; firstShift = lastShift; lastShift = s; };
if (shiftStep <= 0) { shiftStep = 1; };

bestScore = 1.0e+16;
bestShift = 0;

(void) vvCopyOff( qR, rdata, n, 0, ix1-1 );
(void) vvCopyOff( qI, idata, n, 0, ix1-1 );
(void) modulus( qR, qI, n );

thisMax = vMax( qR, n );

if (near( thisMax, 0.0, 1.0e-16 )) { thisMax = 1.0; };

for( thisShift = firstShift; thisShift <= lastShift; thisShift += shiftStep )
   {
    (void) vvCopy( sR, rdata, size );
    (void) vvCopy( sI, idata, size );

    (void) fShift( sR, size, -thisShift );  
    (void) fShift( sI, size, -thisShift );  

    (void) vvCopyOff( qR, sR, n, 0, ix1-1 );
    (void) vvCopyOff( qI, sI, n, 0, ix1-1 );

    (void) modulus( qR, qI, n );
    (void) vsMult( qR, n, refMax/thisMax );
    (void) vvSub( qR, firstR, n );

    s = vSumSq( qR, n );

    if (s < bestScore) 
       {
        bestShift = thisShift;
        bestScore = s;
       };
   }

(void) printf( " Vector: %4.0f Shift: %5.2f\n", yLoc, bestShift );

(void) fShift( rdata, size, -bestShift );  
(void) fShift( idata, size, -bestShift );  

