#!/bin/ksh
#####################################################################
echo "------------------------------------------------"
echo "This job takes into account of the SSMI BUFR data creates ICE"
echo "Concetration Fields for the Arctic Ocean and Antarctic"
echo "Continent and adjoining  water bodies."
echo "------------------------------------------------"
echo "History:  AUG 1997 - First implementation of this new script."
echo "          NOV 1997 - Add copy to /dcom and /scom for   "
echo "                       reliability and users           " 
echo "          Jan 1998 - Add transmission to OSO of files in   "
echo "                       WMO format                          "
echo "          Sep 1998 - Rename WMO files so that they'll actually transmit"
echo "                   - Y2K changes"
echo "          Oct 2004 - Convert to CCS Phase II "
#####################################################################
# Robert Grumbine 1 March 1995.
#  1 March 1995 - Script for handling the production and 
#  dissemination of all ssmi-derived sea ice concentration files
#
#  28 November 1995 - Modified for producing reanalysis files.
#
#  23 January 1996 - Heavily modified to try to circumvent 
#  missing/overloaded file systems on operational cray, and to be 
#  more robust against steps failing internal to the script.
#
#  2 June 1997. Modified to work from BUFR files.  Relies on 
#  dumpscript, with no failover protection possible.
#
#  6 August 1997 - Script version for operations use.
#
#  3 February 2000 - Conversion to IBM SP.  New file management (ln)
#    and argument changes for seaissmi and filtanal
#
# 13 April 2001 - Update land mask usage
#
#  7 October 2003 - Add generation of graphics for web
#
# 20 May 2004     - Update to new, higher resolution, ice analysis
#
#  1 May 2009     - Add AMSRE decoding and processing
# 19 May 2009     - Remove legacy exsaicmrf.sh from end of this script.
#
# 30 March 2012   - Add SSMI-S processing, convert to vertical structure
# 30 March 2012   - Direct production of grib2 files
########################################################################

set -x

export utilexec=/nwprod/util/exec

### Definition of the Fix fields:
export FNLAND=${FNLAND:-${FIXsice}/seaice_nland.map}
export FNLAND127=${FNLAND127:-${FIXsice}/seaice_nland127.map}
export FSLAND=${FSLAND:-${FIXsice}/seaice_sland.map}
export FSLAND127=${FSLAND127:-${FIXsice}/seaice_sland127.map}
export FGLAND5MIN=${FGLAND5MIN:-${FIXsice}/seaice_gland5min}
export FNEWLAND=${FNEWLAND:-${FIXsice}/seaice_newland}
export FNEWLANDPCT=${FNEWLANDPCT:-${FIXsice}/seaice_newland.pct}
export FPOSTERIORI5=${FPOSTERIORI5:-${FIXsice}/seaice_posteriori_5min}
export FPOSTERIORI30=${FPOSTERIORI30:-${FIXsice}/seaice_posteriori_30min}
export FDIST=${FDIST:-${FIXsice}/seaice_alldist.bin}
export FGSHHS=${FGSHHS:-${FIXsice}/seaice_lake_isleout}
echo $FNLAND127

cd $DATA

##############################
#
# START FLOW OF CONTROL
#
# 1) Get the Date from /com/date
#    -- Now done in J-job, not here.
# 2) Run the dumpscript to retrieve a day's data
# 3) Process the bufr output into something readable
# 4) Run the analysis on the files
# -- 2,3,4 are repeated for each satellite being used
# 5) Copy the base analyst's grids to the running location
# 6) Make up the grib files for the polar stereographic data and
#    copy them to running locations
# 7) Construct the modeller's global sea ice grid
#
##############################


########################################
msg="HAS BEGUN!"
postmsg "$jlogfile" "$msg"
########################################

#----------------------------------------------------------
#Begin ordinary processing.
#---------------------------------------------------------
#Dummy variables at this point, formerly used and may be useful
# again in the future.  Robert Grumbine 6 August 1997.
jday=1
refyear=2012

#BUFR------------------------------------------------------
#Run the dumpscript to retrieve a day's data
#----------------------------------------------------------

/nwprod/ush/dumpjb ${PDY}00 12 ssmit
touch ssmit.ibm # ensures that file exists in rare event that dump is empty

#/nwprod/ush/dumpjb ${PDY}00 12 amsre
#touch amsre.ibm # ensures that file exists in rare event that dump is empty

/nwprod/ush/dumpjb ${PDY}00 12 ssmisu
touch ssmisu.ibm # ensures that file exists in rare event that dump is empty

