Module Rongo
In: lib/rongo.rb
Methods
angle    arraydata    box    btype    col    color    connect    convert_to_good_ruby    convert_to_good_ruby_string    data    dot    draw    dumpdata    erase    expand    fcolumn    fformula    get_tmpdir    justification    label    limits    logarithm    ltype    lwidth    points    printer    psfix    ptype    putregularpolygon    quit    relocate    rongo    rongo_interactive    rongo_interactive_showerror    senderror    setnormalcommands    setvwscales    setvwscalesbase    squareview    terminal    viewport    writebuffer    xcolumn    xdata    xlabel    xlogarithm    ycolumn    ydata    ylabel    ylogarithm    ylogarithm   
Public Instance methods
setnormalcommands()
# File lib/rongo.rb, line 44
  def setnormalcommands
    @@rongocommands = @@rongospecialcommandsmultiargs +  
      @@rongospecialcommands + @@rongonormalcommands
  end
setvwscalesbase()
# File lib/rongo.rb, line 49
  def setvwscalesbase
    v = pgqvp(1)
    w = pgqwin
    for i in 0..1
      j=i*2
      k=j+1
      @@rongovwscalesbase[i] = (w[j]-w[k])/(v[j]-v[k])
      @@rongovwscales[i]= - @@rongovwscalesbase[i]
    end
  end
setvwscales()
# File lib/rongo.rb, line 59
  def setvwscales
    x = pgqch/40.0
    for i in 0..1
      j=i*2
      k=j+1
      @@rongovwscales[i] = @@rongovwscalesbase[i]*x
    end
  end
putregularpolygon(x,y,rview,n,type)
# File lib/rongo.rb, line 68
  def putregularpolygon(x,y,rview,n,type)
#
# type is mongo point type
#                        0 = open
#                        1 = skeletal (center connected to vertices)
#                        2 = starred
#                        3 = solid


    px=[]
    py=[]
    if $DEBUG
      print "polygon "
      print x, " "
      print y, " "
      print rview, " ",n, " ", type, "\n"
    end
    theta = (@@textangle+90)/360*2* Math::PI
    dt = 2* Math::PI/n
    for i in 0..n
      if type == 1
        px.push(x); py.push(y);
      elsif type == 2
        thetah = theta-dt/2
        scale = rview*@@rongostarscale
        px.push( x+Math.cos(thetah)*scale*@@rongovwscales[0])
        py.push( y+Math.sin(thetah)*scale*@@rongovwscales[1])
      end
      px.push(x+Math.cos(theta)*rview*@@rongovwscales[0])
      py.push(y+Math.sin(theta)*rview*@@rongovwscales[1])
      theta += dt
    end
    if type == 3
      pgpoly px, py
    else
      pgline px, py
    end
  end
senderror(str)
# File lib/rongo.rb, line 107
  def senderror(str)
    if @@rongo_batch_mode == 1
      raise str
    else
      print str, "\n"
    end
  end
data(x)
# File lib/rongo.rb, line 116
  def data (x)
    if File.exist?(x)
      @@datafilename = x
      print "data file name set to ", @@datafilename, "\n" if $DEBUG
    else
      senderror "data command requested non-existing file #{x}"
    end
  end
writebuffer(x = "tmprongo.rb")
# File lib/rongo.rb, line 126
  def writebuffer (x = "tmprongo.rb")
    if File.exist?(x)
      senderror "writebuffer: file #{x} exists"
    end
    f=open(x,"w+")
    f.print Readline::HISTORY.to_a.join("\n"), "\n"
    f.close
  end
get_tmpdir()
# File lib/rongo.rb, line 134
  def get_tmpdir
    tmpdir = ENV["TMP"]
    tmpdir = ENV["TMPDIR"] unless tmpdir
    tmpdir = "/tmp" unless tmpdir
    tmpdir
  end
col(name, colloc)
# File lib/rongo.rb, line 141
  def col(name, colloc)
    colloc -= 1
    loc = @@validcolnames.index(name)
    if loc == nil
      senderror("Col name #{name} not valid for col")
    else
      infile = open(@@datafilename, "r")
      @@coldata[loc].clear
      while s = infile.gets
        val = s.split[colloc]
        @@coldata[loc].push(val.to_f) if val
      end
    end
  end
fcolumn(name, expression)
# File lib/rongo.rb, line 156
  def fcolumn(name, expression)
    loc = @@validcolnames.index(name)
    if loc == nil
      senderror("Col name #{name} not valid for col")
    else
      infile = open(@@datafilename, "r")
      @@coldata[loc].clear
      while s = infile.gets
        a=s.split
        string=""
        a.each_index{|i|
          string += "c#{i+1}=#{a[i].to_f};"  if a[0]!= '#'
        }
        @@coldata[loc].push(eval(string+expression)) 
      end
    end
  end
