/* ----------------------------------------------------------------
   PWS16VW written by R. Baines

   This module contains the Begin, Command, Data and End functions
   for reading a processed data file.

      Begin   - gets filename.

      Command - opens file.

      Data    - returns one channel of data.

      End     - closes file.

   NOTE: processed data files provide a view on predefined command
         settings.  changing command settings when INPUT = FILE,
         MAY NOT have any change on the view.

   ---------------------------------------------------------------- */

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include "pwstruct.h"
#include "pwsglobl.h"
#include "proto.h"


/* Internal Global Vars */
FILE *fp;                            /* Input File Pointer */
char infname[LLEN];                  /* Input File Name */



/*---------------------------------------------------------------------

  pws16_begin written by R. Baines 11/89.
  to get file name for reading processed file in the PWS16PC program.

  ---------------------------------------------------------------------*/

void pws16_file_begin (void)
{
  do {
     cprintf("Enter INPUT Filename:  ");
     gets(infname);
     printf("Is this correct (Y/N)?  ");  
  } while (tolower(getch()) != 'y');
  
  printf ("\n\nPWS16PC version %s\n",VERSION);
    /* Initialize command structure */
  cmd_reset();
  puts ("\nEnter plot parameters (or help)");
}


/* ---------------------------------------------------

   pws16_command written by R. Baines 11/89
   to open file and if error request name again.

   --------------------------------------------------- */

int pws16_file_command(void)
{
  if (fp)
     fclose(fp);
  
  while (!(fp=fopen(infname,"r+t")))
  {
     error(18);
     cprintf("Enter INPUT Filename:  ");
     gets(infname);
  }    
  return((int) fp);
}


/*---------------------------------------------------------------------

  pws16_data written by R. Baines 11/89.
  to receive data packets for the PWS16PC program.

  ---------------------------------------------------------------------*/

int pws16_file_data (void)
{
   char buffer[LLEN];
   int ibin;
   fgets(buffer, LLEN, fp);
   sscanf(buffer, "%ld %d %d %d %d %d %d", &Dat.length, &Dat.position,
          &Dat.flag, &Dat.spare0, &Dat.spare1, &Dat.datmin, &Dat.datmax);

   for (ibin=0; ibin<900; ibin++)
     fscanf(fp, "%d", &Dat.data[ibin]);

   for (ibin=0; ibin<124; ibin++)
     fscanf(fp, "%d", &Dat.spare[ibin]);

   if (!feof(fp))
     fgets(buffer, LLEN, fp);
   else
     exit_graph();

   return(!feof(fp));
} /* pws16_data */


/* --------------------------------------------------------------

   pws16_end written by R. Baines 11/89.
   to close input file.

   -------------------------------------------------------------- */

void pws16_file_end (void)
{
  fcloseall();
  return;
}

