[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[dennou-ruby:001242] gdcl



堀之内です。

やっとこさ、西澤君の netcdf ビューワーを入れてみました。
これまで諸々のパッチあてが面倒だなと思ってたのですが、
パッチを当たのを用意してくれててインストールが楽に
なりました。Solarisだとruby-gnomeのインストールがやっかいでした
が(全部は入れてない)。

さて、若干のアップデートをお願い。

サンプルプログラムにしろ netcdf ビューワーにしろ
require "numru/gdcl" や GDCL::hogehoge が残ってますが、
めでたく消えてますから直してください。gdclではまりました。

gdcl は dcl の Gtk対応パッチでもあり netcdfビューワーでもあった
んですね(名前が兼ねてる)。前者は前述のようにめでたく消えました。
一方後者は名前変えた方がよくないですか。これはファイル名だけの問
題のようですが。

さらに、新 ruby-dcl 対応がありますね。配列長さを引数から消す。

とりあえず動作を確認したかったので、一応やりました。漏れはあるか
も知れませんが、とりあえず添付します。
=begin header

  draw.rb - a part of main.rb

=end

require "gdk_imlib"

require "narray"
require "numru/dcl"
#require "numru/gdcl"
require "nispack/events"
include NumRu


class Draw
  def initialize
    @xxxxxx = nil
    @xxxxxx=400
    @xxxxxx=400

    @xxxxxx = Array.new(4)

    clear

  end


  def clear
    @xxxxxx = nil
    @xxxxxx = nil
    @xxxxxx=0.2; @xxxxxx=0.8
    @xxxxxx=0.2; @xxxxxx=0.8

    @xxxxxx=nil
    @xxxxxx=""; @xxxxxx=""
    @xxxxxx=""; @xxxxxx=""
    @xxxxxx = Fig.new
    @xxxxxx = Mouse_Events.new(@xxxxxx,@xxxxxx)
  end

  def names(filename,varname,axisxname,axisyname)
    @xxxxxx=filename
    @xxxxxx=varname
    @xxxxxx=axisxname
    @xxxxxx=axisyname
  end

  def range(xmin,xmax,ymin,ymax)
    @xxxxxx=xmin
    @xxxxxx=xmax
    @xxxxxx=ymin
    @xxxxxx=ymax
  end

  def var=(v)
    @xxxxxx = v
  end
  def axisx(ax,log)
    @xxxxxx = ax
    @xxxxxx = log
  end
  def axisy(ay,log)
    @xxxxxx = ay
    @xxxxxx = log
  end
  def var
    @xxxxxx
  end
  def missing_value=(value)
    @xxxxxx=value
  end

  def var_set(st,en)
    @xxxxxx(@xxxxxx,@xxxxxx,@xxxxxx,@xxxxxx,st,en)
  end

  def axis_title(xtitle,xunit,ytitle,yunit)
    @xxxxxx=xtitle
    @xxxxxx=xunit
    @xxxxxx=ytitle
    @xxxxxx=yunit
    @xxxxxx(@xxxxxx,@xxxxxx,@xxxxxx,@xxxxxx)
  end

  def title(str,hash)
    @xxxxxx = str
    @xxxxxx = hash
  end

  def maptype(int)
    @xxxxxx=int
  end
    

  def open_graphics
    set_env

    @xxxxxx = Gtk::DrawingArea.new
    @xxxxxx(@xxxxxx,@xxxxxx)
    @xxxxxx("expose_event"){|w,p|
      expose_event(w)
    }
    @xxxxxx("configure_event"){|w,p|
      configure_event(w)
      @xxxxxx =  w.allocation.width; @xxxxxx=@xxxxxx
      @xxxxxx = w.allocation.height; @xxxxxx=@xxxxxx
    }
    @xxxxxx("button_press_event"){|w,p|
      if p.button==1 then
	px = (p.x-@xxxxxx)/@xxxxxx
	py = 1 - (p.y-@xxxxxx)/@xxxxxx
	print "press:",px,",",py,"\n"
	@xxxxxx(px,py,@xxxxxx,@xxxxxx,@xxxxxx,@xxxxxx)
      else
	w.get_toplevel.hide
      end
    }
    @xxxxxx("button_release_event"){|w,p|
      px = (p.x-@xxxxxx)/@xxxxxx
      py = 1 - (p.y-@xxxxxx)/@xxxxxx
      if @xxxxxx?
	@xxxxxx,@xxxxxx,@xxxxxx,@xxxxxx, = @xxxxxx(px,py)
	configure_event(w)
	expose_event(w)
      end
    }
    @xxxxxx("motion_notify_event"){|w,p|
      px = (p.x-@xxxxxx)/@xxxxxx
      py = 1 - (p.y-@xxxxxx)/@xxxxxx
      if @xxxxxx?
	expose_event(w)
	@xxxxxx(w,px,py)
      end
    }
    @xxxxxx("key_press_event"){|w,p|
      p "key press"
      w.get_toplevel.destroy
    }
    @xxxxxx(Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_MOTION_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::KEY_PRESS_MASK)
    @xxxxxx

    @xxxxxx = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL)
    @xxxxxx("delete_event"){|w,p| w.hide; true}
    @xxxxxx("destroy_event"){exit}
    @xxxxxx
    @xxxxxx(@xxxxxx)
    @xxxxxx(@xxxxxx)
    @xxxxxx

  end

  def draw_x(type)

    @xxxxxx = type

    if @xxxxxx?
      open_graphics
    else
      @xxxxxx
      configure_event(@xxxxxx)
      expose_event(@xxxxxx)
    end

  end

  def save_code(filename)
    file = File.open(filename,"w")

    file.print(@xxxxxx+"\n\n")
    file.print(@xxxxxx+"\n\n")
    file.print(@xxxxxx+"\n\n")
    file.print(@xxxxxx+"\n\n")
    file.print(@xxxxxx+"\n\n")
    file.print(@xxxxxx+"\n\n")
    file.print(@xxxxxx+"\n\n")
    file.print(@xxxxxx+"\n\n")
    file.print(@xxxxxx+"\n\n")
    file.print(@xxxxxx+"\n\n")

    file.close
  end

  def save_image(filename)
    geom = @xxxxxx
    mask = Gdk::Bitmap.new(@xxxxxx, geom[2], geom[3])
    im = GdkImlib::Image.create_from_drawable(@xxxxxx, mask, geom[0], geom[1], geom[2], geom[3])
    im.save(filename)
    p "save image to #{filename}"
  end

  private
  def set_env
    DCL::sglset("LCNTL", false )
    DCL::udlset("LMSG", false )
    @xxxxxx('DCL::sglset("LCNTL", false )')
    @xxxxxx('DCL::udlset("LMSG", false )')

    if @xxxxxx then
      DCL::gllset("LMISS", true )
      DCL::glrset("RMISS", @xxxxxx )
      @xxxxxx('DCL::gllset("LMISS", true )')
      @xxxxxx("DCL::glrset(\"RMISS\", #{@xxxxxx})")
    end
  end

  private
  def axis
    DCL::ussttl(@xxxxxx, @xxxxxx, @xxxxxx, @xxxxxx)
    DCL::usdaxs
    @xxxxxx('DCL::ussttl(x_title, x_unit, y_title, y_unit)')
    @xxxxxx('DCL::usdaxs')
    DCL::sglset("LCLIP", false )
    @xxxxxx('DCL::sglset("LCLIP", false )')
    i=0
    @xxxxxx{|key,value|
      DCL::sgtxzv(@xxxxxx+0.01,@xxxxxx*i,key+"="+value.to_s,0.02,0,-1,1)
      @xxxxxx("i=#{i}")
      @xxxxxx("DCL::sgtxzv(vxmax+0.01,vymax-0.03*i,\"#{key}=#{value.to_s}\",0.02,0,-1,1)")
      i+=1
    }
      
  end

  private
  def draw_1d(title)

    DCL::sgplzu(@xxxxxx, @xxxxxx, 1, 3 )
    @xxxxxx('DCL::sgplzu(axisx, var, 1, 3 )')

    axis

    DCL::uxsttl("t", title, 0 )
    @xxxxxx("title = \"#{title}\"")
    @xxxxxx('DCL::uxsttl("t", title, 0 )')

  end

  private
  def draw_2d(title,type)

    DCL::uwsgxa(@xxxxxx)
    DCL::uwsgya(@xxxxxx)
    @xxxxxx('DCL::uwsgxa(axisx)')
    @xxxxxx('DCL::uwsgya(axisy)')



    DCL::ueitlv
    DCL::uegtla(@xxxxxx, @xxxxxx, 0 )
    DCL::uetonf(@xxxxxx)
    @xxxxxx('DCL::ueitlv')
    @xxxxxx('DCL::uegtla(var.min, var.max, 0 )')
    @xxxxxx('DCL::uetonf(var)')
    DCL::udcntr(@xxxxxx)
    @xxxxxx('DCL::udcntr(var)')


    if type==CONTOUR then
      axis
    elsif type==MAP then
      DCL::umpglb
      @xxxxxx("DCL::umpglb")
    end

    DCL::uzrset("ROFFXT", 0.06)
    DCL::uxsttl("t", title, 0 )
    @xxxxxx('DCL::uzrset("ROFFXT", 0.06)')
    @xxxxxx("title = \"#{title}\"")
    @xxxxxx('DCL::uxsttl("t", title, 0 )')

  end

  private
  def draw

    @xxxxxx(@xxxxxx,@xxxxxx,@xxxxxx,@xxxxxx)

    @xxxxxx(@xxxxxx,@xxxxxx,@xxxxxx,@xxxxxx)


    @xxxxxx
    @xxxxxx

    if @xxxxxx == MAP then
      itr = @xxxxxx
      p itr
      DCL::grfrm
      @xxxxxx("DCL::grfrm")
