#! /usr/bin/env ruby 

=begin
  * class を 3 つ定義
    * arara-init クラス: アプリケーションの情報をもつクラス. 
       * ボタンを押した時の挙動を定義
       * 初期化
       * 初期値生成ボタンを押すと.... 
       * .run: 初期値生成
       * .write: ファイル書き出し
    * NAMELIST クラス
       * NAMELIST VAR クラスを複数個保持
       * 必要に応じて NAMELIST VAR クラスを追加している
    * NAMELIST VAR クラス
       * 変数名
       * 説明
       * (デフォルト)値
=end


require 'gtk2'
require 'jcode'

Gtk.init
$KCODE = 'u'

#=====================================================================
#========== 変数定義 =================================================
#=====================================================================
var = {}
cfgfile_tmp       = "arare-tmp.conf"
cfgfile           = "arare.conf"
cfgfile_info      = "計算設定の定義ファイル. [編集不可]"
flag              = "2005-03-22_pm"                          
flag_info         = "出力ファイルに付ける文字列"
basicfile         = "arare-basic_50_plume.nc"      
basicfile_info    = "基本場を定義する netCDF ファイル"
distrbfile        = "arare-disturb_50_plume.nc"    
distrbfile_info   = "擾乱場を定義する netCDF ファイル"
ExpTitle          = "2D_cumulus_model_deepconv/arare" 
ExpTitle_info     = "データの表題"
ExpSrc            = "GFD_Dennou_Club_deepconv_project(arare)" 
ExpSrc_info       = "データを作成する手順, モデル名等"
ExpInst           = "sugiyama@gfd-dennou.org"	     
ExpInst_info      = "ファイルを最終的に変更した人, 組織"
NX                = "50"
NX_info           = "X 軸方向の格子点数(等間隔)"
NZ                = "50"
NZ_info           = "Z 軸方向の格子点数(等間隔)"
XMin              = "0"
XMin_info         = "X 座標の始点"
XMax              = "10000"
XMax_info         = "X 座標の終点"
ZMin              = "0"
ZMin_info         = "Z 座標の始点"
ZMax              = "10000"
ZMax_info         = "Z 座標の終点"
DelTimeLong       = "3.0d0"
DelTimeLong_info  = "長いタイムステップ"
DelTimeShort      = "1.0d-1" 
DelTimeShort_info = "短いタイムステップ(音波関連項)"
TimeInt           = "3000.0d0" 
TimeInt_info      = "積分時間 [単位: 秒]"
TimeDisp          = "10"
TimeDisp_info     = "出力時間間隔 [単位: 秒]"
BcX               = "C" 
BcX_info          = "X 軸方向の境界境界"
BcZ               = "R"      
BcZ_info          = "Z 軸方向の境界条件"
LLib              = "ssl2"
LLib_info         = "利用する線形計算パッケージの選択"
Grav              = "9.8d0"  
Grav_info         = "重力加速度 [単位: m/s]"
Alpha             = "5.0d-7" 
Alpha_info        = "音波減衰項の係数"
Beta              = "5.0d-1"  
Beta_info         = "時間積分法の選択"
Cm                = "2.0d-1"
Cm_info           = "乱流エネルギーの診断式の係数"
tfil              = "1.0d-1"
tfil_info         = "アッセリンの時間フィルタの係数"
AlphaH            = "5.0d-2"  
AlphaH_info       = "X 軸方向の数値粘性係数"
AlphaV            = "5.0d-2"  
AlphaV_info       = "Z 軸方向の数値粘性係数"
DebugOn           = ""
DebugOn_info      = "デバッグ出力のスイッチを入れるか否かの選択"


#=====================================================================
#========== ウィンドウを作る =========================================
#=====================================================================
window = Gtk::Window.new
window.title = "deepconv/arare arare-init.rb"
window.signal_connect("destroy") {Gtk.main_quit}
window.border_width = 10

#テーブルを使ってウィジットを配置するグリッドを準備する
#引数は, 行, 列, true/false である. 
table = Gtk::Table.new(4, 3, false)
window.add(table)

#=====================================================================
#========== ノートブックを準備 =======================================
#=====================================================================
notebook = Gtk::Notebook.new
#ページインジケータを置く場所の設定
notebook.tab_pos = Gtk::POS_TOP
#テーブルにノートブックを追加
#引数は, ウィジット, left_attach, right_attach, top_attach, bottom_attach 
#1 行目から 8 行目の最上列にノートブックを張り付ける
table.attach_defaults(notebook, 0, 4, 0, 1)

#クラスを定義
class << notebook
  def rotate
    self.tab_pos = (self.tab_pos + 1) % 4
  end

  def tabsborder
    self.show_tabs = !self.show_tabs?
    self.show_border = !self.show_border?
  end
  
  def remove
    self.remove_page(self.page)
  end
  
  def scrollable
    self.scrollable = !self.scrollable?
  end
  
  def popup
    self.enable_popup = !self.enable_popup?
  end
