#!/bin/csh

########################################################################
## NOTE: This file is created by /home/disk/data/loops/new/gemplot.pl.  
##       To make changes permanent, edit /home/disk/data/loops/new/gemplot.pl.
########################################################################
source /usr/local/gempak/Gemenviron
# setenv DISPLAY `startXvfb` # this is done in calling program

set force    = "0"
set nosave   = "0"
set extra    = ""
set full     = 0

echo "$0 is running with arguments $argv[1-] and DISPLAY $DISPLAY"

## debug
#set echo

if ($1 == "-h" || $1 == "-help") then
    echo "Usage: $0 [yyyymmddhh] [force]"
    echo "  where:  yyyymmddhh is an optional model initialization time"
    echo "          DEFAULT is nearest 0Z/12Z time"
endif
if ($1 == "") then
    set latest00 = `time-increment now i4 0 -7200 YYYYMMDDH12`
else
    set latest00 = $1
endif
set j     = 1
foreach i ($argv[2-])
  @ j++
  @ k = $j + 1
  switch ("$i")
    case "full":
     echo "running full size images"
     set full = 1
     set extra = "_full"
     breaksw
    case "clear":
     echo "removing $latest00 gif images"
     /bin/rm $latest00*gif
     breaksw
    case "force":
    case "f":
     set force = 1
     breaksw
    default:
     echo "ignoring argument $i for now"
   endsw
end

if ($force == 1) then
  echo "removing $latest00*[0-9]$extra.gif images"
  /bin/rm $latest00*[0-9]$extra.gif
endif

set initdate00 = 7777
foreach initfcst ("0,0" "0,6" "0,12" "0,18" "0,24" "0,30" "0,36" "0,42" "0,48" "0,54" "0,60" "0,66" "0,72" "0,78" "0,84" "0,90" "0,96" "0,102" "0,108" "0,114" "0,120" "0,126" "0,132" "0,138" "0,144" "0,150" "0,156" "0,162" "0,168" "0,174" "0,180")
#foreach initfcst ("0,0")
  set init = `echo $initfcst |cut -f1 -d,`
  set fcst = `echo $initfcst | cut -f2 -d,`
  if ($fcst < 10) then
     set fcsthr = "00$fcst"
  else if ($fcst < 100) then
     set fcsthr = "0$fcst"
  else 
     set fcsthr = "$fcst"
  endif
  @ initsec = $init * 3600
  @ fcstsec = $fcst * 3600
#  set initdate00 = `time-increment "${latest00}0000" yy 0 $initsec YYYYMMDDHH`
#  set longinitdate = `time-increment "${latest00}0000" yy 0 $initsec "HHZ day dd mon year"`
  if ( $initfcst == "0,0" || $init != "0" || $initdate00 == 7777 ) then
    set split      = `time-increment "${latest00}0000" yy 0 $initsec "YYYYMMDDHH,HHZ day dd mon year"`
    set initdate00   = `echo $split | cut -f1 -d,`
    set longinitdate = `echo $split | cut -f2 -d,`
  endif
  set longfcstdate = `time-increment "${initdate00}0000" yy 0 $fcstsec "HHZ day dd mon year"`
  echo "$0 $initfcst  -->  $init $longinitdate  --> $fcst $longfcstdate --> ${initdate00}_$fcsthr$extra.gif"

