/***/
/* projZ.M: prepare a 2D projection from a 3D spectrum, collapsed along
/*          the Z-Axis.  Only points above a given threshold are considered.
/*
/* Requires command-line definition of: "inName", "outName" and "thresh":
/*
/*   M projZ.M -str inName  ft/test%03.0f.ft3  \
/*                  outName projz.ft2          \
/*             -var thresh 1.2e3 -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 3D plane.
/* Read the header from the first 4D plane.
/* Extract the size parameters.
/***/

    (void) sprintf( thisName, inName, 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 );

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

    if (dimCount > 3)
       {
        (void) printf( "Error: the dimension count is too large.\n" );
        exit( 1 );
       };

    (void) setParm( fdata, NDSIZE,       1, CUR_ZDIM );
    (void) setParm( fdata, FDFILECOUNT,  1, NULL_DIM );

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

    (void) printf( "Sizes %.0f x %.0f x %.0f\n",
                   xSize, ySize, zSize );

/***/
/* For each plane:
/*  Read the plane.
/*  Clip the values below the threshold.
/*  Add the plane to the projection buffer.
/***/

    float projBuff[xSize*ySize], inBuff[xSize*ySize];

    (void) vsSet( projBuff, xSize*ySize, 0.0 );

    length = xSize*ySize;

    for( iz = 1; iz <= zSize; iz++ )
       {
        (void) sprintf( thisName, inName, iz );
        (void) printf( "Plane %.0f of %3.0f\n", iz, zSize );

        inFD = dOpen( thisName, "r" );

        (void) dSeek( inFD, wordLen*FDATASIZE );
        (void) dRead(  inFD,  inBuff, wordLen*length );
        (void) dClose( inFD ); 

        for( ix = 0; ix < length; ix++ )
           {
            if ((inBuff[ix] > -thresh) && (inBuff[ix] < thresh))
               {
                inBuff[ix] = 0.0;
               };
           } 

        (void) vvAdd( projBuff, inBuff, length );
       }

    (void) dWrite( outFD, projBuff, wordLen*length );
    (void) dClose( outFD );
    (void) printf( "Done.\n" );
