#!/bin/sh
#

###########################################################################
# SCRIPT: getgfs_6hr.qsub                                                 #
#                                                                         #
# LANGUAGE: C-Shell                                                       #
#                                                                         #
# MACHINE: CCS                                                            #
#                                                                         #
# ACCOUNT:                                                                #
#-------------------------------------------------------------------------#
# INITIATION METHOD: Via Crontab                                          #
#-------------------------------------------------------------------------#
# PURPOSE: Extract specified variables from GFS forecast output, regrid   #
#          the data to 2.5x2.5 degree.                                    #
#-------------------------------------------------------------------------#
# USAGE: N/A (Crontab)                                                    #
#-------------------------------------------------------------------------#
# INPUT: /com/gfs/prod/gfs.$eymd/gfs.t$hz.pgrbf00                         #
#        /com/gfs/prod/gfs.$eymd/gfs.t$hz.sfluxgrbf00                     #
#        /com/gfs/prod/gfs.$eymd/gfs.t$hz.pgrbf$fhp                       #
#   ${fhp}=forecast hour                                                  #
#   ${eymd}=current date                                                  #
#   ${hz}=hour forecast made                                              #
#                                                                         #
# SOURCED FILES: /nwprod/util/exec/ndate                                  #
#                /nwprod/util/exec/wgrib                                  #
#                /nwprod/util/exec/grbindex                               #  
#                /nwprod/util/exec/copygb                                 #
#                                                                         #
###########################################################################

set -x

######################################################
# Evaluate for the current date.
######################################################
   
eymd=$PDY

######################################################
# Start a for-loop for the 4 forecast hours.
######################################################

for hh in 00 06 12 18 ; do
hz=${hh}z
iftp=1

######################################################
# Define directories.
######################################################

ensdir=$COMIN
wgrb=$utilexec/wgrib

######################################################
# Run ndate to get the verifying date in YYYYMMDDHH
# format for forecast hour -120 (Day 5).
######################################################

eymdh=$eymd$hh
dymdh=`$utilexec/ndate -120 $eymdh`

######################################################
# Define additional directories.
######################################################

adir=$DATA
tdir=$adir/$eymdh

# had to be added by NCO
mkdir -p $tdir

gpfsdir=$COMOUT

#####################################################
# Go to the temp directory
#####################################################

cd $tdir

chfile=$ensdir/gfs.t$hz.pgrbf180

#####################################################
# If this data file already exists, delete all files
# in this directory.
#####################################################

if [ -s avn.pgbarch.f000 ] ; then
/bin/rm *
fi

#####################################################
# Define input file and forecast hour as 0 day.
#####################################################

   filegb1=$ensdir/gfs.t$hz.pgrbf

   fhc=00
   fhn=000

#####################################################
# Define input files.
#####################################################

   file=$filegb1$fhc 
   file2=$ensdir/gfs.t$hz.sfluxgrbf$fhc

#####################################################
# Define output files. 
#####################################################

   archfile=avn.pgbarch.f$fhn
   archfile2=avn.flxarch.f$fhn

#####################################################
# Extract the following variables from the input file
# and create the output files "archfile(2)" with each
# variable appended to it.
# Archfile2 has only prate, archfile is rest of vars.
#####################################################

 $wgrb $file | grep ":HGT:kpds5=7:kpds6=100:kpds7=1000:" | \
   $wgrb $file -i -grib -o $tdir/$archfile

 $wgrb $file | grep ":HGT:kpds5=7:kpds6=100:kpds7=700:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":HGT:kpds5=7:kpds6=100:kpds7=500:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":HGT:kpds5=7:kpds6=100:kpds7=200:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":TMP:kpds5=11:kpds6=100:kpds7=850:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":UGRD:kpds5=33:kpds6=100:kpds7=850:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":UGRD:kpds5=33:kpds6=100:kpds7=500:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":UGRD:kpds5=33:kpds6=100:kpds7=200:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":VGRD:kpds5=34:kpds6=100:kpds7=850:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":VGRD:kpds5=34:kpds6=100:kpds7=500:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":VGRD:kpds5=34:kpds6=100:kpds7=200:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":TMP:kpds5=11:kpds6=105:kpds7=2:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":PRES:kpds5=1:kpds6=1:kpds7=0:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":PRMSL:kpds5=2:kpds6=102:kpds7=0:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file2 | grep ":PRATE:kpds5=59:kpds6=1:kpds7=0:" | \
   $wgrb $file2 -i -grib -o $tdir/$archfile2

