#!/bin/ksh
###############################################################
# 
# script:  cfs_pgb.sh
#
#   This script modified by shrinivas Moorthi on 07/16/2003
#
# pgb
#
#  $1 ... starting forecast hour
#  $2 ... ending forecast hour
#  $3 ... forecast hour increment
#
#  This script assumes that the input sigma file is the form sig.ftxx
#  The output pressure file is the form pgb.ftxx
#
if [ $# -ne 3 ] ; then
	$DATA/err_exit "`date` $0: argument error" 
fi
#
# tsleep=$((JCAP/FHOUT/6*15+15))
# msleep=240
#
fhs=$1
fhe=$2
fhinc=$3
#
prog=pgb
. $DATA/prep_step

$DATA/startmsg
#
#
if [[ $kop -eq 17 ]] ; then
  polist="\
   1000.0, 925.0, 850.0, 700.0, 600.0, 500.0, 400.0, 300.0, 250.0,\
    200.0, 150.0, 100.0,  70.0,  50.0,  30.0,  20.0,  10.0 "
  export POSTGPVARS="IO=$iop,JO=$jop,KPO=$kop,PO=$polist,\
                   POB(51)=1000.,POB(154)=1000.,\
                   POT(39)=0.,POT(51)=0.,POT(52)=0.,"
fi
if [[ $kop -eq 26 ]] ; then
  export POSTGPVARS="IDRTC=0,IOC=$iop,JOC=$jop,IDRT=0,IO=$iop,JO=$jop,\
                   MOO=48,MOW=12,MOOA=48,MOWA=12,pob(051)=1000,pob(154)=1000,\
                   POT(39)=0.,POT(51)=0.,POT(52)=0.,pot(153)=0.,"
fi
if [[ $kop -eq 46 ]] ; then
  polist="\
   1000.0,   975.0, 950.0, 925.0, 900.0, 875.0,\
    850.0,   825.0, 800.0, 750.0, 700.0, 650.0,\
    600.0,   550.0, 500.0, 450.0, 400.0, 350.0,\
    300.0,   250.0, 200.0, 150.0, 100.0,  70.0,\
     50.0,    40.0,  30.0,  20.0,  10.00,  7.0,\
      5.0,     4.0,   3.0,   2.0,   1.0,   0.7,\
      0.5,     0.4,   0.3,   0.2,   0.1,   0.07,\
      0.05,    0.04,  0.03,  0.02,\
"
  export POSTGPVARS="KPO=$kop,PO=$polist,\
                   POB(51)=1000.,POB(154)=1000.,POB(222)=0.,\
                   POT(39)=0.,POT(51)=0.,POT(52)=0.,POT(153)=0.,"
fi
#
#
#
FH=$((fhs-fhinc))
until [[ $((FH=10#$FH+10#$fhinc)) -gt $fhe ]];do [[ $FH -lt 10 ]]&&FH=0$FH
  if [ ! -s pgbf$FH -a ! -s $COM_YMDH_DG/pgbf$FH ] ; then
#   export LOGINP=$DATA/$YMDH/logf$FH
    ${NCP:-/bin/cp} $COM_YMDH/sigf$FH $DATA/$YMDH/sigf$FH
    ${NCP:-/bin/cp} $COM_YMDH/flxf$FH $DATA/$YMDH/flxf$FH
    export SIGINP=$DATA/$YMDH/sigf$FH
    export FLXINP=$DATA/$YMDH/flxf$FH
#   nsleep=0
#   until [[ -s $LOGINP || $((nsleep+=1)) -gt $msleep ]];do sleep $tsleep;done
#   if [[ $nsleep -gt $msleep ]];then $PERR;exit 2;fi
    export PGBOUT=$DATA/$YMDH/pgbf$FH
    $POSTGPSH >>$pgmout 2>errfile
    export err=$?;$DATA/err_chk
  fi
  rc=$?
  if [[ $rc -ne 0 ]];then $PERR;exit 1;fi
  if [ $SENDCOM = YES ] ; then
    for file in $FILES_TO_SEND_COM
    do
      if [ $file = pgb ] ; then
        if [ ! -s $COM_YMDH_DG/pgbf$FH ] ; then
          current_date=$($NDATE $FH $YMDH)
          $force_grib_date_y2k $DATA/$YMDH/pgbf$FH $current_date
          export err=$?;$DATA/err_chk
          ${NCP:-/bin/cp} $DATA/$YMDH/pgbf$FH $COM_YMDH_DG/pgbf$FH
          ln -fs $COM_YMDH_DG/pgbf$FH $COM_YMDH_DG/pgbf$current_date
        fi
      fi
    done
  fi
done
#export err=$?; $DATA/err_chk

