/* ---------------------------------------------------------------------

   PWS16SNP based on FORTRAN code by L. Granroth
            written by R.Baines 9/89

   This is an alternative to getting a view of the averages in Normal
   Plot.  This module contains the functions to produce the Labels for
   the screen when doing Snapshots and Plots the lines in the Snapshot.

   --------------------------------------------------------------------- */


#include <graphics.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "pwstruct.h"
#include "pwsglobl.h"
#include "pwsgraph.h"
#include "proto.h"

/* Internal Global Vars */
int SXorg, SYorg;          /* Snapshot lower left corner of data window */
char buf[LLEN];
int SnapWide, SnapHigh;

/* Internal Function Prototypes */
void display_top_snap_labels(void);
void display_side_snap_labels(void);
void display_bottom_snap_labels(void);


/* ---------------------------------------------------------------------

   Snap_Plot   written by R. Baines 07-26-89
   plots averages (and peaks) in a snapshot format.

   --------------------------------------------------------------------- */

void pws16_snap_plot(void)
{
  /* Frequencies for each channel */
  static float freq[] =
  {    10.0,    17.8,    31.1,    56.2,
      100.0,   178.0,   311.0,   562.0,
     1000.0,  1780.0,  3110.0,  5620.0,
    10000.0, 17800.0, 31100.0, 56200.0,
   100000.0
  };
  float freq_span, freq_range, spec_span, spec_range;
  static int xold, yold;
  static int xpold, ypold;

  int plt_indx;
  int plt_num = 0;
  int plt_data;
  long int plt_sum = 0;

  for (plt_indx = 0; plt_indx < Cmd.numbin; plt_indx++)
     if (Dat.data [plt_indx] != 0)
     {
        plt_sum += Dat.data [plt_indx];
	plt_num++;
     }

  if (plt_num != 0)
     plt_data = plt_sum / plt_num;
  else
     plt_data = 0;

  freq_span = (freq[((Dat.position-1)/4+1)*4] - freq[(Dat.position-1)/4*4]);
  freq_range = (freq[Dat.position-1]-freq[Dat.position/4]) / freq_span;

  spec_span = Zdiv * 1000;
  spec_range = ((float) plt_data - Zmin) / spec_span;

  Xpos = SXorg + freq_range * (SnapWide/4) + ((SnapWide/4)*((Dat.position-1)/4));
  Ypos = SYorg - spec_range * SnapHigh;

  if (Ypos < SYorg-SnapHigh)
     Ypos = SYorg-SnapHigh;
  else if (Ypos > SYorg)
     Ypos = SYorg+1;

  if (Dat.flag == AVERAGES)
  {
    if (Dat.position != 1)
    {
      setcolor(Acolor);
      line(xold, yold, Xpos, Ypos);
    }
    xold = Xpos;
    yold = Ypos;
  }
  else if (Dat.flag == PEAKS)
  {
    if (Dat.position != 1)
    {
      setcolor(Pcolor);
      line(xpold, ypold, Xpos, Ypos);
    }
    xpold = Xpos;
    ypold = Ypos;
  }
}  /* snap_plot */


/* ---------------------------------------------------------------------

   Snap_Label   written by R. Baines 07-26-89
   displays border, labels and tics for snapshot screen.

   --------------------------------------------------------------------- */

void pws16_snap_label(void)
{
  /* Set up Graphics Screen */
  initgraph (&GraphDriver, &GraphMode, BGI_Path);
  testGraphError();

  /* Get values based off graphics mode and driver used */
  MaxX = getmaxx();
  MaxY = getmaxy();
  MaxColor = getmaxcolor();

  if (Bcolor != getbkcolor()) {
     cleardevice();
     setbkcolor(Bcolor);
  }

  if (!ColorsSet)
  {
	 Lcolor = MaxColor;                /* set everything to white for now */
	 Pcolor = MaxColor;
	 Acolor = (MaxColor>2) ? MaxColor/2 : MaxColor;
  }
  setcolor(Lcolor);

  /* set font */
  settextstyle(Font, HORIZ_DIR, USER_CHAR_SIZE);
  testGraphError();

  /* Init variables for later processing */
  SnapHigh = MaxY * 3/4;
  SnapWide = MaxX * 3/5;

  Zdiv = (Zmax != Zmin) ? abs(Zmax-Zmin)/1000 : 1;
  Zmul = SnapHigh / Zdiv;
  SnapHigh = Zdiv * Zmul;
	SnapWide = SnapWide/4*4;

  SXorg = (MaxX-SnapWide) * 4/5;
  SYorg = (MaxY-SnapHigh) * 3/5 + SnapHigh;

  /* draw box */
  rectangle (SXorg-1, SYorg-SnapHigh, SXorg+SnapWide, SYorg+1);

  /* labels */
  display_top_snap_labels();
  display_bottom_snap_labels();
  display_side_snap_labels();
}  /* snap_label */


/* ---------------------------------------------------------------------

   display_top_snap_labels written by R. Baines

   --------------------------------------------------------------------- */

