#!/bin/sh
######################################################################
#
#   Script:  exrun_cfs.sh.sms   
#   Current  Author:  Shrinivas Moorthi
#   This script is partially based on the original SFM script written by
#                    H. M. Juang
#   Current  DATE:    February 2004
#
#   Main script to run (atmosphere/ocean) coupled forecast system (CFS)
#   for seasonal climate prediction
#   Version CFS03

# ####################
#  START CFS03 integration 
# ####################

set -x
echo " ------------------------------------------------------------"
echo "  "
echo "               IBM-SP $ENVIR PROCESSING     "
echo "  "
echo "          COUPLED FORECAST SYSTEM ${RUN} FORECAST     "
echo "  "
echo "                `date`     "
echo "  "
echo "                   JOB  $job  "
echo "  "
echo "  "
echo "    $NET $RUN TIME is $YYYYSTART $MMSTART $DDSTART $HHSTART"
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 " ------------------------------------------------------------"
 
echo " #####################################################"
echo " ${job} $NET$RUN WILL START AT $start_date  "
echo " ${job} $NET$RUN WILL END   AT $end_date  "
#
if [[ CUE2RUN = prod ]] ; then
  echo " ${job} $NET$RUN IN ${cyc}Z CYCLE FORECAST CONTINUES FROM $current_date  "
fi
echo " #####################################################"
set +x


################################
# update or cd to COM directory
################################
export YMDH=$start_date
export COM_YMDH=$COMOUT/${YMDH}_0${ENS_MEM}
mkdir -m 775 -p $COM_YMDH
export ARCHIVE_DIR=$COM_YMDH

################################
# cd to working directory
################################
cd $DATA

#################################
# create archive directory if need
#################################
if [[ $SENDCOM = YES ]] ; then
  mkdir -p $ARCHIVE_DIR
fi 

export fcsthrs=`$NHOUR $end_date $start_date ` ;# total length of forecast in hours
export YYYY=$(echo $YMDH | cut -c1-4)
export MM=$(echo $YMDH | cut -c5-6)
export DD=$(echo $YMDH | cut -c7-8)
export HH=$(echo $YMDH | cut -c9-10)
current_day=$(echo $current_date | cut -c1-8)
    
if [ $current_date != $start_date ] ; then
                   #########################
                   # check recovery files
                   #########################
  if [ -s $RECOVERY/sfcr.$YMDH -a \
       -s $RECOVERY/sigr1.$YMDH -a \
       -s $RECOVERY/sigr2.$YMDH -a \
       -s $RECOVERY/restart.dta.$YMDH -a \
       -s $RECOVERY/sst1x1For$YMDH.grb ] ; then
       ${NCP:-cp} $RECOVERY/sfcr.$YMDH sfcr
       ${NCP:-cp} $RECOVERY/sigr1.$YMDH sigr1
       ${NCP:-cp} $RECOVERY/sigr2.$YMDH sigr2
       ${NCP:-cp} $RECOVERY/restart.dta.$YMDH restart.dta
       ${NCP:-cp} $RECOVERY/sst1x1For$YMDH.grb sst1x1For$YMDH.grb
       msg="Forecast Starting with Restart files in $RECOVERY"
       postmsg "$jlogfile" "$msg"
  else
       echo ' No restart available at' $RECOVERY 'for' $current_date
       exit
  fi
fi
     
##############################################
# Check and fetch restart files or cold start
##############################################
if [ -s sigr1 -a -s sigr2 -a -s sfcr -a "$FCST_RESTART" = YES ] ; then
   ${NCP:-/bin/cp} sigr1 $DATA/sigr1a
   ${NCP:-/bin/cp} sigr2 $DATA/sigr2a
   ${NCP:-/bin/cp} sfcr $DATA/sfcra
   export SIGI=$DATA/sigr1a
   export SIGI2=$DATA/sigr2a
   export SFCI=$DATA/sfcra
   export nhourb=`/nwprod/exec/global_sighdr sigr2 ifhr`
   sst_filename=$RECOVERY/sst1x1For$YMDH.grb