#      DCL::grswnd( @xxxxxx, @xxxxxx, @xxxxxx, @xxxxxx )
#      @xxxxxx("DCL::grswnd(xmin,xmax,ymin,ymax)")
      DCL::grsvpt( @xxxxxx, @xxxxxx, @xxxxxx, @xxxxxx )
      @xxxxxx("DCL::grsvpt(vxmin,vxmax,vymin,vymax)")
#      DCL::grssim((@xxxxxx@xxxxxx)/2,0.0,0.0)
#      @xxxxxx("DCL::grssim((vxmax-vxmin)/2,0.0,0.0)")
      DCL::grsmpl(0.0,90.0,0.0)
      @xxxxxx("DCL::grsmpl(0.0,90.0,0.0)")
#      DCL::grstxy(-180,180,0,90)
#      @xxxxxx("DCL::grstxy(xmin,xmax,ymin,ymax)")
      DCL::grstrn(itr)
      @xxxxxx("DCL::grstrn(#{itr})")
      DCL::umpfit
      @xxxxxx("DCL::umpfit")
      DCL::grstrf
      @xxxxxx("DCL::grstrf")
    else
      itr = 1
      itr += 2 if @xxxxxx; itr += 1 if @xxxxxx
      DCL::grfrm
      @xxxxxx("DCL::grfrm")
      DCL::grswnd( @xxxxxx, @xxxxxx, @xxxxxx, @xxxxxx )
      @xxxxxx("DCL::grswnd(xmin,xmax,ymin,ymax)")
      DCL::grsvpt( @xxxxxx, @xxxxxx, @xxxxxx, @xxxxxx )
      @xxxxxx("DCL::grsvpt(vxmin,vxmax,vymin,vymax)")
      DCL::grstrn(itr)
      @xxxxxx("DCL::grstrn(#{itr})")
      DCL::grstrf
      @xxxxxx("DCL::grstrf")
    end



    DCL::sglset("LCLIP", true )
    @xxxxxx('DCL::sglset("LCLIP", true )')
    if @xxxxxx == LINE then
      draw_1d(@xxxxxx)
    elsif (@xxxxxx==CONTOUR) || (@xxxxxx==MAP) then
      draw_2d(@xxxxxx,@xxxxxx)
    end


  end

  private
  def expose_event(widget)
    if ! @xxxxxx?
      gc = widget.style.fg_gc(widget.state)
      geom = widget.window.get_geometry
      widget.window.draw_pixmap(gc, @xxxxxx, geom[0], geom[1],
				geom[0],geom[1], geom[2], geom[3])
    end
    false
  end

  private
  def configure_event(widget)
    geom = widget.window.get_geometry
    if ! @xxxxxx? then
      @xxxxxx = nil
    end
    if (geom[2]>0 && geom[3]>0) then
      @xxxxxx = Gdk::Pixmap.new(widget.window, geom[2], geom[3], -1)
      @xxxxxx(widget.style.white_gc, true, 0, 0, 
			    geom[2], geom[3])
    end

    DCL::zgsdrw(widget)
    DCL::sgopn(4)
    DCL::zgspmp(@xxxxxx)

    draw

    true
  end





  private
  class Fig

    def initialize
      @xxxxxx = ""
      @xxxxxx = ""
      @xxxxxx = ""
      @xxxxxx = Array.new
      @xxxxxx = Array.new
      @xxxxxx = Array.new
      @xxxxxx = Array.new
      @xxxxxx = Array.new
      @xxxxxx = Array.new
      @xxxxxx = ""

      @xxxxxx = <<"END"