void display_top_snap_labels(void)
{
   double tbeg, tend;
   int lyr, ldoy, lhr, lmn;
   float lsec;
   doy_date day_year;

   /* Top */

   /* Label One */
   settextjustify(LEFT_TEXT, TOP_TEXT);
   sprintf(buf, "VOYAGER-%d - PWS SNAPSHOT", Cmd.spacecraft);

   Ymul = (SYorg-SnapHigh) / 3;
   Xmul = SnapWide;
   setusercharsize(1,1,1,1);
   Ydiv = textheight(buf);
   setusercharsize(Xmul, textwidth(buf), Ymul, Ydiv);
   outtextxy(SXorg, 0, buf);
   Ypos = textheight(buf);

   /* Label Two */
   tbeg = time2sec(Cmd.year, Cmd.day, Cmd.hour, Cmd.minute, Cmd.millisec*1000);
   doy(&day_year, Cmd.day, Cmd.year);
   sprintf(buf, " %02d %03d %02d%02d SCET  (%02d-%02d-%02d)", Cmd.year,
      Cmd.day, Cmd.hour, Cmd.minute, day_year.month, day_year.mday, Cmd.year);

   setusercharsize(1,1,1,1);
   Xdiv = textwidth(buf);
   Ymul *= Percent80;
   setusercharsize(SnapWide, textwidth("BEGIN")+Xdiv, Ymul, Ydiv);

   moveto(SXorg, Ypos);
   outtext("BEGIN");
   Xpos = getx();
   outtext(buf);

   /* Label Three */
   tend = tbeg + (double) Cmd.span;
   sec2time(tend, &lyr, &ldoy, &lhr, &lmn, &lsec);
   doy(&day_year, ldoy, lyr);
   sprintf(buf, " %02d %03d %02d%02d SCET  (%02d-%02d-%02d)", lyr, ldoy,
           lhr, lmn, day_year.month, day_year.mday, lyr);

   Ypos += Ymul;
   outtextxy(SXorg, Ypos, "END");
   outtextxy(Xpos, Ypos, buf);

}

/* ---------------------------------------------------------------------

   display_side_snap_labels written by R. Baines

   --------------------------------------------------------------------- */

void display_side_snap_labels(void)
{
   static char *units[] =
   { "        UNCALIBRATED       ",
     "    CALIBRATED VOLTS (V)   ",
     "  CALIBRATED VOLTS (Vrms)  ",
     "     ELECTRIC FIELD (V/M)  ",
     "  ELECTRIC FIELD (Vrms/M)  ",
     "SPECTRAL DENSITY (V^2^/M^2^/Hz)",
     "    POWER FLUX (W/M^2^/Hz)   "
   };
   int i, margin, tmpSXorg;

   /* Side */
   settextjustify(RIGHT_TEXT, CENTER_TEXT);

   Xpos = SXorg-1;
   Ypos = SYorg+1;
   margin = 0;
   for(i=Zmin; i <= Zmax; i+=1000)
   {
     strcpy(buf, itoa(i/1000, buf, 10));
     outtextxy(Xpos-LongTic-Tic, Ypos, buf);
     line(Xpos, Ypos, Xpos-LongTic, Ypos);
     Ypos = (i==Zmin) ? Ypos-Zmul-1 : Ypos-Zmul;
     margin = max(margin, textwidth(buf));
   }
   margin += LongTic*4;

   settextstyle(Font, VERT_DIR , USER_CHAR_SIZE);
   settextjustify(LEFT_TEXT, BOTTOM_TEXT);

   sprintf(buf, "%s", units[Cmd.units-1]);
   setusercharsize(1,1,1,1);

   Ydiv = textwidth(buf) - textwidth("^") * ctchr(buf, '^');
   setusercharsize(SnapHigh, Ydiv, Xmul, Xdiv);

   tmpSXorg = SXorg-margin-textheight(buf);
   superscriptxy(tmpSXorg, SYorg, SnapHigh, Ydiv, Xmul, Xdiv, buf);
   settextstyle(Font, HORIZ_DIR , USER_CHAR_SIZE);

}

/* ---------------------------------------------------------------------

   display_bottom_snap_labels written by R. Baines

   --------------------------------------------------------------------- */

void display_bottom_snap_labels(void)
{
   int i, width, tmpmul;

   /* Bottom */
   settextjustify(CENTER_TEXT, BOTTOM_TEXT);
   sprintf(buf, "FREQUENCY  (HZ)");
   outtextxy(SXorg+SnapWide/2, MaxY, buf);

   /* Bottom tics */
   Xpos = SXorg-1;
   width = textwidth("0");
   settextjustify(LEFT_TEXT, TOP_TEXT);
   Ymul *= Percent80;
   tmpmul = Xmul * Percent60;
   setusercharsize(tmpmul, Xdiv, Ymul, Ydiv);
   for(i=1; i<6; i++)
   {
     line(Xpos, SYorg+1, Xpos, SYorg+LongTic);
     sprintf(buf, "10^%d", i);
     superscriptxy(Xpos-width, SYorg+LongTic*2, tmpmul, Xdiv, Ymul, Ydiv, buf);
     Xpos = (i==5) ? SXorg+SnapWide : Xpos+SnapWide/4;
   }
}

