#!/bin/csh
cd ~ovens/public_html/chwx/
find . -name '*gif' -mtime +14 -exec /bin/rm {} \;


########################################################################
## 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

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" "0,24")
  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" ) 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/${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 size  = "1600;1050"
     set panel = "0;0;.96;.96"
   else
     set size = "983;645"
     set panel = "0;0;.938;.938"
   endif
   gdplot2_gf << EOF > /dev/null
 GDFILE   = $MODEL/$gemfile
 DEVICE   = gf|${initdate00}_$fcsthr$extra.gif|$size
 GLEVEL   = 0
 GVCORD   = none
 SKIP     = 0
 CONTUR   = 0
 LINE     = 0
 HILO     =  
 HLSYM    =  
 WIND     = BK32
 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 ! 3/32
 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    = 0;0;5;5
 PROJ     = mer
 CLRBAR   = 32/v/LR/1;.073/.75;.02/-1
 TITLE    = 32/1/$fcsthr Hr Fcst 6-Hr Pcpn (in)/SLP/T2/Wind10m/1000-500 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    = -50;150;-30;179.9
 GAREA    = -50;130;-22;179.9
 GAREA    = 28;-20;58;28
 GAREA    = 29.5;-18.1;57.5;27.8
 PROJ     = str/90;8;0/nm
 GDPFUN   = quo(p06m,25.4) ! pmsl
 SCALE    = 0 ! 0 ! 0
 TYPE     = f/c ! c ! c
 TITLE    = 0
 LINE	  = 0  ! 32/1/1/1 ! 18/1/1
 MAP      = 30/1/2
 CLRBAR   = 0
 LATLON   = 24/4/1/2/10;10
 TEXT     = small/21//hw
 HILO     = ! 32///4/6;6/y ! 4///4/6;6/y 
 HLSYM    = ! 2.5/2/21//HW ! 1.5/2/21//HW
 save step3
 run

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

 TITLE    = 29/-2/Univ. of Washington Dept. of Atm. Sci.
 TEXT     = 1.3/23//hw
 LINE     = 0
 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