fformula(name, expression)
# File lib/rongo.rb, line 173
  def fformula(name, expression)
    loc = @@validcolnames.index(name)
    if loc == nil
      senderror("Col name #{name} not valid for col")
    else
      a=[];
      @@coldata[loc].each_index{|i|
        x=@@coldata[0][i];
        y=@@coldata[1][i];
        a.push(eval(expression));
      }
      @@coldata[loc]=a;
    end
  end
logarithm(id)
# File lib/rongo.rb, line 188
  def logarithm(id)
    @@coldata[id].collect!{ |x| x=Math.log10(x)}
  end
xlogarithm()
# File lib/rongo.rb, line 191
  def xlogarithm
    logarithm(0)
  end
ylogarithm()
# File lib/rongo.rb, line 194
  def ylogarithm
    logarithm(1)
  end
arraydata(id, a)
# File lib/rongo.rb, line 198
  def arraydata(id, a)
    @@coldata[id] = a
  end
xdata(a)
# File lib/rongo.rb, line 201
  def xdata(a)
    arraydata(0,a)
  end
ydata(a)
# File lib/rongo.rb, line 204
  def ydata(a)
    arraydata(1,a)
  end
ylogarithm()
# File lib/rongo.rb, line 208
  def ylogarithm
    logarithm(1)
  end
xcolumn(c)
# File lib/rongo.rb, line 212
  def xcolumn(c)
    col "x", c
  end
ycolumn(c)
# File lib/rongo.rb, line 215
  def ycolumn(c)
    col "y", c
  end
dumpdata()
# File lib/rongo.rb, line 218
  def dumpdata
    p @@coldata
    p @@limitval
  end
limits(*xyval)
# File lib/rongo.rb, line 225
  def limits(*xyval)
    if xyval.size < 4
      if xyval.size > 0 
        print "limit: only #{xyval.size} args specified, all ignored \n"
      end
      xyval[0]=@@coldata[0].min
      xyval[1]=@@coldata[0].max
      xyval[2]=@@coldata[1].min
      xyval[3]=@@coldata[1].max
    end
    @@limitval[0..3] = xyval
  end
squareview()
# File lib/rongo.rb, line 240
  def squareview
    x= pgqvp(1)
    p x if $DEBUG
    dx = x[1]-x[0]
    dy = x[3]-x[2]
    x[0]=x[2]=[x[0],x[2]].max
    x[1]=x[3]=[x[1],x[3]].min
    p x if $DEBUG
    pgvsiz x[0], x[1], x[2], x[3]
  end
viewport(*args)
# File lib/rongo.rb, line 251
  def viewport(*args)
    print "box called with ", args.join(" "), "\n" if $DEBUG
    if args.size == 4
      xold = Pgplot.pgqvp(1)
      print "old viewport = " if $DEBUG
      p xold if $DEBUG
      x= []
      x[0] = xold[0]+ args[0]*(xold[1]-xold[0])
      x[1] = xold[0]+ args[1]*(xold[1]-xold[0])
      x[2] = xold[2]+ args[2]*(xold[3]-xold[2])
      x[3] = xold[2]+ args[3]*(xold[3]-xold[2])
      Pgplot.pgvsiz x[0], x[1], x[2], x[3]
      print "new viewport = " if $DEBUG
      xold = Pgplot.pgqvp(1) if $DEBUG
      p xold if $DEBUG
    else
      senderror "viewport requres 4 args, #{args.size} supplied"
    end
  end
convert_to_good_ruby_string(s)

translate rongo script line to ruby script line

# File lib/rongo.rb, line 274
  def convert_to_good_ruby_string(s)
    print "convert_to_good_ruby_string input: ", s, "\n" if $DEBUG
    a = s.split
    commandname = a[0]
    candidate = []
    return s if a.size == 0 
    @@rongocommands.each do |x|
      candidate.push(x) if x.index(commandname) == 0 and commandname.size > 1
    end
    if candidate.size > 1
      x = candidate.join(", ")
      ss = "command \"#{commandname}\" ambiguous, possible name = \"#{x}\""
      senderror ss
    elsif candidate.size == 1
      commandname = candidate[0]
    end
    
    if @@rongospecialcommandsmultiargs.index(commandname)
      joinseperater = '", "'
      firstandend = '"'
    elsif @@rongospecialcommands.index(commandname)
      joinseperater = ' '
      firstandend = '"'
    elsif @@rongocommands.index(commandname)
      joinseperater = ', '
      firstandend = ''
    end
    if @@rongocommands.index(commandname)
      unless  a[1] =~ /^"/
        s = commandname        
        s += " "+firstandend + a[1..a.size].join(joinseperater) + firstandend  if a.size >1
        s += "\n"
      end
    end
    
    print "convert_to_good_ruby_string result: ", s, "\n" if $DEBUG
    s
  end
