if [ $DEBUGSCRIPTS = YES -o $DEBUGSCRIPTS = ON ] ; then
  set -x
fi
table=$1
dsname=$2
retsum=0
rettot=0
file=$DATA/$dsname


pgm=bufr_tranpoessst
export pgm
cwd=`pwd`
cd $DATA
. $DATA/prep_step
cd $cwd

if [ ! -s $file ] ; then
   outstring="$dsname does not exist or has zero length"
   len=${#outstring}
   if [ $len -gt $lenerror ] ; then
      lenerror=$len
   fi
   echo $outstring >> $tmperr
else
   export XLFRTEOPTS="unit_vars=yes:buffering=disable_all"
   export XLFUNIT_11="$file"
fi
if [ ! -s $table ] ; then
   outstring="$table does not exist or has zero length"
   len=${#outstring}
   if [ $len -gt $lenerror ] ; then
      lenerror=$len
   fi
   echo $outstring  >> $tmperr
else

# need to trim BUFR table to prevent BUFRLIB error for number of JSEQ entries
#  in jump/link table exceeding the limit (20000 in standard BUFRLIB)
#  (can happen with 11 or more satellite/cycle combinations, output files)
#  (can trim all message types EXCEPT NC012011, NC012012, NC012017, NC012018)

   grep -v -e "NC012[1-2]" -e "NC01200" -e "NC01201[0,3]" -e "NC0120[2-9]" \
    $table > $DATA/table

   export XLFUNIT_20="$DATA/table"
fi
typsubdir=$(dirname $TANKFILE)
if [ $typsubdir != "." ] ; then
   subtypfil=$(basename $TANKFILE)
   subtyp=`echo $subtypfil | cut -c3-5`
else
   echo "    <tankfile> must be given as : subdirectory/tankfile>" 
   echo "    Last 3 digits of subdirectory are BUFR type." 
   echo "    Last 3 digits of tankfile are BUFR sub-type." 
   echo "    BUFR type and sub-type may be coded as three" 
   echo "    question marks if translation code provides" 
   echo "    BUFR type and sub-type." 
   exit 191
fi

export typsubdir subtypfil subtyp dsname cycle 


#  Determine if source of data is Navy or NESDIS based on subtype from TANKFILE
#  ----------------------------------------------------------------------------

if [ $subtyp -eq 012 ]; then
   source=navy
   subtyp_physret=017
elif [ $subtyp -eq 011 ]; then
   source=nesd
   subtyp_physret=018
else
   msg="Source of data is unknown (neither Navy nor NESDIS)"
   len=${#outstring}
   if [ $len -gt $lenerror ] ; then
      lenerror=$len
   fi
   echo $msg  >> $tmperr
   cwd=`pwd`
   cd $DATA
   $DATA/postmsg "$jlogfile" "$msg"
   cd $cwd
   exit 99
fi


ymdh=$(date -u '+%Y%m%d%H')

if [ $TANKPROTECT = YES ] ; then
   apndstring=_A_P_N_$dsname
else
   apndstring="' '"
fi

export XLFUNIT_51="$DATA/$typsubdir.$subtypfil.$dsname.t00z.metopa.tmpout$$"
export XLFUNIT_52="$DATA/$typsubdir.$subtypfil.$dsname.t06z.metopa.tmpout$$"
export XLFUNIT_53="$DATA/$typsubdir.$subtypfil.$dsname.t12z.metopa.tmpout$$"
export XLFUNIT_54="$DATA/$typsubdir.$subtypfil.$dsname.t18z.metopa.tmpout$$"
export XLFUNIT_55="$DATA/$typsubdir.$subtypfil.$dsname.t00z.noaa19.tmpout$$"
export XLFUNIT_56="$DATA/$typsubdir.$subtypfil.$dsname.t06z.noaa19.tmpout$$"
export XLFUNIT_57="$DATA/$typsubdir.$subtypfil.$dsname.t12z.noaa19.tmpout$$"
export XLFUNIT_58="$DATA/$typsubdir.$subtypfil.$dsname.t18z.noaa19.tmpout$$"
export XLFUNIT_59="$DATA/$typsubdir.$subtypfil.$dsname.t00z.noaa17.tmpout$$"
export XLFUNIT_60="$DATA/$typsubdir.$subtypfil.$dsname.t06z.noaa17.tmpout$$"
export XLFUNIT_61="$DATA/$typsubdir.$subtypfil.$dsname.t12z.noaa17.tmpout$$"
export XLFUNIT_62="$DATA/$typsubdir.$subtypfil.$dsname.t18z.noaa17.tmpout$$"
export XLFUNIT_63="$DATA/$typsubdir.$subtypfil.$dsname.t00z.noaa18.tmpout$$"
export XLFUNIT_64="$DATA/$typsubdir.$subtypfil.$dsname.t06z.noaa18.tmpout$$"
export XLFUNIT_65="$DATA/$typsubdir.$subtypfil.$dsname.t12z.noaa18.tmpout$$"
export XLFUNIT_66="$DATA/$typsubdir.$subtypfil.$dsname.t18z.noaa18.tmpout$$"
export XLFUNIT_67="$DATA/$typsubdir.$subtypfil.$dsname.t00z.unknown.tmpout$$"
export XLFUNIT_68="$DATA/$typsubdir.$subtypfil.$dsname.t06z.unknown.tmpout$$"
export XLFUNIT_69="$DATA/$typsubdir.$subtypfil.$dsname.t12z.unknown.tmpout$$"
export XLFUNIT_70="$DATA/$typsubdir.$subtypfil.$dsname.t18z.unknown.tmpout$$"
export XLFUNIT_71="$DATA/ymd.t00z.tmpout$$"
export XLFUNIT_72="$DATA/ymd.t06z.tmpout$$"
export XLFUNIT_73="$DATA/ymd.t12z.tmpout$$"
export XLFUNIT_74="$DATA/ymd.t18z.tmpout$$"

echo "$typsubdir $subtypfil $apndstring $ymdh" | $EXECbufr/bufr_tranpoessst
retcode=$?

if [ $retcode -eq 4 ]; then
msg="WARNING: BUFR_TRANPOESSST ENCOUNTERED UNKNOWN SATELLITE ID --> non-fatal"
   set +x
   echo
   echo "$msg"
   echo
   set -x
   cwd=`pwd`
   cd $DATA
   $DATA/postmsg "$jlogfile" "$msg"
   retcode=0
   cd $cwd
fi

if [ $retcode -eq 0 ] ; then

   echo " --------------------------------------------- "
   echo " ********** COMPLETED PROGRAM $pgm  **********"
   echo " --------------------------------------------- "
   msg="$pgm completed normally"
   cwd=`pwd`
   cd $DATA
   $DATA/postmsg "$jlogfile" "$msg"
   cd $cwd

#  Set MPI environment variables
#  -----------------------------

   export MP_SHARED_MEMORY=yes
   export XLFRTEOPTS="nlwidth=80"
   export MP_LABELIO=yes

#  Set directories
#  ---------------

   HOMDIR=/nw${envir}
   BATDIR=${HOMDIR}/scripts
   ndate=/nwprod/util/exec/ndate

#  Variables for debugging (don't always need)
#  -------------------------------------------

##export XLFRTEOPTS="buffering=disable_all"
##export MP_COREFILE_FORMAT=lite

#  Set experiment name
#  -------------------

   exp=physstret

#  Set path/file for physical SST retrieval executable
#  ---------------------------------------------------

   physstretexec=$EXECbufr/ingest_physstret_${source}

#  Set resolution and other dependent parameters
#  ---------------------------------------------

   export JCAP=${JCAP:-574}
   export LEVS=${LEVS:-64}
   export LONA=${LONA:-1152}
   export LATA=${LATA:-576}
   export NLAT=$((${LATA}+2))
   export DELTIM=${DELTIM:-$((3600/($JCAP/20)))}

#  Specify physical SST retrieval fixed field and data directories
#  ---------------------------------------------------------------

#####       fixphysstret=/nw${envir}/fix

#  SST dependent bias correction files
#  -----------------------------------

#####       export SATSSTD=$FIXbufr/ingest_satbiascorr.txt

   export NET=gdas


#  Set variables used in script
#    CLEAN up $tmpdir when finished (YES=remove, NO=leave alone)
#    ncp is a fast copy using rcp between NCEP IBM nodes
#      (ncp does not yet work on blue, redefine ncp=/bin/cp)
#    ndate is a date manipulation utility
#  --------------------------------------------------------------

   CLEAN=YES
##ncp=/u/wx20mi/bin/ncp
   ncp=/bin/cp

#  Set runtime and save directories
#  --------------------------------

   tmpdir=$DATA/WORK2
   savdir=$DATA/result2

   [ ! -d $tmpdir ] | mkdir -p $tmpdir
   [ ! -d $savdir ] | mkdir -p $savdir


#  Make physical SST retrieval namelist
#  ------------------------------------

   cat <<EOF > $DATA/physstretparm.anl
 &SETUP
   miter=2,niter(1)=100,niter(2)=100,
   niter_no_qc(1)=50,niter_no_qc(2)=0,
   write_diag(1)=.true.,write_diag(2)=.false.,write_diag(3)=.true.,
   gencode=82,qoption=2,
   factqmin=0.005,factqmax=0.005,deltim=$DELTIM,
   ndat=2,npred=5,iguess=-1,
   oneobtest=.false.,retrieval=.true.,l_foto=.true.,
   use_pbl=.false.,
 /
 &GRIDOPTS
   JCAP=$JCAP,NLAT=$NLAT,NLON=$LONA,nsig=$LEVS,hybrid=.true.,
   regional=.false.,nlayers(63)=3,nlayers(64)=6,
 /
 &BKGERR
   as=0.6,0.6,0.75,0.75,0.75,0.75,1.0,1.0
   vs=0.7,
   hzscl=1.7,0.8,0.5,
   hswgt=0.45,0.3,0.25,
   bw=0.0,norsp=4,
   bkgv_flowdep=.true.,bkgv_rewgtfct=1.5,
   tsfc_sdv=3.0,3.0,
 /
 &ANBKGERR
   anisotropic=.false.,
 /
 &JCOPTS
   jcterm=.false.,jcdivt=.false.,bamp_ext1=2.5e12,bamp_ext2=5.0e11,
   bamp_int1=2.5e13,bamp_int2=2.5e12,
 /
 &STRONGOPTS
   jcstrong=.true.,nstrong=1,nvmodes_keep=8,period_max=6.,period_width=1.5,
   jcstrong_option=2,baldiag_full=.true.,baldiag_inc=.false.,
 /
 &OBSQC
   dfact=0.75,dfact1=3.0,noiqc=.true.,perturb_obs=.false.,oberrflg=.false.,
   perturb_fact=0.1,c_varqc=0.02,
 /
 &OBS_INPUT
   dmesh(1)=0.5,dmesh(2)=145.0,dmesh(3)=240.0,dmesh(4)=160.0,dmesh(5)=180.0,time_window_max=3.0,
   dfile(1)='avhrrma',   dtype(1)='avhrr_${source}',     dplat(1)='metop-a',       dsis(1)='avhrr3_metop-a',          dval(1)=1.0,  dthin(1)=1,
   dfile(2)='avhrr19',   dtype(2)='avhrr_${source}',     dplat(2)='n19',       dsis(2)='avhrr3_n19',          dval(2)=1.0,  dthin(2)=1,
 /
 &SUPEROB_RADAR
 /
 &SINGLEOB_TEST
   maginnov=0.1,magoberr=0.1,oneob_type='t',
   oblat=45.,oblon=180.,obpres=1000.,obdattim=${adate},
   obhourset=0.,
 /
EOF

# Set fixed files
#   berror   = forecast model background error statistics
#   specoef  = CRTM spectral coefficients
#   trncoef  = CRTM transmittance coefficients
#   emiscoef = CRTM coefficients for IR sea surface emissivity model
#   aerocoef = CRTM coefficients for aerosol effects
#   cldcoef  = CRTM coefficients for cloud effects
#   satinfo  = text file with information about assimilation of brightness temperatures
#   satangl  = angle dependent bias correction file (fixed in time)
#   pcpinfo  = text file with information about assimilation of prepcipitation rates
#   ozinfo   = text file with information about assimilation of ozone data
#   errtable = text file with obs error for conventional data (optional)
#   convinfo = text file with information about assimilation of conventional data
#   bufrtabP  = PREPBUFR mnemonic table (text file) ONLY needed for single obs
#               test (oneobstest=.true.)
#   bufrtabT  = TANK BUFR mnemonic table (text file) ONLY needed to tank
#               physical retrievals
#   file_sst  = SST analysis file
#  ---------------------------------------------------------------------------
   fixcrtm=$FIXbufr/crtm_gfs

   berror=$FIXbufr/global_berror_phyret.l${LEVS}y${NLAT}.f77
   emiscoef=$fixcrtm/EmisCoeff/Big_Endian/EmisCoeff.bin
   aercoef=$fixcrtm/AerosolCoeff/Big_Endian/AerosolCoeff.bin
   cldcoef=$fixcrtm/CloudCoeff/Big_Endian/CloudCoeff.bin
   satinfo=$FIXbufr/global_satinfo_phyret.txt
   satangl=$FIXbufr/global_satangbias_phyret.txt
   pcpinfo=$FIXbufr/global_pcpinfo.txt
   ozinfo=$FIXbufr/global_ozinfo_phyret.txt
   satbias=$FIXbufr/global_satbiascorr.txt

   convinfo=$FIXbufr/global_convinfo_phyret.txt

   errtable=$FIXbufr/prepobs_errtable.global

#  Only need this file for single obs test
#  ---------------------------------------

   bufrtabP=$FIXbufr/prepobs_prep.bufrtable

#  This file is needed to tank physical retrievals
#  -----------------------------------------------

   bufrtabT=$FIXbufr/bufrtab.012

#  This file needed when high resolution SST analysis necessary
#  ------------------------------------------------------------

   file_sst=$DATA/sstgrb

   give_rc="YES"
   cword="NO"
   export give_rc cword
   rettot=0
   atime1st=9999999999

#  Loop through the four cycles
#  ----------------------------

   for cycle in t00z t06z t12z t18z ; do
      cyc=$(echo $cycle | cut -c2-3)
      adate=$(cat $DATA/ymd.${cycle}.tmpout$$)
      do_phys_retr=no

#    Ingest METOP-A, NOAA-17, -18, -19 & unknown into AVHRR tank (in this order)
#    ---------------------------------------------------------------------------

      for sat in metopa noaa17 noaa18 noaa19 unknown ; do
         if [ -s $DATA/$typsubdir.$subtypfil.$dsname.$cycle.$sat.tmpout$$ ];then
            sh $USHbufr/tranjb $TANKDIR \
             $DATA/$typsubdir.$subtypfil.$dsname.$cycle.$sat.tmpout$$
            rc=$?
            rettot=$(($rettot+$rc))
            if [ $rc -eq 0 ] ; then
               msg="Appending $sat AVHRR data for $adate$cyc cycle to tank \
$typsubdir/$subtypfil"
               echo $msg
               cwd=`pwd`
               cd $DATA
               $DATA/postmsg "$jlogfile" "$msg"
               cd $cwd
               [ $sat = metopa -o $sat = noaa19 ]  && do_phys_retr=yes
               if [ $adate -eq 0 ] ; then
                  msg="%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\
WARNING: adate = 0!!!!!"
                  echo $msg
                  cwd=`pwd`
                  cd $DATA
                  $DATA/postmsg "$jlogfile" "$msg"
                  cd $cwd
               fi
            fi
         fi
      done
      if [ $adate -ne 0 -a $do_phys_retr = yes ] ; then
         atime=${adate}$cyc
         if [ $atime -lt $atime1st ] ; then
            atime1st=$atime
         fi
         gtime=$(/nwprod/util/exec/ndate -6 $atime)
         export atime gtime
         ymdm6=$(echo $gtime | cut -c1-8)
         hm6=$(echo $gtime | cut -c9-10)
         export hm6
         cp ${COMGDAS}.$ymdm6/gdas1.t${hm6}z.sf03 $DATA/sf03.t${hm6}z
         iret1=$?
         cp ${COMGDAS}.$ymdm6/gdas1.t${hm6}z.sf06 $DATA/sf06.t${hm6}z
         iret2=$?
         cp ${COMGDAS}.$ymdm6/gdas1.t${hm6}z.sf09 $DATA/sf09.t${hm6}z
         iret3=$?
         cp ${COMGDAS}.$ymdm6/gdas1.t${hm6}z.bf03 $DATA/bf03.t${hm6}z
         iret4=$?
         cp ${COMGDAS}.$ymdm6/gdas1.t${hm6}z.bf06 $DATA/bf06.t${hm6}z
         iret5=$?
         cp ${COMGDAS}.$ymdm6/gdas1.t${hm6}z.bf09 $DATA/bf09.t${hm6}z
         iret6=$?
         ymdsst=$(/nwprod/util/ush/finddate.sh $adate d-1)
         cp ${COM}.${ymdsst}/rtgssthr_grb_0.083 $DATA/sstgrb
         iret7=$?
         iret=$(($iret1+$iret2+$iret3+$iret4+$iret5+$iret6+$iret7))
         if [ $iret -eq 0 ] ; then
            echo "All input files ready for physical SST retrievals"

#  Set up $tmpdir
#  --------------
            rm -rf $tmpdir
            mkdir -p $tmpdir
            cd $tmpdir
############rm -rf core*

            export CHKPRT=true
            export FORM=copy
            export LOUD=off
            export DUEX=/nwprod/exec
            export DFIX=/nwprod/fix
            export MPMD=/nwprod/util/ush/mpmd.sh
            export TANK=$TANKDIR

#  Dump physical retrieval tank for this cycle to see if any processing
#   has already occurred for this cycle
#  --------------------------------------------------------------------

            $USHbufr/dumpjb $atime 2.99 012 $subtyp_physret
            rc=$?

            if [ $rc -eq 22 ] ; then

#    No physical retrieval processing has yet occurred for this cycle -
#     dump everything for this cycle from AVHRR tank and use it as input to
#     make physical retrievals
#    ----------------------------------------------------------------------

               msg="Physical SST retrieval dump centered at $atime EMPTY"
               echo $msg
               cwd=`pwd`
               cd $DATA
               $DATA/postmsg "$jlogfile" "$msg"
               cd $cwd
               $USHbufr/dumpjb $atime 3.0 012 $subtyp
               rc=$?

               if [ $rc -eq 22 ] ; then

#       No AVHRR data in dump for this cycle - move on to next cycle
#       ------------------------------------------------------------

                  if [[ "$CLEAN" = "YES" ]];then
                     cd $tmpdir
                     cd ../
                     rm -rf $tmpdir
                  fi
                  msg="######AVHRR data dump centered at $atime ALSO EMPTY, \
move on to next cycle time"
                  echo $msg
                  cwd=`pwd`
                  cd $DATA
                  $DATA/postmsg "$jlogfile" "$msg"
                  cd $cwd
                  continue
               else
                  msg="Use AVHRR data dump centered at $atime as input to phys\
 SST retr processing"
                  echo $msg
                  cwd=`pwd`
                  cd $DATA
                  $DATA/postmsg "$jlogfile" "$msg"
                  cd $cwd
               fi

#       Split AVHRR dump into NOAA-17, -18, -19 and METOP-A files for later
#        input to physical retrieval processing
#       -------------------------------------------------------------------

               pgm=bufr_satsplit
               export pgm
               cwd=`pwd`
               cd $DATA
               . $DATA/prep_step
               cd $cwd

               export XLFUNIT_11=$DATA/012.$subtyp
               export XLFUNIT_51=avhrr19
               export XLFUNIT_52=avhrr17
               export XLFUNIT_53=avhrr18
               export XLFUNIT_54=avhrrma
               $EXECbufr/bufr_satsplit
               err=$?
               if [ $err -eq 0 ]; then
echo " --------------------------------------------- "
echo " ********** COMPLETED PROGRAM bufr_satsplit  **********"
echo " --------------------------------------------- "
                  msg="bufr_satsplit has split AVHRR dump into N-17, -18, -19\
 & METOP-A files-generate phys SST retr"
                  cwd=`pwd`
                  cd $DATA
                  $DATA/postmsg "$jlogfile" "$msg"
                  cd $cwd
               else
echo "*******************************************************"
echo "********  ERROR PROGRAM bufr_satsplit RETURN CODE $err ********"
echo "*******************************************************"
                  msg="ERROR PROGRAM bufr_satsplit RETURN CODE $err"
                  cwd=`pwd`
                  cd $DATA
                  $DATA/postmsg "$jlogfile" "$msg"
                  cd $cwd
               fi
            else

#    Some physical retrieval processing has already occurred for this cycle -
#     use only NEW AVHRR data for this cycle processed above by
#     BUFR_TRANPOESSST as input to make physical retr. (NOAA-17, -18, -19,
#     METOP-A only)
#    --------------------------------------------------------------------------

               msg="SOME phys SST retr for $atime processed-use AVHRR files \
out of BUFR_TRANPOESSST for phys SST retr"
               echo $msg
               cwd=`pwd`
               cd $DATA
               $DATA/postmsg "$jlogfile" "$msg"
               cd $cwd
     $ncp $DATA/$typsubdir.$subtypfil.$dsname.$cycle.noaa19.tmpout$$ ./avhrr19
     $ncp $DATA/$typsubdir.$subtypfil.$dsname.$cycle.noaa17.tmpout$$ ./avhrr17
     $ncp $DATA/$typsubdir.$subtypfil.$dsname.$cycle.noaa18.tmpout$$ ./avhrr18
     $ncp $DATA/$typsubdir.$subtypfil.$dsname.$cycle.metopa.tmpout$$ ./avhrrma
            fi
 
#  Copy executable and fixed files to $tmpdir
#  ------------------------------------------

            $ncp $physstretexec ./ingest_physstret
            $ncp $berror   ./berror_stats
            $ncp $emiscoef ./EmisCoeff.bin
            $ncp $aercoef  ./AerosolCoeff.bin
            $ncp $cldcoef  ./CloudCoeff.bin
            $ncp $satangl  ./satbias_angle
            $ncp $satinfo  ./satinfo
            $ncp $pcpinfo  ./pcpinfo
            $ncp $ozinfo   ./ozinfo
            $ncp $satbias  ./satbias_sst
            $ncp $bufrtabP ./prepobs_prep.bufrtable
            $ncp $bufrtabT ./bftab_sstphr
            $ncp $convinfo ./convinfo
            $ncp $errtable ./errtable
            $ncp $file_sst SST_AN

# Copy CRTM coefficient files based on entries in satinfo file
            nsatsen=`cat $satinfo | wc -l`
            isatsen=1
            while [[ $isatsen -le $nsatsen ]]; do
               flag=`head -n $isatsen $satinfo | tail -1 | cut -c1-1`
               if [[ "$flag" != "!" ]]; then
                  satsen=`head -n $isatsen $satinfo | tail -1 | cut -f 2 -d" "`
                  spccoeff=${satsen}.SpcCoeff.bin
                  if  [[ ! -s $spccoeff ]]; then
                     $ncp $fixcrtm/SpcCoeff/No_AC/Big_Endian/$spccoeff ./
                     $ncp $fixcrtm/TauCoeff/Big_Endian/${satsen}.TauCoeff.bin ./
                  fi
               fi
               isatsen=` expr $isatsen + 1 `
            done

#  Copy sigma, and surface files
#  -----------------------------

            $ncp $FIXbufr/ingest_t06z.abias      ./satbias_in     
            $ncp $DATA/sf03.t${hm6}z ./sigf03
            $ncp $DATA/sf06.t${hm6}z ./sigf06
            $ncp $DATA/sf09.t${hm6}z ./sigf09
            $ncp $DATA/bf03.t${hm6}z ./sfcf03.quad
            $ncp $DATA/bf06.t${hm6}z ./sfcf06.quad
            $ncp $DATA/bf09.t${hm6}z ./sfcf09.quad

#  Interpolate surface files from quadratic to linear grid
#  -------------------------------------------------------

            DATASAV=$DATA

            list="sfcf03 sfcf06 sfcf09"
            for file in $list; do
               export SIGINP=NULL
               export SFCINP=$tmpdir/$file.quad
               export SIGOUT=NULL
               export SFCOUT=$tmpdir/$file
               export LONB=${LONA}
               export LATB=${LATA}
               export FIXGLOBAL=/nwprod/fix
               export DATA=NULL
               export CHGRESEXEC=/nwprod/exec/global_chgres
               export CHGRESSH=/nwprod/ush/global_chgres.sh
               $CHGRESSH
            done

            DATA=$DATASAV

#  Run physical SST retrieval under Parallel Operating Environment (poe)
#  ---------------------------------------------------------------------

            rtime=$(date -u '+%H%M')
            msg="Start physical SST retrieval under POE"
            echo $msg
            cwd=`pwd`
            cd $DATA
            $DATA/postmsg "$jlogfile" "$msg"
            cd $cwd
            poe $tmpdir/ingest_physstret < $DATA/physstretparm.anl \
             > stdout
            rc=$?
            msg="End physical SST retrieval under POE, rc = $rc"
            echo $msg
            cwd=`pwd`
            cd $DATA
            $DATA/postmsg "$jlogfile" "$msg"
            cd $cwd

#  Save output
#  -----------

            mkdir -p $savdir

            cat stdout fort.2* > $savdir/stdout.anl.${atime}.${rtime} 
            cat stdout fort.2* 


#  Loop over first and last outer loops to generate innovation diagnostic
#   files for indicated observation types (groups)
#
#  NOTE:  Since we set miter=2 in physical SST retrieval namelist SETUP, outer
#         loop 03 will contain innovations with respect to the analysis.
#         Creation of o-a innovation files is triggered by write_diag(3)=.true.
#         The setting write_diag(1)=.true. turns on creation of o-g innovation
#         files.
#  ----------------------------------------------------------------------------

#  Collect AVHRR diagnostic and bufr files
#  ---------------------------------------

            list="avhrr_${source}_metop-a avhrr_${source}_n19"
            for type in $list; do
               count=`ls ${tmpdir}/dir.*/${type}* | wc -l`
               if [[ count -gt 0 ]]; then
                  cat ${tmpdir}/dir.*/${type}* > \
                   ${savdir}/diag_${type}.${atime}.${rtime}
               fi
               cp ${savdir}/diag_${type}.${atime}.${rtime} ${COM}.${ymdsst}
            done

#  Assemble bufr files
#  -------------------

###         export XLFRTEOPTS="unit_vars=yes"
###         export XLFUNIT_50="${savdir}/bfsst_avhrr.017.${atime}"  
###         time ls ${tmpdir}/bfsst*.017|/nwprod/exec/bufr_combfr
###         export XLFUNIT_50="${savdir}/bfsst_avhrr.018.${atime}"  
###         time ls ${tmpdir}/bfsst*.018|/nwprod/exec/bufr_combfr
###         export XLFUNIT_50="${savdir}/bfsst_avhrr.${atime}"  
###         time echo "${tmpdir}/bfsst_avhrr.017.${atime}\n${tmpdir}/bfsst_avhrr.018.${atime}"|/nwprod/exec/bufr_combfr
 
            give_rc="YES"
            cword="NO"
            export give_rc cword
            retsum=0
            for physstretfile in $(ls ${tmpdir}/bfsst_avhrr_${source}*) ; do
               if [ -s $physstretfile ] ; then
                  sh $USHbufr/tranjb $TANKDIR $physstretfile
                  rc=$?
                  retsum=$(($retsum+$rc))
                  if [ $rc -eq 0 ] ; then
                     this_file=`basename  $physstretfile`
                     msg="Appending $this_file phys SST retr for $adate$cyc \
cycle to tank b012/xx$subtyp_physret"
                     echo $msg
                     cwd=`pwd`
                     cd $DATA
                     $DATA/postmsg "$jlogfile" "$msg"
                     cd $cwd
                  fi
               else
                  this_file=`basename  $physstretfile`
                  msg="######No $this_file physical SST retr for $adate$cyc \
cycle"
                  echo $msg
                  cwd=`pwd`
                  cd $DATA
                  $DATA/postmsg "$jlogfile" "$msg"
                  cd $cwd
               fi
            done

#  If requested, clean up $tmpdir
#  ------------------------------

            if [[ "$CLEAN" = "YES" ]];then
               cd $tmpdir
               cd ../
               rm -rf $tmpdir
            fi
         else
            msg="######Cannot process phys SST retr for $adate$cyc -one or \
more input files not available (too soon?)"
            echo $msg
            cwd=`pwd`
            cd $DATA
            $DATA/postmsg "$jlogfile" "$msg"
            cd $cwd
         fi
      fi  
   done


#  Fill missing physical retrieval tanks over past 24-hrs (at 6-hr intervals)
#  --------------------------------------------------------------------------

   atime=$atime1st
   atimelast=$(/nwprod/util/exec/ndate -24 $atime1st)
   msg="Fill any missing physical SST retrieval tanks over past 24-hrs at 6-hr\
 intervals"
   echo $msg
   cwd=`pwd`
   cd $DATA
   $DATA/postmsg "$jlogfile" "$msg"
   cd $cwd
   while [ $atime -gt $atimelast ] ; do
      atemp=$(/nwprod/util/exec/ndate -6 $atime)
      atime=$atemp
      adate=$(echo $atime | cut -c1-8)
      cyc=$(echo $atime | cut -c9-10)
      cycle=t${cyc}z
      gtime=$(/nwprod/util/exec/ndate -6 $atime)
      export atime gtime
      ymdm6=$(echo $gtime | cut -c1-8)
      hm6=$(echo $gtime | cut -c9-10)
      export hm6

#  Set up $tmpdir
#  --------------
      rm -rf $tmpdir
      mkdir -p $tmpdir
      cd $tmpdir
############rm -rf core*

      export CHKPRT=true
      export FORM=copy
      export LOUD=off
      export DUEX=/nwprod/exec
      export DFIX=/nwprod/fix
      export MPMD=/nwprod/util/ush/mpmd.sh
      export TANK=$TANKDIR

#  Dump physical retrieval tank for this cycle to see if any processing
#   has already occurred for this cycle
#  --------------------------------------------------------------------

      $USHbufr/dumpjb $atime 2.99 012 $subtyp_physret
      rc=$?

      if [ $rc -eq 22 ] ; then

#    No physical retrieval processing has yet occurred for this cycle -
#     dump everything for this cycle from AVHRR tank and use it as input to
#     make physical retrievals
#    ----------------------------------------------------------------------

         msg="Physical SST retrieval dump centered at $atime EMPTY"
         echo $msg
         cwd=`pwd`
         cd $DATA
         $DATA/postmsg "$jlogfile" "$msg"
         cd $cwd
         $USHbufr/dumpjb $atime 3.0 012 $subtyp
         rc=$?
         
         if [ $rc -eq 22 ] ; then

#       No AVHRR data in dump for this cycle - move on to next cycle
#       ------------------------------------------------------------

            if [[ "$CLEAN" = "YES" ]];then
               cd $tmpdir
               cd ../
               rm -rf $tmpdir
            fi
            msg="######AVHRR data dump centered at $atime ALSO EMPTY, \
move on to next cycle time"
            echo $msg
            cwd=`pwd`
            cd $DATA
            $DATA/postmsg "$jlogfile" "$msg"
            cd $cwd
            continue
         else
            msg="Use AVHRR data dump centered at $atime as input to phys SST \
retr processing"
            echo $msg
            cwd=`pwd`
            cd $DATA
            $DATA/postmsg "$jlogfile" "$msg"
            cd $cwd
         fi

#       Split AVHRR dump into NOAA-17, -18, 19 and METOP-A files for later
#        input to physical retrieval processing
#       ------------------------------------------------------------------

         pgm=bufr_satsplit
         export pgm
         cwd=`pwd`
         cd $DATA
         . $DATA/prep_step
         cd $cwd

         export XLFUNIT_11=$DATA/012.$subtyp
         export XLFUNIT_51=avhrr19
         export XLFUNIT_52=avhrr17
         export XLFUNIT_53=avhrr18
         export XLFUNIT_54=avhrrma
         $EXECbufr/bufr_satsplit
         err=$?
         if [ $err -eq 0 ]; then
echo " --------------------------------------------- "
echo " ********** COMPLETED PROGRAM bufr_satsplit  **********"
echo " --------------------------------------------- "
            msg="bufr_satsplit has split AVHRR dump into N-17, -18, -19 & \
METOP-A files-generate phys SST retr"
            cwd=`pwd`
            cd $DATA
            $DATA/postmsg "$jlogfile" "$msg"
            cd $cwd
         else
echo "*******************************************************"
echo "********  ERROR PROGRAM bufr_satsplit RETURN CODE $err  ********"
echo "*******************************************************"
            msg="ERROR PROGRAM bufr_satsplit RETURN CODE $err"
            cwd=`pwd`
            cd $DATA
            $DATA/postmsg "$jlogfile" "$msg"
            cd $cwd
         fi
      else

#    Physical retrieval processing has already occurred for this cycle - move
#     on to next cycle
#    ------------------------------------------------------------------------

         if [[ "$CLEAN" = "YES" ]];then
            cd $tmpdir
            cd ../
            if [[ $rc -eq 0 ]];then
               rm -rf $tmpdir
            fi
         fi
         msg="Physical SST retrievals centered on $atime ALREADY PROCESSED - \
move on to next cycle time"
         echo $msg
         cwd=`pwd`
         cd $DATA
         $DATA/postmsg "$jlogfile" "$msg"
         cd $cwd
         continue
      fi
      cp ${COMGDAS}.$ymdm6/gdas1.t${hm6}z.sf03 $DATA/sf03.t${hm6}z
      iret1=$?
      cp ${COMGDAS}.$ymdm6/gdas1.t${hm6}z.sf06 $DATA/sf06.t${hm6}z
      iret2=$?
      cp ${COMGDAS}.$ymdm6/gdas1.t${hm6}z.sf09 $DATA/sf09.t${hm6}z
      iret3=$?
      cp ${COMGDAS}.$ymdm6/gdas1.t${hm6}z.bf03 $DATA/bf03.t${hm6}z
      iret4=$?
      cp ${COMGDAS}.$ymdm6/gdas1.t${hm6}z.bf06 $DATA/bf06.t${hm6}z
      iret5=$?
      cp ${COMGDAS}.$ymdm6/gdas1.t${hm6}z.bf09 $DATA/bf09.t${hm6}z
      iret6=$?
      ymdsst=$(/nwprod/util/ush/finddate.sh $adate d-1)
      cp ${COM}.${ymdsst}/rtgssthr_grb_0.083 $DATA/sstgrb
      iret7=$?
      iret=$(($iret1+$iret2+$iret3+$iret4+$iret5+$iret6+$iret7))
      if [ $iret -eq 0 ] ; then
         echo "All input files ready for physical SST retrievals"

#  Copy executable and fixed files to $tmpdir
#  ------------------------------------------

         $ncp $physstretexec ./ingest_physstret
         $ncp $berror   ./berror_stats
         $ncp $emiscoef ./EmisCoeff.bin
         $ncp $aercoef  ./AerosolCoeff.bin
         $ncp $cldcoef  ./CloudCoeff.bin
         $ncp $satangl  ./satbias_angle
         $ncp $satinfo  ./satinfo
         $ncp $pcpinfo  ./pcpinfo
         $ncp $ozinfo   ./ozinfo
         $ncp $satbias  ./satbias_sst
         $ncp $bufrtabP ./prepobs_prep.bufrtable
         $ncp $bufrtabT ./bftab_sstphr
         $ncp $convinfo ./convinfo
         $ncp $errtable ./errtable
         $ncp $file_sst SST_AN

# Copy CRTM coefficient files based on entries in satinfo file
         nsatsen=`cat $satinfo | wc -l`
         isatsen=1
         while [[ $isatsen -le $nsatsen ]]; do
            flag=`head -n $isatsen $satinfo | tail -1 | cut -c1-1`
            if [[ "$flag" != "!" ]]; then
               satsen=`head -n $isatsen $satinfo | tail -1 | cut -f 2 -d" "`
               spccoeff=${satsen}.SpcCoeff.bin
               if  [[ ! -s $spccoeff ]]; then
                  $ncp $fixcrtm/SpcCoeff/No_AC/Big_Endian/$spccoeff ./
                  $ncp $fixcrtm/TauCoeff/Big_Endian/${satsen}.TauCoeff.bin ./
               fi
            fi
            isatsen=` expr $isatsen + 1 `
         done

#  Copy sigma, and surface files
#  -----------------------------

         $ncp $FIXbufr/ingest_t06z.abias      ./satbias_in     
         $ncp $DATA/sf03.t${hm6}z ./sigf03
         $ncp $DATA/sf06.t${hm6}z ./sigf06
         $ncp $DATA/sf09.t${hm6}z ./sigf09
         $ncp $DATA/bf03.t${hm6}z ./sfcf03.quad
         $ncp $DATA/bf06.t${hm6}z ./sfcf06.quad
         $ncp $DATA/bf09.t${hm6}z ./sfcf09.quad

#  Interpolate surface files from quadratic to linear grid
#  -------------------------------------------------------

         DATASAV=$DATA

         list="sfcf03 sfcf06 sfcf09"
         for file in $list; do
            export SIGINP=NULL
            export SFCINP=$tmpdir/$file.quad
            export SIGOUT=NULL
            export SFCOUT=$tmpdir/$file
            export LONB=${LONA}
            export LATB=${LATA}
            export FIXGLOBAL=/nwprod/fix
            export DATA=NULL
            export CHGRESEXEC=/nwprod/exec/global_chgres
            export CHGRESSH=/nwprod/ush/global_chgres.sh
            $CHGRESSH
         done

         DATA=$DATASAV

#  Run physical SST retrieval under Parallel Operating Environment (poe)
#  ---------------------------------------------------------------------

         rtime=$(date -u '+%H%M')
         msg="Start physical SST retrieval under POE"
         echo $msg
         cwd=`pwd`
         cd $DATA
         $DATA/postmsg "$jlogfile" "$msg"
         cd $cwd
         poe $tmpdir/ingest_physstret < $DATA/physstretparm.anl \
          > stdout
         rc=$?
         msg="End physical SST retrieval under POE, rc = $rc"
         echo $msg
         cwd=`pwd`
         cd $DATA
         $DATA/postmsg "$jlogfile" "$msg"
         cd $cwd

#  Save output
#  -----------

         mkdir -p $savdir

         cat stdout fort.2* > $savdir/stdout.anl.${atime}.${rtime} 
         cat stdout fort.2* 

#  Loop over first and last outer loops to generate innovation diagnostic
#   files for indicated observation types (groups)
#
#  NOTE:  Since we set miter=2 in physical SST retrieval namelist SETUP, outer
#         loop 03 will contain innovations with respect to the analysis.
#         Creation of o-a innovation files is triggered by write_diag(3)=.true.
#         The setting write_diag(1)=.true. turns on creation of o-g innovation
#         files.
#  ----------------------------------------------------------------------------

#  Collect AVHRR diagnostic and bufr files
#  ---------------------------------------

         list="avhrr_${source}_metop-a avhrr_${source}_n19"
         for type in $list; do
            count=`ls ${tmpdir}/dir.*/${type}* | wc -l`
            if [[ count -gt 0 ]]; then
               cat ${tmpdir}/dir.*/${type}* > \
                ${savdir}/diag_${type}.${atime}.${rtime}
            fi
            cp ${savdir}/diag_${type}.${atime}.${rtime} ${COM}.${ymdsst}
         done


#  Assemble bufr files
#  -------------------

###      export XLFRTEOPTS="unit_vars=yes"
###      export XLFUNIT_50="${savdir}/bfsst_avhrr.017.${atime}"
###      time ls ${tmpdir}/bfsst*.017|/nwprod/exec/bufr_combfr
###      export XLFUNIT_50="${savdir}/bfsst_avhrr.018.${atime}"
###      time ls ${tmpdir}/bfsst*.018|/nwprod/exec/bufr_combfr
###      export XLFUNIT_50="${savdir}/bfsst_avhrr.${atime}"
###      time echo "${tmpdir}/bfsst_avhrr.017.${atime}\n${tmpdir}/bfsst_avhrr.018.${atime}"|/nwprod/exec/bufr_combfr
 
         give_rc="YES"
         cword="NO"
         export give_rc cword
         retsum=0
         for physstretfile in $(ls ${tmpdir}/bfsst_avhrr_${source}*) ; do
            if [ -s $physstretfile ] ; then
               sh $USHbufr/tranjb $TANKDIR $physstretfile
               rc=$?
               retsum=$(($retsum+$rc))
               if [ $rc -eq 0 ] ; then
                  this_file=`basename  $physstretfile`
                  msg="Appending $this_file phys SST retr for $adate$cyc cycle\
 to tank b012/xx$subtyp_physret"
                  echo $msg
                  cwd=`pwd`
                  cd $DATA
                  $DATA/postmsg "$jlogfile" "$msg"
                  cd $cwd
               fi
            else
               this_file=`basename  $physstretfile`
               msg="######No $this_file physical SST retr for $adate$cyc cycle"
               echo $msg
               cwd=`pwd`
               cd $DATA
               $DATA/postmsg "$jlogfile" "$msg"
               cd $cwd
            fi
         done

#  If requested, clean up $tmpdir
#  ------------------------------

         if [[ "$CLEAN" = "YES" ]];then
            cd $tmpdir
            cd ../
            if [[ $rc -eq 0 ]];then
               rm -rf $tmpdir
            fi
         fi
      fi
   done
else
   echo "*******************************************************"
   echo "********  ERROR PROGRAM $pgm RETURN CODE $retcode  ********"
   echo "*******************************************************"
   msg="ERROR PROGRAM $pgm RETURN CODE $retcode"
   cwd=`pwd`
   cd $DATA
   $DATA/postmsg "$jlogfile" "$msg"
   cd $cwd
fi
     
if [ $retsum -gt $retcode ] ; then
   retcode=$retsum
fi
if [ $rettot -gt $retcode ] ; then
   retcode=$rettot
fi

exit $retcode
