/***/
/* xa4d.M: extract the first XA plane of a 4D hypercomplex FID.
/*
/* Requires command-line definition of strings "inName" and "outName",
/* and variables "xaYLoc" and "xaZLoc"  e.g.:
/*
/*   M xa4d.M -str inName fid/test%02.0f%03.0f.fid outName xa.fid -var xaYLoc 1 xaZLoc 1 -quit 
/***/

/***/
/* Parameters taken from "fdatap.h"; these will be pre-defined
/* in future implementations of "M":
/***/

    FDATASIZE = 512;

    float fdata[FDATASIZE];
    char  thisName[256];

    FDDIMCOUNT   =   9;
    FDDIMORDER1  =  24;
    FDDIMORDER2  =  25;
    FDDIMORDER3  =  26;
    FDDIMORDER4  =  27;
    FDFILECOUNT  = 442;
    NULL_DIM     = 0;

/***/
/* Open the first 4D plane.
/* Read the header from the first 4D plane.
/* Extract the size parameters.
/***/

    (void) printf( thisName, inName, 1, 1 );

    inFD  = dOpen( thisName, "r" );
    outFD = dOpen( outName,  "w" );

    (void) dReadHdr( inFD, fdata, 0 );
    (void) dClose( inFD );

    dimCount = getParm( fdata, FDDIMCOUNT, NULL_DIM );

    xSize = getParm( fdata, NDSIZE, CUR_XDIM );
    ySize = getParm( fdata, NDSIZE, CUR_YDIM );
    zSize = getParm( fdata, NDSIZE, CUR_ZDIM );
    aSize = getParm( fdata, NDSIZE, CUR_ADIM );

    if (dimCount < 4)
       {
        (void) printf( "Error: the dimension count is too small.\n" );
        exit( 1 );
       };

/***/
/* Adjust header of input to reflect an output with Y and A axis exchanged:
/***/

    (void) setParm( fdata, NDSIZE, aSize, CUR_YDIM );
    (void) setParm( fdata, NDSIZE, ySize, CUR_ZDIM );

    yID = getParm( fdata, FDDIMORDER2, NULL_DIM );
    aID = getParm( fdata, FDDIMORDER4, NULL_DIM );

    (void) setParm( fdata, FDDIMORDER2, aID, NULL_DIM );
    (void) setParm( fdata, FDDIMORDER4, yID, NULL_DIM );

    (void) setParm( fdata, FDFILECOUNT,  ySize*zSize, NULL_DIM );

    (void) dWrite( outFD, fdata, wordLen*FDATASIZE );

/***/
/* Read the first complex 1D vector from every XY plane in with Z=1:
/***/

    float buff[xSize*2];

    iz = xaZLoc;

    for( ia = 1; ia <= aSize; ia++ )
       {
        (void) printf( thisName, inName, ia, iz );

        inFD   = dOpen( thisName, "r" );
        offset = wordLen*2*xSize*(xaYLoc - 1);

        (void) dSeek( inFD, wordLen*FDATASIZE + offset );
        (void) dRead(  inFD,  buff, wordLen*xSize*2 );
        (void) dWrite( outFD, buff, wordLen*xSize*2 );
        (void) dClose( inFD ); 
       }

    (void) dClose( outFD );
