/* sherryts.c
 *                *
 *              Emilio          4/1/99
 *                Updated program.
 *              Emilio          3/3-9/99
 *                Wrote program. Debugged and ran.
 *                *
 * COMPILE AS: gccemu sherryts
 * RUN AS: sherryts -o <name of output file>
 ******************************************************************************/

#include "emu.h"

const char * cpszUsage = {"usage: sherryts \n\
\n\
Command-line switches specific to this program are:\n\
\n\
"};


/* local functions */
void Setup(void);
void Process(void);
void Output(void);

/* local variables */
short ***out;
int MaxTotPixels;


int main(int argc, char *argv[])
{
  Initialize(&argc, argv, DOMASK);

  Setup();
  Process();
  Output();

  CleanUp();

  return(0);
}


/* ================================================================= */
void Setup(void)
{
  int n, v, p, i;

  MaxTotPixels = 0;
  for (n=0; n < FlMskSitesN; n++)
    if (Masks[n].cellcount > MaxTotPixels)
      MaxTotPixels = Masks[n].cellcount;

  /* allocate memory and initialize out[][][] to 0.0  */
  out = alloc3d_s(0,MskSitesN-1,0,nVars-1,0,MaxTotPixels-1);
  for (n=0; n < MskSitesN; n++)
    for (v=0; v < nVars; v++)
      for (i=0; i < MaxTotPixels; i++)
        out[n][v][i] = 0;
}
/* ================================================================= */
void Process(void)
{
  int v, n, p, i, sindex;    int bug=0;
  float *x;
  short *pvals;


  x = alloc1d_f(0,nRows*nCols-1);
  pvals = alloc1d_s(0,MskSitesN-1);


  for (v=0; v < nVars; v++)
  {
    ReadXY(x, pstVars[v], NONE, NONE);

    for (p=0; p < MskSitesN; p++)
      pvals[p] = 0;

    for (i=0; i < nRows*nCols; i++)
      if (masktest(pMask[i]))
      {
        sindex = siteindex(FlMskSitesIDs, pMask[i]);

        out[sindex][v][pvals[sindex]++] = (short) x[i];
      }
  }  /* END v FOR-LOOP (loop thru each variable) */


  free1d_f(x,0,nRows*nCols-1);
  free1d_s(pvals,0,MskSitesN-1);
}
/* ================================================================= */
void Output(void)
{
  FILE  *fout;
  int v, n, p = 0;    int bug=0;


  fout = OUTPUTfile(pszOutputFile, ".spect");

  /* write out headers */

  for (n=0; n < MskSitesN; n++)
    for (v=0; v < nVars; v++)
      fprintf(fout, "%d,", MskSitesIDs[n]);

  if (nVars > 1)
  {
    fprintf(fout, "\n");
    for (n=0; n < MskSitesN; n++)
      for (v=0; v < nVars; v++)
        fprintf(fout, "band %d,", v+1);
  }

  /* write out data (out[][][]) */
  for (p=0; p < MaxTotPixels; p++)
  {
    fprintf(fout, "\n");

    for (n=0; n < MskSitesN; n++)
      if (p < Masks[n].cellcount)
        for (v=0; v < nVars; v++)
          fprintf(fout, "%d,", out[n][v][p]);
      else
        fprintf(fout, " ,");

  } /* END p FOR-LOOP */

  fclose(fout);
}

/* ================================================================= */
void ProcessCommandLineArgs(int * pArgc, char * argv[])
{
}
/* ================================================================= */
void LocalCleanUp(void)
{
  free3d_s(out,0,MskSitesN-1,0,nVars-1,0,MaxTotPixels-1);
}