#----------------------------------------------------------
#Process the bufr output into something readable
#----------------------------------------------------------

##################Decode SSMI data
export pgm=seaice_ssmibufr
. prep_step

ln -s ssmit.ibm fort.14

touch bufrout
#ln -s bufrout fort.51

startmsg
$EXECsice/seaice_ssmibufr >> $pgmout 2> errfile
export err=$?;err_chk

echo bufrout > delta
mv fort.51 bufrout

##################Decode AMSRE data
#export pgm=seaice_amsrbufr
#. prep_step
#
#export XLFRTEOPTS="unit_vars=yes"
#ln -s amsre.ibm fort.11
#
#startmsg
#$EXECsice/seaice_amsrbufr >> $pgmout 2> errfile
#mv fort.51 amsr.bufr
#export err=$?;err_chk

##################Decode SSMI-S data
export pgm=seaice_ssmisubufr
. prep_step

export XLFRTEOPTS="unit_vars=yes"
ln -sf ssmisu.ibm fort.11

startmsg
$EXECsice/seaice_ssmisubufr >> $pgmout 2> errfile
mv fort.51 ssmisu.bufr
export err=$?;err_chk

#----------------------------------------------------------
# Run the analysis on the files
# Input files =  delta, $FIXsice/seaice_nland127.map, $FIXsice/seaice_sland127.map
# Output files = n3ssmi.$PDY, s3ssmi.$PDY, ssminorth.$PDY, ssmisouth.$PDY
# Arguments (currently not used) - $jday : Julian Day,
#				   $refyear : 4 digit year
#				   248      : satellite number (248 = F15)
#----------------------------------------------------------

#Process SSMI data to analyst grids
export pgm=seaice_seaissmi
. prep_step

cp $FIXsice/seaice_TBthark.tab .
cp $FIXsice/seaice_TBowark.tab .
cp $FIXsice/seaice_TBowant.tab .
cp $FIXsice/seaice_TBfyark.tab .
cp $FIXsice/seaice_TBfyant.tab .
cp $FIXsice/seaice_TBccark.tab .
cp $FIXsice/seaice_TBccant.tab .

startmsg
$EXECsice/seaice_seaissmi delta $FNLAND127 $FSLAND127 \
           n3ssmi.$PDY s3ssmi.$PDY ssminorth12.$PDY ssmisouth12.$PDY \
           $jday $refyear 248 $FGSHHS $FDIST >> $pgmout 2>errfile
export err=$?;err_chk

#----------------------------------------------------------
# Run the analysis on the AMSR files
# Input files =  amsr.bufr, $FIXsice/seaice_nland127.map, $FIXsice/seaice_sland127.map
# Output files = n3amsr.$PDY, s3amsr.$PDY, amsrnorth.$PDY, amsrsouth.$PDY
#New 1 May 2009
#----------------------------------------------------------
#export pgm=seaice_seaiamsr
#. prep_step
#
#cp $FIXsice/seaice_TBthark.tab.amsr .
#cp $FIXsice/seaice_TBowark.tab.amsr .
#cp $FIXsice/seaice_TBowant.tab.amsr .
#cp $FIXsice/seaice_TBfyark.tab.amsr .
#cp $FIXsice/seaice_TBfyant.tab.amsr .
#cp $FIXsice/seaice_TBccark.tab.amsr .
#cp $FIXsice/seaice_TBccant.tab.amsr .
#
#
#startmsg
#$EXECsice/seaice_seaiamsr amsr.bufr $FNLAND127 $FSLAND127 \
#           n3amsr.$PDY s3amsr.$PDY amsrnorth12.$PDY amsrsouth12.$PDY \
#           $FGSHHS $FDIST >> $pgmout 2>errfile
#export err=$?;err_chk

#----------------------------------------------------------
# Run the analysis on the SSMI-S datafiles
# Input files =  delta, $FIXsice/seaice_nland127.map, $FIXsice/seaice_sland127.map
# Output files = n3ssmi.$PDY, s3ssmi.$PDY, ssmisnorth.$PDY, ssmissouth.$PDY
# Arguments (currently not used) - $jday : Julian Day,
#                                  $refyear : 4 digit year
#           (used)                 285      : satellite number (285 = F17)
# 249 = F16, 286 = F18
#----------------------------------------------------------

#Process SSMI data to analyst grids
export pgm=seaice_seaissmis
. prep_step