require "gtk"
require "narray"
require "numru/dcl"
require "numru/netcdf"

include NumRu
END

      @xxxxxx = <<"END"
DCL::gropn(1)
END
      ymin=@xxxxxx; ymax=@xxxxxx
      vxmin=@xxxxxx; vxmax=@xxxxxx
      vymin=@xxxxxx; vymax=@xxxxxx
      @xxxxxx[0] = "xmin = axisx[0]"
      @xxxxxx[1] = "xmax = axisx[-1]"
      @xxxxxx[2] = "ymin = 0.0"
      @xxxxxx[3] = "ymax = 1.0"
      @xxxxxx[0] = "vxmin = 0.2"
      @xxxxxx[1] = "vxmax = 0.8"
      @xxxxxx[2] = "vymin = 0.2"
      @xxxxxx[3] = "vymax = 0.8"

      @xxxxxx[0] = 'x_title = ""'
      @xxxxxx[1] = 'x_unit = ""'
      @xxxxxx[2] = 'y_title = ""'
      @xxxxxx[3] = 'y_unit = ""'

      @xxxxxx = <<"END"
DCL::grcls
END

    end

    def head
      @xxxxxx
    end

    def var_set(filename,varname,axisxname,axisyname,st,en)
      @xxxxxx = <<"END"
filename = "#{filename}"

file = NetCDF.open(filename,mode="r")