else
#
#      To get past initial conditions from HPSS (R2 & GODAS)
#
   if [[ $IC_FROM_HPSS = YES ]] ; then
     .  $USHcfs/cfs_coldic.sh $YMDH reanl2 $DATA $ocnic_date
   else
#
#     The following is for currently available operational data (R2 & GODAS)
#     Get and convert surface file from R2-reanalysis
#
      file=sfc.anl.$YMDH.ieee
      ls -l $R2_DIR/$file
      ${NCP:-cp} $R2_DIR/$file $DATA/$file
      . $USHcfs/cfs_conv_sfc.sh $YMDH $DATA
#
#      Get and convert sigma file from R2-reanalysis
      file=sig.anl.$YMDH.ieee
      ls -l $R2_DIR/$file
      ${NCP:-cp} $R2_DIR/$file $DATA/$file
      rm -rf fort.*
      ln -s $DATA/$file               fort.10
      ln -s $DATA/siganl.$YMDH.conv   fort.11
      $convsigexec > convsig.out
      export err=$?;$err_chk

      if [ $ENS_MEM = 2 ] ; then
        YMDH1=`$NDATE -24 $YMDH`
        file=sig.anl.$YMDH1.ieee
        ${NCP:-cp} $R2_DIR2/$file $DATA/$file
        rm -rf fort.*
        ln -s $DATA/$file               fort.10
        ln -s $DATA/siganl.$YMDH.conv2  fort.11
        $convsigexec > convsig.out
        export err=$?;$err_chk
        mv $DATA/siganl.$YMDH.conv $DATA/siganl.$YMDH.conv1
        $sigavg -w 0.9,0.1 siganl.$YMDH.conv1 siganl.$YMDH.conv2 siganl.$YMDH.conv
        export err=$?;$err_chk
      fi
#
      export SIGINP=$DATA/siganl.$YMDH.conv
      #export SFCINP=$DATA/sfcanl.$YMDH.conv
#
#     Get Ocean Initial Conditions
#
      SYYYYMM=$(echo $start_date | cut -c1-6)
#
      YMDO=$(echo $($NDATE $GODAS_FREQ $ocnic_date) | cut -c1-8)
      YYYYO=$(echo $ocnic_date | cut -c1-4)
      MMO=$(echo $ocnic_date | cut -c5-6)
      ${NCP:-cp} $GODASDIR/$godas_name.$YMDO/restart.00$YYYYO.$MMO.$DDSS_OCN.dta $DATA/restart.dta
   fi
#
   echo $SIGINP
   echo $SFCINP
   export SIGOUT=$DATA/siganl.$YMDH
   #export SFCOUT=$DATA/sfcanl.$YMDH
#
   export CHGRESVARS="ntrac=$ntrac,idvt=$varid,ncldt=$numcld"
   export SIGLEVEL=$FIXcfs/cfs_siglevel.l$LEVS.txt
#
   /nwprod/ush/cfs_chgres.sh
#
  ##############################################################################
  ## Added by Julia on Sep 11,2006 to skip the chgres for the sfcanl file      #
  ## Missing of the global_lonsperlat.t62.txt file, the cfs_chgres program     #
  ## could not perform the chgres for the surface analysis file, which should  #
  ## be fine for the CFS because the horizontal resolution is the same as that #
  ## of the R2 analysis                                                        #
  ##############################################################################
   cp $DATA/sfcanl.$YMDH.conv $DATA/sfcanl.$YMDH  
   export SFCOUT=$DATA/sfcanl.$YMDH

   export SIGI=$SIGOUT
   export SFCI=$SFCOUT
   export nhourb=0
#
#  Change the date on Ocean Restart to the start_date
#
   SDD=$(echo $YMDH | cut -c7-8)
   if [ $SDD != $DDSS_OCN ] ; then
     SYYYY=$(echo $YMDH | cut -c1-4)
     SMM=$(echo $YMDH | cut -c5-6)
