#! /usr/bin/ruby
#  乃Z燁E静的ゃBネルゃッV南北較N送, 潜熱南北較N送量
#
# 2014-12-9 矃E渁B perl ゃPゃbッFプトから曃Nき換え.

require "getoptlong"        # for option_parse
require "numru/ggraph"
require "pp"
include NumRu

## ゃFプシッ@ッE见v
parser = GetoptLong.new
parser.set_options(
  ###    global option   ###
  ['--area',                      GetoptLong::REQUIRED_ARGUMENT],
  ['--time_start',                GetoptLong::REQUIRED_ARGUMENT],
  ['--time_end',                  GetoptLong::REQUIRED_ARGUMENT],
  ['--wsn',                       GetoptLong::REQUIRED_ARGUMENT]
)
parser.each_option do |name, arg|
    eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_')} = '#{arg}'"  # strage option value to $OPT_val
end

time_start = ($OPT_time_start||1).to_f
time_end = ($OPT_time_end||9999).to_f


#                 _   _          _
# ぃZず v ぃHぁAいて v, [v](y,z,*), v* をB烇.

# デーゃ\誃ぃ\较Vぃ\
path = 'V.nc'
gp_v = GPhys::IO.open(path, var).cut('time'=>time_start..time_end)

# exit じゃダチB? foreach ぃぃBころ文句言われる.

__END__


exit


# 
foreach $file (v, HE, 'LE', PE, SE){
# _
# v : v-avr.out(x,y,z)
   system("gtsel $file str=$str end=$end step=$step out:$file-anal.out");
   system("gtavr $file-anal.out out:$file-avr.out");
#  _
# [v](y,z,*)
   system("gtsel $file-avr.out x=0 out:$file-avr-zm.out");
#                                _         _
# eddy comonent of [v](y,z,*) : [v] - int [v] d}
   system("gtedy x=F y=T z=F $file-avr-zm.out out:$file-avr-zm-zeddy.out");
   system("gtsub $file-avr-zm.out $file-avr-zm-zeddy.out out:$file-avr-zm-zmean.out");
#  _
#  v* : v-var-eddy.ouy (x,y,z)
   system("gtedy $file-avr.out out:$file-avr-eddy.out");

   $ndata = $end - $str;
   $ndata = $ndata + 1;
   system("cat $file-avr.out $file-avr.out > $file-avr.out2");
   $count = 1;
   $countp = 2;
   while($countp < $ndata){
     $count  = $count + 1 ;
     $countp = $countp + 1 ;
     system("cat $file-avr.out $file-avr.out$count > $file-avr.out$countp");
   }

   system("gtsub $file-anal.out $file-avr.out$count out:$file-dist.out");
}




exit




# 引数デフゃDッHト倁A
#
#(ゃBッDッV凃理)
$str = 1;
$end = 9999;
$step = 1;
$lay = 2;
$color = "T";
$wsn = 1;

# 引数见v
foreach (@ARGV) {
	if(/=/){
                if(/str/) {
	              @opt_str = split(/=/,$_);
		      $str = $opt_str[1];
                } elsif(/end/) {
	              @opt_end = split(/=/,$_);
		      $end = $opt_end[1];
                } elsif(/step/) {
	              @opt_step = split(/=/,$_);
		      $step = $opt_step[1];
                } elsif(/lay/) {
	              @opt_lay = split(/=/,$_);
		      $lay = $opt_lay[1];
                } elsif(/color/) {
	              @opt_color = split(/=/,$_);
		      $color = $opt_color[1];
                } elsif(/wsn/) {
	              @opt_wsn = split(/=/,$_);
		      $wsn = $opt_wsn[1];
                } else {
			$options = $options . ' ' . $_ ;
	        }
	    }
}
#
# 元B南北較N送量
foreach $file (SE, 'LE', HE, PE){
   system("gtmlt v $file out:v$file");   
   $commands_total = "gtsel v$file str=$str end=$end step=$step && gtavr && gtsel x=0 z=0 out:v$file-bar && gtmlt v$file-bar LATLEN out:TMP && gthead TMP ITEM/$file-trnsprt && gtcurv title:$file-trnsprt unit:'W' wsn=$wsn lay=$lay y=1 title:$file-total && mv dcl.ps $file-yusou.ps && mv gtool.out $file-yusou.out";   

   system("$commands_total");   
}
#
# 各Bデーゃ\を用
foreach $file (v, HE, 'LE', PE, SE){
# _
# v : v-avr.out(x,y,z)
   system("gtsel $file str=$str end=$end step=$step out:$file-anal.out");
   system("gtavr $file-anal.out out:$file-avr.out");
#  _
# [v](y,z,*)
   system("gtsel $file-avr.out x=0 out:$file-avr-zm.out");
#                                _         _
# eddy comonent of [v](y,z,*) : [v] - int [v] d}
   system("gtedy x=F y=T z=F $file-avr-zm.out out:$file-avr-zm-zeddy.out");
   system("gtsub $file-avr-zm.out $file-avr-zm-zeddy.out out:$file-avr-zm-zmean.out");
#  _
#  v* : v-var-eddy.ouy (x,y,z)
   system("gtedy $file-avr.out out:$file-avr-eddy.out");

   $ndata = $end - $str;
   $ndata = $ndata + 1;
   system("cat $file-avr.out $file-avr.out > $file-avr.out2");
   $count = 1;
   $countp = 2;
   while($countp < $ndata){
     $count  = $count + 1 ;
     $countp = $countp + 1 ;
     system("cat $file-avr.out $file-avr.out$count > $file-avr.out$countp");
   }

   system("gtsub $file-anal.out $file-avr.out$count out:$file-dist.out");
}

