/* ---------------------------------------------------------------------

   PWS16PLT written by L. Granroth
            modified and additional code by R.Baines  7/89

   Plots a channel of data. This file contains Two Sets of Functions.
   The first is the function to do normal plots of averages, peaks, etc...
   The second does color scaling of the averages.

   --------------------------------------------------------------------- */


/* Includes */
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "pwstruct.h"
#include "pwsglobl.h"
#include "pwsgraph.h"
#include "proto.h"

/* Internal Functions */
int proc_data(int);
int findcolor(int);

/* Internal Global Vars */
static int data[900];

/*--------------------------------------------------------------------

  pws_plot  written by L. Granroth
            modified for PC by B. Baines 07-26-89

  to plot data for the PWS16PC program.

---------------------------------------------------------------------*/

void pws16_plot (void)
{
  int i, ibin, fx, fy, newmag, leftXpos, rightXpos, x, y,
      leftXpos_tic, rightXpos_tic;
  boolean gap;
  double t;

  /* -------------------- pws16_plot---------------------- */

  if (fpOut)
     fileout(Dat.position, Dat.data, Dat.datmin, Dat.datmax);


    /* shift up to next channel if not doing first channel */
  Ypos = Yorg-Zmul*(Dat.position-Cmin);

    /* Set values used in displaying tics */
  leftXpos = Xorg-1;
  rightXpos = Xorg+NumWide;
  leftXpos_tic = leftXpos - Tic;
  rightXpos_tic = rightXpos + Tic;

    /* Bottom Tics on Sides */
  setcolor(Lcolor);
  if (Dat.position == Cmin)
  {
     line(leftXpos, Yorg+1, leftXpos-LongTic, Yorg+1);
     line(rightXpos, Yorg+1, rightXpos+LongTic, Yorg+1);
  }
  else
  {
     line(leftXpos, Ypos, leftXpos-LongTic, Ypos);
     line(rightXpos, Ypos, rightXpos+LongTic, Ypos);
  }

  /* if NOT GyroFrequency then plot left and right tics */
  if (Dat.flag != GYROFREQUENCY)
    for (i=0; Ztic[i] > 0; i++)
    {
      line(leftXpos, Ypos-Ztic[i], leftXpos_tic, Ypos-Ztic[i]);
      line(rightXpos, Ypos-Ztic[i], rightXpos_tic, Ypos-Ztic[i]);
    }

  Xpos = Xorg;
  /* Plot data */
  switch (Dat.flag)
  {
    /* averages */
    case AVERAGES:

         setcolor(Acolor);
         for (ibin=0; ibin<Cmd.numbin; ibin++)
           if (Dat.data[ibin] != 0)
             line(Xpos+ibin, Ypos, Xpos+ibin, Ypos-proc_data(Dat.data[ibin]));

         break;

    /* peaks */
    case PEAKS:

         if (NumWide >= Cmd.span/4)
            break;

	 setcolor(Pcolor);
	 gap = FALSE;
	 i=Cmd.numbin;
	 while (Dat.data[--i] == 0);

         moveto(Xpos+i, Ypos-proc_data(Dat.data[i]));
	 for (ibin=i-1; ibin>=0; ibin--)
	    if (Dat.data[ibin] != 0)
	       if (gap) {
		  x = Xpos+ibin;
		  y = Ypos-proc_data(Dat.data[ibin]);
		  moveto(x, y);
		  putpixel(x, y, Pcolor);
		  gap = FALSE;
	       }
	       else
		  lineto(Xpos+ibin, Ypos-proc_data(Dat.data[ibin]));
	    else
	       gap = TRUE;
	 break;

  /* gyrofrequency */
    case GYROFREQUENCY:

         setcolor(Gcolor);
         newmag = TRUE;
         for (ibin=0; ibin<Cmd.numbin; ibin++)
	     if (Dat.data[ibin] > 0 ) {
		t =  log10(Dat.data[ibin])-f0;
		fy = Yorg - (t * Fscale);
		fx = Xorg + ibin;
		if ((fy > Yorg) || (fy < NumHigh+Yorg)) {
		   moveto(fx, fy);
		   newmag=TRUE;
		}
		else if (newmag) {
                   moveto(fx, fy);
                   newmag=FALSE;
                }
                else
                   lineto(fx, fy);
             }
         break;
  }
} /* pws_plot */



/*--------------------------------------------------------------------

  pws_gray_plot written for PC by B. Baines 08-13-89
  to plot data for the PWS16PC program.

--------------------------------------------------------------------*/

void pws16_gray_plot (void)
{
  int i, ibin;
  float z;

  /* ------------------pws16_gray_plot-------------------- */




  if (Dat.flag != AVERAGES)
     return;

  /* shift up to next channel if not doing first channel */
  if (Dat.position == Cmin)
  {
     Ypos = Yorg;
     Xpos = Xorg-1;
     for (ibin=0; ibin<Cmd.numbin; ibin++);
       putpixel(Xorg+ibin, Ypos, findcolor(data[ibin]));

     /* LeftTic and RightTic */
     line(Xpos, Ypos, Xpos-LongTic, Ypos);
     line(Xorg+NumWide, Ypos, Xorg+NumWide+LongTic, Ypos);
  }
  else
  {
    Ypos = Yorg - Zmul * (Dat.position - Cmin);
    Xpos = Xorg - 1;

      /* LeftTic and RightTic */
    if (Dat.position == Cmax)
    {
       line(Xpos, Ypos-1, Xpos-LongTic, Ypos-1);
       line(Xorg+NumWide, Ypos-1, Xorg+NumWide+LongTic, Ypos-1);
    }
    else
    {
       line(Xpos, Ypos, Xpos-LongTic, Ypos);
       line(Xorg+NumWide, Ypos, Xorg+NumWide+LongTic, Ypos);
    }

    Ypos += Zmul;
    moveto(Xpos, Ypos);

      /* Plot data */
    for (ibin=0; ibin<Cmd.numbin; ibin++)
      if (Dat.data[ibin] != 0) {
        z = NINT((data[ibin] - Dat.data[ibin]) / Zmul);
        for (i = Zmul; i > 0; i--)
          putpixel(Xorg+ibin, Ypos-i, findcolor(data[ibin] - z*i));
      }
  }

  memcpy(data, Dat.data, Cmd.numbin*sizeof(data[0]));

} /* pws_gray_plot */

int findcolor(int value)
{
   int i;

   for (i=NUM_CHAN-1;(value<Range[i]); i--)
      if (i == 0)
         return(0);

   return(i);
}


/* ---------------------------------------------------------------------

   proc_data written R. Baines
   processes one piece of data to decide where to plot point on vertical
   axis.  This is used to cut down repitition of code.

   --------------------------------------------------------------------- */

int proc_data (int value)
{
    int z;

    z = NINT((value - Zmin) * Zscale);

    if (z < 0)
      z = 0;
    else if (z >= Zmul)
      z = Zmul-1;

    return(z);
}