end


def create_bbox(horizontal, title, spacing, layout)
	frame = Gtk::Frame.new(title)

	bbox = (horizontal ? Gtk::HButtonBox : Gtk::VButtonBox).new
	bbox.border_width = 5
	frame.add(bbox)

	bbox.layout_style = layout
	bbox.spacing = spacing

	bbox.add(Gtk::Button.new(Gtk::Stock::OK))
	bbox.add(Gtk::Button.new(Gtk::Stock::CANCEL))
	bbox.add(Gtk::Button.new(Gtk::Stock::HELP))

	frame
end


#=====================================================================
#========== ノートブックの中身 =======================================
#=====================================================================
###
###I/O ファイル
###
io_main = Gtk::VBox.new(false, 0)
#notebook.append_page(io_main, Gtk::Label.new("I/O ファイルfileset]"))
notebook.append_page(io_main, Gtk::Label.new("fileset"))

#--- フレーム分割
io_frame1 = Gtk::Frame.new("Input File")
io_frame2 = Gtk::Frame.new("Output File")

io_main.pack_start(io_frame1, false, true, 10)
io_main.pack_start(io_frame2, false, true, 10)

io_box11 = Gtk::VBox.new(false, 0)
io_box11.border_width = 10
io_frame1.add(io_box11)

io_frame11 = Gtk::Frame.new("cfgfile")
io_frame12 = Gtk::Frame.new("basicfile")
io_frame13 = Gtk::Frame.new("distrbfile")
io_box11.pack_start(io_frame11, true, true, 10)
io_box11.pack_start(io_frame12, true, true, 10)
io_box11.pack_start(io_frame13, true, true, 10)

io_box111 = Gtk::VBox.new(false, 0)
io_box111.border_width = 10
io_frame11.add(io_box111)
io_box121 = Gtk::VBox.new(false, 0)
io_box121.border_width = 10
io_frame12.add(io_box121)
io_box131 = Gtk::VBox.new(false, 0)
io_box131.border_width = 10
io_frame13.add(io_box131)

io_label1 = Gtk::Label.new("arare.conf")
io_box111.pack_start(io_label1, true, true, 0)
io_label11 = Gtk::Label.new(cfgfile_info)
io_box111.pack_start(io_label11, true, true, 0)

io_basicfile = Gtk::Entry.new
io_basicfile.max_length = 50
io_basicfile.text = basicfile
io_basicfile.select_region(0, -1)
io_box121.pack_start(io_basicfile, true, true, 0)
io_label2 = Gtk::Label.new(basicfile_info)
io_box121.pack_start(io_label2, true, true, 0)

io_distrbfile = Gtk::Entry.new
io_distrbfile.max_length = 50
io_distrbfile.text = distrbfile
io_distrbfile.select_region(0, -1)
io_box131.pack_start(io_distrbfile, true, true, 0)
io_label3 = Gtk::Label.new(distrbfile_info)
io_box131.pack_start(io_label3, true, true, 0)


io_box21 = Gtk::VBox.new(false, 0)
io_box21.border_width = 10
io_frame2.add(io_box21)

io_frame21 = Gtk::Frame.new("flag")
io_box21.pack_start(io_frame21, true, true, 10)

io_box211 = Gtk::VBox.new(false, 0)
io_box211.border_width = 10
io_frame21.add(io_box211)

io_flag = Gtk::Entry.new
io_flag.max_length = 50
io_flag.text = flag
io_flag.select_region(0, -1)
io_box211.pack_start(io_flag, true, true, 0)
io_label4 = Gtk::Label.new(flag_info)
io_box211.pack_start(io_label4, true, true, 0)


###
###実験名
###
exp_main = Gtk::VBox.new(false, 0)
#notebook.append_page(exp_main, Gtk::Label.new("実験設定"))
notebook.append_page(exp_main, Gtk::Label.new("expname"))

exp_frame1 = Gtk::Frame.new("ExpTitle")
exp_main.pack_start(exp_frame1, false, true, 10)

exp_frame2 = Gtk::Frame.new("ExpSrc")
exp_main.pack_start(exp_frame2, false, true, 10)

exp_frame3 = Gtk::Frame.new("ExpInst")
exp_main.pack_start(exp_frame3, false, true, 10)

exp_box1 = Gtk::VBox.new(false, 0)
exp_box1.border_width = 10
exp_frame1.add(exp_box1)

exp_box2 = Gtk::VBox.new(false, 0)
exp_box2.border_width = 10
exp_frame2.add(exp_box2)

exp_box3 = Gtk::VBox.new(false, 0)
exp_box3.border_width = 10
exp_frame3.add(exp_box3)

