#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exhiresw_prelim.sh.sms
# Script description:  Runs WRF Preprocessing System (WPS) and REAL code for the WRF-NMM and 
#                      WRF-ARW model
#
# Author:        Eric Rogers       Org: NP22         Date: 2004-07-02
#
# Abstract: The scripts gets all the input files needed for the WRF-NMM and WRF-ARW hiresw run
#           and runs the WRF WPS and REAL codes which interpolate the operational Nam initial conditions
#           to the nested WRF-NMM domain and create lateral boundary conditions
#
# Script history log:
# 2003-11-01  Matt Pyle - Original script for parallel
# 2004-07-02  Eric Rogers - Preliminary modifications for production.
# 2004-10-01  Eric Rogers - Modified to run special real executable for Alaska NMM
# 2007-04-09  Matthew Pyle - Modified to run WPS rather than wrfsi
# 2009-09-24  Shawna Cokley - Streamlines way script obtains date information -
#                             pulls from $PDY rather than copying a file to the working directory

set -x

LENGTH=48

### NEST options are east, west, ak, hi, pr
### MODEL is arw or nmm

msg="JOB $job FOR WRF NEST=${NEST}${MODEL}_${MEMBER} HAS BEGUN"
postmsg "$jlogfile" "$msg"

yy=`echo $PDY | cut -c1-4`
mm=`echo $PDY | cut -c5-6`
dd=`echo $PDY | cut -c7-8`

ystart=`echo $PDY | cut -c1-4`
mstart=`echo $PDY | cut -c5-6`
dstart=`echo $PDY | cut -c7-8`

start=$ystart$mstart$dstart$cyc

end=`/nwprod/util/exec/ndate 48 $start`

yend=`echo $end | cut -c1-4`
mend=`echo $end | cut -c5-6`
dend=`echo $end | cut -c7-8`

whr=00
count=0
last=48
incr=3

#
# Check to see how many storms are being run by the GFDL hurricane model.
# If number is >= 2, do not make any hiresw runs.
# If number is 1, cancel job if this is a WRF-EM run.
#
#if [ "$NEST" != "pr" -a "$NEST" != "hi" -a "$NEST" != "guam" ] ; then
  $USHhiresw/hiresw_chkhur.sh $MODEL
  err=$?
  if [ $err -eq 99 ] ; then
    exit
  fi
  echo "export NEST=$NEST" > $COMOUT/${RUN}.t${cyc}z.envir.sh
  echo "export MODEL=$MODEL" >> $COMOUT/${RUN}.t${cyc}z.envir.sh
#fi

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

# Puerto Rico : Use grid #221 (awip32)
# Hawaii : Use grid #243 (awiphi)
# East & West : Use grid #212 (awip3d)
# Alaska & Guam : Use GFS (1 degree pgrb)

mkdir -p $DATA/run_ungrib

cp $PARMhiresw/hiresw_Vtable.NAM  $DATA/run_ungrib/Vtable 

  cat $PARMhiresw/hiresw_${NEST}_${MODEL}.namelist.wps_in | sed s:YSTART:$ystart: | sed s:MSTART:$mstart: \
 | sed s:DSTART:$dstart: | sed s:HSTART:$cyc: | sed s:YEND:$yend: \
 | sed s:MEND:$mend:     | sed s:DEND:$dend: | sed s:HEND:$cyc: > $DATA/run_ungrib/namelist.wps


while [ $whr -le $last ]
do

if [ $NEST = "pr" ] 
then 
  cp ${COMNAM}/nam.t${cyc}z.awip32${whr}.tm00 $DATA/run_ungrib/.
  err1=$?
  mod=nam
  type=awip32
  suf=.tm00
elif [ $NEST = "hi" ]
then
  cp ${COMNAM}/nam.t${cyc}z.awiphi${whr}.tm00 $DATA/run_ungrib/.
  err1=$?
  mod=nam
  type=awiphi
  suf=.tm00
elif [ $NEST = "ak" -o $NEST = "guam" ]
then
  cp ${COMGFS}/gfs.t${cyc}z.pgrbf${whr} $DATA/run_ungrib/.
  err1=$?
  cp $PARMhiresw/hiresw_Vtable.GFS  $DATA/run_ungrib/Vtable 
  mod=gfs
  type=pgrbf
  suf=""
else
  cp ${COMNAM}/nam.t${cyc}z.awip3d${whr}.tm00 $DATA/run_ungrib/.
  err1=$?
  mod=nam
  type=awip3d
  suf=.tm00
fi

if [ $err1 -ne 0 ] 
then
  err_exit "$whr input GRIB file from Nam not found.  FATAL ERROR"
fi