varname = "#{varname}"
varobj = file.var(varname)
st = [#{st.join(",")}]; en = [#{en.join(",")}]
var = varobj.get("start"=>st,"end"=>en)

axisxname = "#{axisxname}"
axisxobj = file.var(axisxname)
axisx = axisxobj.get

END
      if (axisyname!="") and (!axisyname.nil?) then
	@xxxxxx << "axisyname = \"#{axisyname}\"\n"
	@xxxxxx << "axisyobj = file.var(axisyname)\n"
	@xxxxxx << "axisy = axisyobj.get\n"
      end

      @xxxxxx << "\nfile.close\n"

    end
    def var
      @xxxxxx
    end

    def open
      @xxxxxx
    end

    def parm_set(str)
      @xxxxxx(str)
    end
    def parm
      @xxxxxx("\n")
    end

    def window_set(xmin,xmax,ymin,ymax)
      @xxxxxx[0] = "xmin = #{xmin}"
      @xxxxxx[1] = "xmax = #{xmax}"
      @xxxxxx[2] = "ymin = #{ymin}"
      @xxxxxx[3] = "ymax = #{ymax}"
    end
    def window
      @xxxxxx("\n")
    end
  
    def viewport_set(vxmin,vxmax,vymin,vymax)
      @xxxxxx[0] = "vxmin = #{vxmin}"
      @xxxxxx[1] = "vxmax = #{vxmax}"
      @xxxxxx[2] = "vymin = #{vymin}"
      @xxxxxx[3] = "vymax = #{vymax}"
    end
    def viewport
      @xxxxxx("\n")
    end

    def frame_clear
      @xxxxxx
    end
    def frame_set(str)
      @xxxxxx(str)
    end
    def frame
      @xxxxxx("\n")
    end

    def title_set(xt,xu,yt,yu)
      @xxxxxx[0] = "x_title = \"#{xt}\""
      @xxxxxx[1] = "x_unit = \"#{xu}\""
      @xxxxxx[2] = "y_title = \"#{yt}\""
      @xxxxxx[3] = "y_unit = \"#{yu}\""
    end
    def title
      @xxxxxx("\n")
    end


    def draw_clear
      @xxxxxx
    end
    def draw_set(str)
      @xxxxxx(str)
    end
    def draw
      @xxxxxx("\n")
    end

    def close
      @xxxxxx
    end

  end



end
#!/usr/bin/ruby

require "gtk"

require "numru/netcdf"

require "nispack"

include NumRu



class Variables
  def initialize; @xxxxxx=Hash.new; end
  def vobj=(obj); @xxxxxx=obj; end
  def dname=(name); @xxxxxx=name; end
  def vobj; @xxxxxx; end
  def dname; @xxxxxx; end
  def set_att(name,val); @xxxxxx[name]=val; end
  def att; @xxxxxx; end
end

class Var
  def initialize; @xxxxxx=Hash.new; end
  def name=(str); @xxxxxx=str; end
  def name; @xxxxxx; end
  def missing_value=(value); @xxxxxx=value; end
  def missing_value; @xxxxxx; end
  def set_dim(str,value); dims[str]=value; end
  def dims; @xxxxxx; end
  def title=(str); @xxxxxx=str; end
  def unit=(str); @xxxxxx=str; end
  def title
    if @xxxxxx? || @xxxxxx=="" then
      @xxxxxx
    else
      @xxxxxx+" ("+@xxxxxx+")"
    end
  end
  def clear
    @xxxxxx=nil
    @xxxxxx=nil
    @xxxxxx
    @xxxxxx=nil
  end
end

class Dimensions
  def initialize; @xxxxxx = Hash.new; end
  def value=(value); @xxxxxx=value; end
  def select=(select); @xxxxxx=select; end
  def value; @xxxxxx; end
  def set_att(name,val); @xxxxxx[name]=val; end
  def att; @xxxxxx; end
  def select; @xxxxxx; end
  def select_value; @xxxxxx[@xxxxxx]; end
end



# draw tyep constant
LINE = 1
CONTOUR = 2
MAP = 4


def s2f(str)
  if str=="" then
    false
  else
    str.to_f
  end
end
def min(x,y)
  if x<y then
    x
  else
    y
  end
end



$drawtype = LINE

$var = Var.new
$x_axis = Axis.new
$y_axis = Axis.new

$vars = Hash.new
$dims = Hash.new

accel = Gtk::AccelGroup.new

# sub windows
var_window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL).set_title("Variable")
axis_window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL).set_title("Axis")
$dims_window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL).set_title("Dimensions")
attr_window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL).set_title("Attributes")
movie_window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL).set_title("Movie")
map_window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL).set_title("Map")
var_window.signal_connect("delete_event"){true}
axis_window.signal_connect("delete_event"){true}
$dims_window.signal_connect("delete_event"){true}
attr_window.signal_connect("delete_event"){true}
movie_window.signal_connect("delete_event"){true}
map_window.signal_connect("delete_event"){true}


# OpenFile
def file_open(filename)
  $filename = filename
  print "open ",$filename,"\n"
  $file = NetCDF.open($filename,mode="r")

  clear_var
  $vars.clear
  $dims.clear
  $dimtable.hide if !$dimtable.nil?

  $file.var_names.each_index{|i|
    vn = $file.var_names[i]
    var = $file.var(vn)
    dims = var.dim_names
    if (dims.length==1) && (dims[0]==vn) && (! $dims.has_key?(vn)) then
      $dims[vn] = Dimensions.new
      $dims[vn].value = var.get
      $dims[vn].select = Select_value.new(var.get)
      var.each_att{|att| $dims[vn].set_att(att.name,att.get)}
    elsif ! $vars.has_key?(vn)
      $vars[vn] = Variables.new
      $vars[vn].vobj = var
      $vars[vn].dname = dims
      var.each_att{|att| $vars[vn].set_att(att.name,att.get)}
    end
    clist = Gtk::CList.new(["name","value"])
    clist.set_usize(300, 200)
    clist.set_column_width(0,100)
    var.each_att{|att|
      val = att.get
      if val.type==NArray then
	val = att.get.to_a.join(",")
      end
      clist.append([att.name,val])
    }
    $attr_note.append_page_menu(clist.show,Gtk::Label.new(vn).show,Gtk::Label.new(vn))
  }
  clist = Gtk::CList.new(["name","value"])
  clist.set_usize(300, 200)
  clist.set_column_width(0,100)
  $file.each_att{|att|
    val = att.get
    if val.type==NArray then
      val = att.get.to_a.join(",")
    end
    clist.append([att.name,val])
  }
  $attr_note.append_page_menu(clist.show,Gtk::Label.new("global").show,Gtk::Label.new("global").show)


  $dimtable = Gtk::Table.new(2, $dims.length, false)
  i=0
  $dims.each_key{|dim|
    $dimtable.attach(Gtk::Label.new(dim).set_usize(30,20).show, 0, 1, i, i+1 )
    $dimtable.attach($dims[dim].select.box, 1, 2, i, i+1 )
    i += 1
  }
  $dims_window.add($dimtable.show)

  $var_select.set($vars.keys)
  $var_select.active

end


