#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exdgex_prelim.sh.sms
# Script description:  Runs WRF standard initialization (SI) for the WRF-NMM DGEX 
#
# Author:        Eric Rogers       Org: NP22         Date: 2006-04-21
#

set -x

msg="JOB $job FOR REGION=$REGION HAS BEGUN"
postmsg "$jlogfile" "$msg"

mod=ETA

export CYCLE=$PDY$cyc
echo "export REGION=$REGION" >> $COMOUT/${RUN}_${REGION}.t${cyc}z.envir.sh
echo "export CYCLE=$CYCLE" >> $COMOUT/${RUN}_${REGION}.t${cyc}z.envir.sh

if [ "$SENDSMS" = "YES" ] ; then
  $SMSBIN/smslabel NEST "$REGION"
fi

LENGTH=03
MODEL=nmm

export PS4='DGEX_PRELIM_T$SECONDS + '

DATAROOT=$DATA
chmod 755 $DATAROOT

cd $DATAROOT
                                                                                
PERINSTALLROOT=/nw${envir}
INSTALLROOT=/nw${envir}
DATAROOT_GRIB=$DATA/wrfsi
mkdir -p $DATA/wrfsi
mkdir -p $DATAROOT_GRIB/extdata
mkdir -p $DATAROOT_GRIB/log
mkdir -p $DATAROOT_GRIB/GEOG
mkdir -p $DATAROOT_GRIB/extprd
mkdir -p $DATAROOT_GRIB/static
mkdir -p $DATAROOT_GRIB/work
PEREROOT=/nw${envir}

mkdir -p cdl
mkdir -p static

cp $PARMdgex/dgex_${REGION}.wrfsi.rotlat.cdl $DATAROOT/cdl/wrfsi.rotlat.cdl
cp $FIXdgex/dgex_${REGION}_static.wrfsi.rotlat_nonetcdf_mod $DATAROOT/static/static.wrfsi.rotlat_nonetcdf
cp $PARMdgex/dgex_wrf.nl $DATAROOT/static/wrf.nl

cp /com/date/t${cyc}z .

CYCer=`cat t${cyc}z | cut -c 7-16`
CYCstart=`/nwprod/util/exec/ndate -0 $CYCer`
CYCdgex=`/nwprod/util/exec/ndate 78 $CYCer`

echo DATEXX${CYCstart} > nmcdate
echo DATEXX${CYCdgex} > nmcdate.dgex

echo "export CYCLEdgex=`cat nmcdate.dgex | cut -c7-16`" >> $COMOUT/${RUN}_${REGION}.t${cyc}z.envir.sh
echo "export CYCLEnam=`cat nmcdate | cut -c7-16`" >> $COMOUT/${RUN}_${REGION}.t${cyc}z.envir.sh

#
# get sigma files from GFS for boundary conditions
#

$USHdgex/dgex_getgfsbc.sh $CYCstart 192

hr=078
                                                                                                        
while [ $hr -le 192 ] ; do
                                                                                                        
if [ ! -s gfsbc${hr}_ops ] ; then
                                                                                                        
# WRF-NMM boundary condition makes needs 3-hourly coefficient files, so if 
# one doesn't exist, make one using global_sigavg
                                                                                                        
let "hrm3=hr-3"
typeset -Z3 hrm3
let "hrp3=hr+3"
typeset -Z3 hrp3
                                                                                                        
/nwprod/exec/global_sigavg -w 0.5,0.5 gfsbc${hrm3}_ops gfsbc${hrp3}_ops gfsbc${hr}_ops
                                                                                                        
fi
                                                                                                        
let "hr=hr+3"
typeset -Z3 hr
                                                                                                        
done
                                                                                                        
icnt=0
hr=078
 
while [ $hr -le 192 ] ; do
  mv gfsbc${hr}_ops gfsbc${icnt}
  let "hr=hr+3"
  typeset -Z3 hr
  let "icnt=icnt+1"
done