cp $FIXsice/seaice_TBthark.tab.ssmisu .
cp $FIXsice/seaice_TBowark.tab.ssmisu .
cp $FIXsice/seaice_TBowant.tab.ssmisu .
cp $FIXsice/seaice_TBfyark.tab.ssmisu .
cp $FIXsice/seaice_TBfyant.tab.ssmisu .
cp $FIXsice/seaice_TBccark.tab.ssmisu .
cp $FIXsice/seaice_TBccant.tab.ssmisu .
echo ssmisu.bufr > delta

startmsg
$EXECsice/seaice_seaissmis delta $FNLAND127 $FSLAND127 \
           n3ssmis.$PDY s3ssmis.$PDY ssmisnorth12.$PDY ssmissouth12.$PDY \
           $jday $refyear 285 $FGSHHS $FDIST >> $pgmout 2>errfile
export err=$?;err_chk

#RG Debugging
if [ $err -ne 0 ] ; then
  exit 1
fi

#---------------------------------------------------------
#
# Blend the AMSRE, SSMI, SSMI-S ice concentration analyses
# 
# 27 July 2011: Modification, change umask to init in output names
#               the posteriori filter will produce the umask files
# 30 March 2012: use names directly (above), add ssmis
#---------------------------------------------------------
export pgm=seaice_blend
$EXECsice/seaice_blend amsrnorth12.$PDY ssminorth12.$PDY ssmisnorth12.$PDY initnorth12.$PDY \
                       amsrsouth12.$PDY ssmisouth12.$PDY ssmissouth12.$PDY initsouth12.$PDY \
                       $FNLAND127 $FSLAND127 >> $pgmout 2> errfile
export err=$?;err_chk

#----------- End of Blending process ---------------------

#----------- Do the a posteriori filtering -- no dependance on sst field ---
# New 27 July 2011
#
cp $FPOSTERIORI5  . 
cp $FPOSTERIORI30 . 
export pgm=seaice_posteriori_5min.x
$EXECsice/seaice_posteriori_5min.x seaice_posteriori_5min \
      initnorth12.$PDY initsouth12.$PDY \
      umasknorth12.$PDY umasksouth12.$PDY
export err=$?;err_chk



#-----------------------------------------------------------
#Produce the old resolution files
#-----------------------------------------------------------
$EXECsice/seaice_north_reduce umasknorth12.$PDY umasknorth.$PDY \
          $FNLAND127 $FNLAND >> $pgmout 2>errfile
export err=$?;err_chk
$EXECsice/seaice_south_reduce umasksouth12.$PDY umasksouth.$PDY \
          $FSLAND127 $FSLAND >> $pgmout 2>errfile
export err=$?;err_chk

#-----------------------------------------------------------
#Construct the graphics for DMZ display
#-----------------------------------------------------------
$EXECsice/seaice_north12xpm umasknorth12.$PDY $FNLAND127 nh12.$PDY.xpm \
			    >> $pgmout 2>errfile
export err=$?;err_chk
convert nh12.$PDY.xpm nh12.$PDY.gif

$EXECsice/seaice_south12xpm umasksouth12.$PDY $FSLAND127 sh12.$PDY.xpm \
			    >> $pgmout 2>errfile
export err=$?;err_chk
convert sh12.$PDY.xpm sh12.$PDY.gif

$EXECsice/seaice_northxpm umasknorth.$PDY $FNLAND nh.$PDY.xpm \
			    >> $pgmout 2>errfile
export err=$?;err_chk
convert nh.$PDY.xpm nh.$PDY.gif

$EXECsice/seaice_southxpm umasksouth.$PDY $FSLAND sh.$PDY.xpm \
			    >> $pgmout 2>errfile
export err=$?;err_chk
convert sh.$PDY.xpm sh.$PDY.gif


#-----------------------------------------------------------
#Copy the base analyst's grids to the running location
#-----------------------------------------------------------

