/***/
/* bruk_ranceZ.M: Rance-mode gradient shuffling for Z-Axis. (Bruker)
/*                Work-around for zLoc/aLoc 4D bug.
/***/

/* Process only every pair of cubes, one real, one imaginary:  */

thisALoc = 1 + integer( zLoc/zSize );
thisZLoc = 1 + ((zLoc - 1) % zSize);

if (yLoc != 1)   
   {
    exit( 0 );
   };

if (thisZLoc != 1)
   {
    exit( 0 );
   };

if (!(thisALoc % 2))
   {
    exit( 0 );
   };

float cubeR[size*specnum*zSize*2], cubeI[size*specnum*zSize*2];
float sumR[size], sumI[size], difR[size], difI[size];

(void) dReadB( inUnit, cubeR, wordLen*size*specnum*zSize*2 );
(void) dReadB( inUnit, cubeI, wordLen*size*specnum*zSize*2 );

offset = 0;

for( i = 0; i < specnum*zSize; i++ )
   {
    /* Move vectors from real and imaginary cubes:                 */

    (void) vvCopyOff( sumR, cubeR, size, 0, offset );
    (void) vvCopyOff( sumI, cubeR, size, 0, offset+size );
    (void) vvCopyOff( difR, cubeI, size, 0, offset );
    (void) vvCopyOff( difI, cubeI, size, 0, offset+size );

    /* Form adjusted sums and differences:                         */

    (void) vvCopy( rdata, sumR, size );
    (void) vvCopy( idata, sumI, size );

    (void) vvSub( sumR, difR, size );
    (void) vvSub( sumI, difI, size );
    (void) vvAdd( difR, rdata, size );
    (void) vvAdd( difI, idata, size );

    (void) vNeg( difI, size );   /* Alternative to 90 degree phase. */
    
    /* Insert adjusted vectors back into real and imaginary cubes:  */

    (void) vvCopyOff( cubeR, sumR, size, offset,      0 );
    (void) vvCopyOff( cubeR, sumI, size, offset+size, 0 );
    (void) vvCopyOff( cubeI, difI, size, offset,      0 );
    (void) vvCopyOff( cubeI, difR, size, offset+size, 0 );

    offset += 2*size;
   }

(void) dWrite( outUnit, cubeR, wordLen*size*specnum*zSize*2 );
(void) dWrite( outUnit, cubeI, wordLen*size*specnum*zSize*2 );