#    $set_restart $SYYYY $SMM $SDD
     $convert_restart $SYYYY $SMM $SDD
     export err=$?;$err_chk
     mv restart.dta.new_date restart.dta
   fi
#
#  sst analysis file name for the first day
#
   sst_filename=$soSST
fi

######################
#  Set nhours for the chunk of the forecast in this cycle
######################
#nhours=$((nhourb+FH_CYCL))
nhours=$((segment*FH_CYCL))
if [ $nhours -gt $fcsthrs ] ; then nhours=$fcsthrs ; fi
#
export FH_INI=$nhourb
#
echo "`date` $0: fcst from $nhourb to $nhours is starting" >>ERROR.out
#
export FHCYC=${FHCYC:-24}
export FIXGLOBAL=$FIXcfs
export FIXDIR=$FIXcfs

$startmsg

# The time loop for the coupled run in 24 hr increment
until [[ $nhourb -ge $nhours ]]; do
  export FHINI=$nhourb
  if [[ $FHINI -lt 10 ]] ; then FHINI=0$FHINI ; fi
  export FHMAX=$((nhourb+$FHCYC))
  if [[ $FHMAX -lt 10 ]] ; then export FHMAX=0$FHMAX ; fi
#
  export FNTSFA=$sst_filename         ;# Observed or predicted SST grib file
  export FTSFS=0.0                    ;# e-folding time for SST
# export FNACNA=$ice_filename         ;# Observed sea ice file (left blank)
  export FNACNA=                      ;# Observed sea ice file (left blank)
  export FASFS=0.0                    ;# e-folding time for SST
  export FAISS=0.0
  export CYCLVARS='FALBL=0.0,FALBS=0.0,'
#
  export SIGO='sigf$FH'
  export SFCO='sfcf$FH'
  export FLXO='flxf$FH'
  export LOGO='logf$FH'
  export D3DO='d3df$FH'
  export SIGR1=$DATA/sigr1
  export SIGR2=$DATA/sigr2
  export SFCR=$DATA/sfcr
#
  export VDATE=`$NDATE $nhourb $start_date`
  vyyyymmdd=`echo $VDATE | cut -c1-8`
#
#  Execute  Atmospheric model forecast
#
  export FCSTEXEC=$FCSTexec
  $USHcfs/cfs_atmos.sh
  echo VDATE = $VDATE after GFS forecast $(date)
#
################### take the flux from the flx file
  . $USHcfs/cfs_coupa2o.sh
  echo VDATE = $VDATE after A2O $(date)
#
 ################### run ocean model
  $USHcfs/cfs_ocean.sh EXECDIR_OCN $DATA $VDATE \
                       $mom3rt $COM_YMDH >> $pgmout 2>errfile
  echo VDATE = $VDATE after MOM3 forecast $(date)
#
  NXTDATE=`$NDATE $FHMAX $start_date`
  ${NCP:-/bin/cp} restart.dta restart.dta.$NXTDATE
#
################### create new sst file in regular lat/lon gribbed file
  . $USHcfs/cfs_coupo2a.sh
#
  vyyyymmdd=`echo $NXTDATE | cut -c1-8`
  if [ ! -s sst1x1For$vyyyymmdd.grb ] ; then
    echo New sst file from ocean does not exist
    exit 7
  fi
  export sst_filename=sst1x1For$vyyyymmdd.grb
  echo VDATE = $VDATE after O2A $(date)
#
  nhourb=$FHMAX
  ${NCP:-/bin/cp} $DATA/sigr1 $DATA/sigr1a
  ${NCP:-/bin/cp} $DATA/sigr2 $DATA/sigr2a
  ${NCP:-/bin/cp} $DATA/sfcr  $DATA/sfcra
  export SIGI=$DATA/sigr1a
  export SIGI2=$DATA/sigr2a
  export SFCI=$DATA/sfcra

  if [ $((FH_INI+INCHOUR)) -eq $FHMAX -o $FHMAX -eq $nhours ] ; then