foreach $file ('LE', SE){
#  _  _
# [S][v] 元B郃B
  system("gtmlt $file-avr-zm.out v-avr-zm.out && gtsel y=0 out:v$file-zonal.out");
  system("gtmlt v$file-zonal.out LATLEN out:TMP && gthead TMP ITEM/$file-zonal    && gtcurv y=1 wsn=$wsn lay=$lay");
  system("mv gtool.out $file-yusou-zonal.out");
  if($wsn == 2){
     system("mv dcl.ps $file-yusou-zonal.ps");
  }
#  _  _
# [S][v] z-eddy 郃B
  system("gtmlt $file-avr-zm-zeddy.out v-avr-zm-zeddy.out && gtsel y=0 out:v$file-zonal-zeddy.out");
  system("gtmlt v$file-zonal-zeddy.out LATLEN out:TMP && gthead TMP ITEM/$file-zonal-zeddy  && gtcurv y=1 wsn=$wsn lay=$lay");
  system("mv gtool.out $file-yusou-zonal-zeddy.out");
  if($wsn == 2){
    system("mv dcl.ps $file-yusou-zonal-zeddy.ps");
  }
#  _  _
# [S][v] 鉛直幃E均部
  system("gtmlt $file-avr-zm-zmean.out v-avr-zm-zmean.out && gtsel y=0 out:v$file-zonal-zmean.out");
  system("gtmlt v$file-zonal-zmean.out LATLEN out:TMP && gthead TMP ITEM/$file-zonal-zmean && gtcurv y=1 wsn=$wsn lay=$lay");
  system("mv gtool.out $file-yusou-zonal-zmean.out");
  if($wsn == 2){
    system("mv dcl.ps $file-yusou-zonal-zmean.ps");
  }
#  __ __
# [S* v*]
  system("gtmlt $file-avr-eddy.out v-avr-eddy.out && gtsel x=0 z=0 out:v$file-eddy.out");
  system("gtmlt v$file-eddy.out LATLEN  out:TMP && gthead TMP ITEM/$file-eddy   && gtcurv y=1 wsn=$wsn lay=$lay");
  system("mv gtool.out $file-yusou-eddy.out");
  if($wsn == 2){
    system("mv dcl.ps $file-yusou-eddy.ps");
  }
# ______
# [S'v']
  system("gtmlt $file-dist.out v-dist.out && gtavr && gtsel x=0 z=0 out:v$file-dist.out");
  system("gtmlt v$file-dist.out LATLEN  out:TMP && gthead TMP ITEM/$file-dist   && gtcurv y=1 wsn=$wsn lay=$lay");
  system("mv gtool.out $file-yusou-dist.out");
  if($wsn == 2){
    system("mv dcl.ps $file-yusou-dist.ps");
  }

  system("gtadd $file-yusou-eddy.out $file-yusou-dist.out out:$file-yusou-jouran.out");
system("gtadd $file-yusou-zonal.out $file-yusou-jouran.out out:$file-yusou-total.out");

#               _  _
# 元B較N送量から [S][v] 鉛直幃E均部分を引く
  system("gtsub $file-yusou.out $file-yusou-zonal-zmean.out out:$file-yusou-totalMzonalzmean.out");

#
# 作図
#  $command_uchiwake = "gtcurv $file-yusou.out $file-yusou-zonal.out $file-yusou-eddy.out $file-yusou-dist.out" .
  $command_uchiwake = "gtcurv $file-yusou-totalMzonalzmean.out $file-yusou-zonal-zeddy.out $file-yusou-eddy.out $file-yusou-dist.out" .
     "  y=1 wsn=$wsn lay=$lay tick=F title:$file-trnsprt $options -sg:lcorner=.false.";
   if($color eq "T"){
	$command_uchiwake = $command_uchiwake .
                    " ltype=1,1,1,1 lidx=888,298,558,748 ";
   }
    if($wsn == 2){
	$command_uchiwake = $command_uchiwake .
                    " && mv dcl.ps $file-yusou-uchiwake.ps";
    }
  system("$command_uchiwake");
}

