/***/
/* absIR.M: IR absorbance correction for 2D plane series.
/*
/*   Requires Command-Line Definition of these variables:
/*     string specName   Spectral Input Template.
/*     string bgName     Background Input Template.
/*     string outName    Corrected Output Template.
/*     float  scale      Scale factor for background.
/***/

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

    FDATASIZE = 512;

    char  thisInName[256], thisBGName[256], thisOutName[256];
    float fdata[FDATASIZE];

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

    (void) sprintf( thisInName, specName, 1 );

    specFD  = dOpen( thisInName, "r" );

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

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

    count = xSize*ySize;

/***/
/* For each plane:
/*  Read the spectral and background planes.
/*  Find the min value if the plane is inside the correction range.
/***/

    float specBuff[count], bgBuff[count];

    for( iz = 1; iz <= zSize; iz++ )
       {
        (void) sprintf( thisInName,  specName, iz );
        (void) sprintf( thisBGName,  bgName, iz );
        (void) sprintf( thisOutName, outName, iz );

        print thisOutName, "\r"

        specFD  = dOpen( thisInName,  "r" );
        bgFD    = dOpen( thisBGName,  "r" );
        outFD   = dOpen( thisOutName, "w" );

        (void) dSeek( specFD,  wordLen*FDATASIZE );
        (void) dRead( specFD,  specBuff, wordLen*count );

        (void) dSeek( bgFD,  wordLen*FDATASIZE );
        (void) dRead( bgFD,  bgBuff, wordLen*count );

        (void) vsMult( bgBuff, count, scale );
        (void) vsAdd( bgBuff, count, 1.0 );
        (void) vvDiv( specBuff, bgBuff, count );
        (void) vLog( specBuff, count );
        (void) vNeg( specBuff, count );

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

        (void) dClose( specFD );
        (void) dClose( bgFD );
        (void) dClose( outFD );
       }

   print "\n";
   print "Done.\n";