if [ $SENDCOM = "YES" ]
then
  # Raw files on per-instrument basis
  cp n3ssmi.$PDY  ${COMOUT}/seaice.t${cyc}z.n3ssmi
  cp s3ssmi.$PDY  ${COMOUT}/seaice.t${cyc}z.s3ssmi
  cp n3ssmis.$PDY  ${COMOUT}/seaice.t${cyc}z.n3ssmis
  cp s3ssmis.$PDY  ${COMOUT}/seaice.t${cyc}z.s3ssmis
  cp n3amsr.$PDY  ${COMOUT}/seaice.t${cyc}z.n3amsr
  cp s3amsr.$PDY  ${COMOUT}/seaice.t${cyc}z.s3amsr
  cp ssminorth12.$PDY  ${COMOUT}/seaice.t${cyc}z.ssminorth12
  cp ssmisouth12.$PDY  ${COMOUT}/seaice.t${cyc}z.ssmisouth12
  cp ssmisnorth12.$PDY  ${COMOUT}/seaice.t${cyc}z.ssmisnorth12
  cp ssmissouth12.$PDY  ${COMOUT}/seaice.t${cyc}z.ssmissouth12
  cp amsrnorth12.$PDY  ${COMOUT}/seaice.t${cyc}z.amsrnorth12
  cp amsrsouth12.$PDY  ${COMOUT}/seaice.t${cyc}z.amsrsouth12

  #Summary files 
  cp umasknorth12.$PDY  ${COMOUT}/seaice.t${cyc}z.umasknorth12
  cp umasksouth12.$PDY  ${COMOUT}/seaice.t${cyc}z.umasksouth12
  cp umasknorth.$PDY  ${COMOUT}/seaice.t${cyc}z.umasknorth
  cp umasksouth.$PDY  ${COMOUT}/seaice.t${cyc}z.umasksouth
  cp nh12.$PDY.gif    ${COMOUT}/seaice.t${cyc}z.nh12.gif
  cp sh12.$PDY.gif    ${COMOUT}/seaice.t${cyc}z.sh12.gif
  cp nh.$PDY.gif      ${COMOUT}/seaice.t${cyc}z.nh.gif
  cp sh.$PDY.gif      ${COMOUT}/seaice.t${cyc}z.sh.gif

  if [ $SAVEMRF = "YES" ]
  then
    cp n3ssmi.$PDY  ${COMMRF}/seaice.t${cyc}z.n3ssmi
    cp s3ssmi.$PDY  ${COMMRF}/seaice.t${cyc}z.s3ssmi
    cp umasknorth12.$PDY  ${COMMRF}/seaice.t${cyc}z.umasknorth12
    cp umasksouth12.$PDY  ${COMMRF}/seaice.t${cyc}z.umasksouth12
    cp umasknorth.$PDY  ${COMMRF}/seaice.t${cyc}z.umasknorth
    cp umasksouth.$PDY  ${COMMRF}/seaice.t${cyc}z.umasksouth
    cp nh12.$PDY.gif    ${COMMRF}/seaice.t${cyc}z.nh12.gif
    cp sh12.$PDY.gif    ${COMMRF}/seaice.t${cyc}z.sh12.gif
    cp nh.$PDY.gif      ${COMMRF}/seaice.t${cyc}z.nh.gif
    cp sh.$PDY.gif      ${COMMRF}/seaice.t${cyc}z.sh.gif
  fi


  if [ $SENDDBN = "YES" ]
  then
    $DBNROOT/bin/dbn_alert MODEL OMBICE $job ${COMOUT}/seaice.t${cyc}z.nh12.gif
    $DBNROOT/bin/dbn_alert MODEL OMBICE $job ${COMOUT}/seaice.t${cyc}z.sh12.gif
    $DBNROOT/bin/dbn_alert MODEL OMBICE $job ${COMOUT}/seaice.t${cyc}z.nh.gif
    $DBNROOT/bin/dbn_alert MODEL OMBICE $job ${COMOUT}/seaice.t${cyc}z.sh.gif
  fi
fi

#-----------------------------------------------------------
# Make up the grib files for the polar stereographic data and
# copy them to running locations
#-----------------------------------------------------------
#For grib1:
# Get the century from /com/date's file
echo $PDY | cut -c1-2 > psin
echo $PDY | cut -c3-4  >> psin
echo $PDY | cut -c5-6  >> psin
echo $PDY | cut -c7-8  >> psin
cat psin

#For Grib2:
cp /com/date/t00z idate
# for debugging purposes echo 'DATE  20120328000000WASHINGTON' > idate
cp $PARMsice/seaice_pds .

for pole in north12 south12 north south
do

  #Grib1 and WMO File construction
  export pgm=seaice_psg$pole
  . prep_step

  rm fort.*
  ln -s umask${pole}.$PDY        fort.11
  ln -s ${pole}psg.$PDY          fort.51
  ln -s wmo${pole}psg.${PDY}.grb fort.52

  startmsg
  $EXECsice/seaice_psg${pole} < psin >> $pgmout 2>errfile
  export err=$?;err_chk
  
done

