#!/bin/ksh
set -x

################################################################################
# Script: exngac_post.sh.sms
# Purpose: run NGAC Postprocessor to generate NGAC products
# Auther: Sarah Lu
#
# Revision Log
#    2012-01-16 Sarah Lu,  Initial script 
#    2012-01-19 Sarah Lu,  Modify err_check
#    2012-03-09 Sarah Lu,  Revise file management
#    2012-06-18 Sarah Lu,  Revise the code to comply NCO script guideline
################################################################################

#  Set environment.
export MP_LABELIO=yes

export POST_USH=${POST_USH:-$USHngac/ngac_post.sh}
export SHOUR=0
export grid3="0 6 0 0 0 0 0 0 360 181 0 0 90000000 0 48 -90000000 359000000 1000000 1000000 0"
export DATA_FCST=${DATA_FCST:-${DATA_IN}/ngac_forecast_00_${envir}}   ## working directory for fcst job
TMPFILES="2d 3d"
if [ $OPT_FLE = multiple -a $OPT_LUMP = NO ]; then
   TMPFILES="$TMPFILES 01 02 03 04 05 06 07"
fi

##
##  Copy or create the ngac products to be post-processed
##

if [ $WRITE_DOPOST = .false. ]; then

##  Run NCEP Unified Post to create post files
 msg="Run NCEP Post to Generate Post Product"
 postmsg "$jlogfile" "$msg"

 tsleep=60      #seconds to sleep before checking again...1 min
 msleep=10      #maximum number of times to sleep... 10 min

 FH=$SHOUR
 while [ $FH -le $FHMAX ]; do
   if [ $FH -lt 10 ]; then FH=0$FH; fi
   $POST_USH $FH 
   nsleep=0
   fileout=$DATA/NGAC3d.GrbF$FH
   until [[ -s $fileout || $((nsleep+=1)) -gt $msleep ]];do sleep $tsleep;done
   if [[ ! -s $fileout ]]; then
     echo "Cannot find $fileout"
     export err=8; err_chk
   fi
   FH=`expr $FH + $FHOUT`
 done

else 
##  Link post files 
 msg="Copy post files from forecast working directory"
 postmsg "$jlogfile" "$msg"

 FH=$SHOUR
 while [ $FH -le $FHMAX ]; do
   if [ $FH -lt 10 ]; then FH=0$FH; fi
   for FLAG in $DFILES; do
     filein=$DATA_FCST/NGAC${FLAG}.GrbF$FH
     fileout=$DATA/NGAC${FLAG}.GrbF$FH
##   ${NCP:-cp} $filein $fileout
     ln -sf $filein $fileout
   done
   FH=`expr $FH + $FHOUT`
 done

fi

##
##  Lump AOD files
##

if [ $OPT_LUMP = YES ]; then

  msg="Lump AOD files"
  postmsg "$jlogfile" "$msg"

  FH=$SHOUR
  while [ $FH -le $FHMAX ]; do
    if [ $FH -lt 10 ]; then FH=0$FH; fi
    for FLAG in 01 02 03 04 05 06 07; do
      if [ $FH  -eq $SHOUR ]; then
#     ${NCAT:-cat} $DATA/${RUN}.${cycle}.a${FLAG}f$FH  > $DATA/${RUN}.${cycle}.a${FLAG}
      ${NCAT:-cat} $DATA/NGAC${FLAG}.GrbF$FH  > $DATA/NGAC${FLAG}
      else
#     ${NCAT:-cat} $DATA/${RUN}.${cycle}.a${FLAG}f$FH  >> $DATA/${RUN}.${cycle}.a${FLAG}
      ${NCAT:-cat} $DATA/NGAC${FLAG}.GrbF$FH  >> $DATA/NGAC${FLAG}
      fi
    done
    FH=`expr $FH + $FHOUT`
  done
fi

##
##  Mapped to 1 x 1 deg
##