whr=`expr $whr + $incr`

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

done

cd $DATA/run_ungrib

mv ${mod}.t${cyc}z.${type}00${suf} GRIBFILE.AAA
mv ${mod}.t${cyc}z.${type}03${suf} GRIBFILE.AAB
mv ${mod}.t${cyc}z.${type}06${suf} GRIBFILE.AAC
mv ${mod}.t${cyc}z.${type}09${suf} GRIBFILE.AAD
mv ${mod}.t${cyc}z.${type}12${suf} GRIBFILE.AAE
mv ${mod}.t${cyc}z.${type}15${suf} GRIBFILE.AAF
mv ${mod}.t${cyc}z.${type}18${suf} GRIBFILE.AAG
mv ${mod}.t${cyc}z.${type}21${suf} GRIBFILE.AAH
mv ${mod}.t${cyc}z.${type}24${suf} GRIBFILE.AAI
mv ${mod}.t${cyc}z.${type}27${suf} GRIBFILE.AAJ
mv ${mod}.t${cyc}z.${type}30${suf} GRIBFILE.AAK
mv ${mod}.t${cyc}z.${type}33${suf} GRIBFILE.AAL
mv ${mod}.t${cyc}z.${type}36${suf} GRIBFILE.AAM
mv ${mod}.t${cyc}z.${type}39${suf} GRIBFILE.AAN
mv ${mod}.t${cyc}z.${type}42${suf} GRIBFILE.AAO
mv ${mod}.t${cyc}z.${type}45${suf} GRIBFILE.AAP
mv ${mod}.t${cyc}z.${type}48${suf} GRIBFILE.AAQ

pwd

### run_ungrib

cd $DATA/run_ungrib/
cp $EXEChiresw/hiresw_ungrib ungrib.exe

./ungrib.exe >> $pgmout 2>errfile

#########################################################
# RUN MULTIPLE METGRID STEPS USING POE
#########################################################

# Create a script to be poe'd
cd $DATA
rm -rf poescript

echo "$USHhiresw/hiresw_wps_metgrid_gen.sh $NEST $cyc 00 03 1 $MODEL" > poescript
echo "$USHhiresw/hiresw_wps_metgrid_gen.sh $NEST $cyc 06 09 2 $MODEL" >> poescript
echo "$USHhiresw/hiresw_wps_metgrid_gen.sh $NEST $cyc 12 15 3 $MODEL" >> poescript
echo "$USHhiresw/hiresw_wps_metgrid_gen.sh $NEST $cyc 18 21 4 $MODEL" >> poescript
echo "$USHhiresw/hiresw_wps_metgrid_gen.sh $NEST $cyc 24 27 5 $MODEL" >> poescript
echo "$USHhiresw/hiresw_wps_metgrid_gen.sh $NEST $cyc 30 33 6 $MODEL" >> poescript
echo "$USHhiresw/hiresw_wps_metgrid_gen.sh $NEST $cyc 36 39 7 $MODEL" >> poescript
echo "$USHhiresw/hiresw_wps_metgrid_gen.sh $NEST $cyc 42 45 8 $MODEL" >> poescript
echo "$USHhiresw/hiresw_wps_metgrid_gen.sh $NEST $cyc 48 48 9 $MODEL" >> poescript

 
chmod 775 $DATA/poescript
export MP_PGMMODEL=mpmd
export MP_CMDFILE=poescript
export MP_LABELIO=YES
export MP_INFOLEVEL=3
export MP_STDOUTMODE=ordered

poe

# wait for all 9 WPS codes to finish to proceed

ic=0

while [ $ic -lt 1000 ] ; do

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

#### this block seems redundant with the checks on wpsdone above

count=`ls $DATA/met*.int | wc -l`

echo INITIAL count is $count

while [ $count -lt 17 ]
do
sleep 20
count=`ls $DATA/met*.int | wc -l`
echo $count now
done

## bonus sleep to make sure file is completely copied to the $DATA dir
sleep 5

cat $DATA/metgrid.log.0000_* > $COMOUT/${NEST}${MODEL}.t${cyc}z.metgrid.log

#########################################################
# RUN REAL PROGRAM TO GENERATE WRFINPUT/WRFBDY FILES
#########################################################

cd $DATA

cycstart=`echo ${PDY}${cyc}`

start=$ystart$mstart$dstart

end=`/nwprod/util/exec/ndate $LENGTH $cycstart`

yend=`echo $end | cut -c1-4`
mend=`echo $end | cut -c5-6`
dend=`echo $end | cut -c7-8`
hend=`echo $end | cut -c9-10`

## for large domains, special namelist.input files are required for the model
## for small domains, use the same namelist for both real and the model

