/***/
/* maxPlane.M: report 3D plane loc with max abs intensity.
/*
/* Requires command-line definition of strings "inName".
/*   M maxPlane.M -str inName vkbak1/ir%03.0f.dat -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;
    FDQUADFLAG   = 106;
    NULL_DIM     =   0;

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

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

    inFD  = dOpen( thisName, "r" );

    (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 );

    itemp    = getParm( fdata, FDQUADFLAG, NULL_DIM );

    if (itemp == 0 || itemp == 2)
       {
        quadState = 2;
       }
    else
       {
        quadState = 1;
       };

    if (dimCount != 3)
       {
        (void) printf( "Error: 3D data is required.\n" );
        exit( 1 );
       };

    count = xSize*ySize*quadState/4;

/***/
/* For each plane:
/*  Read the plane.
/*  Find the max abs value.
/***/

    float buff[count];

    maxVal = 0.0;
    maxLoc = 1;

    for( iz = 1; iz <= zSize; iz++ )
       {
        (void) sprintf( thisName, inName, iz );

        inFD = dOpen( thisName, "r" );

        (void) dSeek( inFD, wordLen*FDATASIZE );
        (void) dRead( inFD,  buff, wordLen*count );
        (void) dClose( inFD );

        (void) vAbs( buff, count );

        thisMax = vMedian( buff, count );

        if (thisMax > maxVal)
           {
            maxVal = thisMax;
            maxLoc = iz;
           };
       }

    (void) printf( "%.0f\n", maxLoc );