exp_ExpTitle = Gtk::Entry.new
exp_ExpTitle.max_length = 50
exp_ExpTitle.text = ExpTitle
exp_ExpTitle.select_region(0, -1)
exp_box1.pack_start(exp_ExpTitle, true, true, 0)
exp_label1 = Gtk::Label.new(ExpTitle_info)
exp_box1.pack_start(exp_label1, true, true, 0)

exp_ExpSrc = Gtk::Entry.new
exp_ExpSrc.max_length = 100
exp_ExpSrc.text = ExpSrc
exp_ExpSrc.select_region(0, -1)
exp_box2.pack_start(exp_ExpSrc, true, true, 0)
exp_label2 = Gtk::Label.new(ExpSrc_info)
exp_box2.pack_start(exp_label2, true, true, 0)

exp_ExpInst = Gtk::Entry.new
exp_ExpInst.max_length = 50
exp_ExpInst.text = ExpInst
exp_ExpInst.select_region(0, -1)
exp_box3.pack_start(exp_ExpInst, true, true, 0)
exp_label3 = Gtk::Label.new(ExpInst_info)
exp_box3.pack_start(exp_label3, true, true, 0)


###
###座標
###
grid_main = Gtk::VBox.new(false, 0)
#notebook.append_page(grid_main, Gtk::Label.new("空間座標"))
notebook.append_page(grid_main, Gtk::Label.new("gridset"))

grid_frame1 = Gtk::Frame.new("格子点数")
grid_main.pack_start(grid_frame1, false, true, 10)

grid_frame2 = Gtk::Frame.new("計算領域(X) [単位: m]")
grid_main.pack_start(grid_frame2, false, true, 10)

grid_frame3 = Gtk::Frame.new("計算領域(Z) [単位: m]")
grid_main.pack_start(grid_frame3, false, true, 10)

grid_box1 = Gtk::HBox.new(false, 0)
grid_box1.border_width = 10
grid_frame1.add(grid_box1)

grid_box2 = Gtk::HBox.new(false, 0)
grid_box2.border_width = 10
grid_frame2.add(grid_box2)

grid_box3 = Gtk::HBox.new(false, 0)
grid_box3.border_width = 10
grid_frame3.add(grid_box3)

grid_subframe1 = Gtk::Frame.new("NX")
grid_box1.pack_start(grid_subframe1, true, true, 10)

grid_subframe2 = Gtk::Frame.new("NZ")
grid_box1.pack_start(grid_subframe2, true, true, 10)

grid_subframe3 = Gtk::Frame.new("XMin")
grid_box2.pack_start(grid_subframe3, true, true, 10)

grid_subframe4 = Gtk::Frame.new("XMax")
grid_box2.pack_start(grid_subframe4, true, true, 10)

grid_subframe5 = Gtk::Frame.new("ZMin")
grid_box3.pack_start(grid_subframe5, true, true, 10)

grid_subframe6 = Gtk::Frame.new("ZMax")
grid_box3.pack_start(grid_subframe6, true, true, 10)

grid_subbox1 = Gtk::VBox.new(false, 0)
grid_subbox1.border_width = 10
grid_subframe1.add(grid_subbox1)

grid_subbox2 = Gtk::VBox.new(false, 0)
grid_subbox2.border_width = 10
grid_subframe2.add(grid_subbox2)

grid_subbox3 = Gtk::VBox.new(false, 0)
grid_subbox3.border_width = 10
grid_subframe3.add(grid_subbox3)

grid_subbox4 = Gtk::VBox.new(false, 0)
grid_subbox4.border_width = 10
grid_subframe4.add(grid_subbox4)

grid_subbox5 = Gtk::VBox.new(false, 0)
grid_subbox5.border_width = 10
grid_subframe5.add(grid_subbox5)

grid_subbox6 = Gtk::VBox.new(false, 0)
grid_subbox6.border_width = 10
grid_subframe6.add(grid_subbox6)

grid_NX = Gtk::Entry.new
grid_NX.max_length = 50
grid_NX.text = NX
grid_NX.select_region(0, -1)
grid_subbox1.pack_start(grid_NX, true, true, 0)
grid_label1 = Gtk::Label.new(NX_info)
grid_subbox1.pack_start(grid_label1, true, true, 0)

grid_NZ = Gtk::Entry.new
grid_NZ.max_length = 50
grid_NZ.text = NZ
grid_NZ.select_region(0, -1)
grid_subbox2.pack_start(grid_NZ, true, true, 0)
grid_label2 = Gtk::Label.new(NZ_info)
grid_subbox2.pack_start(grid_label2, true, true, 0)

grid_XMin = Gtk::Entry.new
grid_XMin.max_length = 50
grid_XMin.text = XMin
grid_XMin.select_region(0, -1)
grid_subbox3.pack_start(grid_XMin, true, true, 0)
grid_label = Gtk::Label.new(XMin_info)
grid_subbox3.pack_start(grid_label, true, true, 0)

