#! /usr/bin/perl
#  $B4%Ag@EE*%(%M%k%.!<FnKLM"Aw(B, $B@xG.FnKLM"AwNL(B
#
# $B0z?t%G%U%)%k%HCM(B
#
#($B%(%i!<=hM}(B)
$str = 1;
$end = 9999;
$step = 1;
$lay = 2;
$color = "T";
$wsn = 1;

# $B0z?t2r@O(B
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 . ' ' . $_ ;
	        }
	    }
}
#
# $BA4FnKLM"AwNL(B
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");   
}
#
# $B3F<o%G!<%?$rMQ0U(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$B&R(B
   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] $BA4It(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 $BItJ,(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] $B1tD>J?6QItJ,(B
  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");

#               _  _
# $BA4M"AwNL$+$i(B [S][v] $B1tD>J?6QItJ,$r0z$/(B
  system("gtsub $file-yusou.out $file-yusou-zonal-zmean.out out:$file-yusou-totalMzonalzmean.out");

#
# $B:n?^(B
#  $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");


# $B0LCV%(%M%k%.!<FnKLM"AwNL(B
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");


# $B4%Ag@EE*%(%M%k%.!<FnKLM"AwNL(B
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@xG.%(%M%k%.!<FnKLM"AwNL(B
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");

# $B?e>x5$FnKLM"AwNL(B
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");