## Select figure type
type_itemfact = Gtk::ItemFactory.new(Gtk::ItemFactory::TYPE_OPTION_MENU, "<type>", accel)
type_itemfact.create_item("/Line", nil, Gtk::ItemFactory::ITEM){
  $drawtype = LINE
  clear_axisx
  $axisx_select.active
}
type_itemfact.create_item("/Contour", nil, Gtk::ItemFactory::ITEM){
  $drawtype = CONTOUR
  clear_axisx
  $axisx_select.active
}
type_itemfact.create_item("/Map", nil, Gtk::ItemFactory::ITEM){
  $drawtype = MAP
  clear_axisx
  $axisx_select.active
}
$typeomenu = type_itemfact.get_widget("<type>").show


# Select Values
## Combo
$var_select = Var_select.new("variable")
$axisx_select = Var_select.new("x-axis")
$axisy_select = Var_select.new("y-axis")
$var_select.signal{set_var}
$axisx_select.signal{set_axisx}
$axisy_select.signal{set_axisy}

def set_var
  $var.name = $var_select.get
  if $var.name =~ /^select/ then
    $var.name = nil
  else
    print "variable name is ",$var.name,"\n"
    $vars[$var.name].att.each{|key,value|
      if key=~/miss/ then $var.missing_value=value[0]; $miss_entry.set_text($var.missing_value.to_s) end
      if key=~/name/ then $var.title=value; $title_entry.set_text($var.title) end
      if key=~/unit/ then $var.unit=value; $title_entry.set_text($var.title) end
    }
    ax = $vars[$var.name].dname
    clear_axisx
    if ax.length == 1 then
      $x_axis.name = ax[0]
      $x_axis.value = $dims[$x_axis.name].value
      $typeomenu.set_sensitive(false)
      $drawtype = LINE
      set_draw
    else
      $typeomenu.set_sensitive(true)
      $axisx_select.set(ax)
      $axisx_select.active
    end
  end
end
def set_axisx
  $x_axis.name = $axisx_select.get
  if $x_axis.name =~ /^select/ then
    $x_axis.clear
  else
    print "Axis(x) name is ",$x_axis.name,"\n"
    $x_axis.value = $dims[$x_axis.name].value
    $x_axis.title = $x_axis.name; $x_title.set_text($x_axis.title)
    $dims[$x_axis.name].att.each{|key,value|
      if key=~/unit/ then $x_axis.unit=value; $x_unit.set_text($x_axis.unit) end
    }

    clear_axisy
    ay = $vars[$var.name].dname.collect{|i| i}
    ay.delete($x_axis.name)
    if $drawtype == LINE then
      set_draw
    else
      $axisy_select.set(ay)
      $axisy_select.active
    end
  end
end
def set_axisy
  $y_axis.name = $axisy_select.get
  if $y_axis.name =~ /^select/ then
    $y_axis.clear
  else
    print "Axis(y) name is ",$y_axis.name,"\n"
    $y_axis.value = $dims[$y_axis.name].value
    $y_axis.title = $y_axis.name; $y_title.set_text($y_axis.title)
    $dims[$y_axis.name].att.each{|key,value|
      if key=~/unit/ then $y_axis.unit=value; $y_unit.set_text($y_axis.unit) end
    }
    
    set_draw
  end
end
def set_draw
  dims = $vars[$var.name].dname.collect{|i| i}
  dims.delete($x_axis.name)
  dims.delete($y_axis.name)
  clear_draw
  $var.dims.clear
  dims.each{|dim|
    $dims[dim].select.active
    $var.set_dim(dim,$dims[dim].select_value)
  }
  $movie_combo.set_popdown_strings(dims)
  $drawbutton.set_sensitive(true)
  $movie_box.set_sensitive(true)
end
def clear_var
  $var.clear
  clear_axisx
  $var_select.not_active
end
def clear_axisx
  $x_axis.clear
  clear_axisy
  $axisx_select.not_active
  $logxbutton.set_active(false)
  $x_title.set_text("")
end
def clear_axisy
  $y_axis.clear
  clear_draw
  $axisy_select.not_active
  $logybutton.set_active(false)
  $y_title.set_text("")
end
def clear_draw
  $drawbutton.set_sensitive(false)
  $dims.each_value{|dim| dim.select.not_active}
  $movie_box.set_sensitive(false)
  $movie_combo.entry.set_text("-")
end

## Draw button
$drawbutton = Gtk::Button.new("draw").show.set_sensitive(false)

$drawbutton.signal_connect("clicked"){ draw }

