#!/bin/sh
#

###########################################################################
# SCRIPT: excpc_get_gfs_ensc.sh.sms                                       #
#                                                                         #
# LANGUAGE: C-Shell                                                       #
#                                                                         #
# MACHINE: CCS                                                            #
#                                                                         #
# ACCOUNT:                                                                #
#-------------------------------------------------------------------------#
# INITIATION METHOD: Via Crontab                                          #
#-------------------------------------------------------------------------#
# PURPOSE: Extract specified variables from the ensemble control run      #
#          output, regrid the data to 2.5x2.5 degree.                     #
#-------------------------------------------------------------------------#
# USAGE: N/A (Crontab)                                                    #
#-------------------------------------------------------------------------#
# INPUT: /com/gens/prod/gefs.${eymd}/00/pgrb2a/gec00.t00z.pgrbaf${fh}     #
#        /com/gens/prod/gefs.${eymd}/00/pgrb2b/gec00.t00z.pgrbbf${fh}     #
#   ${fh}=forecast hour                                                   #
#   ${eymd}=current date                                                  #
#                                                                         #
# SOURCED FILES: /nwprod/util/exec/ndate                                  #
#                /nwprod/util/exec/wgrib                                  #
#                /nwprod/util/exec/grbindex                               #
#                /nwprod/util/exec/copygb                                 #
#                /nwprod/util/exec/cnvgrib                                #
#                                                                         #
###########################################################################

set -x

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

######################################################
# Define directories which hold the data.
# Model data is split into 2 files pgrb2a and pgrb2b.
######################################################

ensdir=$COMIN
ensdir2=$COMIN2

wgrb=$utilexec/wgrib
wgrb2=$utilexec/wgrib2

######################################################
# Define forecast hour as 00Z.
# Run ndate to get the verifying date in YYYYMMDDHH
# format for forecast hour -120 (5day).
######################################################
eymdh=${eymd}00
dymdh=`$utilexec/ndate -120 $eymdh`
nfhres=980

gpfsdir=$COMOUT

#####################################################
# Define temp and output directories
#####################################################

tdir=$DATA/$eymdh
dldir=$DATA/$dymdh

mkdir -p $tdir
mkdir -p $dldir

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

cd $tdir

chfile=$ensdir/gec00.t00z.pgrb2af360

######################################################
# Define start and end forecast hours for week 2
######################################################

efhs=0
efhe=360
   
#####################################################
# Begin for-loop for control run and define input
# files.
#####################################################

for mid in c00 ; do

       filegb1=$ensdir/ge$mid.t00z.pgrb2af
       filegb2=$ensdir2/ge$mid.t00z.pgrb2bf

#####################################################
# Begin while-loop which goes from forecast start to
# forecast end, using a 12-hour interval.
#####################################################

fhr=$efhs

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

   fhc=$fhr
   fhn=$fhr

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

#####################################################
# Rename input files, adding forecast hour.
#####################################################

   filea=$filegb1$fhc
   fileb=$filegb2$fhc

#####################################################
# Extract the following variables from the input file
# and create an output file "archfile" with each
# variable appended to it.
#####################################################

   archfile=ensc.pgbarch.f$fhn

 > $tdir/$archfile

 $wgrb2 $filea -append \
     -if ":(HGT|TMP|UGRD|VGRD):(1000|850|700|500|200) mb:" -grib $tdir/$archfile \
     -if ":PRMSL:" -grib $tdir/$archfile \
     -if ":(LHTFL):(surface):" -grib $tdir/$archfile \
     -if ":(SOILM):(0-2 m below ground):" -grib $tdir/$archfile \
     -if ":(TMP):(2 m above ground):" -grib $tdir/$archfile \
     -if ":(UGRD|VGRD):(10 m above ground):" -grib $tdir/$archfile \
     -if ":(ULWRF):(top of atmosphere):" -grib $tdir/$archfile

 $wgrb2 $fileb -append \
     -if ":(PRATE):(surface):" -grib $tdir/$archfile \
     -if ":(VVEL):(500) mb:" -grib $tdir/$archfile

#####################################################
# Convert grib2 data to grib1
#####################################################

ffile=ensc.pgbtemp.f$fhn

 $utilexec/cnvgrib -g21 -nv $archfile $ffile

#####################################################
# If the forecast hour is less than $nfhres (980), 
# create a temp file and regrid it to 2.5x2.5 degrees
# to match climotology.
#####################################################

  if [ $fhr -le "$nfhres" ] ; then
  arfile=$ffile
  arfile2=ensc.pgbarch.f$fhn
  $utilexec/grbindex $arfile $arfile.index
  $utilexec/copygb -g2 $arfile $arfile.index $arfile2
  fi

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

 fhr=`expr $fhr + 12`

done

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 ensc.pgbarch.f* $gpfsdir
fi

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

exit
