#!/bin/sh
######################################################################
#
#   Script:  excfs_postprocessing.sh.sms   
#   Author:  Shrinivas Moorthi
#   DATE:    February, 2004
#   Based on SFM script by Hann-Ming Henry Juang
#
#
#   Main driver to do postprocessing for CFS (Climate Forecast SYSTEM)
#   Coupled Model Integrations
#   History: 2001.11.06 First Implementation  for SFM

# ###############################
#  START CFS POST PROCESSING
# ###############################

set +x
echo " ------------------------------------------------------------"
echo "  "
echo "               IBM-SP $ENVIR PROCESSING     "
echo "  "
echo "            SEASONAL FORECAST MODEL POSTPROCESSING     "
echo "  "
echo "                `date`     "
echo "  "
echo "                   JOB  $job  "
echo "  "
echo "  "
echo " ------------------------------------------------------------"
echo "          processing info for this execution"
echo " Home directory is ............................ $HOMEcfs"
echo " Processing directory for files.. ............. $DATA"
echo "  "
echo " Executable file directory is ................. $EXECcfs"
echo " Fixed field directory is ..................... $FIXcfs"
echo " Unix control language file is ................ $USHcfs"
echo "  "
echo " Network id is ................................ $NET"
echo " Run id for $com processing is ................ $RUN"
echo "  "
echo " standard output in file ...................... $pgmout"
echo " unique processing id for run ................. $pid"
echo " SENDCOM=YES means save com files ............. $SENDCOM"
echo " ------------------------------------------------------------"
set -x

################################################
# looping until no job to do or wait too long
################################################
BREAK_ON_RUN=NO
wait_count=0

while [ $wait_count -le 180 -a $BREAK_ON_RUN = NO ]
do
  wait_count=` expr $wait_count + 1 `
  while [ -s $POSTDEFS/post_def$cyc.* ]
  do
    ###############################################
    # Run it if there is definition files for post
    ###############################################
    ls -rt $POSTDEFS/post_def$cyc.* | sed 's/.*\.//g' | read postid
    . $POSTDEFS/post_def$cyc.$postid

    export COMIN=$INPDIR
    export COM_YMDH=$OUTDIR
    export COM_YMDH_DG=$COM_YMDH/daily_grib
    export COM_YMDH_MG=$CLIMDIR/monthly_grib
    export COM_DG=$CLIMDIR/daily_grib_0${ENS_MEM}
    mkdir -p $COM_YMDH_DG
    mkdir -p $COM_YMDH_MG
    mkdir -p $COM_DG

    #############################
    # Create date related temp dir
    ###############################
    export YMDH=$start_date
    mkdir -p $DATA/$YMDH
    cd $DATA/$YMDH

    ############################################
    # Copy necessary files from COM to temp dir
    ############################################
    if [[ $FHS -eq 0 ]] ; then
      FHINI=-$INTSIG
    else
      FHINI=$FHS
    fi
    for file in $FILES_TO_SEND_COM
    do
#     if [ $file = sig -o $file = flx -o $file = pgb -o $file = extvars ]
      if [ $file = sig -o $file = flx ]
      then
        h=$FHINI
        if [[ $FHINI -eq 0 ]] ; then
          h=-$INTSIG
        fi
        until [[ $((h=10#$h+10#$INTSIG)) -gt $FHE ]];do [[ $h -lt 10 ]]&&h=0$h
          if [ -s $COMIN/$YMDH/${file}f$h ]; then
            ${NCP:-/bin/cp} $COMIN/$YMDH/${file}f$h .
          fi
        done
      fi
    done

    export FHS=$FHS
    ###########################
    # Run postprocessing job
    ###########################
    $USHcfs/cfs_post.sh $MEMBER

    ############################################################
    # rename the post define and delete them if one member done
    ############################################################
    if [ $? = 0 ] ; then
      mv $POSTDEFS/post_def$cyc.$postid $POSTDEFS/done$cyc.$postid
    else
      exit
    fi
#
    if [[ $RUN = forepost ]] ; then
       if [[ $FHE = $nhours ]] ; then
#       clean up temporary files and stop the post job
          if [ $? = 0 ] ; then
             cd $DATA
             rm -rf $YMDH
          fi
          BREAK_ON_RUN=YES
       fi

       if [ $FHE = $ENDHOUR ] ; then
#       clean up temperary files and stop the post job
          if [ $? = 0 ] ; then
             cd $DATA
             rm -rf $YMDH
             rm $POSTDEFS/done*.* 
          fi

#   Thin the output files directory to only keep the 12-hourly files of
#   certain file types(flx, pgb, extvars, ocn, csst) up to 45 days
          tot_err=0
          for file in $FILES_TO_45DAYS
          do
             if [ $file = flx -o $file = csst -o $file = sig ]
             then
                fdir=$COM_YMDH
             else
                fdir=$COM_YMDH_DG
             fi

             start_hour=00
             end_hour=$ENDHOUR

             each_err=0
             #$USHcfs/cfs_keepfile.sh $file $start_hour $end_hour $fdir $each_err
             /nwpara/ush/cfs_keepfile.sh $file $start_hour $end_hour $fdir $each_err
 
             tot_err=`expr $tot_err + $each_err` 
          done

   # Copy the restart file to COM for future use 
          mv $RECOVERY/*$YMDH* $COM_DG/.	

   # Run script to extract extravars into time-series
          $USHcfs/cfs_time.sh $start_date $end_date $INTSIG $CLIMDIR $MEMBER

   # Remove the hourly files in the COM directory
          if [ $tot_err -eq 0 ]
	  then
             rm -rf $COM_YMDH
          fi

          BREAK_ON_RUN=YES
       fi
    fi
#
    wait_count=0
  done
  ######################
  # wait for next post 
  ######################
  sleep 30

done