def draw

  st = Array.new($vars[$var.name].dname.size,0)
  en = Array.new($vars[$var.name].dname.size,0)
  ax = 0
  ay = 0

  $vars[$var.name].dname.each{|dim|
    index = $vars[$var.name].dname.index(dim)
    if dim == $x_axis.name then
      ax = index
      st[index]=0
      en[index]=-1
    elsif ($drawtype != LINE) && (dim == $y_axis.name) then
      ay = index
      st[index]=0
      en[index]=-1
    else
      st[index] = $dims[dim].select.index
      en[index] = $dims[dim].select.index
      $var.set_dim(dim,$dims[dim].select_value)
    end
  }

  var = $vars[$var.name].vobj.get("start"=>st, "end"=>en)
  if ($drawtype != LINE) && (ax > ay) then
    var = var.transpose(1,0)
  end

  $x_axis.min = s2f($x_min.get_text)
  $x_axis.max = s2f($x_max.get_text)
  $y_axis.min = s2f($y_min.get_text)
  $y_axis.max = s2f($y_max.get_text)

  if !$x_axis.min then
    $x_axis.min = $x_axis.value[0]
  end
  if !$x_axis.max then
    $x_axis.max = $x_axis.value[-1]
  end
  if !$y_axis.min then
    if $drawtype==LINE
      $y_axis.min = var.min
    else
      $y_axis.min = $y_axis.value[0]
    end
  end
  if !$y_axis.max then
    if $drawtype==LINE
      $y_axis.max = var.max
    else
      $y_axis.max = $y_axis.value[-1]
    end
  end

  if (min($x_axis.min,$x_axis.max)<=0) and $x_axis.log? then
    dialog = Gtk::Dialog.new.set_title("Error Message")
    dialog.set_modal(true)
    dialog.vbox.pack_start(Gtk::Label.new("X-Axis has negative value for log scale").show)
    ok_button=Gtk::Button.new("OK").show
    ok_button.signal_connect("clicked"){dialog.destroy}
    dialog.action_area.pack_start(ok_button)
    dialog.show
  elsif (min($y_axis.min,$y_axis.max)<=0) and $y_axis.log? then
    dialog = Gtk::Dialog.new.set_title("Error Message")
    dialog.set_modal(true)
    dialog.vbox.pack_start(Gtk::Label.new("Y-Axis has negative value for log scale").show)
    ok_button=Gtk::Button.new("OK").show
    ok_button.signal_connect("clicked"){dialog.destroy}
    dialog.action_area.pack_start(ok_button)
    dialog.show
  else

    $fig = Draw.new if $fig.nil?

    $fig.names($filename,$var.name,$x_axis.name,$y_axis.name)
    $fig.axisx($x_axis.value,$x_axis.log?)
    $fig.axisy($y_axis.value,$y_axis.log?)
    $fig.range($x_axis.min,$x_axis.max,$y_axis.min,$y_axis.max)

    $x_axis.title=$x_title.get_text
    $x_axis.unit=$x_unit.get_text
    $y_axis.title=$y_title.get_text
    $y_axis.unit=$y_unit.get_text
    $fig.axis_title($x_axis.title,$x_axis.unit,$y_axis.title,$y_axis.unit)
    $var.missing_value=$miss_entry.get_text
    if ($var.missing_value!="") and (!$var.missing_value.nil?) then
      $fig.missing_value = $var.missing_value
    end

    $fig.var_set(st,en)
    $fig.var = var

    $var.title = $title_entry.get_text
    $var.unit = ""
    $fig.title($var.title,$var.dims)
    if $drawtype==MAP then
      p $map_combo.entry.get_text
      $fig.maptype($maps.index($map_combo.entry.get_text)+30)
    end

    $fig.draw_x($drawtype)
  end

end

## main table
table = Gtk::Table.new(2,6,false)
table.attach(Gtk::Label.new("Variable").show,0,1,0,1)
table.attach($var_select.combo,1,2,0,1)
table.attach(Gtk::Label.new("Figure type").show,0,1,1,2)
table.attach($typeomenu,1,2,1,2)
table.attach(Gtk::Label.new("Axis(x)").show,0,1,2,3)
table.attach($axisx_select.combo,1,2,2,3)
table.attach(Gtk::Label.new("Axis(y)").show,0,1,3,4)
table.attach($axisy_select.combo,1,2,3,4)
table.attach($drawbutton,1,2,5,6)
table.show


# FileSelection
fs = Gtk::FileSelection.new("")
fs.ok_button.signal_connect("clicked"){
  fs.hide
  $filecall.call(fs.get_filename)
}
fs.cancel_button.signal_connect("clicked"){fs.hide}


# Main Menu
main_itemfact = Gtk::ItemFactory.new(Gtk::ItemFactory::TYPE_MENU_BAR, "<main>", accel)
main_itemfact.create_item("/File(_F)", nil, Gtk::ItemFactory::BRANCH)
main_itemfact.create_item("/File(_F)/Open(_O)", nil, Gtk::ItemFactory::ITEM){
  fs.set_title("Open File")
  $filecall = proc{|filename| file_open(filename)}
  fs.show
}
main_itemfact.create_item("/File(_F)/Save Image(_S)", "<control>S", Gtk::ItemFactory::ITEM){
  if !$fig.nil? then
    fs.set_title("Save Image")
    $filecall = proc{|filename|
      if !FileTest.exist?(filename)
	$fig.save_image(filename)
      else
	dialog = Gtk::Dialog.new
	dialog.set_modal(true)
	dialog.vbox.pack_start(Gtk::Label.new("Can "+filename+" overwrite ?").show)
	ok_button=Gtk::Button.new("OK").show
	ok_button.signal_connect("clicked"){dialog.destroy; $fig.save_image(filename)}
	cancel_button=Gtk::Button.new("Cancel").show
	cancel_button.signal_connect("clicked"){dialog.destroy}
	dialog.action_area.pack_start(ok_button)
	dialog.action_area.pack_start(cancel_button)
	dialog.show
      end
    }
    fs.show
  end
}
main_itemfact.create_item("/File(_F)/Save Code(_C)", "<control>C", Gtk::ItemFactory::ITEM){
  if !$fig.nil? then
    fs.set_title("Save Code")
    $filecall = proc{|filename|
      if !FileTest.exist?(filename)
	$fig.save_code(filename)
      else
	dialog = Gtk::Dialog.new
	dialog.set_modal(true)
	dialog.vbox.pack_start(Gtk::Label.new("Can "+filename+" overwrite ?").show)
	ok_button=Gtk::Button.new("OK").show
	ok_button.signal_connect("clicked"){dialog.destroy; $fig.save_code(filename)}
	cancel_button=Gtk::Button.new("Cancel").show
	cancel_button.signal_connect("clicked"){dialog.destroy}
	dialog.action_area.pack_start(ok_button)
	dialog.action_area.pack_start(cancel_button)
	dialog.show
      end
    }
    fs.show
  end
}
#main_itemfact.create_item("/File(_F)/Close(_C)", "<control>C", Gtk::ItemFactory::ITEM){file_close}
main_itemfact.create_item("/File(_F)/Quit(_Q)", "<control>Q", Gtk::ItemFactory::ITEM){finish}