for pole in north12 south12 north south
do

  #Grib2 construction:
  export pgm=seaice_grib2
  . prep_step

  export XLFRTEOPTS="unit_vars=yes"
  export XLFUNIT_4="idate"
  export XLFUNIT_10="umask$pole.${PDY}"
  export XLFUNIT_20="seaice.t${cyc}z.${pole}psg.grib2"
  export XLFUNIT_12=$PARMsice/gds.umask$pole
  startmsg
  $EXECsice/seaice_grib2 >> $pgmout 2> errfile
  export err=$?;err_chk

done


if [ $SENDCOM = "YES" ]
then
  cp north12psg.${PDY} ${COMOUT}/seaice.t${cyc}z.north12psg
  cp south12psg.${PDY} ${COMOUT}/seaice.t${cyc}z.south12psg
  cp northpsg.${PDY} ${COMOUT}/seaice.t${cyc}z.northpsg
  cp southpsg.${PDY} ${COMOUT}/seaice.t${cyc}z.southpsg
  cp seaice.t${cyc}z.northpsg.grib2 ${COMOUT}
  cp seaice.t${cyc}z.southpsg.grib2 ${COMOUT}
  cp seaice.t${cyc}z.north12psg.grib2 ${COMOUT}
  cp seaice.t${cyc}z.south12psg.grib2 ${COMOUT}

  if [ $SAVEMRF = "YES" ]
  then
     cp north12psg.${PDY} ${COMMRF}/seaice.t${cyc}z.north12psg
     cp south12psg.${PDY} ${COMMRF}/seaice.t${cyc}z.south12psg
     cp northpsg.${PDY} ${COMMRF}/seaice.t${cyc}z.northpsg
     cp southpsg.${PDY} ${COMMRF}/seaice.t${cyc}z.southpsg
  fi

  ##########################
  # Conver to grib2 format
  ##########################
  for fil in north12psg south12psg northpsg southpsg
  do
     # -- delete in favor of direct construction: 
     #   $utilexec/cnvgrib -g12 -p40 ${COMOUT}/seaice.t${cyc}z.${fil} ${COMOUT}/seaice.t${cyc}z.${fil}.grib2
     $utilexec/wgrib2 ${COMOUT}/seaice.t${cyc}z.${fil}.grib2 -s > ${COMOUT}/seaice.t${cyc}z.${fil}.grib2.idx
  done

  if [ $SENDDBN = "YES" ]
  then
    for fil in north12psg south12psg northpsg southpsg
    do
      $DBNROOT/bin/dbn_alert MODEL OMBICE $job ${COMOUT}/seaice.t${cyc}z.${fil}

      if [ $SENDDBN_GB2 = YES ]
      then

      $DBNROOT/bin/dbn_alert MODEL OMBICE_GB2 $job ${COMOUT}/seaice.t${cyc}z.${fil}.grib2
      $DBNROOT/bin/dbn_alert MODEL OMBICE_GB2_WIDX $job ${COMOUT}/seaice.t${cyc}z.${fil}.grib2.idx

      fi

    done
  fi
fi

if [ $SENDDBN = "YES" ]
then
  cp wmonorthpsg.${PDY}.grb $pcom/seaice.t${cyc}z.wmonorthpsg.${job}
  cp wmosouthpsg.${PDY}.grb $pcom/seaice.t${cyc}z.wmosouthpsg.${job}
  $DBNROOT/bin/dbn_alert GRIB_LOW $NET $job  $pcom/seaice.t${cyc}z.wmonorthpsg.${job}
  $DBNROOT/bin/dbn_alert GRIB_LOW $NET $job  $pcom/seaice.t${cyc}z.wmosouthpsg.${job}
fi



#-----------------------------------------------------------
# Construct the modeller's global sea ice grid
#-----------------------------------------------------------
# QC denotes whether or not it will be possible to produce the
# SST-quality controlled sea ice grids for modellers.
# This step looks for the existance of either today's or 
# yesterday's sstgrb file from the gdas directory.
#-----------------------------------------------------------

qc="false"

if [ -s /com/gfs/prod/sst.$PDY/rtgssthr_grb_0.083 ]
then 
  cp /com/gfs/prod/sst.$PDY/rtgssthr_grb_0.083 oned
  qc="true"
else
  if [ -s /com/gfs/prod/sst.$PDYm1/rtgssthr_grb_0.083 ]
  then 
    cp /com/gfs/prod/sst.$PDYm1/rtgssthr_grb_0.083 oned
    qc="true"
  fi