convert_to_good_ruby(filename)

translate rongo script file to ruby script file.

# File lib/rongo.rb, line 315
  def convert_to_good_ruby(filename)
    ruby_filename = get_tmpdir + "/." +File.basename(filename);
    print "Tmp file name = ", ruby_filename, "\n" if $DEBUG
    infile=open(filename, "r")
    outfile=open(ruby_filename, "w+")
    while s = infile.gets
      outfile.print convert_to_good_ruby_string(s)
    end
    infile.close 
    outfile.close 
    system("cat "+    ruby_filename) if $DEBUG
    ruby_filename
  end
btype(xopt, yopt)
# File lib/rongo.rb, line 329
  def btype (xopt, yopt)

       @@limitval[4] = xopt
       @@limitval[5] = yopt
     end
box(*args)

BOX commmad — box XOPT YOPT works as wrapper for PGPLOT PGBOX. The arguments are different from that of MONGO box command.

    Options (for parameters XOPT and YOPT):
      A : draw Axis (X axis is horizontal line Y=0, Y axis is vertical
                     line X=0).
      B : draw bottom (X) or left (Y) edge of frame.
      C : draw top (X) or right (Y) edge of frame.
      G : draw Grid of vertical (X) or horizontal (Y) lines.
      I : Invert the tick marks; ie draw them outside the viewport
      instead of inside.
      L : label axis Logarithmically (see below).
      N : write Numeric labels in the conventional location below the
      viewport (X) or to the left of the viewport (Y).
      P : extend ("Project") major tick marks outside the box (ignored if
       option I is specified).
      M : write numeric labels in the unconventional location above the
      viewport (X) or to the right of the viewport (Y).
      T : draw major Tick marks at the major coordinate interval.
      S : draw minor tick marks (Subticks).
      V : orient numeric labels Vertically. This is only applicable to Y.
      The default is to write Y-labels parallel to the axis.
      1 : force decimal labelling, instead of automatic choice (see PGNUMB).
      2 : force exponential labelling, instead of automatic.
# File lib/rongo.rb, line 359
  def box(*args)
    print "box called\n" if $DEBUG
    p @@limitval if $DEBUG
    if args.size == 2 
      @@limitval[4] = args[0]
      @@limitval[5] = args[1]
    end
    p @@limitval if $DEBUG
    #    pgenv(@@limitval[0],@@limitval[1],@@limitval[2],    
    #     @@limitval[3],@@limitval[4],@@limitval[5])
    pgswin @@limitval[0],@@limitval[1],@@limitval[2],@@limitval[3]
    p @@limitval[4], 0, 0, @@limitval[5], 0, 0 if $DEBUG
    pgbox @@limitval[4], 0, 0, @@limitval[5], 0, 0
    setvwscalesbase
    setvwscales
  end
connect()

MONGO connect

# File lib/rongo.rb, line 378
  def connect
    pgline @@coldata[0], @@coldata[1]
  end
ptype(*x)

MONGO ptype

# File lib/rongo.rb, line 383
  def ptype(*x)
    if x.size == 2
      @@rongopointtype = x
    elsif x.size == 1
      @@rongopointtype[0] = (x[0]/10).to_i
      @@rongopointtype[1] = x[0]%10
    else
      p x.size if $DEBUG
      p x.join(" ")  if $DEBUG
      if x.size > 2
        senderror "ptype: too many args #{x}"
      else
        senderror "ptype: no args given. Two required"
      end
    end
  end
points()
 MONGO points this function calls putregularpolygon for each

coordinates point to draw points, to achive the compatibility with MONGO. PGPLOT pgpt works differently, but may be faster in some cases.

# File lib/rongo.rb, line 404
  def points
#    Rongo.setvwscales
    pgbbuf
    for i in 0...@@coldata[0].size
      putregularpolygon @@coldata[0][i], @@coldata[1][i],
        1.0,  @@rongopointtype[0],@@rongopointtype[1]
    end
    pgebuf
    #    pgpt @@coldata[0], @@coldata[1], @@rongopointtype
  end
dot()

MONGO dot

# File lib/rongo.rb, line 416
  def dot
