#!/bin/sh
#
######################################################################################
# UNIX Script Documentation Block                                                    #
#                                                                                    #
# Script name:         sref_prep_sig2pgb.sh                                          #
# Script description:  convert global ensemble sigma data into pgrb format for WRF   #
#                      ensemble                                                      #
#                                                                                    #
# Author:        Jun Du       Org: NP22         Date: 2005-07-15                     #
#                                                                                    #
# Abstract: The script gets global ensemble sigma data and flux grib data to convert #
#           them into pgrib data needed for LBCs used by the WRF-NMM and WRF-EM      #
#           ensemble run's.                                                          #
#                                                                                    #
# Script history log:                                                                #
# 2005-07-?  David Michaud  - cut from a global post script                          #
# 2005-07-?  Jun Du - write it into a script using for SREF WRF members              #
# 2006-04-05 Jun Du - modified directory name and file names to match the new global #
#                     ensemble output                                                #
# 2007-08-13 Jun Du - add capability of selecting either 1.0 degree or 0.5 degree to
#                     be converted to
# 2008-05-20 Jun Du - (1) $GLBPAIR's name is modified to include gefs lowres ctl run (gec00)
#                     (2) gens output's vertical levels set to be the same as gfs'
# 2011-07-18 Jun Du - Output GEFS in 0.5Deg due to the increased resolution of GEFS to T254
######################################################################################

set -aux

DATE=$1
CYC=$2
GLBPAIR=$3
SREFMEM=$4
fhr=$5
ENDHOUR=$6
INCHOUR=$7
resolution=$8
model=$9

export NTHREADS=8

#
## GFS 0.5 Degree Files
#
if [ $resolution -eq 0.5 -a $model = GFS ]; then
   export POSTGPVARS="IO=720,JO=361,KPO=47,PO=1000.,975.,950.,925.,900.,875.,850.,825.,800.,775.,750.,725.,700.,675.,650.,625.,600.,575.,550.,525.,500.,475.,450.,425.,400.,375.,350.,325.,300.,275.,250.,225.,200.,175.,150.,125.,100.,70.,50.,30.,20.,10.,7.,5.,3.,2.,1.,POB(51)=1000.,KZZ=8,ZZ=305.,457.,610.,914.,1829.,2743.,3658.,4572.,KPTO=6,"
   export IGEN_ANL=81
   export IGEN_FCST=96
   export COMIN_SIG=$COMGFS/gfs.$DATE
   export IO=720
   export JO=361
fi

if [ $resolution -eq 0.5 -a $model = gens ]; then
   export POSTGPVARS="IO=720,JO=361,KPO=47,PO=1000.,975.,950.,925.,900.,875.,850.,825.,800.,775.,750.,725.,700.,675.,650.,625.,600.,575.,550.,525.,500.,475.,450.,425.,400.,375.,350.,325.,300.,275.,250.,225.,200.,175.,150.,125.,100.,70.,50.,30.,20.,10.,7.,5.,3.,2.,1.,POB(51)=1000.,KZZ=8,ZZ=305.,457.,610.,914.,1829.,2743.,3658.,4572.,KPTO=6,"
   export IGEN_ANL=81
   export IGEN_FCST=96
   export COMIN_SIG=$COMENS/gefs.$DATE
   export IO=720
   export JO=361
fi

#
## GEFS 1.0 Degree Files
#
if [ $resolution -eq 1.0 -a $model = gens ]; then
#  export POSTGPLIST=${FIXglobal}/global_kplist.1d.txt
   export POSTGPVARS="IO=360,JO=181,KPO=47,PO=1000.,975.,950.,925.,900.,875.,850.,825.,800.,775.,750.,725.,700.,675.,650.,625.,600.,575.,550.,525.,500.,475.,450.,425.,400.,375.,350.,325.,300.,275.,250.,225.,200.,175.,150.,125.,100.,70.,50.,30.,20.,10.,7.,5.,3.,2.,1.,POB(51)=1000.,KZZ=8,ZZ=305.,457.,610.,914.,1829.,2743.,3658.,4572.,KPTO=6,"
   export IGEN_ANL=81
   export IGEN_FCST=96
   export COMIN_SIG=$COMENS/gefs.$DATE
   export IO=360
   export JO=181
fi

while [ $fhr -le $ENDHOUR ]; do

 if [ $model = GFS ]; then
   until test -f ${COMIN_SIG}/gfs.t${CYC}z.sf93
   do
      sleep 20 
   done
   until test -f ${COMIN_SIG}/gfs.t${CYC}z.sfluxgrbf93
   do
      sleep 20 
   done
   cp ${COMIN_SIG}/gfs.t${CYC}z.sf$fhr sigfile
   cp ${COMIN_SIG}/gfs.t${CYC}z.sfluxgrbf$fhr flxfile
 fi

 if [ $model = gens ]; then
   until test -f ${COMIN_SIG}/${CYC}/sfcsig/ge${GLBPAIR}.t${CYC}z.sf93
   do
      sleep 20 
   done
   until test -f ${COMIN_SIG}/${CYC}/sflux/ge${GLBPAIR}.t${CYC}z.sfluxgrbf93
   do
      sleep 20 
   done
   cp ${COMIN_SIG}/${CYC}/sfcsig/ge${GLBPAIR}.t${CYC}z.sf$fhr sigfile
   cp ${COMIN_SIG}/${CYC}/sflux/ge${GLBPAIR}.t${CYC}z.sfluxgrbf$fhr flxfile
 fi

 export SIGINP=sigfile
 export FLXINP=flxfile
 #export FLXINP=/dev/null
 export FLXIOUT=/dev/null
 export PGBOUT=pgbfile
 export PGIOUT=pgifile
 export IGEN=$IGEN_FCST
 export ANOMCATSH=${USHglobal}/global_anomcat.sh
 ${USHglobal}/global_postgp.sh

 if [ $model = GFS ]; then
   mv pgbfile $GFSOUT/gfs.t${CYC}z.master.grbf$fhr
#  mv pgifile $GFSOUT/gfs.t${CYC}z.master.grbif$fhr
 fi
 if [ $model = gens ]; then
   mv pgbfile $GFSOUT/${model}_${SREFMEM}.t${CYC}z.pgrbf$fhr
#  mv pgifile $GFSOUT/${model}_${SREFMEM}.t${CYC}z.pgrbif$fhr
 fi

 fhr=`expr $fhr + $INCHOUR`
 if [ $fhr -lt 10 ];then fhr=0$fhr;fi
done