fi

if [ "$qc" = "true" ]
then

  ###################################
  # Filter the ice concentration field (in lat-long space only so far)
  # Input files: oned, umasknorth.$PDY, umasksouth.$PDY, 
  #              $FIXsice/seaice_halfdeg.map
  # Output files: latlon.$PDY, nps.$PDY, sps.$PDY
  # Arguments:  275.3  - Ocean Temperature above which ice is assumed 
  #                         not to exist.  Degrees Kelvin.
  ###################################


  export pgm=seaice_filtanal
  . prep_step

  $EXECutil/wgrib oned | grep TMP | $EXECutil/wgrib -i oned -o sst -nh -bin
  if [ ! -s sst ]
  then
    echo failed to get an sst field!
  fi  
  
  startmsg
  $EXECsice/seaice_filtanal sst umasknorth12.${PDY} umasksouth12.${PDY} \
        latlon.$PDY nps.$PDY sps.$PDY $FIXsice/seaice_gland5min 275.3 \
        $FIXsice/seaice_nland127.map $FIXsice/seaice_sland127.map \
        >> $pgmout 2>errfile
  export err=$?;err_chk

  ###################################
  # Fill in the ice concentration field with older data as required
  # Input files: $COMm1/fill.$PDYm1, latlon.$PDY, $COMm1/age.$PDYm1
  # Output Files: age.$PDY, fill.$PDY
  ###################################


  export pgm=seaice_icegrid
  . prep_step
 
  startmsg
  
  $EXECsice/seaice_icegrid $COMINm1/seaice.${cycle}.fill5min latlon.$PDY \
                           $COMINm1/seaice.${cycle}.age \
                           age.$PDY fill5min.$PDY >> $pgmout 2>errfile
  export err=$?;err_chk
  #Construct 30' grids:
  $EXECsice/seaice_global_reduce fill5min.$PDY fill.$PDY \
            $FIXsice/seaice_gland5min $FIXsice/seaice_newland >> $pgmout 2>errfile
  export err=$?;err_chk

  #Construct graphic of modeler's grids:
  $EXECsice/seaice_global5minxpm fill5min.$PDY $FIXsice/seaice_gland5min \
                             global5min.$PDY.xpm >> $pgmout 2>errfile
  export err=$?;err_chk
  $EXECsice/seaice_globalxpm fill.$PDY $FIXsice/seaice_newland global.$PDY.xpm \
			     >> $pgmout 2>errfile
  export err=$?;err_chk
  convert -flip global5min.$PDY.xpm global5min.$PDY.gif 
  convert -flip global.$PDY.xpm global.$PDY.gif
  
  
  #########Grib1 construction and wmo file:
  #Engrib the 30' lat-long file
  #Put the 4 digit year date in file ein
  echo $PDY > ein

  export pgm=seaice_ice2grib
  . prep_step

  rm fort.*
  ln -s fill.$PDY             fort.11
  touch eng.$PDY
  ln -s eng.$PDY              fort.51
  ln -s wmoglobice.${PDY}.grb fort.52

  startmsg
  $EXECsice/seaice_ice2grib < ein >> $pgmout 2>errfile
  export err=$?;err_chk

  #Engrib the 5' lat-long file
  #Put the 4 digit year date in file ein
  echo $PDY > ein

  export pgm=seaice_ice2grib5min
  . prep_step

  rm fort.*
  ln -s fill5min.$PDY             fort.11
  touch eng5min.$PDY
  ln -s eng5min.$PDY              fort.51
#Note that there is no wmo file for high res global
  startmsg
  $EXECsice/seaice_ice2grib5min< ein >> $pgmout 2>errfile
  export err=$?;err_chk

  #########Grib2 construction of lat-long gribs:
  #For Grib2:
  cp /com/date/t00z idate
  # for debugging purposes echo 'DATE  20120328000000WASHINGTON' > idate
  cp $PARMsice/seaice_pds .
 

#Low res grid:
    export pgm=seaice_grib2
    . prep_step

    export XLFRTEOPTS="unit_vars=yes"
    export XLFUNIT_12=$PARMsice/gds.fill
    export XLFUNIT_4="idate"
    export XLFUNIT_10="fill.$PDY"
    export XLFUNIT_20="seaice.t${cyc}z.grb.grib2"
    startmsg
    $EXECsice/seaice_grib2 >> $pgmout 2> errfile
    export err=$?;err_chk