if [ $MAPPING  = .true. ]; then

 msg="Map NGAC products from T126 to 1x1 deg"
 postmsg "$jlogfile" "$msg"

 FH=$SHOUR
 while [ $FH -le $FHMAX ]; do
   if [ $FH -lt 10 ]; then FH=0$FH; fi
   for FLAG in $TMPFILES; do
     filein=$DATA/NGAC${FLAG}.GrbF$FH
     fileout=$DATA/${RUN}.${cycle}.a${FLAG}f$FH
     if [ $POST_GRIBVERSION = grib1 ]; then
       $copygb -xg3 $filein $fileout
     elif [ $POST_GRIBVERSION = grib2 ]; then
       $copygb2 -g "$grid3" -x  $filein $fileout
     fi
   done
   FH=`expr $FH + $FHOUT`
 done

 if [ $OPT_LUMP = YES ]; then
   for FLAG in 01 02 03 04 05 06 07; do
     filein=$DATA/NGAC${FLAG}
     fileout=$DATA/${RUN}.${cycle}.a${FLAG}
     if [ $POST_GRIBVERSION = grib1 ]; then
       $copygb -xg3 $filein $fileout
     elif [ $POST_GRIBVERSION = grib2 ]; then
       $copygb2 -g "$grid3" -x  $filein $fileout
     fi
   done
 fi

else

 msg="Copy T126 post files to working directory"
 postmsg "$jlogfile" "$msg"

 FH=$SHOUR
 while [ $FH -le $FHMAX ]; do
   if [ $FH -lt 10 ]; then FH=0$FH; fi
   for FLAG in $TMPFILES; do
     filein=$DATA/NGAC${FLAG}.GrbF$FH
     fileout=$DATA/${RUN}.${cycle}.a${FLAG}f$FH
     ${NCP:-cp} $filein $fileout
   done
   FH=`expr $FH + $FHOUT`
 done

 if [ $OPT_LUMP = YES ]; then
   for FLAG in 01 02 03 04 05 06 07; do
     filein=$DATA/NGAC${FLAG}
     fileout=$DATA/${RUN}.${cycle}.a${FLAG}
     ${NCP:-cp} $filein $fileout
   done
 fi

fi


##
##  Copy POST files from DATA to COMOUT
##

if [[ "$SENDCOM" = "YES" ]]
then
 msg="Copy POST files from DATA to COMOUT"
 postmsg "$jlogfile" "$msg"

 FH=$SHOUR
 while [ $FH -le $FHMAX ]; do
    if [ $FH -lt 10 ]; then FH=0$FH; fi
    for FLAG in $TMPFILES; do
      ${NCP:-cp}  $DATA/${RUN}.${cycle}.a${FLAG}f$FH $COMOUT/${RUN}.${cycle}.a${FLAG}f$FH
     ################################################################
     # Alert the a2d and a3d grib2 files to DBNET to TOC
     ################################################################
     if [ $SENDDBN = YES ]
     then
        $DBNROOT/bin/dbn_alert MODEL `echo NGAC_A${FLAG}_GB2_PARA | tr "[a-z]" "[A-Z]"` $job $COMOUT/${RUN}.${cycle}.a${FLAG}f$FH
#SH     $DBNROOT/bin/dbn_alert MODEL `echo NGAC_A${FLAG}_GB2 | tr "[a-z]" "[A-Z]"` $job $COMOUT/${RUN}.${cycle}.a${FLAG}f$FH
     fi
     ################################################################

    done
    FH=`expr $FH + $FHOUT`
 done

 if [ $OPT_LUMP = YES ]; then
   ${NCP:-cp} $DATA/${RUN}.${cycle}.a01 $AOD01
   ${NCP:-cp} $DATA/${RUN}.${cycle}.a02 $AOD02
   ${NCP:-cp} $DATA/${RUN}.${cycle}.a03 $AOD03
   ${NCP:-cp} $DATA/${RUN}.${cycle}.a04 $AOD04
   ${NCP:-cp} $DATA/${RUN}.${cycle}.a05 $AOD05
   ${NCP:-cp} $DATA/${RUN}.${cycle}.a06 $AOD06
   ${NCP:-cp} $DATA/${RUN}.${cycle}.a07 $AOD07
 fi

  ################################################################
  # Alert the aod grib2 files to DBNET to TOC
  ################################################################
  if [ $SENDDBN = YES ]
  then
        $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2_PARA $job $AOD01
        $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2_PARA $job $AOD02
        $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2_PARA $job $AOD03
        $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2_PARA $job $AOD04
        $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2_PARA $job $AOD05
        $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2_PARA $job $AOD06
        $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2_PARA $job $AOD07

#SH     $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2      $job $AOD01
#SH     $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2      $job $AOD02
#SH     $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2      $job $AOD03
#SH     $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2      $job $AOD04
#SH     $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2      $job $AOD05
#SH     $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2      $job $AOD06
#SH     $DBNROOT/bin/dbn_alert MODEL NGAC_AOD_GB2      $job $AOD07
  fi
  ################################################################
fi

