#set -x
############################################################################
#                                                                          #
#  This is the postprocessing script for converting sigma to grib files    #
#  (The code does not destroy or modify the original sigma file)           #
#                                                                          #
#  TUNABLE PARAMETERS:                                                     #
#  ===================                                                     #
#  tmpdir:    temporary working directory                                  #
#  sigma :    INPUT sigma file (full name)                                 #
#  pgb   :    OUTPUT grib file (full name)                                 #
############################################################################ 

tmpdir=$1
sigma=$2
pgb=$3
postgp=/home/szunyogh/bin/global_postgp

#echo $0: sigma file: $sigma output grib file: $pgb
#echo $0: tmpdir: $tmpdir

if [ ! -d $tmpdir ]
then
   mkdir -p $tmpdir
fi
cd $tmpdir
   
export SIGINP=$sigma
export FLXINP=FLX
export FLXIOUT=flxo
export PGBOUT=${pgb}
  
ln -sf $SIGINP     postgp.inp.sig
ln -sf $FLXINP     postgp.inp.flx
ln -sf $PGBOUT     postgp.out.pgb

#   ${postgp}  -Wl,-T < fcst | egrep -v 'dummy|Step' | grep -v '^$'
echo '&NAMPGB IO=144, JO=73, IGEN=0 /' | $postgp -Wl,-T > postp.$$
errcode=$?
if [ $errcode -ne 0 ]
then
   egrep -v 'dummy|Step' postp.$$
else
   rm -f postp.$$
fi
rm -f postgp.inp.sig postgp.inp.flx postgp.out.pgb
chmod -x $pgb
exit $errcode