grid_XMax = Gtk::Entry.new
grid_XMax.max_length = 50
grid_XMax.text = XMax
grid_XMax.select_region(0, -1)
grid_subbox4.pack_start(grid_XMax, true, true, 0)
grid_label = Gtk::Label.new(XMax_info)
grid_subbox4.pack_start(grid_label, true, true, 0)

grid_ZMin = Gtk::Entry.new
grid_ZMin.max_length = 50
grid_ZMin.text = ZMin
grid_ZMin.select_region(0, -1)
grid_subbox5.pack_start(grid_ZMin, true, true, 0)
grid_label = Gtk::Label.new(ZMin_info)
grid_subbox5.pack_start(grid_label, true, true, 0)

grid_ZMax = Gtk::Entry.new
grid_ZMax.max_length = 50
grid_ZMax.text = ZMax
grid_ZMax.select_region(0, -1)
grid_subbox6.pack_start(grid_ZMax, true, true, 0)
grid_label = Gtk::Label.new(ZMax_info)
grid_subbox6.pack_start(grid_label, true, true, 0)


###
###時刻
###
time_main = Gtk::VBox.new(false, 0)
#notebook.append_page(time_main, Gtk::Label.new("時刻関係"))
notebook.append_page(time_main, Gtk::Label.new("timeset"))

time_frame1 = Gtk::Frame.new("DelTime")
time_main.pack_start(time_frame1, false, true, 10)

time_frame2 = Gtk::Frame.new("TimeInt")
time_main.pack_start(time_frame2, false, true, 10)

time_frame3 = Gtk::Frame.new("TimeDisp")
time_main.pack_start(time_frame3, false, true, 10)

time_box1 = Gtk::HBox.new(false, 0)
time_box1.border_width = 10
time_frame1.add(time_box1)

time_box2 = Gtk::HBox.new(false, 0)
time_box2.border_width = 10
time_frame2.add(time_box2)

time_box3 = Gtk::HBox.new(false, 0)
time_box3.border_width = 10
time_frame3.add(time_box3)

time_subframe1 = Gtk::Frame.new("DelTimeShort")
time_box1.pack_start(time_subframe1, true, true, 10)

time_subframe2 = Gtk::Frame.new("DelTimeLong")
time_box1.pack_start(time_subframe2, true, true, 10)

time_subbox1 = Gtk::VBox.new(false, 0)
time_subbox1.border_width = 10
time_subframe1.add(time_subbox1)

time_subbox2 = Gtk::VBox.new(false, 0)
time_subbox2.border_width = 10
time_subframe2.add(time_subbox2)

time_DelTimeShort = Gtk::Entry.new
time_DelTimeShort.max_length = 50
time_DelTimeShort.text = DelTimeShort
time_DelTimeShort.select_region(0, -1)
time_subbox1.pack_start(time_DelTimeShort, true, true, 0)
time_label = Gtk::Label.new(DelTimeShort_info)
time_subbox1.pack_start(time_label, true, true, 0)

time_DelTimeLong = Gtk::Entry.new
time_DelTimeLong.max_length = 50
time_DelTimeLong.text = DelTimeLong
time_DelTimeLong.select_region(0, -1)
time_subbox2.pack_start(time_DelTimeLong, true, true, 0)
time_label = Gtk::Label.new(DelTimeLong_info)
time_subbox2.pack_start(time_label, true, true, 0)

time_label = Gtk::Label.new(TimeInt_info)
time_box2.pack_start(time_label, true, true, 0)
time_TimeInt = Gtk::Entry.new
time_TimeInt.max_length = 10
time_TimeInt.text = TimeInt
time_TimeInt.select_region(0, -1)
time_box2.pack_start(time_TimeInt, true, true, 0)

time_label = Gtk::Label.new(TimeDisp_info)
time_box3.pack_start(time_label, true, true, 0)
time_TimeDisp = Gtk::Entry.new
time_TimeDisp.max_length = 10
time_TimeDisp.text = TimeDisp
time_TimeDisp.select_region(0, -1)
time_box3.pack_start(time_TimeDisp, true, true, 0)


###
###境界条件
###
bc_main = Gtk::VBox.new(false, 0)
#notebook.append_page(bc_main, Gtk::Label.new("境界条件"))
notebook.append_page(bc_main, Gtk::Label.new("bcset"))

bc_frame_x = Gtk::Frame.new("BcX")
bc_main.pack_start(bc_frame_x, false, true, 10)

bc_box1 = Gtk::VBox.new(false, 0)
bc_box1.border_width = 10
bc_frame_x.add(bc_box1)

bc_x_box = Gtk::HBox.new(false, 0)
bc_x_box.border_width = 10
bc_box1.add(bc_x_box)

bc_frame_z = Gtk::Frame.new("BcZ")
bc_main.pack_start(bc_frame_z, false, true, 10)

