#!/bin/sh
############################################################
# Script -- cdas2_v1_getpentad.sh.sms
# Purpose: to retrieve the pentad data from CPC workstation
#          and convert it to GRIB format
# History: 2005-02-01 First implementation
############################################################

set -x

cd $DATA
/nwprod/util/ush/setup.sh

#########################################################
# Start retrieving the pentad file (in grads format)from 
# the CPC workstation
#########################################################
date=`echo $PDY | cut -c5-8`
# the pentad file generated by CPC on Jan 01 contains the previous year's data
# so the filename is appended with the previous year. On Jan 06, the script 
# needs to bring over that file. A new file will be generated on the 6th with
# the new year appended.
if [ $date = "0106" ]; then
   yyyy=`echo $PDYm6 |cut -c1-4` 
else
   yyyy=`echo $PDYm5 |cut -c1-4`
fi
remote_file=$REMOTEDIR/cmap_pen_rt_v0011_out.lnx.$yyyy
local_file=pingrain.$yyyy

export account_cpc=${account_cpc:-$LOGNAME}
#############################################################
# Use scp to copy Pingping's pentad file from CPC workstation
#############################################################
scp $account_cpc@$cpc_workstation:$remote_file $local_file

#  end scp instruction input ###
scperror=$?
if [ $scperror -ne 0 ] || [ ! -s $local_file ]
then
  echo "Transfer of file $REMOTEDSN from $1 failed."
  export err=$scperror
  $DATA/err_chk
else
  echo "The pentad data is updated"
fi

###################################
# Convert the file into GRIB format
###################################
export pgm=cdas2_v1_gribpentad

$EXECcdas2/cdas2_v1_gribpentad $local_file pingrain.grb $yyyy

#####################################
# convert data to T62 gaussian grid
#####################################
rm pingrainT62.grb
/nwprod/util/exec/copygb -g98 -i3 -x pingrain.grb pingrainT62.grb

if [ $SENDCOM = "YES" ]
then
   cp pingrainT62.grb $COMOUT/.
   cp pingrainT62.grb $COMOUT/pingrainT62.grb.$yyyy
fi

msg="$job HAS COMPLETED NORMALLY."
echo $msg

postmsg "$jlogfile" "$msg"

############## END OF SCRIPT #######################

