Skip to content
Snippets Groups Projects
dualplot.cpp 15.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • //**************************************************************************//
    //* FRIDA: fast reliable interactive data analysis                         *//
    //* dualplot.cpp: different mechanisms for screen and paper output         *//
    //* (C) Joachim Wuttke 1990-, v2(C++) 2001-                                *//
    //* http://frida.sourceforge.net                                           *//
    //**************************************************************************//
    
    #include <stdlib.h>
    
    #include <string.h>
    
    #include <iostream>
    #include <math.h>
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    
    #include "mystd.h"
    #include "plotaux.h"
    #include "coord.h"
    #include "asi.h"
    
    #include "gar.h"
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    //**************************************************************************//
    //*   CRange                                                               *//
    //**************************************************************************//
    
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    CRange::CRange()
    
        inf = -INFINITY;
        sup = +INFINITY;
    
    }
    	
    CRange::CRange( double infin, double supin )
    {
    
        if( infin > supin )
    
            throw string( "invalid plot range" );
    
        if( infin == supin )
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
        inf = infin;
        sup = supin;
    
    void CRange::set_from_string( const string& in )
    
        if ( in=="*" ) {
            inf = -INFINITY;
            sup = +INFINITY;
            return;
    
        string s1, s2;
        mystd::string_extract_word( in, &s1, &s2 );
        if( s1=="*" ){
            inf = -INFINITY;
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
        } else {
    
            if ( mystd::any2dbl(s1,&inf) ){
    
                inf = -INFINITY;
                throw "invalid lower bound ["+s1+"], expecting real number or '*'";
            }
        }            
        if( s2=="*" ){
            sup = +INFINITY;
        } else {
    
            if ( mystd::any2dbl(s2,&sup) ){
    
                sup = +INFINITY;
                throw "invalid upper bound ["+s2+"], expecting real number or '*'";
            }
        }            
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
    void CRange::Round( int logflag, double relmargin, double digits )
    {
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
        if        ( inf>sup )
            throw string( "BUG: Plot/Range/Round: inf>sup" );
    
        if        (inf==sup) {
    
            if( logflag ){
                inf /= 10;
                sup *= 10;
            } else {
                inf -= 1;
                sup += 1;
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
            }
    
        } 
    
        double inf2, sup2;
        double mydigits = digits;
        if ( !logflag ) { // lin limits
            double margin = relmargin * (sup - inf);
            do {
                if (mydigits>=8.5) return; // do not round
                sup2 = mystd::round_decimal( sup+margin, mydigits );
                inf2 = mystd::round_decimal( inf-margin, mydigits );
                if(sup<0 && sup2>0) sup2 = 0;
                if(inf2<0 && inf>0) inf2 = 0;
                mydigits += 0.5;
            } while (!((inf2<inf) && (sup<sup2)));
            *this = CRange(inf2, sup2);
        } else { // log limits
            double ratio = sup / inf;
            double margin = exp( relmargin*log(ratio) );
            do {
                if (mydigits>=8.5) return; // do not round
                sup2 = mystd::round_decimal( sup*margin, mydigits );
                inf2 = mystd::round_decimal( inf/margin, mydigits );
                mydigits += 0.5;
            } while ( !((inf2<inf) && (sup<sup2)) );
            *this = CRange(inf2, sup2);
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
        }
    
    }	
    
    bool CRange::finite() const
    {
    
        return inf!=-INFINITY && sup!=+INFINITY;
    
    }
    
    bool CRange::contained(double val)  const
    {
    
        return inf <= val && val <= sup;
    
    //! Map application scale (inf..sup) to linear plot scale (0..1).
    
    double CRange::value2plotcoord(int logflag, double v) const
    
        if ( !finite() )
    
            throw string( "undefined plot range" );
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
        if (logflag) {
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
            if( inf<0 || v<0 )
    
                throw string( "negative value in log range" );
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
            return log(v/inf) / log(sup/inf);
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
        } else {
            return (v-inf) / (sup-inf);
        }
    
    //! Map linear plot scale (0..1) to application scale (inf..sup).
    
    double CRange::plotcoord2value(int logflag, double c) const
    {
        if ( !finite() )
            throw string( "undefined plot range" );
        if (logflag) {
            return inf * exp( c*log(sup/inf) );
        } else {
            return inf + c*(sup-inf);
        }
    }
    
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    string CRange::str() const
    
        if( inf==-INFINITY )
            ret += "*";
        else
            ret += strg(inf);
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
        else
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    //**************************************************************************//
    //*  CAxis                                                                 *//
    //**************************************************************************//
    
    void CAxis::Ask( const string& quest )
    
        string def, in, in1, in2;
    
    
        def = (log ? "log " : "") + R.str();
        in = sask( quest, def );
        
        if        (in==def)
            return;
        
        mystd::string_extract_word(in, &in1, &in2);
        if        (in1=="l") {
            SetLog(!log);
            in = in2;
        } else if (in1=="i") {
            SetLog(0);
            in = in2;
    
            } else if (in1=="g") {
    
            SetLog(1);
            in = in2;
        } 
        
        if (in!="")
            R.set_from_string( in );
        
        if (log && R.inf<=0 && R.inf!=-INFINITY) {
            R.inf = -INFINITY;
            throw string( "log scale requires range above 0" );
        }
        if (!log && ( R.inf!=-INFINITY && R.inf<-3e38 ||
                      R.sup!=+INFINITY && R.sup>3e38 ) ) {
            R.inf = -INFINITY;
            R.sup = +INFINITY;
            throw string( "lin scale exceeds typical PS implementation range "
                          "(abs<3e38)" );
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    void CAxis::SetLog( const bool _log )
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        if ( _log!=log ) {
            log = _log;
    
            CRange RX = AlternateR;
            AlternateR = R;
            R  = RX;
        }
    
    string CAxis::info()
    {
        string ret;
        ret  =   log ? "log" : "lin";
        ret += " " + strg(R.inf);
        ret += " " + strg(R.sup);
        return ret;
    }
    
    
    double CAxis::pc(double v) const // value -> plot-coordinate
    
        return CPLOT_PSMAX * R.value2plotcoord(log, v);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    
    //**************************************************************************//
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    //**************************************************************************//
    
    
        // Start gnuplot. Use input redirection so that gnuplot receives
        // commands from a gp_fifo which is created here.
    
    
        string fn_fifo = string("/tmp/gnuplot-") + getenv( "LOGNAME" );
    
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        mystd::system( "rm -f "+fn_fifo );
    
        if (mkfifo( fn_fifo.c_str(), 0666 )) {
            printf("SEVERE SYSTEM ERROR cannot make fifo %s"
                   " - will not be able to print\n", fn_fifo.c_str() );
            return;
        }
    
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        mystd::system( "gnuplot -noraise < " + fn_fifo + " &" );
    
    
        // we use open instead of fopen or ofstream,
        // because we need non-blocking mode.
        if (!(gp_fifo = open(fn_fifo.c_str(), O_WRONLY))) {
            printf("SEVERE SYSTEM ERROR cannot open gp_fifo"
                   " - will not be able to print\n");
            return;
        }
        fcntl(gp_fifo,F_SETFL,O_NONBLOCK);
    
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        gp_write( string("set terminal x11") );
    
        // cout << "DEBUG dp Clear beg\n";
        gp_fno = 0;
        gp_fnames = string("");
    
        // new wups file:
        string cmd;
        ps_accu.clear();
        ps_accu.push_back( "\n%% output created by frida2\n");
        ps_snum = 0;
        ps_pnum = 0;
        ps_cnum = 0;
        ps_Doc.clear();
    
    void CPlot::Multiask()
    
        maxpoints = iask("Max # points in plot", maxpoints);
        X.force = bask("Force x points into frame", X.force);
        Y.force = bask("Force y points into frame", Y.force);
    
    Wuttke, Joachim's avatar
    c  
    Wuttke, Joachim committed
        gp_write( "set nologscale" );
        string whichlog="";
    
        if (X.log) whichlog += "x";
        if (Y.log) whichlog += "y";
    
    Wuttke, Joachim's avatar
    c  
    Wuttke, Joachim committed
        if (whichlog!="")
            gp_write( "set logscale " + whichlog );
    
        snprintf( outlin, CPLOT_LINSIZ, "\n%d %g %g xSetCoord\n", 
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
                  X.log, X.R.inf, X.R.sup );
    
        ps_accu.push_back( outlin );
    
        snprintf( outlin, CPLOT_LINSIZ, "%d %g %g ySetCoord\n", 
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
                  Y.log, Y.R.inf, Y.R.sup );
    
        ps_accu.push_back( outlin );
    
        snprintf( outlin, CPLOT_LINSIZ, "%% %d %g %g %d zSetCoord\n\n", 0, 0., 0., 0 );
    
        ps_accu.push_back( outlin );
    
        int ntack, ntpt;
        double *tack, ticklim[2];
    
        ps_accu.push_back( "\n/xPlotFrame {" );
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        if ( X.log && X.R.inf<= 0 )
            throw "BUG: x log incompatible with limits " + X.R.str();
    
        plotaux::calc_ticks( X.log, X.R.inf, X.R.sup, 
                             &ntack, &tack, &ntpt, ticklim );
    
        ps_ticktack(ntack, tack, ntpt, ticklim, &X);
        free(tack);
    
        snprintf( outlin, CPLOT_LINSIZ-4, "   {(%s", X.C.ps_str().c_str() );
        strncat( outlin, ")}\n", CPLOT_LINSIZ );
    
        ps_accu.push_back( outlin );
        ps_accu.push_back( "      0 10   0  0     0  90 "
                           "OneAxx Axx Tic xTacL xNumL %% low x axis\n" );
        ps_accu.push_back( "      0 10   0 10     0 270 "
                           "OneAxx Axx Tic xTacH       %% top x axis\n" );
        ps_accu.push_back( "      xCL\n" );
        ps_accu.push_back( "} def\n" );
        
        ps_accu.push_back( "\n/yPlotFrame {" );
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        if ( Y.log && Y.R.inf<= 0 )
            throw "BUG: y log incompatible with limits " + Y.R.str();
    
        plotaux::calc_ticks( Y.log, Y.R.inf, Y.R.sup, 
                             &ntack, &tack, &ntpt, ticklim );
    
        ps_ticktack(ntack, tack, ntpt, ticklim, &Y);
        free(tack);
    
        snprintf( outlin, CPLOT_LINSIZ, "   {(%s", Y.C.ps_str().c_str() );
        strncat( outlin, ")}\n", CPLOT_LINSIZ );
    
        ps_accu.push_back( outlin );
        ps_accu.push_back( "      0 10   0  0    90   0 "
                           "OneAxx Axx Tic yTacL yNumL %% left y axis\n" );
        ps_accu.push_back( "      0 10  10  0    90 180 "
                           "OneAxx Axx Tic yTacH % yNumH %% right yaxis\n" );
        ps_accu.push_back( "      yCL\n" );
        ps_accu.push_back( "} def\n" );
        ps_accu.push_back( "\n%% modeDD\nplotbefore\n" );
    
    void CPlot::Line( const bool as_line, const int style_no,
    
    		  const vector<double> xp, const vector<double> yp,
    
    		  const vector<double>* z,
    		  const string xco, const string yco,
                      const string info )
    
        // Checks:
        uint np=xp.size();
        if ( np<0 )
            throw string( "PROG ERR NPLot::Line x.size=0" );
        if ( np!=yp.size() )
    
            throw string( "PROG ERR NPLot::Line x.size<>y.size" );
    
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        // Data to tmp file:
    
        char gp_fnam[40];
    
        sprintf( gp_fnam, "/tmp/%s-%d-%03d.gnu", getenv("LOGNAME"),
                 iPlot, gp_fno++);
    
        if (gp_fnames!="") gp_fnames += ", ";
        gp_fnames += string("\"") + gp_fnam + "\"" + 
    
            //		string(" title \"") + xco + string (" -> ") 
            //		                    + yco + string ("\"");
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        if ( as_line )
    
            gp_fnames += " with lines";
        FILE *gp_fd;
    
        if (!(gp_fd = fopen(gp_fnam, "w")))
            throw string("cannot save gnuplot data to ") + gp_fnam;
    
        for (uint i=0; i<np; i++){
            // invalid points should be intercepted outside Line(..)
            if( xp[i]<X.R.inf || xp[i]>X.R.sup )
                throw "Plot::Line: x["+strg(i)+"]="+strg(xp[i])+" out of range";
            if( yp[i]<Y.R.inf || yp[i]>Y.R.sup )
                throw "Plot::Line: y["+strg(i)+"]="+strg(yp[i])+" out of range";
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
            fprintf(gp_fd, "%16.8g %16.8g\n", xp[i], yp[i]);
    
        fclose(gp_fd);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        // Live display:
        string cmd = "plot ";
    
        char aux[80];
    
        sprintf( aux, "[%12.8g:%12.8g] [%12.8g:%12.8g] ", 
                 X.R.inf, X.R.sup, Y.R.inf, Y.R.sup);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        cmd += aux + gp_fnames;
        gp_write( cmd );
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        // Postscript copy:
    
        snprintf( outlin, CPLOT_LINSIZ, "\n%3u [", ++ps_snum );
    
        ps_accu.push_back( outlin );
        for (uint i=0; i<z->size(); i++){
    
            snprintf( outlin, CPLOT_LINSIZ, " %12g", (*z)[i]);
    
            ps_accu.push_back( outlin );
        }
    
        snprintf( outlin, CPLOT_LINSIZ, " ] zValues\n" );
    
        ps_accu.push_back( outlin );
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        if ( as_line )
    
            snprintf( outlin, CPLOT_LINSIZ, "%2d cstyle", 1 );
    
            snprintf( outlin, CPLOT_LINSIZ, "%2d pstyle", style_no+1 );
    
        ps_accu.push_back( outlin );
    
        snprintf( outlin, CPLOT_LINSIZ-2, " %% (%s -> %s)", xco.c_str(), yco.c_str() );
        strncat( outlin, "\n", CPLOT_LINSIZ );
    
        ps_accu.push_back( outlin );
        for (uint i=0; i<np; i++) {
    
            snprintf( outlin, CPLOT_LINSIZ,
    
                      "%7.3f%7.3f%7.3f t%c %% %14.7g wx %14.7g wy\n",
    
                      X.pc(xp[i]), Y.pc(yp[i]), 0.0,
    
            ps_accu.push_back( outlin );
        }
    
    void CPlot::Doc (vector<string> lDoc)
    
        for (uint i=0; i<lDoc.size(); i++)
            ps_Doc.push_back(lDoc[i]);
    
    void CPlot::Save( bool full_outfile )
    
        string ps_outdir, ps_head, ps_dict;
    
        // read configuration parameters:
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        ps_outdir = NRead::wofmac( "\\psdir" );
        ps_head = NRead::wofmac( "\\pshead" );
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
        if ( full_outfile )
            ps_dict = NRead::wofmac( "\\psdict" );
    
    
        // construct output file name:
        FILE *pssav;
        char outf[20];
        string flong, cmd;
        while(1) {
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
            if (ps_fnum>=999)
                throw string( "graph file number overflow" );
    
            sprintf(outf, "%sl%d.%s", ps_outdir.c_str(), ++ps_fnum, 
                    full_outfile ? "ps" : "psa" );
            if (!(pssav = mystd::glob_fopen(outf, "", "", "r")))
                break; // legal exit
            fclose(pssav);
        }
        printf("save plot in %s\n", outf);
    
        // copy headers to output file:
        cmd = string("cat ") + ( full_outfile ? ps_dict : "" ) + " " +
            ps_head + " > " + outf + "\n";
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        mystd::system( cmd );
    
        
        // a redundant check of existence of the outfile:
        if (!(pssav = mystd::glob_fopen(outf, "", "", "r", &flong))) {
            printf("have not created file %s\n", outf);
            return;
        }
        fclose(pssav);
    
        // append specific output to output file:
        if (!(pssav = fopen(flong.c_str(), "a+"))) {
            printf ("cannot write (append) to  file %s\n", flong.c_str());
            return;
        }
        for( uint i=0; i<ps_accu.size(); ++i ){
    
            // fprintf does not work here because output line may contain "%"
            fwrite( ps_accu[i].c_str(), 1, ps_accu[i].size(), pssav );
    
        }
    
        // additional output (do not append this to ps_accu to allow
        // further incrementation of ps_accu):
        fprintf(pssav, "\nblack 0 -4 13 newlist\n");
        for (uint i=0; i<ps_Doc.size(); i++) 
            fprintf(pssav, "{(%s)} infline\n", ps_Doc[i].c_str());
    
        fprintf(pssav, 
                "\n{(%s)}  /filename exch def 10 -2.8 18 showfilename\n\n"
                "EndFrame\n", outf);
    
        // output completed:
        fclose(pssav);
    
        string cmd;
        cout << "entering mygnuplot - to leave, type q\n";
        while(1) {
            cmd = sask("mygnuplot> ");
            if (cmd.substr(0,1)=="q") return;
            gp_write(cmd);
        }
    
    string CPlot::info()
    {
        string ret;
        ret  =   "x: " + X.info();
        ret += "  y: " + X.info();
        return ret;
    }
    
    void CPlot::gp_write( string in )
    
        string out = in + "\n";
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        // printf( "DEBUG GNUPLOT '%s'\n", out.c_str() );
    
        if( write( gp_fifo, out.c_str(), out.size() ) <= 0 )
            throw string( "could not write to gp_fifo" );
    
    void CPlot::ps_ticktack(uint ntack, double *tack, int ntpt, double *ticklim,
    
        uint i;
        ps_accu.push_back( "[\n" );
        if (A->log && ( tack[0]<1e-3 || tack[ntack-1]>1e3 )) {
            for (i=0; i<ntack; i++) {
    
                snprintf( outlin, CPLOT_LINSIZ, "   %9.6f {(10)(%d)sp()} %%{(%g)}\n", 
    
                          A->pc(tack[i]), (int)(log10(tack[i])),
    
                          (float) tack[i]);
                ps_accu.push_back( outlin );
            }
        } else {
            for (i=0; i<ntack; i++) {
    
                snprintf( outlin, CPLOT_LINSIZ, "   %9.6f {(%g)}\n", 
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
                          A->pc(tack[i]), (float) tack[i]);
    
                ps_accu.push_back( outlin );
            }
        }
        ps_accu.push_back( "   ] SetTacVec\n" );
    
        snprintf( outlin, CPLOT_LINSIZ, "   %g %g %d %d SetTicVec%s\n", 
    
    Wuttke, Joachim's avatar
    ?  
    Wuttke, Joachim committed
                  A->pc(ticklim[0]), A->pc(ticklim[1]), ntack+2, ntpt,
                  (A->log? "Log" : "Lin"));
    
        ps_accu.push_back( outlin );