if [ $NEST = "hi" -o $NEST = "guam" ] ; then
  cp $PARMhiresw/hiresw_${MODEL}_namelist.input_in_small namelist.input_in
  cp $PARMhiresw/hiresw_${MODEL}_namelist.input_in_small namelist.input_in_model
elif [ $NEST = "pr" ] ; then
  cp $PARMhiresw/hiresw_${MODEL}_namelist.input_in_newpr namelist.input_in
  cp $PARMhiresw/hiresw_${MODEL}_namelist.input_in_newpr namelist.input_in_model
else
  cp $PARMhiresw/hiresw_${MODEL}_namelist.input_in namelist.input_in
  cp $PARMhiresw/hiresw_${MODEL}_namelist.input_in_model namelist.input_in_model
fi


### number of input levels depends on source model data

if [ $NEST = "hi" -o $NEST = "east" -o $NEST = "west" ] ; then
NUMLEVS=40
elif [ $NEST = "pr" ] ; then
NUMLEVS=43
elif [ $NEST = "ak" -o $NEST = "guam" ] ; then
NUMLEVS=27
fi


cat namelist.input_in | sed s:YSTART:$ystart: | sed s:MSTART:$mstart: \
 | sed s:DSTART:$dstart: | sed s:HSTART:$cyc: | sed s:YEND:$yend: \
 | sed s:MEND:$mend:     | sed s:DEND:$dend: | sed s:HEND:$hend:  \
 | sed s:NUMLEV:$NUMLEVS: > namelist.input

cat namelist.input_in_model | sed s:YSTART:$ystart: | sed s:MSTART:$mstart: \
 | sed s:DSTART:$dstart: | sed s:HSTART:$cyc: | sed s:YEND:$yend: \
 | sed s:MEND:$mend:     | sed s:DEND:$dend: | sed s:HEND:$hend:  \
 | sed s:NUMLEV:$NUMLEVS: > namelist.input_model

rm fort.*

cp $PARMhiresw/hiresw_LANDUSE.TBL LANDUSE.TBL
cp $PARMhiresw/hiresw_ETAMPNEW_DATA ETAMPNEW_DATA
cp $PARMhiresw/hiresw_ETAMPNEW_DATA micro_lookup.dat
cp $PARMhiresw/hiresw_VEGPARM.TBL VEGPARM.TBL
cp $PARMhiresw/hiresw_SOILPARM.TBL SOILPARM.TBL
cp $PARMhiresw/hiresw_GENPARM.TBL GENPARM.TBL
cp $FIXhiresw/hiresw_nmm_co2.35_hybrid_50mbtop co2.35_hybrid_50mbtop
cp $FIXhiresw/hiresw_nmm_co2.35_hybrid_50mbtop co2_trans

cp $FIXhiresw/hiresw_RRTM_DATA RRTM_DATA
cp $FIXhiresw/hiresw_tr49t67 tr49t67
cp $FIXhiresw/hiresw_tr49t85 tr49t85
cp $FIXhiresw/hiresw_tr67t85 tr67t85

export pgm=hiresw_${MODEL}_real
. prep_step

if [ $MODEL = nmm ] ; then
  export XLFUNIT_14=co2.35_hybrid_50mbtop
fi

startmsg

$EXEChiresw/hiresw_${MODEL}_real >> $pgmout 2>errfile
export err=$?
if [ $err -ne 0 ]; then
   mv $DATA $COMOUT/.
   err_chk
fi

# Copy 3 files needed to run WRF forecast to COM

cp wrfbdy_d01 $COMOUT/${NEST}${MODEL}.t${cyc}z.wrfbdy_d01
cp wrfinput_d01 $COMOUT/${NEST}${MODEL}.t${cyc}z.wrfinput_d01
cp namelist.input_model $COMOUT/${NEST}${MODEL}.t${cyc}z.namelist.input

if [ ! -f $COMOUT/${NEST}${MODEL}.t${cyc}z.wrfbdy_d01 ] || [ ! -f $COMOUT/${NEST}${MODEL}.t${cyc}z.wrfinput_d01 ]; then
   mv $DATA $COMOUT/.
   err_exit "WRF initial or boundary condition files missing"
fi
 
# Cat REAL log files and send to COM

cat $DATA/TASKOUTPUT/0000/rsl.error.0000 $DATA/TASKOUTPUT/0000/rsl.out.0000 >  $COMOUT/${NEST}${MODEL}.t${cyc}z.real.log

msg="JOB $job FOR WRF NEST=${NEST}${MODEL}_${MEMBER} HAS COMPLETED NORMALLY"
postmsg "$jlogfile" "$msg"
