#/bin/sh
################################################################################
#
# cardinal
#
# script to display current counts of N.A. upper air data
#
# F.Achorn/NCO	 08/15/06	Modify script to work in production
# K.Kempisty/NCO 02/23/11	Cleaned up script to write to an operationally
#				supported working directory.
################################################################################
set -eax
envir=prod

# DEFINE THE MACHINE AND ASSOCIATED VARIABLES.
machi=`hostname | cut -c1`
user=`whoami`
# If machi is m, then machine is mist. If machi is d, then machine is dew.
# If machi is c, then machine is cirrus. If machi is s, then machine is stratus.

if [ $# -eq 1 ]
then 
  hour=$1
  if [ "$hour" = "00" -o "$hour" = "06" -o "$hour" = "12" -o "$hour" = "18" ]
  then
    date=`cat /com/date/t${hour}z  | cut -c7-14` 
    echo date=$date
  else  
    echo cardinal only works for 00Z, 06Z, 12Z, and 18Z
    echo you entered ${hour}Z.  Try again.
    exit
  fi

# Script to prompt for CARDINAL parameters

else 
  while [ -z "$date" ]
  do
     echo "Enter date (ccyymmdd) Please note 4 digit year is required:"
     read date
     yc=`echo $date | cut -c1-2`
     if test $yc != "19" -a $yc != "20"
     then
       echo "You entered date $date.  This is incorrect."
       exit
     fi
     echo "Date entered: $date"
     echo
  done
 
  tz='00'
  while [ "$tz" != "00z" -a "$tz" != "06z" -a "$tz" != "12z" -a "$tz" != "18z" ]
  do
     echo "Enter time (00z, 06z, 12z or 18z):"
     read tz
     echo "Time entered: $tz"
     hour=`echo $tz|cut -c 1-2`
     echo
  done
fi


echo "Starting CARDINAL for $date $hour"
 
set -xa

export EXEChrly=/nw${envir}/exec
export PARMhrly=/nw${envir}/parm
export SORChrly=/nw${envir}/scripts

#export DATA=/ptmp/$LOGNAME/cardinal
#export DATA=/gpfs/${machi}/nco/pmb/$LOGNAME/cardinal
if [ $LOGNAME = 'wx12sd' ]; then
   export DATA=/nco/ops/sdm/cardinal
else
   export DATA=/nco/ops/sdm2/cardinal
fi
mkdir -p $DATA
cd $DATA
rm -rf *
export pgmout=allout
sh $SORChrly/excardinal.sh $date $hour
exit