#High res grid
    export pgm=seaice_grib2
    . prep_step

    export XLFRTEOPTS="unit_vars=yes"
    export XLFUNIT_12=$PARMsice/gds.fill5min
    export XLFUNIT_4="idate"
    export XLFUNIT_10="fill5min.$PDY"
    export XLFUNIT_20="seaice.t${cyc}z.5min.grb.grib2"
    startmsg
    $EXECsice/seaice_grib2 >> $pgmout 2> errfile
    export err=$?;err_chk

 

###############################################################################
#  Now perform some qc analyses of the output.  9 June 2011
###############################################################################
### 
  export pgm=seaice_monitor_c12th
  . prep_step
  startmsg
$EXECsice/seaice_monitor_c12th  fill5min.$PDY ${COMINm1}/seaice.${cycle}.fill5min \
                           $FGLAND5MIN seaice_delta_$PDY.xpm seaice_monitor_${PDY}.kml \
                           > seaice_monitor_${PDY}.txt 
  export err=$?; err_chk
  convert -flip seaice_delta_$PDY.xpm seaice_delta_$PDY.gif

  export pgm=seaice_edge
  . prep_step
  startmsg
$EXECsice/seaice_edge fill5min.$PDY $FGLAND5MIN seaice_edge_${PDY}.kml > seaice_edge_${PDY}.txt
  export err=$?; err_chk



  if [ $SENDCOM = "YES" ]
  then
    cp eng5min.$PDY    ${COMOUT}/seaice.${cycle}.5min.grb
    cp latlon.$PDY ${COMOUT}/seaice.${cycle}.latlon
    cp age.$PDY ${COMOUT}/seaice.${cycle}.age
    cp fill5min.$PDY ${COMOUT}/seaice.${cycle}.fill5min
    cp global5min.$PDY.gif ${COMOUT}/seaice.t${cyc}z.global5min.gif
    
    cp seaice.t${cyc}z.grb.grib2 ${COMOUT}/seaice.t${cyc}z.grb.grib2
    cp seaice.t${cyc}z.5min.grb.grib2 ${COMOUT}/seaice.t${cyc}z.5min.grb.grib2


    cp eng.$PDY    ${COMOUT}/engice.${cycle}.grb
    cp eng.$PDY    ${COMOUT}/seaice.${cycle}.grb
    cp fill.$PDY ${COMOUT}/seaice.${cycle}.fill
    cp global.$PDY.gif ${COMOUT}/seaice.t${cyc}z.global.gif

    cp seaice_delta_$PDY.gif   ${COMOUT}/seaice_delta.${cycle}.gif
    cp seaice_monitor_$PDY.kml ${COMOUT}/seaice_monitor.${cycle}.kml
    cp seaice_monitor_$PDY.txt ${COMOUT}/seaice_monitor.${cycle}.txt
    cp seaice_edge_$PDY.kml    ${COMOUT}/seaice_edge.${cycle}.kml
    cp seaice_edge_$PDY.txt    ${COMOUT}/seaice_edge.${cycle}.txt


    if [ $SAVEMRF = "YES" ]
    then
       cp eng5min.$PDY    ${COMMRF}/seaice.${cycle}.5min.grb
       cp latlon.$PDY ${COMMRF}/seaice.${cycle}.latlon
       cp age.$PDY ${COMMRF}/seaice.${cycle}.age
       cp fill5min.$PDY ${COMMRF}/seaice.${cycle}.fill5min
       cp global5min.$PDY.gif ${COMMRF}/seaice.t${cyc}z.global5min.gif
   
       cp eng.$PDY    ${COMMRF}/engice.${cycle}.grb
       cp eng.$PDY    ${COMMRF}/seaice.${cycle}.grb
       cp fill.$PDY ${COMMRF}/seaice.${cycle}.fill
       cp global.$PDY.gif ${COMMRF}/seaice.t${cyc}z.global.gif
    fi

    ###########################
    # Convert to grib2 format
    ###########################
    #direct grib2 now $utilexec/cnvgrib -g12 -p40 ${COMOUT}/seaice.${cycle}.grb ${COMOUT}/seaice.${cycle}.grb.grib2
    $utilexec/wgrib2 ${COMOUT}/seaice.${cycle}.grb.grib2 -s >${COMOUT}/seaice.${cycle}.grb.grib2.idx
    #direct grib2 now $utilexec/cnvgrib -g12 -p40 ${COMOUT}/seaice.${cycle}.5min.grb ${COMOUT}/seaice.${cycle}.5min.grb.grib2
    $utilexec/wgrib2 ${COMOUT}/seaice.${cycle}.5min.grb.grib2 -s >${COMOUT}/seaice.${cycle}.5min.grb.grib2.idx
    #direct grib2 now $utilexec/cnvgrib -g12 -p40 ${COMOUT}/engice.${cycle}.grb  ${COMOUT}/engice.${cycle}.grb.grib2

    if [ $SENDDBN = "YES" ]
    then
      $DBNROOT/bin/dbn_alert MODEL OMBICE $job ${COMOUT}/seaice.${cycle}.5min.grb
      $DBNROOT/bin/dbn_alert MODEL OMBICE $job ${COMOUT}/seaice.t${cyc}z.global5min.gif
      $DBNROOT/bin/dbn_alert MODEL OMBICE $job ${COMOUT}/seaice.${cycle}.grb
      $DBNROOT/bin/dbn_alert MODEL OMBICE $job ${COMOUT}/seaice.t${cyc}z.global.gif

      if [ $SENDDBN_GB2 = YES ]
      then 

      $DBNROOT/bin/dbn_alert MODEL OMBICE_GB2 $job ${COMOUT}/seaice.${cycle}.grb.grib2
      $DBNROOT/bin/dbn_alert MODEL OMBICE_GB2 $job ${COMOUT}/seaice.${cycle}.5min.grb.grib2
      $DBNROOT/bin/dbn_alert MODEL OMBICE_GB2 $job ${COMOUT}/engice.${cycle}.grb.grib2
      $DBNROOT/bin/dbn_alert MODEL OMBICE_GB2_WIDX $job ${COMOUT}/seaice.${cycle}.grb.grib2.idx
      $DBNROOT/bin/dbn_alert MODEL OMBICE_GB2_WIDX $job ${COMOUT}/seaice.${cycle}.5min.grb.grib2.idx

      fi

    fi
  fi