#
# Release the mkbnd jobs on SMS
#
mkdir -p $DATA/mkbnd

$SMSBIN/setev release_mkbnd

PDY=`cat nmcdate | cut -c7-14`
yy=`cat nmcdate | cut -c7-10`
mm=`cat nmcdate | cut -c11-12`
dd=`cat nmcdate | cut -c13-14`
cyc=`cat nmcdate | cut -c15-16`

PDYdgex=`cat nmcdate.dgex | cut -c7-14`
yydgex=`cat nmcdate.dgex | cut -c7-10`
mmdgex=`cat nmcdate.dgex | cut -c11-12`
dddgex=`cat nmcdate.dgex | cut -c13-14`
cycdgex=`cat nmcdate.dgex | cut -c15-16`

chmod 755 envir.sh

whr=78
count=0
last=81
incr=3

PDY=`cat nmcdate | cut -c7-14`
yy=`cat nmcdate| cut -c7-10`
mm=`cat nmcdate | cut -c11-12`
dd=`cat nmcdate | cut -c13-14`
cyc=`cat nmcdate | cut -c15-16`

while [ $whr -le $last ]
do

mkdir -p $DATA/wrfsibeta/eta
RAWDIR=$DATA/wrfsibeta/eta

# get 78, 81-h NAM GRIB files

if [ $REGION = conus ]
then
  if [ -s /com/nam/${envir}/nam.${PDY}/nam.t${cyc}z.awip32${whr}.tm00 ] 
  then
     cp /com/nam/${envir}/nam.${PDY}/nam.t${cyc}z.awip32${whr}.tm00 $DATA/wrfsibeta/eta/.
  else
     err_exit "NAM GRIB file not found.  FATAL ERROR"
  fi
else
  if [ -s /com/nam/${envir}/nam.${PDY}/nam.t${cyc}z.awipak${whr}.tm00 ] 
  then 
     cp /com/nam/${envir}/nam.${PDY}/nam.t${cyc}z.awipak${whr}.tm00 $DATA/wrfsibeta/eta/.
  else
     err_exit "NAM GRIB file not found.  FATAL ERROR"
  fi
fi

whr=`expr $whr + $incr`

if [ $whr -lt 10 ]
then
whr=0$whr
fi

done

DATE=`cat nmcdate.dgex | cut -c7-14`

pwd

$USHdgex/dgex_wrfnmm_grib_prep.pl -i $INSTALLROOT -d $DATAROOT_GRIB -r $RAWDIR -l $LENGTH -s $CYCdgex -t 3 $mod

echo "creating SI jobs to submit"

#
# Release the WRF-SI jobs on SMS
#

$SMSBIN/setev release_wrfsi

ic=0

icnttest=1
itestbrk=2

while [ $ic -lt 1000 ] ; do

 icntb=1
 itest=1
 while [ $icntb -le $icnttest ] ; do
  ls -l $DATA/wrfsidone.$icntb
  err1=$?
  if [ $err1 -eq 0 ] ; then
   let "itest=itest+1"
  fi
  let "icntb=icntb+1"
 done
 
 if [ $itest -eq $itestbrk ] ; then
   break
 else
   let "ic=ic+1"
   sleep 10
 fi
 
 if [ $ic -ge 1000 ] ; then
  exit 1
 fi
 
done

year=`echo $DATE | cut -c1-4`
count=`ls $DATAROOT/real* | wc -l`

echo INITIAL count is $count

countck=3

while [ $count -lt $countck ]
do
sleep 20
count=`ls $DATAROOT/real* | wc -l`
echo $count now
done

## bonus sleep to make sure file is completely copied to the siprd dir
sleep 15

cat $DATA/*hinterp > $COMOUT/dgex_${REGION}.t${CYCstart}z.hinterp.log
cat $DATA/*vinterp > $COMOUT/dgex_${REGION}.t${CYCstart}z.vinterp.log
cat $DATA/*wrfprep > $COMOUT/dgex_${REGION}.t${CYCstart}z.wrfprep.log

exit
