#!/bin/sh

set -x
#################################################################
#  Data preparation for Eta ensemble run
#  Programmer: Jun Du
#  Date: 03/20/2001
#        04/12/2002, J. Du, add KF Eta ensemble run
#        10/16/2003, J. Du, (1) changed resolution from 48km 
#                    to 32km; (2) added physics diversity members 
#                    and combined KF runs into generic Eta members;
#                    (3) separated global ensemble pair numbers 
#                    from regional ensemble pair numbers to allow 
#                    using different pair numbers from global and 
#                    regional ensemble systems; (4) save etabcs00
#                    at /nwges directory for future rerun purpose.
#        06/15/2005, J. Du, use 4 pairs of global ensemble members 
#                    as LBCs instead of previously 2 pairs
#        03/22/2006, J. Du, add 03z and 15z two off-time cycles
#        06/25/2006  HUI-YA CHUANG, modified to run 32 km ETA MOS
#
#  Part I:   (22 processors) data fetching
#            ( 1 processor ) truncation of 12km ndas
#                            to 32km ndas
#  Part II:  ( 5 processors) converting global data
#                            into Eta format
#                            and creating LBCs
#  Part III: ( 1 processor ) creating perturbed initial
#                            conditions with regional 
#                            breeding for both BMJ and KF 
#                            versions of Eta
#################################################################

#check if it's a cold start or continuous cycling for breeding?
if [ $cyc -eq 00 ]
then
 export hiscyc=t18z
fi
if [ $cyc -eq 06 ]
then
 export hiscyc=t00z
fi
if [ $cyc -eq 12 ]
then
 export hiscyc=t06z
fi
if [ $cyc -eq 18 ]
then
 export hiscyc=t12z
fi

if [ -s $HISDATA/restrt06.$hiscyc.ctl ]
then
   export ETA_TYPE=CONT
else
   export ETA_TYPE=INIT
fi


################################################
#
# PART I: Fetching data
#         Truncation of 12km ndas to 32km ndas
#
################################################
set +x
echo "    "
echo " ***********************************************************"
echo " Begin fetching data and truncation of ndas from 12 to 32km."
echo " ***********************************************************"
echo "    "
set -x

msg="Begin fetching data and truncation of ndas from 12 to 32km."
postmsg "$jlogfile" "$msg"

rm -rf poescript

############################
# Fetching of Global Data
############################
HR=00
while [ $HR -le $ENDHOUR ]
do
   echo "$USHsref/eta32k_prep_get_glb.sh $HR" >> poescript
   HR=`expr $HR + $INCHOUR`
done

export MP_PGMMODEL=mpmd
export MP_CMDFILE=poescript
export MP_LABELIO=YES
export MP_INFOLEVEL=3
export MP_STDOUTMODE=ordered

poe


#######################################
# Truncation of 12km ndas to 32 km ndas
#######################################
export MP_PGMMODEL=spmd
$USHsref/eta32k_prep_get_ndas.sh

set +x
echo "    "
echo " ***********************************************************"
echo " Fetching data and truncation of ndas from 12 to 32km completed."
echo " ***********************************************************"
echo "    "
set -x

msg="Fetching data and truncation of ndas from 12 to 32km completed."
postmsg "$jlogfile" "$msg"


##################################################
#  Part II: converting global data into Eta format
#           and creating LBCs
##################################################
msg="Begin Converting Global Data to Eta Format"
postmsg "$jlogfile" "$msg"

rm -rf poescript

# NODE 1
#echo "$USHsref/eta32k_prep_glb2eta.sh ctl" > poescript
$USHsref/eta32k_prep_glb2eta.sh ctl

#export MP_PGMMODEL=mpmd
#export MP_CMDFILE=poescript
#export MP_LABELIO=YES
#export MP_INFOLEVEL=3
#export MP_STDOUTMODE=ordered

#poe

set +x
echo "    "
echo " ****** g2r and LBCs COMPLETED ####### "
echo " ****** g2r and LBCs COMPLETED ####### "
echo " ****** g2r and LBCs COMPLETED ####### "
echo "    "
set -x

msg="Converting Global Data to Eta Format and LBC creation completed"
postmsg "$jlogfile" "$msg"


##################################################################
# Save all necessary data for model forecast and rerun in future
##################################################################
mv $DATA/nbcout.${cycle}_*  $GESDIR/.
#mv $DATA/nhb$RES.${cycle}_* $GESDIR/.
mv $DATA/ndas.$cycle.nhb$RES $GESDIR/.
mv $DATA/ndas.$cycle.nfc$RES $GESDIR/.
#mv $DATA/init.$cycle.*      $GESDIR/.
mv $DATA/ndas.$cycle.restrt03      $GESDIR/.
mv $DATA/etabcs00           $GESDIR/.
mv $DATA/nbc${RES}_*        $GESDIR/.

exit