##
## Nearly duplicate the 0_05 plot.
  if (-e "${initdate00}_$fcsthr$extra.gif" && $force == 0) then
   echo "${initdate00}_$fcsthr$extra.gif already exists"
  else if (-e "yesterday/${initdate00}_$fcsthr$extra.gif" && $force == 0) then
   echo "copying yesterday/${initdate00}_$fcsthr$extra.gif back"
   /bin/cp yesterday/${initdate00}_$fcsthr$extra.gif ${initdate00}_$fcsthr$extra.gif
  else
    echo ""
    set gemfile = ""
    if (-e "${initdate00}_$fcsthr$extra.gif" && $force == 1) /bin/rm ${initdate00}_$fcsthr$extra.gif
    if (-e /tmp/${initdate00}_avn.gem) then
      setenv MODEL /tmp
      set gemfile = ${initdate00}_avn.gem
    else if (-e $MODEL/gfs/${initdate00}_gfs.gem) then
      set gemfile = gfs/${initdate00}_gfs.gem
    else if (-e $MODEL/gfs/${initdate00}_gfs003.gem) then
      set gemfile = gfs/${initdate00}_gfs003.gem
    else if (-e $MODEL/gfs/${initdate00}_gfs703.gem) then
      set gemfile = gfs/${initdate00}_gfs703.gem
    else if (-e $MODEL/${initdate00}_avn.gem) then
      set gemfile = ${initdate00}_avn.gem
    else if (-e /home/disk/freshair_data/gempak/model/gfs/${initdate00}_gfs.gem) then
      setenv MODEL /home/disk/freshair_data/gempak/model
      set gemfile = gfs/${initdate00}_gfs.gem
    endif
    echo "Making the plot for $longfcstdate using $MODEL/$gemfile..."
   if ($full == "1") then
     set xysize  = "1600;1050"
     set panel = "0;0;.96;.96"
   else
     set xysize = "800;800"
     set panel = "0;0;.9;.95/0"
   endif
   gdplot2_gf << EOF > /dev/null
 GDFILE   = $MODEL/$gemfile
 DEVICE   = gf|${initdate00}_$fcsthr$extra.gif|$xysize
 GLEVEL   = 0
 GVCORD   = none
 SKIP     = 0
 CONTUR   = 0
 LINE     = 0
 HILO     =  
 HLSYM    =  
 WIND     = BM1
 REFVEC   =  
 TEXT     = 1/21//hw
 STNPLT   =  
 SATFIL   = 
 RADFIL   =  
 LUTFIL   =  
 STREAM   =  
 POSN     = 0
 COLORS   = 101=white
 MARKER   = 0
 GRDLBL   = 0 
 FILTER   = 1
# Bogus area so that I can put the color bar in a good spot
 CLEAR    = y
 CINT     = .01;.03;.10;.25;.50;.75;1.0;1.25;1.5;1.75;2.0;2.5;3.0;4;5;6;7;8;9 ! 4
 FINT     = .01;.03;.10;.25;.50;.75;1.0;1.25;1.5;1.75;2.0;2.5;3.0;4;5;6;7;8;9
 FLINE    = 0;13;21-30;14-20;5
 SCALE    = 0 ! 0
#
 GDPFUN   = mul(p06m,0)
 TYPE     = f
 LATLON   = 0
 MAP      = 0
 PANEL    = 0
 GAREA    = 60;-105;61;-103
 PROJ     = mer
 CLRBAR   = 32/v/LR/1;.073/.75;.02/-1
 TITLE    = 32/1/$fcsthr Hr Fcst 6-Hr Pcpn (in)/SLP (mb)/1000-500 mb Thickness valid $longfcstdate
 TEXT     = 1.3/23//hw
# need an hour where precip has accumulated for colorbar to plot
 GDATTIM  = f24
 save step1
 run

 CLEAR    = no
 GDATTIM  = f$fcsthr
 TITLE    = 32/3/(GFS initialized $longinitdate)
 TEXT     = medium/23//hw
 LINE     = 0
 CLRBAR   = 0
 save step2
 run

#
# 983;645 gives .938 ratio for inner image
 PANEL    = $panel
 GAREA    = #51.2;0;10;10
 PROJ     = lcc/30;0;60
 GDPFUN   = quo(p06m,25.4) ! pmsl
 SCALE    = 0 ! 0
 TYPE     = f/c ! c
 TITLE    = 0
 LINE	  = 0  ! 32/1/1/1
 MAP      = 30/1/1
 CLRBAR   = 0
 LATLON   = 24/4/1/2/10;10
 TEXT     = small/21//hw
 HILO     = ! 32///4/6;6/y 
 HLSYM    = ! 2.5/2/21//HW
 save step3
 run

# put high/low pcpn text over the top of everything
 GDPFUN   = ldf(hght) ! ldf(hght) ! ldf(hght)
 GLEVEL   = 500:1000 ! 500:1000 ! 500:1000
 GVCORD   = pres ! pres ! pres
 TYPE     = c ! c ! c
 cint     = 6/546 ! 6//540 ! 6/522/522
 scale    = -1 ! -1 ! -1
 HILO     = 
 HLSYM    = 
 MAP      = 0
 LATLON   = 0
 line     = 2/12/2 ! 4/12/2 ! 7/12/2
 save step4
 run

 TITLE    = 29/-2/Univ. of Washington Dept. of Atm. Sci.
 TEXT     = 1.3/23//hw
 LINE     = 0
 save step5
 run

exit
EOF

   date
  endif
end
echo "should be done, doing gpend and other cleanup at `date`"
# gpend is not needed in 5.6.H using gplot2_gf !!! Wow!!!
# gpend 

ipcs -aq
# /usr/local/wx/bin/clear_ipc -f
# stopXvfb $DISPLAY