#
#  save necessary files to com
#
    if [ $SENDCOM = YES ]; then
      for file in $FILES_TO_SEND_COM
      do
        if [ $file = sig -o $file = sfc -o $file = flx ] ; then
          FH=$FH_INI
          if [[ $FH_INI -eq 0 ]] ; then
            FH=-$FHOUT
          fi
          until [[ $((FH=10#$FH+10#$FHOUT)) -gt $FHMAX ]]
          do
            [[ $FH -lt 10 ]]&&FH=0$FH
            if [ $file = flx ] ; then
	      fcst_date=$($NDATE $FH $YMDH)
              $force_grib_date_y2k $DATA/${file}f$FH $fcst_date
	    fi
            ${NCP:-cp} $DATA/${file}f$FH $COM_YMDH/${file}f$FH
#           ${NCP:-cp} $COMOUT/${file}f$FH $COM_YMDH/${file}f$FH
            ln -fs $COM_YMDH/${file}f$FH $COM_YMDH/${file}f$($NDATE $FH $YMDH)
          done
        fi
        if [ $file = csst ] ; then
          FH=$FH_INI
          until [[ $((FH=10#$FH+10#$FHCYC)) -gt $FHMAX ]]
          do
            datenow=$($NDATE $FH $YMDH)
            oday=$(echo $datenow | cut -c1-8)
            ${NCP:-cp} $DATA/sst1x1For${oday}.grb $COM_YMDH/${file}f$datenow
          done
        fi
      done
    fi

    if [ $SAVEGES = YES ] ; then           #  create recovery files
      ${NCP:-cp} $SFCR $RECOVERY/sfcr.$YMDH
      ${NCP:-cp} $SIGR1 $RECOVERY/sigr1.$YMDH
      ${NCP:-cp} $SIGR2 $RECOVERY/sigr2.$YMDH
      ${NCP:-cp} restart.dta $RECOVERY/restart.dta.$YMDH
      ${NCP:-cp} sst1x1For$vyyyymmdd.grb $RECOVERY/sst1x1For$YMDH.grb
#
# create stamp corresponding g to the above restart
#
      current_date=$($NDATE $FHMAX $YMDH)
      if [[ $current_date -lt $end_date ]] ; then
        echo $start_date $current_date $end_date  $YYYYSTART $YYYYEND $cycle > $RECOVERY/stamp.$RUN
      fi
    fi
#
#  post processing of the forecasts (pgb and average)
#
    archive_dir=$COM_YMDH
    $USHcfs/cfs_prepare_post.sh $BATCH_POST $COM_YMDH \
             $start_date:$FH_INI:$FHMAX:$FHCYC \
             $end_date:$fcsthrs:$INCHOUR:$FHOUT:$FHOUT:$nhours \
             $ARCHIVE_DIR  $ENS_MEM $CLIMCOM $VYYYYMM
#    In the above line $ENS_MEM is used to represent an ensemble member
#
    FH_INI=$((FH_INI+INCHOUR))
  fi
done                #   Coupled forecast for this cycle finished
echo ' finished' ${cyc}'z cycle run for the ensemble member `$ENS_MEM ` starting from' $start_date

##############################################
# end of one cycle for a member of ensemble
##############################################
current_date=$($NDATE $FHMAX $YMDH)
#
# create continuation stamp or remove stamp if the whole run ic complete
#
if [[ $current_date -lt $end_date ]] ; then
# create next member stamp or remove stamp if all done
  echo $start_date $current_date $end_date  $YYYYSTART $YYYYEND $cycle > $RECOVERY/stamp.$RUN
else
  echo $start_date $current_date $end_date  > $POSTDEFS/stamp.done
  YMDH_DONE=$YMDH

##################################
# remove the old member recovery
##################################
 rm -f $RECOVERY/sfcr.$YMDH_DONE
 rm -f $RECOVERY/sigr1.$YMDH_DONE
 rm -f $RECOVERY/sigr2.$YMDH_DONE
 rm -f $RECOVERY/stamp.$RUN
fi