#####################################################################
#    process  Sea Ice Analysis in GRIB2 format and route to GTS
#####################################################################
      
  cp ${COMOUT}/seaice.${cycle}.5min.grb.grib2 .

  export pgm=tocgrib2
  . prep_step
  export XLFUNIT_11=seaice.${cycle}.5min.grb.grib2
  export XLFUNIT_31=" "
  export XLFUNIT_51=grib2_awips_seaice.t${cyc}z.5min.grb

  startmsg

  $EXECutil/tocgrib2 < $PARMutil/grib2_seaice.5min.grb >> $pgmout 2>errfile  #NCO must check this
  export err=$?;err_chk

  if [ $SENDCOM = "YES" ]
  then
    cp wmoglobice.${PDY}.grb $pcom/seaice.t${cyc}z.wmoglobice.${job}
    cp grib2_awips_seaice.t${cyc}z.5min.grb $pcom/grib2_awips_seaice.t${cyc}z.5min.grb
  fi

  ##############################
  # Distribute Data
  ##############################
  
  if [ $SENDDBN = "YES" ]
  then
    $DBNROOT/bin/dbn_alert GRIB_LOW $NET $job $pcom/seaice.t${cyc}z.wmoglobice.${job}
    $DBNROOT/bin/dbn_alert GRIB_LOW $NET $job $pcom/grib2_awips_seaice.t${cyc}z.5min.grb
  fi

else
   msg="Job $job cannot produce qc'd sea ice concentration field due"
   postmsg "$jlogfile" "$msg"
   msg="to absence of SST file for $PDY and $PDYm1 cycle $cycle"
   postmsg "$jlogfile" "$msg"

fi 
#end of producing qc'd files

if [ $qc = "true" ]
then
  msg="HAS COMPLETED NORMALLY!"
  echo $msg
  postmsg "$jlogfile" "$msg"

   #####################################################################
   # GOOD RUN
   set +x
   echo "**************JOB $job COMPLETED NORMALLY ON THE IBM SP"
   echo "**************JOB $job COMPLETED NORMALLY ON THE IBM SP"
   echo "**************JOB $job COMPLETED NORMALLY ON THE IBM SP"
   set -x
   #####################################################################
else
   #####################################################################
   # FAILED
   set +x
   echo "**************ABNORMAL TERMIMATION JOB $job ON THE IBM SP"
   echo "**************ABNORMAL TERMIMATION JOB $job ON THE IBM SP"
   echo "**************ABNORMAL TERMIMATION JOB $job ON THE IBM SP"
   set -x
   #####################################################################
fi

############## END OF SCRIPT #######################
