/*@@@@@@@@@@@@@@@@@@@@@@@@ extractor.c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 Random Option Pricing
 Summer 2002
 Authors: A. Korobchevsky
          S. Ferrando
 Ryerson University ALL RIGHTS RESERVED
 
 Purpose: Performs plots (g2 library is used)
          Parses command line arguments
          Extracts the option values from input file

 Command line arguments:
   $1 - Output postcript file name
	$2 - Number of nodes in each curve
   $3 - Starting X value (for labeling)
   $4 - Final X value (for labeling)
   $5+  Input file names


 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/




#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include "g2.h"
#include "g2_PS.h"


// Description of constants

// BEGIN extractorProperties
#define AXIS_LABELS 10            /* Number of ticks on each axis */
#define SCALE 0.9                /* Relative scale of the plot */
#define TICK_LENGTH 3            /* Size of the ticks on both axis */
#define LAYOUTS 5						/* Number of line formats which represent output files */ 
#define X_SIZE 600					/* Horizontal size of the plotting area */
#define Y_SIZE 400					/* Vertical size of the plotting area */
#define X_ORIGIN 150					/* X of lower left corner of the plotting area */
#define Y_ORIGIN 50					/* Y of lower left corner of the plotting area */
// END extractorProperties



main(int argc, char** argv)
{ char filename[40];              /* Input data files */
  char temp[40];                  /* Translates numbers into strings for labeling the plots */
  double x,    					    /* Horisontal coordinate of displayed point */ 
         y,                       /* Vertical coordinate of displayed point */ 
         pts[2], 						 /* Length of dashes for each curve */
         min,                     /* Minimum value of all functions displayed */
         max;							 /* Maximum value of all functions displayed */

  int id, 								 /* g2 device identifier */
       M0, 								 /* Starting horisonal label */
       M, 								 /* Final horisonal label */
       fileCounter,               /* Iterates through input files and layout arrays */
       first = 1,						 /* Flag true when the first point in file is drawn */
       numPoints,                 /* Number of points drawn for each plot */
       i;

  FILE *fp;
 
// BEGIN extractorProperties2
  /* Change those values for altering plot layout */
  int colors[]  = {6, 19, 4, 10, 13};            /* Colors for each graph (0..26)*/
  int width[]   = {1, 1.3, 1.6, 1.9, 2.3};          /* Line width for each graph */
  int dashes[]  = {2, 2, 3, 2, 1, 2, 2, 3, 5, 2};   /* Dashes length for each graph */
// END extractorProperties2

  if (argc < 5)
  { fprintf(stderr, "Usage: ./extractor outfile points M0 M inputfilenames\n");
    exit(1); 
  }

  numPoints = atoi(argv[2]);
  M0 = atoi(argv[3]);
  M = atoi(argv[4]);

  // First path through data files: vertical automatic scaling of the plots
  min = 200000000;   max = -1;
  for (fileCounter = 5; fileCounter < argc; fileCounter++)
  { strcpy(filename, argv[fileCounter]);
    if ((fp = fopen(filename, "r")) == NULL)
    { fprintf(stderr, "Error opening file %s. \n", filename);
      exit(1);
    } 

    // Ignore the header till $ is reached
    while (!feof(fp))
    { fscanf(fp, "%s", temp);
      if (strchr(temp, '$')) 
        break;
    }

    while (!feof(fp))
    { fscanf(fp, "%s", temp);
      y = atof(temp);
      if (y > max)
        max = y;
      if (y < min)
        min = y;
    }

    fclose(fp);
  }
  printf("arg3 = %d, arg4 = %d, min = %f, max = %f\n", M0, M, min, max);   



  // Set up g2
  id = g2_open_PS(argv[1], g2_A4, g2_PS_land);
  g2_set_coordinate_system(id, X_ORIGIN, Y_ORIGIN, SCALE, SCALE);
  g2_set_font_size(id, (int)(12 * SCALE));


  // Drawing and labeling horisontal axis
  g2_line(id, 0, 0, X_SIZE, 0);
  for (i = 0; i <= AXIS_LABELS; i++)
  { sprintf(temp, "%d", M0 + (M - M0) / AXIS_LABELS * i);   
    g2_string(id, -10 + X_SIZE / AXIS_LABELS * i, -15, temp);
    g2_line(id, X_SIZE / AXIS_LABELS * i, -TICK_LENGTH, X_SIZE / AXIS_LABELS * i, TICK_LENGTH);
  }
  g2_line(id, 0, Y_SIZE, X_SIZE, Y_SIZE);

  // Drawing and labeling vertical axis
  g2_line(id, 0, 0, 0, Y_SIZE);
  for (i = 0; i <= AXIS_LABELS; i++) 
  { sprintf(temp, "%.4f", min + (max - min) / AXIS_LABELS * i);
    g2_string(id, -38, -5 + Y_SIZE / AXIS_LABELS * i, temp);
    g2_line(id, -TICK_LENGTH, Y_SIZE / AXIS_LABELS * i, TICK_LENGTH, Y_SIZE / AXIS_LABELS * i);
  }
  g2_line(id, X_SIZE, 0, X_SIZE, Y_SIZE);

  // Process files one at a time
  for (fileCounter = 5; fileCounter < argc; fileCounter++)
  { first = 1;
    strcpy(filename, argv[fileCounter]);
    if ((fp = fopen(filename, "r")) == NULL)
    { fprintf(stderr, "Error opening file %s. \n", filename);
      exit(1);
    } 

    // Ignore the header till $ is reached
    while (!feof(fp))
    { fscanf(fp, "%s", temp);
      if (strchr(temp, '$')) 
        break;
    }

    // Line setting
    g2_pen(id, colors[fileCounter % LAYOUTS]);
    pts[0] = dashes[2 * (fileCounter % LAYOUTS)];   
    pts[1] = dashes[2 * (fileCounter % LAYOUTS) + 1];
    g2_set_dash(id, 2, pts);
    g2_set_line_width(id, width[fileCounter % LAYOUTS]);

    // Annotations
    g2_string(id, -40, Y_SIZE - 50 + fileCounter * 20, filename); 
    g2_line(id, -90, Y_SIZE - 47 + fileCounter * 20, -50, Y_SIZE - 47 + fileCounter * 20);

    // Drawing the graphs
    x = 0; 
    for (i = 0; !feof(fp) && i < numPoints; i++)
    { fscanf(fp, "%s", temp);
      y = atof(temp);
      x += X_SIZE / numPoints;
      if (first)
      { g2_plot(id, x, Y_SIZE * (y - min) / (max - min));
        first = 0;
      }
      g2_line_to(id, x, Y_SIZE * (y - min) / (max - min));
    }
    fclose(fp); 
  }
  
  g2_close(id);
}