bc_box2 = Gtk::VBox.new(false, 0)
bc_box2.border_width = 10
bc_frame_z.add(bc_box2)

bc_z_box = Gtk::HBox.new(false, 0)
bc_z_box.border_width = 10
bc_box2.add(bc_z_box)

bc_BcX = Gtk::RadioButton.new("周期境界")
bc_BcX.active = true
bc_x_box.pack_start(bc_BcX, true, true, 0)

bc_BcX2 = Gtk::RadioButton.new(bc_BcX, "固定壁")
bc_x_box.pack_start(bc_BcX2, true, true, 0)

bc_BcX3 = Gtk::RadioButton.new(bc_BcX, "フリースリップ")
bc_x_box.pack_start(bc_BcX3, true, true, 0)


bc_BcZ = Gtk::RadioButton.new("周期境界")
bc_z_box.pack_start(bc_BcZ, true, true, 0)

bc_BcZ2 = Gtk::RadioButton.new(bc_BcZ, "固定壁")
bc_BcZ2.active = true
bc_z_box.pack_start(bc_BcZ2, true, true, 0)

bc_BcZ3 = Gtk::RadioButton.new(bc_BcZ, "フリースリップ")
bc_z_box.pack_start(bc_BcZ3, true, true, 0)


bc_label = Gtk::Label.new(BcX_info)
bc_box1.pack_start(bc_label, true, true, 0)

bc_label = Gtk::Label.new(BcZ_info)
bc_box2.pack_start(bc_label, true, true, 0)


### 
### 実験パラメタ
###
prm_main = Gtk::VBox.new(false, 0)
#notebook.append_page(prm_main, Gtk::Label.new("実験パラメタ"))
notebook.append_page(prm_main, Gtk::Label.new("arareprm"))

prm_frame = Gtk::Frame.new("Grav")
prm_main.pack_start(prm_frame, false, true, 10)

prm_box = Gtk::HBox.new(false, 0)
prm_box.border_width = 10
prm_frame.add(prm_box)

prm_label = Gtk::Label.new(Grav_info)
prm_box.pack_start(prm_label, true, true, 0)

prm_Grav = Gtk::Entry.new
prm_Grav.max_length = 10
prm_Grav.text = Grav
prm_Grav.select_region(0, -1)
prm_box.pack_start(prm_Grav, true, true, 0)


prm_frame = Gtk::Frame.new("Alpha")
prm_main.pack_start(prm_frame, false, true, 10)

prm_box = Gtk::HBox.new(false, 0)
prm_box.border_width = 10
prm_frame.add(prm_box)

prm_label = Gtk::Label.new(Alpha_info)
prm_box.pack_start(prm_label, true, true, 0)

prm_Alpha = Gtk::Entry.new
prm_Alpha.max_length = 10
prm_Alpha.text = Alpha
prm_Alpha.select_region(0, -1)
prm_box.pack_start(prm_Alpha, true, true, 0)


prm_frame = Gtk::Frame.new("Beta")
prm_main.pack_start(prm_frame, false, true, 10)

prm_box = Gtk::HBox.new(false, 0)
prm_box.border_width = 10
prm_frame.add(prm_box)

prm_label = Gtk::Label.new(Beta_info)
prm_box.pack_start(prm_label, true, true, 0)

prm_Beta = Gtk::RadioButton.new("クランク・ニコルソン法")
prm_Beta.active = true
prm_box.pack_start(prm_Beta, true, true, 0)

prm_Beta2 = Gtk::RadioButton.new(prm_Beta, "陰解法")
prm_box.pack_start(prm_Beta2, true, true, 0)



prm_frame = Gtk::Frame.new("Cm")
prm_main.pack_start(prm_frame, false, true, 10)

prm_box = Gtk::HBox.new(false, 0)
prm_box.border_width = 10
prm_frame.add(prm_box)

prm_label = Gtk::Label.new(Cm_info)
prm_box.pack_start(prm_label, true, true, 0)

prm_Cm = Gtk::Entry.new
prm_Cm.max_length = 10
prm_Cm.text = Cm
prm_Cm.select_region(0, -1)
prm_box.pack_start(prm_Cm, true, true, 0)


prm_frame = Gtk::Frame.new("tfil")
prm_main.pack_start(prm_frame, false, true, 10)

prm_box = Gtk::HBox.new(false, 0)
prm_box.border_width = 10
prm_frame.add(prm_box)

prm_label = Gtk::Label.new(tfil_info)
prm_box.pack_start(prm_label, true, true, 0)

prm_tfil = Gtk::Entry.new
prm_tfil.max_length = 10
prm_tfil.text = tfil
prm_tfil.select_region(0, -1)
prm_box.pack_start(prm_tfil, true, true, 0)



prm_frame = Gtk::Frame.new("AlphaH/AlphaV")
prm_main.pack_start(prm_frame, false, true, 10)