main_itemfact.create_item("/Window(_W)", nil, Gtk::ItemFactory::BRANCH)
main_itemfact.create_item("/Window(_W)/_Variable", nil, Gtk::ItemFactory::CHECK_ITEM){|w|
  if w.active? then var_window.show else var_window.hide end
}
main_itemfact.create_item("/Window(_W)/_Axis", nil, Gtk::ItemFactory::CHECK_ITEM){|w|
  if w.active? then axis_window.show else axis_window.hide end
}
main_itemfact.create_item("/Window(_W)/_Dimensions", nil, Gtk::ItemFactory::CHECK_ITEM){|w|
  if w.active? then $dims_window.show else $dims_window.hide end
}
main_itemfact.create_item("/Window(_W)/_Attributes", nil, Gtk::ItemFactory::CHECK_ITEM){|w|
  if w.active? then attr_window.show else attr_window.hide end
}
main_itemfact.create_item("/Window(_W)/_Movie", nil, Gtk::ItemFactory::CHECK_ITEM){|w|
  if w.active? then movie_window.show else movie_window.hide end
}
main_itemfact.create_item("/Window(_W)/_Map", nil, Gtk::ItemFactory::CHECK_ITEM){|w|
  if w.active? then map_window.show else map_window.hide end
}

main_itemfact.create_item("/Help(_H)", nil, Gtk::ItemFactory::LAST_BRANCH)
main_itemfact.create_item("/Help(_H)/_About", nil, Gtk::ItemFactory::ITEM){
  dialog = Gtk::Dialog.new.set_title("About this program")
  dialog.vbox.pack_start(Gtk::Label.new("This program is made by Seiya Nishizawa").show)
  dialog.vbox.pack_start(Gtk::Label.new("seiya@xxxxxx").show)
  ok_button=Gtk::Button.new("OK").show
  ok_button.signal_connect("clicked"){dialog.destroy}
  dialog.action_area.pack_start(ok_button)
  dialog.show
}
#main_itemfact.get_widget("/File(F)/Save Image(S)").set_sensitive(false)

# main window
totalbox = Gtk::VBox.new(false,0)
totalbox.pack_start(main_itemfact.get_widget("<main>").show,false,true,0)
totalbox.pack_start(table,false,true,0)
totalbox.show
totalbox.signal_connect("destroy"){finish}

main_window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL)
accel.attach(main_window)
main_window.set_title("gdcl")
main_window.add(totalbox)
main_window.show


# Variable window
$title_entry = Gtk::Entry.new
$miss_entry = Gtk::Entry.new
table = Gtk::Table.new(2,2,false)
table.attach(Gtk::Label.new("title").show,0,1,0,1)
table.attach($title_entry.show,1,2,0,1)
table.attach(Gtk::Label.new("missing value").show,0,1,1,2)
table.attach($miss_entry.show,1,2,1,2)
var_window.add(table.show)

# Axis window
$logxbutton = Gtk::CheckButton.new("log scale")
$logxbutton.signal_connect("clicked"){|w| $x_axis.log = w.active?}
$x_title = Gtk::Entry.new
$x_title.signal_connect("activate"){|w| $x_axis.title = w.get_text}
$x_unit = Gtk::Entry.new
$x_unit.signal_connect("activate"){|w| $x_axis.unit = w.get_text}
$x_min = Gtk::Entry.new
$x_min.signal_connect("activate"){|w|
  str = w.get_text
  if str=="" then
    $x_axis.min = false
  else
    $x_axis.min = str.to_f
  end
  }
$x_max = Gtk::Entry.new
$x_max.signal_connect("activate"){|w|
  str = w.get_text
  if str=="" then
    $x_axis.max = false
  else
    $x_axis.max = str.to_f
  end
  }
table = Gtk::Table.new(2,5,false)
table.attach($logxbutton.show,1,2,0,1)
table.attach(Gtk::Label.new("title").show,0,1,1,2)
table.attach($x_title.show,1,2,1,2)
table.attach(Gtk::Label.new("unit").show,0,1,2,3)
table.attach($x_unit.show,1,2,2,3)
table.attach(Gtk::Label.new("min").show,0,1,3,4)
table.attach($x_min.show,1,2,3,4)
table.attach(Gtk::Label.new("max").show,0,1,4,5)
table.attach($x_max.show,1,2,4,5)
x_frame = Gtk::Frame.new("X-Axis")
x_frame.add(table.show)

$logybutton = Gtk::CheckButton.new("log scale")
$logybutton.signal_connect("clicked"){|w| $y_axis.log = w.active?}
$y_title = Gtk::Entry.new
$y_title.signal_connect("activate"){|w| $y_axis.title = w.get_text}
$y_unit = Gtk::Entry.new
$y_unit.signal_connect("activate"){|w| $y_axis.unit = w.get_text}
$y_min = Gtk::Entry.new
$y_min.signal_connect("activate"){|w|
  str = w.get_text
  if str=="" then
    $y_axis.min = false
  else
    $y_axis.min = str.to_f
  end
  }
