#!/usr/bin/ruby

#
# ● このプログラムは, (k, l, eksp) の 3列からなるテキストデータを使って
#    gnuplot で 3次元図を作成するプログラムである(メッシュ数 150x150). 
#
# ● 図の生成に必要なファイル
#    1. このファイル, 
#    2. gpprint2txt のスクリプトによって生成されるテキストデータ ※ (k, l, eksp)の3列からなるテキストデータ
#    3. もとの NetCDF ファイル
#
# ● 使い方
#
# % ruby gplot-eksp-grid150.rb ncfilename@eksp
#
# (例) % ruby gplot-eksp-grid150.rb ../exp4-1-1_beta4500_klm85_ijm256.nc@eksp
#

require "numru/gphys"
require "numru/gphys/gpcommon"
include NumRu

filename = File.basename(ARGV[0])
gturl = ARGV[0]
gphys = GPhys::IO.open_gturl(gturl)
topname = filename.slice(0..7)
fileval = filename.split(/@/)
endname = fileval[1]

l = gphys.coord(0).val
k = gphys.coord(1).val
t = gphys.coord(2).val

a = 0
tnum2 = 0

while(tnum2 <= t.length-1) do 

  tmp = t[a] * 10        # tmp を 10 倍する
  tmp = tmp.round        # tmp の小数点以下を四捨五入して Integer に変換
  tmp = (tmp.to_f / 10)  # tmp を Float に変換して 10 で割る
  tx = "#{tmp}"          # tmp を文字オブジェクトに変換
  tx = tx.slice!(0..2)

system("gnuplot << EOF
set terminal postscript eps enhanced \"Helvetica\" 30 color
#set terminal postscript enhanced \"Helvetica\" 16 color
#set terminal png
set output \"#{topname}_#{endname}-3D-m150_t=#{tx}0.ps\"
#set output \"#{topname}_#{endname}-3D_t=#{tx}0.png\"
#set size 0.90,0.90
set size 1.80,1.80
set ticslevel 1.5 # 上部の 3D 図の y軸上の位置の指定
#set logscale z
set cntrparam levels 150   # 等高線を何段階に分けて描くかの設定
set cntrparam levels discrete 0,1e-8,5e-8,1e-7,5e-7,1e-6,5e-6,1e-5,5e-5,1e-4,5e-4,1e-3,5e-3
set dgrid3d 150,150,2  # raw data to grid data (third param is norm)
set hidden3d
set isosample 150,150  # 3D 図のメッシュの細かさ指定(数字大ほど細かい)
#set nocontour
set contour base
#set key 105,10
#set pm3d at b
set title \"Energy Spectrum (#{topname} / t=#{tx}0) * 150 mesh *\" 0.0, 0.0 font \"Helvetica,30\"
set xlabel \"X-wavenumber\" 0.0, -0.2 font \"Helvetica,30\"
set ylabel \"Y-wavenumber\" 1.0,  0.0 font \"Helvetica,30\"
set zlabel \"Energy spectrum\" 0.0, 0.0 font \"Helvetica,30\"
set xtics 50
set ytics 50
set ztics 5e-4
#set view 66, 30
set view 50, 48        # 3D 図の見る角度の指定
set xrange [ -85.0000 : 85.0000 ]
set yrange [ -85.0000 : 85.0000 ]
set zrange [ 0 : 5e-4 ] noreverse nowriteback
#set zrange [ 1e-9 : 1e-3 ] noreverse nowriteback
splot \"#{topname}-#{endname}_t=#{tx}0.txt\" notitle with lines 
#splot \"#{topname}-#{endname}_t=#{tx}0.txt\" title \"Energy Spectrum (t=#{tx})\"  with lines 
EOF")

a += 10
tnum2 += 1

end