prm_box = Gtk::HBox.new(false, 0)
prm_box.border_width = 10
prm_frame.add(prm_box)

prm_subframe = Gtk::Frame.new("X 方向")
prm_box.pack_start(prm_subframe, true, true, 10)

prm_subbox = Gtk::VBox.new(false, 0)
prm_subbox.border_width = 10
prm_subframe.add(prm_subbox)

prm_AlphaH = Gtk::Entry.new
prm_AlphaH.max_length = 10
prm_AlphaH.text = AlphaH
prm_AlphaH.select_region(0, -1)
prm_subbox.pack_start(prm_AlphaH, true, true, 0)

prm_label = Gtk::Label.new(AlphaH_info)
prm_subbox.pack_start(prm_label, true, true, 0)

prm_subframe = Gtk::Frame.new("Z 方向")
prm_box.pack_start(prm_subframe, true, true, 10)

prm_subbox = Gtk::VBox.new(false, 0)
prm_subbox.border_width = 10
prm_subframe.add(prm_subbox)

prm_AlphaV = Gtk::Entry.new
prm_AlphaV.max_length = 10
prm_AlphaV.text = AlphaV
prm_AlphaV.select_region(0, -1)
prm_subbox.pack_start(prm_AlphaV, true, true, 0)

prm_label = Gtk::Label.new(AlphaV_info)
prm_subbox.pack_start(prm_label, true, true, 0)



###
### 線形計算パッケージ
###
lin_main = Gtk::VBox.new(false, 0)
#notebook.append_page(lin_main, Gtk::Label.new("行列計算"))
notebook.append_page(lin_main, Gtk::Label.new("linlib"))

lin_frame = Gtk::Frame.new("LLib")
lin_main.pack_start(lin_frame, false, true, 10)

lin_box0 = Gtk::VBox.new(false, 0)
lin_box0.border_width = 10
lin_frame.add(lin_box0)

lin_box = Gtk::HBox.new(false, 0)
lin_box.border_width = 10
lin_box0.pack_start(lin_box, true, true, 10)

lin_LLib = Gtk::RadioButton.new("SSL 2")
lin_LLib.active = true
lin_box.pack_start(lin_LLib, true, true, 0)

lin_LLib2 = Gtk::RadioButton.new(lin_LLib, "LAPACK (未実装!)")
lin_box.pack_start(lin_LLib2, true, true, 0)

lin_label = Gtk::Label.new(LLib_info)
lin_box0.pack_start(lin_label, true, true, 0)


###
### デバッグ
###
debug_main = Gtk::VBox.new(false, 0)
#notebook.append_page(debug_main, Gtk::Label.new("デバッグ"))
notebook.append_page(debug_main, Gtk::Label.new("debugset"))

debug_frame = Gtk::Frame.new("DebugOn")
debug_main.pack_start(debug_frame, false, true, 10)

debug_box0 = Gtk::VBox.new(false, 0)
debug_box0.border_width = 10
debug_frame.add(debug_box0)

debug_box = Gtk::HBox.new(false, 0)
debug_box.border_width = 10
debug_box0.pack_start(debug_box, true, true, 10)

debug_DebugOn = Gtk::RadioButton.new("通常モード")
debug_DebugOn.active = true
debug_box.pack_start(debug_DebugOn, true, true, 0)

debug_DebugOn2 = Gtk::RadioButton.new(debug_DebugOn, "デバッグモード")
debug_box.pack_start(debug_DebugOn2, true, true, 0)

debug_label = Gtk::Label.new(DebugOn_info)
debug_box0.pack_start(debug_label, true, true, 0)


#===================================================================
#======== 現在表示するページ =======================================
#===================================================================
notebook.page = 0
notebook.scrollable = true
notebook.enable_popup = true

#button = Gtk::Button.new("next page")
#button.signal_connect("clicked") {notebook.next_page}
#table.attach_defaults(button, 0, 1, 2, 3)

#button = Gtk::Button.new("prev page")
#button.signal_connect("clicked") {notebook.prev_page}
#table.attach_defaults(button, 1, 2, 2, 3)



#===================================================================
#======== キャンセルボタン =========================================
#===================================================================
button = Gtk::Button.new("キャンセル")
button.signal_connect("clicked") {
  Gtk.main_quit
}
table.attach_defaults(button, 3, 4, 1, 2)


#===================================================================
#======== 計算実行ファイル =========================================
#===================================================================
button = Gtk::Button.new("計算実行")
button.signal_connect("clicked") {
  system("/home/sugiyama/work/deepconv/arare/bin/arare")
}
table.attach_defaults(button, 2, 3, 1, 2)




