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

foreach initfcst ("-168,0" "-162,0" "-156,0" "-150,0" "-144,0" "-138,0" "-132,0" "-126,0" "-120,0" "-114,0" "-108,0" "-102,0" "-96,0" "-90,0" "-84,0" "-78,0" "-72,0" "-66,0" "-60,0" "-54,0" "-48,0" "-42,0" "-36,0" "-30,0" "-24,0" "-18,0" "-12,0" "-6,0" "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" ) 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..."
   gdplot2_gf << EOF > /dev/null
 GDFILE   = $MODEL/$gemfile
 GDATTIM  = f$fcsthr
 DEVICE   = gf|${initdate00}_$fcsthr$extra.gif|800;800
 GLEVEL   = 500
 GVCORD   = pres
 SKIP     = 0
 CONTUR   = 0
 LINE     = 32/1/2/1
 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     = 
 SCALE    = 0
 FINT     = -45;-40;-35;-30;-25;-20;-15;-10;-5;0;5;10;15;20;25;30;35;40;45;50;55;60
 FLINE    = 0;1-25;0
 PANEL    = 0
# need to do this so bogus plot is white
 GDPFUN   = add(mul(vwnd,0),-50)
 TYPE     = f
 LATLON   = 0
 CLRBAR   = 32/v/ll/.91;.1/.75;.03
 GAREA    = 0;-175;5;-170
 PROJ     = mer
 MAP      = 0
 TITLE    = 32/1/$fcsthr Hr Fcst @ Meridional Wind and Westerly Jets (m/s) valid $longfcstdate
 TEXT     = 1.3/23//hw
 save step1
 run
 CLEAR    = no
 TITLE    = 32/3/(initialized $longinitdate)
 TEXT     = medium/23//hw
 TYPE     = c
 LINE     = 0
 CLRBAR   =
 save step2
 run
#
 CINT     = 0 ! 5/20
 PANEL    = 0;0;.9;.95/0
 CLEAR    = no
 GAREA    = -12;-165;33;2
 PROJ     = lea/65;-120;0
 GDPFUN   = vwnd ! uwnd
 SCALE    = 0 ! 0
 TYPE     = f ! c
 TITLE    = 29/-2/Univ. of Washington Dept. of Atm. Sci.
 line     = 32/1/1/0
 MAP      = 26/1/2
 CLRBAR   = 0
 LATLON   = 27/3/1/0;1/15;15
 TEXT     = 1.3/23//hw
 save step3
 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