exit;


system("gtsel HE str=$str end=$end step=$step && gtavr out:HE-avr.out");

system("barbar.sh HE v $str $end $step && gtsel HEv.bar y=0 && gtavr out:HEvbar
&& gtmlt HEvbar LATLEN && gtcurv y=1 wsn=$wsn  title:'HEv-bar'  lay=$lay && mv dcl.ps HEv-bar.ps && mv gtool.out HEv-bar.out");

system("dashdash.sh HE v $str $end $step && gtsel HEv.dash x=0 z=0 && gtavr out:HEvdashbar && gtmlt HEvdashbar LATLEN && gtcurv y=1 wsn=$wsn  title:'HEv-dash'  lay=$lay && mv dcl.ps HEv-dash.ps && mv gtool.out HEv-dash.out");


# 位XゃBネルゃッV南北較N送量
system("gtmlt PE v out:PEv");
system("gtsel PEv str=$str end=$end step=$step && gtavr && gtsel x=0 z=0 out:phivbar && gtmlt phivbar LATLEN && gtcurv title:phi-trnsprt unit:'W' wsn=$wsn lay=$lay y=1 && mv dcl.ps phi-yusou.ps && mv gtool.out phi-yusou.out");

system("barbar.sh PE v $str $end $step && gtsel PEv.bar y=0 && gtavr out:PEvbar && gtmlt PEvbar LATLEN  && gtcurv y=1 wsn=$wsn  title:'PEv-bar' lay=$lay && mv dcl.ps PEv-bar.ps && mv gtool.out PEv-bar.out");

system("dashdash.sh PE v $str $end $step && gtsel PEv.dash x=0 z=0 && gtavr out:PEvdashbar && gtmlt PEvdashbar LATLEN && gtcurv y=1 wsn=$wsn  title:'PEv-dash'  lay=$lay && mv dcl.ps PEv-dash.ps && mv gtool.out PEv-dash.out");


# 乃Z燁E静的ゃBネルゃッV南北較N送量
system("gtadd HE-yusou.out PE-yusou.out && gtcurv title:s-trnsprt unit:'W' wsn=$wsn lay=$lay y=1 && mv dcl.ps s-yusou.ps && mv gtool.out s-yusou.out");

system("gtadd HEv-bar.out PEv-bar.out && gtcurv title:'s-trnsprt(mean)' unit:'W' wsn=$wsn lay=$lay y=1 && mv dcl.ps Sv-bar.ps && mv gtool.out Sv-bar.out");

system("gtadd HEv-dash.out PEv-dash.out && gtcurv title:'s-trnsprt(dist)' unit:'W' wsn=$wsn lay=$lay y=1 && mv dcl.ps Sv-dash.ps && mv gtool.out Sv-dash.out");


# 潜熱ゃBネルゃッV南北較N送量
system("gtmlt LE v out:LEv");
system("gtsel LEv str=$str end=$end step=$step && gtavr && gtsel x=0 z=0 out:levbar && gtmlt levbar LATLEN && gtcurv title:le-trnsprt unit:'W' wsn=$wsn lay=$lay y=1 && mv dcl.ps le-yusou.ps && mv gtool.out le-yusou.out");

system("barbar.sh LE v $str $end $step && gtsel LEv.bar y=0 && gtavr out:LEvbar
&& gtmlt LEvbar LATLEN && gtcurv y=1 wsn=$wsn  title:'LEv-bar'lay=$lay && mv dcl.ps LEv-bar.ps && mv gtool.out LEv-bar.out");

system("dashdash.sh LE v $str $end $step && gtsel LEv.dash x=0 z=0 && gtavr out:LEvdashbar && gtmlt LEvdashbar LATLEN && gtcurv y=1 wsn=$wsn  title:'LEv-dash' lay=$lay && mv dcl.ps LEv-dash.ps && mv gtool.out LEv-dash.out");

# 氃G蒃N気南北VN送量
system("gtmlt q v out:qv");
system("gtsel qv z=0 out:qv-vm.out");
system("gtmlt qv-vm.out Ps fact1=10.204082  out:qvPs");
system("gtmlt qvPs LATLEN title:qflux unit:'Kg/s' out:q-yusou.out");
system("gtsel qvPs x=0 str=$str end=$end step=$step && gtavr out:q-yusou.out && gtmlt q-yusou.out LATLEN && gtcurv wsn=$wsn lay=$lay y=1 && mv dcl.ps q-yusou.ps");