#===================================================================
#======== 初期値生成ボタン =========================================
#===================================================================
button = Gtk::Button.new("初期値生成")
button.signal_connect("clicked") {
  #ファイルのオープン
  OutputFile = open(cfgfile_tmp, "w")

  puts "writeing Output File.... "

  OutputFile.puts  '! Config file for deepconv/arare '
  OutputFile.print '!   generated by arare-gtk.rb'
  OutputFile.print "\n \n"

  OutputFile.puts  "&fileset"
  OutputFile.print "! ", flag_info, "  \n"
  OutputFile.print "  flag       = \" ", io_flag.text, " \"  \n"
  OutputFile.print "! ",  basicfile_info, "  \n"
  OutputFile.print "  basicfile  = \" ", io_basicfile.text, " \"  \n"
  OutputFile.print "! ",  distrbfile_info, "  \n"
  OutputFile.print "  distrbfile = \" ", io_distrbfile.text, " \"  \n"
  OutputFile.puts  '/'

  OutputFile.puts  "&expname"
  OutputFile.print "! ", ExpTitle_info, "  \n"
  OutputFile.print "  ExpTitle = \" ", exp_ExpTitle.text, " \"  \n"
  OutputFile.print "! ", ExpSrc_info, "  \n"
  OutputFile.print "  ExpSrc   = \" ", exp_ExpSrc.text, " \"  \n"
  OutputFile.print "! ", ExpInst_info, "  \n"
  OutputFile.print "  ExpInst  = \" ", exp_ExpInst.text, " \"  \n"
  OutputFile.puts  '/'

  OutputFile.puts  "&gridset"
  OutputFile.print "! ", NX_info, "  \n"
  OutputFile.print "  NX    = ", grid_NX.text, "  \n"
  OutputFile.print "! ", NZ_info, "  \n"
  OutputFile.print "  NZ    = ", grid_NZ.text, "  \n"
  OutputFile.print "! ", XMin_info, "  \n"
  OutputFile.print "  XMin  = ", grid_XMin.text, "  \n"
  OutputFile.print "! ", XMax_info, "  \n"
  OutputFile.print "  XMax  = ", grid_XMax.text, "  \n"
  OutputFile.print "! ", ZMin_info, "  \n"
  OutputFile.print "  ZMin  = ", grid_ZMin.text, "  \n"
  OutputFile.print "! ", ZMax_info, "  \n"
  OutputFile.print "  ZMax  = ", grid_ZMax.text, "  \n"
  OutputFile.puts  '/'

  OutputFile.puts  "&timeset"
  OutputFile.print "! ", DelTimeLong_info, "  \n"
  OutputFile.print "  DelTimeLong  = ", time_DelTimeLong.text, "  \n"
  OutputFile.print "! ", DelTimeShort_info, "  \n"
  OutputFile.print "  DelTimeShort = ", time_DelTimeShort.text, "  \n"
  OutputFile.print "! ", TimeInt_info, "  \n"
  OutputFile.print "  TimeInt      = ", time_TimeInt.text, "  \n"
  OutputFile.print "! ", TimeDisp_info, "  \n"
  OutputFile.print "  TimeDisp     = ", time_TimeDisp.text, "  \n"
  OutputFile.puts  '/'

  OutputFile.puts  "&bcset"
  OutputFile.print "! ", BcX_info, "  \n"
  i = 0
  for radio in bc_BcX.group
    if radio.active?
      if i == 0
        OutputFile.print "  BcX = \"F\"  \n"
      elsif i == 1
        OutputFile.print "  BcX = \"R\"  \n"
      elsif i == 2 
        OutputFile.print "  BcX = \"C\"  \n"
      end
    end
    i = i + 1
  end
  OutputFile.print "! ", BcZ_info, "  \n"
  i = 0
  for radio in bc_BcZ.group
    if radio.active?
      if i == 0
        OutputFile.print "  BcZ = \"F\"  \n"
      elsif i == 1
        OutputFile.print "  BcZ = \"R\"  \n"
      elsif i == 2
        OutputFile.print "  BcZ = \"C\"  \n"
      end
    end
    i = i + 1
  end
  OutputFile.puts  '/'


  OutputFile.puts  "&arareprm"
  OutputFile.print "! ", Grav_info, "  \n"
  OutputFile.print "  Grav   = ", prm_Grav.text, "  \n"
  OutputFile.print "! ", Alpha_info, "  \n"
  OutputFile.print "  Alpha  = ", prm_Alpha.text, "  \n"
  OutputFile.print "! ", Beta_info, "  \n"
  i = 0
  for radio in prm_Beta.group
    if radio.active?
      if i == 0
        OutputFile.print "  Beta   = 1.0d0  \n"
      elsif i == 1
        OutputFile.print "  Beta   = 0.5d0  \n"
      end
    end
    i = i + 1
  end
  OutputFile.print "! ", Cm_info, "  \n"
  OutputFile.print "  Cm     = ", prm_Cm.text, "  \n"
  OutputFile.print "! ", tfil_info, "  \n"
  OutputFile.print "  tfil   = ", prm_tfil.text, "  \n"
  OutputFile.print "! ", AlphaH_info, "  \n"
  OutputFile.print "  AlphaH = ", prm_AlphaH.text, "  \n"
  OutputFile.print "! ", AlphaV_info, "  \n"
  OutputFile.print "  AlphaV = ", prm_AlphaV.text, "  \n"
  OutputFile.puts  '/'


  OutputFile.puts  "&linlib"
  OutputFile.print "! ", LLib_info, "  \n"
  i = 0
  for radio in lin_LLib.group
    if radio.active?
      if i == 1
        OutputFile.print "  LLib = \"ssl2\"   \n"
      elsif 
        puts "Not Supported LAPACK"
      end
    end
    i = i + 1
  end
  OutputFile.puts  '/'

  OutputFile.puts  "&debugset"
  OutputFile.print "! ", DebugOn_info, "  \n"
  i = 0
  for radio in debug_DebugOn.group
    if radio.active?
      if i == 0
        OutputFile.print "  DebugOn = .true.  \n"
      elsif i == 1
        OutputFile.print "  DebugOn = .false.  \n"
      end
    end
    i = i + 1
  end
  OutputFile.puts  '/'

  OutputFile.close
  command = 'iconv -t euc-jp -f utf-8 '+cfgfile_tmp+' > '+cfgfile
  system(command)

}
table.attach_defaults(button, 1, 2, 1, 2)





