home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / misc / b186_1 / Source / c / plot < prev    next >
Text File  |  1987-09-27  |  5KB  |  223 lines

  1. /*
  2.  
  3.        This file is part of the PDP software package.
  4.          
  5.        Copyright 1987 by James L. McClelland and David E. Rumelhart.
  6.        
  7.        Please refer to licensing information in the file license.txt,
  8.        which is in the same directory with this source file and is
  9.        included here by reference.
  10. */
  11.  
  12.  
  13. /* file: plot.c
  14.  
  15.     Simple program that makes x[i] vs y plots for 23x79 display
  16.     
  17.     First version implemented by JLM
  18.     
  19.     Date of last revision:  8-12-87/JLM
  20.     
  21. */
  22.  
  23. #include <stdio.h>
  24. #include <math.h>
  25. #include <ctype.h>
  26.  
  27. #define C_MIN        9
  28. #define C_MAX        69
  29. #define L_MIN        0
  30. #define L_MAX        20
  31. #define C_RANGE        (C_MAX - C_MIN)
  32. #define L_RANGE        (L_MAX - L_MIN)
  33. #define C_CENTER    (C_MIN + C_RANGE/2)
  34. #define L_CENTER    (L_MIN + L_RANGE/2)
  35.  
  36. struct dimension {
  37.     double min;
  38.     double max;
  39.     double val;
  40.     double scale;
  41.     int l;
  42.     char title[20];
  43. } x, y;
  44.  
  45. char in[100];
  46. char line[BUFSIZ];
  47. char graph_title[20];
  48. char label[26][20];
  49. FILE *format, *data, *plot;
  50.  
  51. int nlabels = 0;
  52. char symbol = '\0';
  53. char screen[23][80];
  54.  
  55. char *
  56. next(sp,lp) char *sp,*lp; {
  57.     while(*lp && isspace(*lp)) *lp++;
  58.     if (*lp == '\0') return(NULL);
  59.     while(*lp && !isspace(*lp)) *sp++ = *lp++;
  60.     *sp = '\0';
  61.     return(lp);
  62. }
  63.  
  64. main(argc,argv) int argc; char **argv; {
  65.     if ( (format = fopen(argv[1],"r")) == NULL) {
  66.         fprintf(stderr,"Can't open format file.");
  67.         exit(1);
  68.     }
  69.     if ( (data = fopen(argv[2],"r")) == NULL) {
  70.         fprintf(stderr,"Can't open data file.");
  71.         exit(1);
  72.     }
  73.     if (argc > 3) {
  74.         if ( (plot = fopen(argv[3],"w")) == NULL) {
  75.         fprintf(stderr,"Can't open output file.");
  76.         exit(1);
  77.         }
  78.     }
  79.     else plot = stdout;
  80.     get_format();
  81.     make_plot();
  82.     put_plot();
  83. }
  84.  
  85. get_format() {
  86.     int label_index = 0;
  87.     x.min = 0.0;
  88.     x.max = 50.0;
  89.     y.min = 0.0;
  90.     y.max = 1.0;
  91.     while (fscanf(format,"%s",in) != EOF) {
  92.         switch (in[0]) {
  93.           case 'x':
  94.               get_dimspec(&x);
  95.             break;
  96.           case 'y':
  97.               get_dimspec(&y);
  98.             break;
  99.           case 't':
  100.               fscanf(format,"%s",graph_title);
  101.             break;
  102.           case 'l':
  103.             fscanf(format,"%s",label[label_index++]);
  104.             break;
  105.           case 's':
  106.               fscanf(format,"%s",in);
  107.             symbol = in[0];
  108.             break;
  109.           default:
  110.               fprintf(stderr,"bad format specification.\n");
  111.             exit(1);
  112.         }
  113.     }
  114.     nlabels = label_index;
  115.     if (x.l) {x.max = log10(x.max); x.min = log10(x.min);}
  116.     if (y.l) {y.max = log10(y.max); y.min = log10(y.min);}
  117.     x.scale = x.max - x.min;
  118.     y.scale = y.max - y.min;
  119. }
  120.  
  121. get_dimspec(dim) struct dimension *dim; {
  122.     fscanf(format,"%s",in);
  123.     switch(in[0]) {
  124.         case 'M':
  125.             fscanf(format,"%lf",&dim->max);
  126.             break;
  127.         case 'm':
  128.             fscanf(format,"%lf",&dim->min);
  129.             break;
  130.         case 't':
  131.             fscanf(format,"%s",dim->title);
  132.             break;
  133.         case 'l':
  134.             dim->l = 1;
  135.             break;
  136.         default:
  137.             return;
  138.     }
  139. }
  140.  
  141. make_plot() {
  142.     label_plot();
  143.     insert_data();
  144. }
  145.  
  146. label_plot() {
  147.     int l,c,len,i,j;
  148.     
  149.     for (c = C_MIN-1; c <= C_MAX+1; c++) {
  150.         screen[L_MAX+1][c] = screen[L_MIN][c] = '-';
  151.     }
  152.     for (l = L_MIN; l <= L_MAX; l++) {
  153.         screen[l][C_MIN-1] = screen[l][C_MAX+1] = '|';
  154.     }
  155.     len = strlen(graph_title);
  156.     strcpy(&screen[L_MIN][C_CENTER-(len/2)],graph_title);
  157.     screen[L_MIN][C_CENTER-(len/2)+len] = '-';
  158.     len = strlen(x.title);
  159.     strcpy(&screen[L_MAX+1][C_CENTER-(len/2)],x.title);
  160.     screen[L_MAX+1][C_CENTER-(len/2)+len] = '-';
  161.     len = strlen(y.title);
  162.     for (i = 0, l = L_CENTER - (len/2);i < len; i++,l++) {
  163.         screen[l][0] = y.title[i];
  164.     }
  165.     sprintf(&screen[L_MIN][2],"%5.2f",y.max);
  166.     sprintf(&screen[L_CENTER][2],"%5.2f",(y.min + (y.max - y.min)/2.0));
  167.     sprintf(&screen[L_MAX][2],"%5.2f",y.min);
  168.     sprintf(&screen[L_MAX+2][C_MIN-4],"%7.2f",x.min);
  169.     sprintf(&screen[L_MAX+2][C_CENTER-4],"%7.2f",
  170.                          (x.min + (x.max - x.min)/2.0));
  171.     sprintf(&screen[L_MAX+2][C_MAX-4],"%7.2f",x.max);
  172.     for (i = 0; i < nlabels; i++) {
  173.         sprintf(&screen[i+2][C_MAX+3],"%c", (char) i + 'a');
  174.         len = strlen(label[i]);
  175.         for (c = C_MAX+5,j = 0; j < len && c < 79; c++,j++) {
  176.             screen[i+2][c]= label[i][j];
  177.         }
  178.     }
  179. }
  180.  
  181. insert_data() {
  182.     int i,l,c;
  183.     char *sp;
  184.     char *lp;
  185.     double tx,ty;
  186.     while(fgets(line,BUFSIZ,data) != NULL) {
  187.         if ((lp = next(in,line)) == NULL) continue;
  188.         sscanf(in,"%lf",&tx);
  189.         if (x.l) x.val = log10(tx);
  190.         else x.val = tx;
  191.         c = C_MIN + ((double)(C_RANGE)/x.scale)*(x.val-x.min);
  192.         if (c < C_MIN || c > C_MAX) continue;
  193.         for (i = 0; (lp = next(in,lp)) != NULL; i++) {
  194.         if (sscanf(in,"%lf",&ty) == 0) continue;
  195.         if (y.l) y.val = log10(ty);
  196.         else y.val = ty;
  197.         l = L_MAX - (int)(L_RANGE*((y.val - y.min)/y.scale));
  198.         if (l <= L_MIN || l > L_MAX) continue;
  199.         sp = &screen[l][c];
  200.         if (symbol) *sp = symbol;
  201.         else if (*sp == '\0') *sp = i + 'a';
  202.         else if (*sp >= 'a' && *sp <= 'z') *sp = '2';
  203.         else if (*sp >= '2' && *sp <= '8') *sp += 1;
  204.         else if (*sp == '9') *sp = '*';
  205.         }
  206.     }
  207. }
  208.  
  209. put_plot() {
  210.     int l,c;
  211.     char ch;
  212.     for (l = 0; l < 23; l++) {
  213.          for (c = 0; c < 79; c++) {
  214.              ch = screen[l][c];
  215.         if (ch == '\0') putc(' ',plot);
  216.         else putc(ch,plot);
  217.          }
  218.          putc('\n',plot);
  219.     }
  220. }
  221.        
  222.     
  223.