#####################################################
# Create a temp file and regrid it to 2.5x2.5 degrees
# to match climotology for each output file.
#####################################################

  mv avn.pgbarch.f$fhn avn.pgbtest.f$fhn
  arfile=avn.pgbtest.f$fhn
  arfile2=avn.pgbintp.f$fhn
  $utilexec/grbindex $arfile $arfile.index
  $utilexec/copygb -g2 $arfile $arfile.index $arfile2

  arfile=avn.flxarch.f$fhn
  arfile2=avn.flxinpt.f$fhn
  $utilexec/grbindex $arfile $arfile.index
  $utilexec/copygb -g2 $arfile $arfile.index $arfile2

#####################################################
# Condense the two output files into one.
#####################################################

 cat avn.pgbintp.f$fhn avn.flxinpt.f$fhn > avn.pgbarch.f$fhn

 mv avn.pgbarch.f$fhn avn.pgbarch.f$fhn.t$hz

#####################################################
# Start and end forecast hours for day 1 - day 7.5 
#####################################################

efhs=12
efhe=192

#####################################################
# Begin while-loop which goes from day 1 to day 7.5,
# using a 12-hour interval.
#####################################################
   
fhr=$efhs

while [ $fhr -le "$efhe" ] ; do

   fhc=$fhr
   fhn=$fhr
   fhx=`expr $fhr - 6`

#####################################################
# Add zeros if the hour value is less than 100 or 10.
#####################################################

   if [ $fhr -lt 100 ] ; then
   fhn=0$fhr
   fi

   if [ $fhr -lt 10 ] ; then
   fhc=0$fhr
   fhn=00$fhr
   fi

   fhp=$fhx

   if [ $fhx -lt 10 ] ; then
   fhp=0$fhx
   fi

#####################################################
# Define input files
##################################################### 
  
   file=$filegb1$fhc
   file3=$filegb1$fhp 

#####################################################
# Define output files
#####################################################

   archfile=avn.pgbarch.f$fhn

   archfile2=avn.flxarch.f$fhn
   archfile3=avn.flxarch.f$fhp

#####################################################
# Extract the following variables from the input file
# and create the output files "archfile(2,3)" with each
# variable appended to it.
#####################################################

 $wgrb $file | grep ":HGT:kpds5=7:kpds6=100:kpds7=1000:" | \
   $wgrb $file -i -grib -o $tdir/$archfile

 $wgrb $file | grep ":HGT:kpds5=7:kpds6=100:kpds7=700:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":HGT:kpds5=7:kpds6=100:kpds7=500:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":HGT:kpds5=7:kpds6=100:kpds7=200:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":TMP:kpds5=11:kpds6=100:kpds7=850:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":UGRD:kpds5=33:kpds6=100:kpds7=850:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":UGRD:kpds5=33:kpds6=100:kpds7=500:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":UGRD:kpds5=33:kpds6=100:kpds7=200:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":VGRD:kpds5=34:kpds6=100:kpds7=850:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":VGRD:kpds5=34:kpds6=100:kpds7=500:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":VGRD:kpds5=34:kpds6=100:kpds7=200:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":PRES:kpds5=1:kpds6=1:kpds7=0:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":PRMSL:kpds5=2:kpds6=102:kpds7=0:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":TMP:kpds5=11:kpds6=105:kpds7=2:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":PRATE:kpds5=59:kpds6=1:kpds7=0:" | \
   $wgrb $file -i -grib -o $tdir/$archfile2

 $wgrb $file3 | grep ":PRATE:kpds5=59:kpds6=1:kpds7=0:" | \
   $wgrb $file3 -i -grib -o $tdir/$archfile3