#===================================================================
#======== ファイル読み込みボタン ===================================
#===================================================================
button = Gtk::Button.new("ファイル読み込み")

fileop_button = Gtk::CheckButton.new("show fileop buttons", true)
dir_list_button = Gtk::CheckButton.new("show dir list", true)

button.signal_connect("clicked") do
  filew = Gtk::FileSelection.new("File selection")
  filew.filename = ""
  filew.show_fileops = fileop_button.active?
  filew.dir_list.parent.hide unless dir_list_button.active?
  if filew.run == Gtk::Dialog::RESPONSE_OK
    puts filew.filename
    file_in = open(filew.filename,"r") 
    while line = file_in.gets 
      line.chomp!                  #改行文字を除去
      next if line.strip.empty?
      next if line =~ /^\s*!/
      next if line =~ /^\s*&/
      next if line =~ /^\s*\//
      line =~ /\s*(\w*)\s+=\s*\"*\s(\S+)\"*\s+/
      var[$1] = $2
#      print $1
#      print "\n"
#      print var[$1]
#      print "\n"
    end    
  end

  io_flag.text           = var['flag']
  io_basicfile.text      = var['basicfile']
  io_distrbfile.text     = var['distrbfile']
  exp_ExpTitle.text      = var['ExpTitle']
  exp_ExpSrc.text        = var['ExpSrc']
  exp_ExpInst.text       = var['ExpInst']
  grid_NX.text           = var['NX']
  grid_NZ.text           = var['NZ']
  grid_XMin.text         = var['XMin']
  grid_XMax.text         = var['XMax']
  grid_ZMin.text         = var['ZMin']
  grid_ZMax.text         = var['ZMax']
  time_DelTimeLong.text  = var['DelTimeLong'] 
  time_DelTimeShort.text = var['DelTimeShort'] 
  time_TimeInt.text      = var['TimeInt']
  time_TimeDisp.text     = var['TimeDisp'] 
  prm_Grav.text          = var['Grav'] 
  prm_Alpha.text         = var['Alpha'] 
  prm_Cm.text            = var['Cm'] 
  prm_tfil.text          = var['tfil'] 
  prm_AlphaH.text        = var['AlphaH']  
  prm_AlphaV.text        = var['AlphaV'] 
    
  if var['BcX'] =~ /C/
    bc_BcX.active = true 
  elsif var['BcX'] =~ /R/
    bc_BcX2.active = true 
  elsif var['BcX'] =~ /F/
    bc_BcX3.active = true 
  else
    print "undifned BcX \n"
  end

  if var['BcZ'] =~ /C/
    bc_BcZ.active = true 
  elsif var['BcZ'] =~ /R/
    bc_BcZ2.active = true 
  elsif var['BcZ'] =~ /F/
    bc_BcZ3.active = true 
  else
    print "undifned BcZ \n"
  end

  if var['DebugOn'] =~ /.false./
    debug_DebugOn.active = true 
  else
    debug_DebugOn2.active = true 
  end

  if var['LLib'] =~ /ssl2/
    lin_LLib.active = true 
  else
    lin_LLib2.active = true 
  end

  if var['Beta'] =~ /0.5d0/
    prm_Beta.active = true 
  elsif var['Beta'] =~ /1.0d0/
    prm_Beta2.active = true 
  else
    print "undifined Beta \n"
  end


  filew.destroy
end
table.attach_defaults(button, 0, 1, 1, 2)


window.show_all
Gtk.main