#    Rongo.setvwscales
    putregularpolygon @@cposx, @@cposy, 1.0,
      @@rongopointtype[0],@@rongopointtype[1]
#    pgpt1 @@cposx, @@cposy, @@rongopointtype
  end
quit()

quit — replacement for MONGO end

# File lib/rongo.rb, line 425
  def quit
    pgend
    exit
  end
erase()
# File lib/rongo.rb, line 430
  def erase
    pgeras
  end
xlabel(s)

MONGO xlabel

# File lib/rongo.rb, line 435
  def xlabel(s)
    pglab s, "", ""
  end
ylabel(s)

MONGO ylabel

# File lib/rongo.rb, line 440
  def ylabel(s)
    pglab  "", s,  ""
  end
relocate(x,y)

MONGO relocate

# File lib/rongo.rb, line 445
  def relocate(x,y)
    @@cposx = x
    @@cposy = y
    pgmove x, y
  end
expand(x)

MONGO expand

# File lib/rongo.rb, line 453
  def expand(x)
    Pgplot.pgsch x
    setvwscales
  end
terminal(x = "/xserve")
# File lib/rongo.rb, line 458
  def terminal(x = "/xserve")
    pgopen x
  end
printer(pp = "pgplot.ps/vcps")
# File lib/rongo.rb, line 461
  def printer(pp = "pgplot.ps/vcps")
# Removing the next line and writing pgopen pp resulted
# in the following internal error message
#: [BUG] unknown node type 0
#ruby 1.8.1 (2003-12-25) [i686-linux]
#
#Abort
    xx = pp
    @@rongoprinterfilename=xx.split("/")[0]
    print @@rongoprinterfilename
    pgopen xx
  end
ltype(t)
# File lib/rongo.rb, line 474
  def ltype(t)
    pgsls t
  end
lwidth(w)
# File lib/rongo.rb, line 478
  def lwidth(w)
    pgslw w
  end
color(c)
# File lib/rongo.rb, line 481
  def color(c)
    pgsci c
  end
justification(s)
# File lib/rongo.rb, line 484
  def justification(s)
    if s == "center" then
      @@justification = 0.5
    elsif s == "left" then
      @@justification = 0.0
    elsif s == "right" then
      @@justification = 1.0
    else
      @@justification = s.to_f
    end
  end
angle(x)
# File lib/rongo.rb, line 496
  def angle(x)
    @@textangle = x.to_f
  end
label(s)
# File lib/rongo.rb, line 499
  def label(s)
    pgptxt @@cposx, @@cposy, @@textangle, @@justification, s
  end
draw(x,y)
# File lib/rongo.rb, line 503
  def draw (x,y)
    pgdraw x, y
    @@cposx = x
    @@cposy = y
  end
psfix(filename = nil)
# File lib/rongo.rb, line 509
  def psfix(filename = nil)
    filename = @@rongoprinterfilename unless filename
    f = open(filename, "r")
    text = f.gets(nil)
    f.close
    bbox = nil
    text.each{ |s| bbox = s if s =~ /^%%BoundingBox/}
    p bbox if $DEBUG
    f=open(filename, "w+")
    text.each do |s|
      if s =~ /^%%BoundingBox/
        f.print bbox
      else
        f.print s
      end
    end
    f.close
  end
rongo_interactive_showerror(s,x)
# File lib/rongo.rb, line 529
  def rongo_interactive_showerror(s,x)
    p x if $DEBUG
    if x.kind_of? NameError 
      print 'Unknown command "', s.chomp, "\"\n"
    elsif x.kind_of? ArgumentError
      errm = x.to_s
      errm = errm[errm.index("'")+1,errm.size]
      print'Command "', s.chomp, '"', " failed, ", errm, "\n"
    else
      print 'Command "', s.chomp, '"', " failed, ",x , "\n"
    end
  end
rongo_interactive()
# File lib/rongo.rb, line 544
  def rongo_interactive
    print "Entering interactive mode\n"
    stty_save = `stty -g`.chomp
    begin
      while s = Readline.readline("* ",true)
        s= convert_to_good_ruby_string(s)
        begin
          eval s
        rescue
          rongo_interactive_showerror(s,$!)
        end
      end
    rescue Interrupt
      system("stty", stty_save)
      exit
    end
  end
rongo(fname)
# File lib/rongo.rb, line 562
  def rongo(fname)
    if fname 
      @@rongo_batch_mode = 1
      s = convert_to_good_ruby(fname)
      system("cat "+s) if $DEBUG
      load s, false
      File.delete(s)
    else
      rongo_interactive
    end
  end