#####################################################
# Create a temp file and regrid it to 2.5x2.5 degrees
# to match climotology for each output file.
#####################################################

  mv avn.pgbarch.f$fhn avn.pgbtest.f$fhn
  arfile=avn.pgbtest.f$fhn
  arfile2=avn.pgbintp.f$fhn
  $utilexec/grbindex $arfile $arfile.index
  $utilexec/copygb -g2 $arfile $arfile.index $arfile2

  arfile=avn.flxarch.f$fhn
  arfile2=avn.flxintp.f$fhn
  $utilexec/grbindex $arfile $arfile.index
  $utilexec/copygb -g2 $arfile $arfile.index $arfile2

  arfile=avn.flxarch.f$fhp
  arfile2=avn.flxintp.f$fhp
  $utilexec/grbindex $arfile $arfile.index
  $utilexec/copygb -g2 $arfile $arfile.index $arfile2

 mfile1=$tdir/avn.flxintp.f$fhp
 mfile2=$tdir/avn.flxintp.f$fhn

#####################################################
# Condense the remaining two output files into one.
#####################################################

 cat avn.pgbintp.f$fhn avn.flxarch.f$fhn > avn.pgbarch.f$fhn

 mv avn.pgbarch.f$fhn avn.pgbarch.f$fhn.t$hz

#####################################################
# Go to the next 12-hour forecast period and end
# while-loop for days 1 - 7.5
#####################################################

 fhr=`expr $fhr + 12`

done

#####################################################
# Start and end forecast hours for day 8 - day 15 
#####################################################

efhs=204
efhe=360

#####################################################
# Begin while-loop which goes from day 8 to day 15,
# using a 12-hour interval.
#####################################################
   
fhr=$efhs

while [ $fhr -le "$efhe" ] ; do

   fhc=$fhr
   fhn=$fhr

#####################################################
# Define input and output file.
#####################################################

   file=$filegb1$fhc

   archfile=avn.pgbarch.f$fhn
   
#####################################################
# Extract the following variables from the input file
# and create an output file "archfile" with each
# variable appended to it.
#####################################################

 $wgrb $file | grep ":HGT:kpds5=7:kpds6=100:kpds7=1000:" | \
   $wgrb $file -i -grib -o $tdir/$archfile

 $wgrb $file | grep ":HGT:kpds5=7:kpds6=100:kpds7=700:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":HGT:kpds5=7:kpds6=100:kpds7=500:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":HGT:kpds5=7:kpds6=100:kpds7=200:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":TMP:kpds5=11:kpds6=100:kpds7=850:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":UGRD:kpds5=33:kpds6=100:kpds7=850:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":UGRD:kpds5=33:kpds6=100:kpds7=500:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":UGRD:kpds5=33:kpds6=100:kpds7=200:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":VGRD:kpds5=34:kpds6=100:kpds7=850:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":VGRD:kpds5=34:kpds6=100:kpds7=500:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":VGRD:kpds5=34:kpds6=100:kpds7=200:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":PRES:kpds5=1:kpds6=1:kpds7=0:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":PRMSL:kpds5=2:kpds6=102:kpds7=0:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":TMP:kpds5=11:kpds6=105:kpds7=2:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 $wgrb $file | grep ":PRATE:kpds5=59:kpds6=1:kpds7=0:" | \
   $wgrb $file -i -grib -append -o $tdir/$archfile

 mv avn.pgbarch.f$fhn avn.pgbarch.f$fhn.t$hz

#####################################################
# Go to the next 12-hour forecast period and end
# while-loop for days 8 to 15. 
#####################################################

 fhr=`expr $fhr + 12`

done

#####################################################
# If gpfs directory doesn't exist, make it, and then
# copy the new model output files to this directory.
#####################################################

if [ ! -d $gpfsdir ] ; then
mkdir -p $gpfsdir
fi

if [ "$SENDCOM" = 'YES' ] ; then
cp -p avn.pgbarch.f* $gpfsdir
fi

#####################################################
# End for-loop for the 4 forecast hours
#####################################################

done

#####################################################
# Exit the program.
#####################################################

exit
