home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / misc / sci / gfft / source / okplot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-29  |  7.1 KB  |  236 lines

  1. /***************************************************************************
  2.  *          Copyright (C) 1994  Charles P. Peterson                  *
  3.  *         4007 Enchanted Sun, San Antonio, Texas 78244-1254             *
  4.  *              Email: Charles_P_Peterson@fcircus.sat.tx.us                *
  5.  *                                                                         *
  6.  *          This is free software with NO WARRANTY.                  *
  7.  *          See gfft.c, or run program itself, for details.              *
  8.  *              Support is available for a fee.                      *
  9.  ***************************************************************************
  10.  *
  11.  * Program:     gfft--General FFT analysis
  12.  * File:        ok_plot.c
  13.  * Purpose:     plot data (invoking separate plotter program)
  14.  * Author:      Charles Peterson (CPP)
  15.  * History:     24-November-1993 CPP; Created.
  16.  *              14-July-1994 CPP (1.01); Give helpful notes on failure.
  17.  *              9-Aug-94 CPP (1.10); Hidden3D now optional
  18.  *              28-Aug-94 CPP (1.12); Use Z for Y in 3D
  19.  * Comment:     Only gnuplot supported now; others may be added.
  20.  *              There may be some system dependency here.
  21.  *              Eventually, it is intended to be made system independent.
  22.  *              Also, might add custom 'gfft' plotter.
  23.  */
  24.  
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28.  
  29. #ifdef AMIGA
  30. #include <dos/dos.h>
  31. #include <clib/dos_protos.h>
  32. #endif
  33.  
  34. #include "gfft.h"
  35. #include "settings.h"
  36.  
  37. int Old_File_Count = 0;  /* This may be read or reset to zero externally */
  38. static char **Old_File_Name = NULL;
  39. static BOOLEAN plotted_already = FALSE;
  40. static BOOLEAN re_plotting = FALSE;
  41.  
  42. extern double LowestFrequencyWritten;
  43. extern double HighestFrequencyWritten;
  44.  
  45. static void do_ok_plot (BOOLEAN this_is_re_plot);
  46.  
  47. void do_re_plot (void)
  48. {
  49. /* Further checking could be included here */
  50.     re_plotting = TRUE;
  51.     do_ok_plot (TRUE);
  52. }
  53.  
  54. void ok_plot (void)
  55. {
  56.     re_plotting = FALSE;
  57.     do_ok_plot (FALSE);
  58. }
  59.  
  60. static void do_ok_plot (BOOLEAN this_is_re_plot)
  61. {
  62.     char buffer[COMMAND_BUFFER_SIZE];
  63.     FILE *comfile_ptr;
  64.     FILE *gnuplot_comfile_ptr;
  65.     char *save_name;
  66.  
  67.     plotted_already = TRUE;
  68.  
  69. /**************************************************************************\
  70.  * Write command file
  71. \**************************************************************************/
  72.  
  73.     comfile_ptr = fopen (PLOT_COMMAND_FILE_NAME,"w");
  74.     if (!comfile_ptr)
  75.     {
  76.     error_message (CANT_CREATE_PLOTTER_FILE);
  77.     RAISE_ERROR (NOTHING_SPECIAL);  /* longjmp outa here! */
  78.     }
  79.  
  80. #ifdef AMIGA
  81.     fprintf (comfile_ptr, "stack %d\n", GNUPLOT_STACKSIZE);
  82.     fprintf (comfile_ptr,"echo \" \"\n");
  83.     fprintf (comfile_ptr,
  84. "echo \" Press <RETURN> to end plot display once it appears.\"\n");
  85.     fprintf (comfile_ptr,"echo \" \"\n");
  86.     fprintf (comfile_ptr,"%s%s%s",
  87. "echo \" ",
  88. "(All is OK now, but if gnuplot has a problem later, try fewer bins.)",
  89. "\"\n");
  90.     fprintf (comfile_ptr,"echo \" \"\n");
  91.     fprintf (comfile_ptr,"echo \" Generating plot using GNUPLOT\"\n");
  92.     fprintf (comfile_ptr,"echo \" \"\n");
  93.     fprintf (comfile_ptr,
  94. "echo \" Copyright(C) 1986 - 1993   Thomas Williams, Colin Kelley\"\n");
  95.     fprintf (comfile_ptr,"wait 1\n");
  96.     fprintf (comfile_ptr,"failat 1\n");  /* exit script on gnuplot error */
  97. #endif
  98.  
  99.     fprintf (comfile_ptr,"gnuplot %s\n",
  100.              GNUPLOT_COMMAND_FILE_NAME);
  101.  
  102. #ifdef AMIGA
  103.     fprintf (comfile_ptr,"delete \"%s\" QUIET\n",GNUPLOT_COMMAND_FILE_NAME);
  104.     fprintf (comfile_ptr,"endcli\n");
  105. #endif
  106.  
  107.     fclose (comfile_ptr);
  108.  
  109. /**************************************************************************\
  110.  * Write GNUPLOT command file
  111. \**************************************************************************/
  112.  
  113.     gnuplot_comfile_ptr = fopen (GNUPLOT_COMMAND_FILE_NAME,"w");
  114.     if (!gnuplot_comfile_ptr)
  115.     {
  116.     error_message (CANT_CREATE_PLOTTER_FILE);
  117.     RAISE_ERROR (NOTHING_SPECIAL);  /* longjmp outa here! */
  118.     }
  119.     if (Terminal && Terminal != NullString && strlen(Terminal))
  120.     {
  121.     fprintf (gnuplot_comfile_ptr, "set terminal %s\n", Terminal);
  122.     }
  123.     if (PlotOutput && PlotOutput != NullString && strlen(PlotOutput))
  124.     {
  125.     fprintf (gnuplot_comfile_ptr, "set output \'%s\'\n", PlotOutput);
  126.     }
  127.     if (!Time3D)
  128.     {
  129.     if (LowY != LOWEST_Y) fprintf (gnuplot_comfile_ptr, 
  130.                        "set yrange [%g:]\n", LowY);
  131.     if (HighY != HIGHEST_Y) fprintf (gnuplot_comfile_ptr, 
  132.                      "set yrange [:%g]\n", HighY);
  133.     }
  134.     else   /* in 3d, amplitude is z */
  135.     {
  136.     if (LowY != LOWEST_Y) fprintf (gnuplot_comfile_ptr, 
  137.                        "set zrange [%g:]\n", LowY);
  138.     if (HighY != HIGHEST_Y) fprintf (gnuplot_comfile_ptr, 
  139.                      "set zrange [:%g]\n", HighY);
  140.     }
  141.     if (LogX) fprintf (gnuplot_comfile_ptr, "set log x\n");
  142.     if (LogY && !Time3D) fprintf (gnuplot_comfile_ptr, "set log y\n");
  143.     if (LogY && Time3D) fprintf (gnuplot_comfile_ptr, "set log z\n");
  144.     if (RotX != DEF_ROT_X)
  145.     {
  146.     fprintf (gnuplot_comfile_ptr, "set view %g\n", RotX);
  147.     }
  148.     if (RotZ != DEF_ROT_Z)
  149.     {
  150.     fprintf (gnuplot_comfile_ptr, "set view ,%g\n", RotZ);
  151.     }
  152.     if (this_is_re_plot)
  153.     {
  154.     if (LowFrequency != LOWEST_FREQUENCY &&
  155.         HighFrequency != HIGHEST_FREQUENCY)
  156.     {
  157.         fprintf (gnuplot_comfile_ptr, "set xrange [%g:%g]\n",
  158.              LowFrequency, HighFrequency);
  159.     }
  160.     else if (LowFrequency != LOWEST_FREQUENCY)
  161.     {
  162.         fprintf (gnuplot_comfile_ptr, "set xrange [%g:%g]\n",
  163.              LowFrequency, HighestFrequencyWritten);
  164.     }
  165.     else if (HighFrequency != HIGHEST_FREQUENCY)
  166.     {
  167.         fprintf (gnuplot_comfile_ptr, "set xrange [%g:%g]\n",
  168.              LowestFrequencyWritten, HighFrequency);
  169.     }
  170.     }
  171.     if (Time3D)
  172.     {
  173.     fprintf (gnuplot_comfile_ptr, "set parametric\n");
  174.     if (Hidden3D)
  175.     {
  176.         fprintf (gnuplot_comfile_ptr, "set hidden3d\n");
  177.     }
  178.     fprintf (gnuplot_comfile_ptr, "splot ");
  179.     }
  180.     else
  181.     {
  182.     fprintf (gnuplot_comfile_ptr, "plot ");
  183.     }
  184.     if (CombinePlots)
  185.     {
  186.     int i;
  187.  
  188.     if (re_plotting && Old_File_Count)
  189.     {
  190.         if (strlen (OkWriteName) ==
  191.         strlen (Old_File_Name[Old_File_Count - 1]))
  192.         {
  193.         if (!strcmp(OkWriteName,Old_File_Name[Old_File_Count - 1]))
  194.         {
  195.             Old_File_Count--;
  196.         }
  197.         }
  198.     }
  199.  
  200.     for (i = 0; i < Old_File_Count; i++)
  201.     {
  202.         fprintf (gnuplot_comfile_ptr, "\'%s\' with lines,",
  203.              Old_File_Name[i]);
  204.     }
  205.     }
  206.     fprintf (gnuplot_comfile_ptr, "\'%s\' with lines\n",OkWriteName);
  207.     fclose (gnuplot_comfile_ptr);
  208.  
  209.     save_name = gmalloc (strlen(OkWriteName) + 1, NOTHING_SPECIAL);
  210.     strcpy (save_name, OkWriteName);
  211.     Old_File_Count = (CombinePlots) ? ++Old_File_Count : 1;
  212.  
  213.     Old_File_Name = grealloc (Old_File_Name, Old_File_Count *
  214.                   sizeof (char *), NOTHING_SPECIAL);
  215.     Old_File_Name[Old_File_Count-1] = save_name;
  216.  
  217.  
  218. /**************************************************************************\
  219.  * Write shell command
  220. \**************************************************************************/
  221.  
  222. #ifdef AMIGA
  223.     sprintf (buffer,
  224.          "newcli CON:0/0/640/200/Gfft-Gnuplot-Shell from %s", 
  225.          PLOT_COMMAND_FILE_NAME);
  226. #else
  227.     sprintf (buffer, "%s", PLOT_COMMAND_FILE_NAME);
  228. #endif
  229.  
  230. /**************************************************************************\
  231.  * Execute shell command
  232. \**************************************************************************/
  233.  
  234.     system (buffer);
  235. }
  236.