$y_max = Gtk::Entry.new
$y_max.signal_connect("activate"){|w|
  str = w.get_text
  if str=="" then
    $y_axis.max = false
  else
    $y_axis.max = str.to_f
  end
  }
table = Gtk::Table.new(2,5,false)
table.attach($logybutton.show,1,2,0,1)
table.attach(Gtk::Label.new("title").show,0,1,1,2)
table.attach($y_title.show,1,2,1,2)
table.attach(Gtk::Label.new("unit").show,0,1,2,3)
table.attach($y_unit.show,1,2,2,3)
table.attach(Gtk::Label.new("min").show,0,1,3,4)
table.attach($y_min.show,1,2,3,4)
table.attach(Gtk::Label.new("max").show,0,1,4,5)
table.attach($y_max.show,1,2,4,5)
y_frame = Gtk::Frame.new("Y-Axis")
y_frame.add(table.show)

axisbox = Gtk::VBox.new
axisbox.pack_start(x_frame.show)
axisbox.pack_start(y_frame.show)
axis_window.add(axisbox.show)


# Attributes window
$attr_note = Gtk::Notebook::new()
$attr_note.set_tab_pos(Gtk::POS_TOP)
box = Gtk::VBox.new(false,0)
box.pack_start($attr_note.show)
box.show
attr_window.add(box)


# Movie winsow
start_xpm = ["11 11 2 1",  "  c None", "+  c black",
  "+          ",
  "+++        ",
  "+++++      ",
  "+++++++    ",
  "+++++++++  ",
  "+++++++++++",
  "+++++++++  ",
  "+++++++    ",
  "+++++      ",
  "+++        ",
  "+          "]
stop_xpm = ["11 11 2 1", "   c None", "+  c black",
  "           ",
  " +++++++++ ",
  " +++++++++ ",
  " +++++++++ ",
  " +++++++++ ",
  " +++++++++ ",
  " +++++++++ ",
  " +++++++++ ",
  " +++++++++ ",
  " +++++++++ ",
  "           "]
rec_xpm = ["11 11 2 1", "   c None", "+  c red",
  "    +++    ",
  "  +++++++  ",
  " +++++++++ ",
  " +++++++++ ",
  "+++++++++++",
  "+++++++++++",
  "+++++++++++",
  " +++++++++ ",
  " +++++++++ ",
  "  +++++++  ",
  "    +++    "]
  

$movie = false
$rec = false
$fnum = "001"
movie_window.realize
pix, mask = Gdk::Pixmap.create_from_xpm_d(movie_window.window, nil, start_xpm) 
start_pixmap =  Gtk::Pixmap.new(pix, mask)
pix, mask = Gdk::Pixmap.create_from_xpm_d(movie_window.window, nil, stop_xpm) 
stop_pixmap =  Gtk::Pixmap.new(pix, mask)
pix, mask = Gdk::Pixmap.create_from_xpm_d(movie_window.window, nil, rec_xpm) 
rec_pixmap =  Gtk::Pixmap.new(pix, mask)
start_button = Gtk::ToggleButton.new
start_button.add(Gtk::HBox.new.pack_start(start_pixmap))
stop_button = Gtk::Button.new
stop_button.add(Gtk::HBox.new.pack_start(stop_pixmap))
rec_button = Gtk::ToggleButton.new
rec_button.add(Gtk::HBox.new.pack_start(rec_pixmap))
start_button.signal_connect("clicked"){|w|
  if w.active? then
    $movie = Gtk.timeout_add(500){
      $dim_movie = $movie_combo.entry.get_text
      $dims[$dim_movie].select.next 
      draw
      if $rec then
	$fig.save_image("dcl"+$fnum+".ppm")
	$fnum.next!
      end
      true
    }
  else
    Gtk.timeout_remove($movie)
  end
}
stop_button.signal_connect("clicked"){
  if start_button.active? then
    start_button.set_active(false)
    Gtk.timeout_remove($movie)
  end
}
rec_button.signal_connect("clicked"){|w|
  $rec = w.active?
}
$movie_combo = Gtk::Combo.new.set_usize(50,30)
$movie_combo.entry.set_editable(false)
$movie_combo.entry.set_text("-")
$movie_box = Gtk::HBox.new(false,0)
$movie_box.pack_start($movie_combo)
$movie_box.pack_start(start_button)
$movie_box.pack_start(stop_button)
$movie_box.pack_start(rec_button)
movie_window.add($movie_box.show_all)


# Map window
$maps =  ["ORTHOGRAPHIC","POLAR STEREO","AZIMUTHAL","AZIMUTHAL EQ. AREA"]
$map_combo = Gtk::Combo.new
$map_combo.entry.set_editable(false)
$map_combo.set_popdown_strings($maps)
box = Gtk::VBox.new(false,0)
box.pack_start($map_combo.show)
box.show
map_window.add(box)


# quit
def file_close
  if $file then
    clear_var
    print "close\n"
    $file.close
    $file = nil
    $vars.clear
    $dimtable.hide
    $dims.clear
  end
end
def finish
  file_close
  print "Bye\n"
  Gtk.main_quit
end
main_window.signal_connect("delete_event"){finish}
main_window.signal_connect("destroy"){finish}



# argv
if ARGV[0] != nil then file_open(ARGV.shift) end


# main loop
Gtk.main