#!/bin/sh
###############################################################################
#                                                                             #
# This script is the prep step for the GODAS model.                           #
# Data and fields are pulled from com directories.                            #
# QC is done on subsurface temp profiles.                                     #
#                                                                             #
#                                                                   May 2003  #
#                                                                             #
###############################################################################

#####################################################################
echo "------------------------------------------------"
echo "JGODAS_PREP processing"
echo "------------------------------------------------"
echo "History: "
echo " MAY 2003 - First implementation of this new script."
echo " MAY 2004 - D.Stokes - Correct input time_mean file."
echo "                     - Clean up search for flux and SST fields. "
echo "                     - Add err_chks to exit if bathy or tesac missing."
echo " DEC 2005 - D.Stokes - Added daily averaging for bathy and tesac data."
echo "                     - Moved qc of buoy data (editprf) to run before daily averaging."
echo " JUL 2006 - D.Stokes - Processing one 29-day buoy file rather than 29 daily buoy files."
echo "                     - Removed calls to warnmsg.sh and godas_v1_errwarn.sh because" 
echo "                       use of prodllsubmit (required to send the email warnings)" 
echo "                       is no longer encouraged.  Added warning posts to jlogfile."
echo " FEB 2007 - D.Stokes - Add processing of altimeter data."
echo " SEP 2007 - D.Stokes - Widen temporal window surface boundary conditions "
echo "                       for extended GODAS analysis.  (variable ndysbc)."
echo "                     - Processing of profile data modified to allow for deeper assimilation"
echo " MAR 2008 - D.Stokes - Process all altimeter bufr files within one call to executable."
echo "                     - Remove call to godas_v1_findfields now that all required"
echo "                       data was grabbed by DUMP job"
echo "                     - Improve diagnostic msgs"
#####################################################################

set -x

cd $DATA

msg="Begin GODAS PREP PROCESSING for $job on `hostname`"
postmsg "$jlogfile" "$msg"

# define executables
EXECgodas=${EXECgodas:-/nwprod/exec}
EXECtmSrtPrf=${EXECtmSrtPrf:-${EXECgodas}/godas_v1_tmSrtPrf}
EXECeditPrf=${EXECeditPrf:-${EXECgodas}/godas_v1_editPrf}
EXECavePrfDly=${EXECavePrfDly:-${EXECgodas}/godas_v1_avePrfDly}
EXECmrgPrf=${EXECmrgPrf:-${EXECgodas}/godas_v1_mrgPrf}
EXECmkAsmPrf=${EXECmkAsmPrf:-${EXECgodas}/godas_v1_mkAsmPrf}
EXECmkAsmPrfs=${EXECmkAsmPrfs:-${EXECgodas}/godas_v1_mkAsmPrfs}
EXECmkLSAchv=${EXECmkLSAchv:-${EXECgodas}/godas_v1_mkLSAchv}
EXECgetBufrJsn=${EXECgetBufrJsn:-${EXECgodas}/godas2_v1_getBufrJsn}
EXECdlyAlt2cyc=${EXECdlyAlt2cyc:-${EXECgodas}/godas_v1_dlyAlt2cyc}
EXECtrkAveCyc=${EXECtrkAveCyc:-${EXECgodas}/godas_v1_trkAveCyc}
EXECmkAsmAlt=${EXECmkAsmAlt:-${EXECgodas}/godas_v1_mkAsmAlt}
EXECgdas2g3i=${EXECgdas2g3i:-${EXECgodas}/godas_v1_gdas2g3i}
EXECsst2g3i=${EXECsst2g3i:-${EXECgodas}/godas_v1_sst2g3i}
EXECwgrib=${EXECwgrib:-${utilexec}/wgrib}


# Get date info from dump job or use default if not available
#   Orig version of dump script had output going to model_date_info. 
#   Migrate towards model_date_info.dump (because this step adding more vars)
if [ -s $COMIN/model_date_info.dump ]; then
  cp $COMIN/model_date_info.dump .
elif
 [ -s $COMIN/model_date_info ]; then
  cp $COMIN/model_date_info ./model_date_info.dump
fi
if [ -s model_date_info.dump ]; then
  read model_end_day ndays_bathy < model_date_info.dump
  err=$?
  if [ $err -ne 0 ]; then
     msg="Warning: Model info not input.  Set defaults"
     postmsg "$jlogfile" "$msg"
     unset model_end_day ndays_bathy
  fi
fi

#if ndays_bathy not set, use default of 29
echo ${ndays_bathy:=29}

export lag=${lag:-15}
export runlen=${runlen:-1}
export numruns=${numruns:-1}
icadj=`expr $runlen \* $numruns - 1`


echo ${model_end_day:=`sh $utilscript/finddate.sh $PDY d-$lag`}
if [ icadj -ne 0 ]; then
  echo ${model_start_day:=`sh $utilscript/finddate.sh $model_end_day d-$icadj`}
else
  echo ${model_start_day:=$model_end_day}
fi

echo $model_start_day $model_end_day $ndays_bathy $runlen $numruns > model_date_info.prep

# This script was originally written to prepare data for the standard godas only.
# The script was later modified to process SBC's for the extended run also, but
# the above model date information pertains to the standard run.  

if test "$SENDCOM" = 'YES'
then
    [ -s model_date_info.prep ] && cp model_date_info.prep $COMOUT
fi


dsub=`expr $ndays_bathy - 1`
export StartDY=`sh $utilscript/finddate.sh $PDYm1 d-$dsub` 
export EndDY=$PDYm1


# set the time stamp of the time_mean field used to qc the temperature files
yrM=`echo "$model_start_day" | cut -c -4`
moM=`echo "$model_start_day" | cut -c 5-6`
dyM=`echo "$model_start_day" | cut -c 7-8`
export dteMSfx=00$yrM.$moM.$dyM


set +x
echo " "
echo "############################################################"
echo "  Copy dumped bathy, tesac and buoy data from ${COMIN}. "
echo "  If missing, print warning msg and exit with error."
echo "############################################################"
echo " "
set -x


if [ -s $COMIN/bathy.noqc ]; then
  cp $COMIN/bathy.noqc ./bathy.noqc
else
  export msg="BATHY DATA NOT FOUND.  CHECK DUMP OUTPUT"
  postmsg "$jlogfile" "$msg"
#   (consider looking in other directories)
  export err=21; err_chk
fi

if [ -s $COMIN/tesac.noqc ]; then
  cp $COMIN/tesac.noqc ./tesac.noqc
else
  export msg="TESAC DATA NOT FOUND.  CHECK DUMP OUTPUT"
  postmsg "$jlogfile" "$msg"
#   (consider looking in other directories)
  export err=22; err_chk
fi


if [ -s $COMIN/buoy.noqc ]; then
  cp $COMIN/buoy.noqc ./buoy.noqc
else
  export msg="BUOY DATA NOT FOUND.  CHECK DUMP OUTPUT"
  postmsg "$jlogfile" "$msg"
#   (consider looking in other directories)
  export err=23; err_chk
fi


set +x
echo " "
echo " "
echo "############################################################"
echo "QC temperature profile data"
echo "############################################################"
echo " "
echo " "
echo "############################################################"
echo "     Time sort bathy"
echo "############################################################"
echo " "
set -x

export pgm=godas_v1_tmSrtPrf
. prep_step

export XLFUNIT_11="bathy.noqc"
export XLFUNIT_51="bathy.srt"

startmsg
$EXECtmSrtPrf  >> $pgmout 2> errfile
export err=$?;err_chk

set +x
echo " "
echo "############################################################"
echo "     Edit bathy"
echo "############################################################"
echo " "
set -x

echo "Copy time_mean.$dteMSfx.nc file from previous run"
# used in two calls to godas_v1_editPrf. netcdf, so no xlfunit.

if [ -s $COMBASE1.$PDYm1/time_mean.$dteMSfx.nc ]; then
  cp $COMBASE1.$PDYm1/time_mean.$dteMSfx.nc ./time_mean.nc
else
  msg='CANNOT FIND TIME_MEAN FILE.  EXIT.'
  postmsg "$jlogfile" "$msg"
  export err=25; err_chk
fi


export pgm=godas_v1_editPrf
. prep_step

# no XLFUNIT for time_mean.nc
export XLFUNIT_11="bathy.srt"
export XLFUNIT_51="bathy.edt"

startmsg

$EXECeditPrf  >> $pgmout 2> errfile
export err=$?;err_chk

set +x
echo " "
echo "############################################################"
echo "     Daily average bathy"
echo "############################################################"
echo " "
set -x

export pgm=godas_v1_avePrfDly
. prep_step

export XLFUNIT_11="bathy.edt"
export XLFUNIT_51="bathy.dav"

startmsg

$EXECavePrfDly  >> $pgmout 2> errfile
export err=$?;err_chk


set +x
echo " "
echo "############################################################"
echo "     Time sort tesac"
echo "############################################################"
echo " "
set -x

export pgm=godas_v1_tmSrtPrf
. prep_step

export XLFUNIT_11="tesac.noqc"
export XLFUNIT_51="tesac.srt"

startmsg
$EXECtmSrtPrf  >> $pgmout 2> errfile
export err=$?;err_chk

set +x
echo " "
echo "############################################################"
echo "     Edit tesac"
echo "############################################################"
echo " "
set -x

export pgm=godas_v1_editPrf
. prep_step

# no XLFUNIT for time_mean.nc
export XLFUNIT_11="tesac.srt"
export XLFUNIT_51="tesac.edt"

startmsg

$EXECeditPrf  >> $pgmout 2> errfile
export err=$?;err_chk

set +x
echo " "
echo "############################################################"
echo "     Daily average tesac"
echo "############################################################"
echo " "
set -x

export pgm=godas_v1_avePrfDly
. prep_step

export XLFUNIT_11="tesac.edt"
export XLFUNIT_51="tesac.dav"

startmsg

$EXECavePrfDly  >> $pgmout 2> errfile
export err=$?;err_chk



set +x
echo " "
echo "############################################################"
echo "     Time sort buoy"
echo "############################################################"
echo " "
set -x

export pgm=godas_v1_tmSrtPrf
. prep_step

export XLFUNIT_11="buoy.noqc"
export XLFUNIT_51="buoy.srt"

startmsg
$EXECtmSrtPrf  >> $pgmout 2> errfile
export err=$?;err_chk

set +x
echo " "
echo "############################################################"
echo "     Edit buoy"
echo "############################################################"
echo " "
set -x

export pgm=godas_v1_editPrf
. prep_step

# no XLFUNIT for time_mean.nc
export XLFUNIT_11="buoy.srt"
export XLFUNIT_51="buoy.edt"

startmsg

$EXECeditPrf  >> $pgmout 2> errfile
export err=$?;err_chk

set +x
echo " "
echo "############################################################"
echo "     Daily average buoy"
echo "############################################################"
echo " "
set -x

export pgm=godas_v1_avePrfDly
. prep_step

export XLFUNIT_11="buoy.edt"
export XLFUNIT_51="buoy.dav"

startmsg

$EXECavePrfDly  >> $pgmout 2> errfile
export err=$?;err_chk


set +x
echo " "
echo "############################################################"
echo "Merge temperature profile data and write to assimilation file"
echo "############################################################"
echo " "
echo " "
echo "############################################################"
echo "     Merge bathy and tesac"
echo "############################################################"
echo " "
set -x

export pgm=godas_v1_mrgPrf
. prep_step

export XLFUNIT_11="bathy.dav"
export XLFUNIT_12="tesac.dav"
export XLFUNIT_51="bat_tes.edt"

startmsg

$EXECmrgPrf  >> $pgmout 2> errfile
export err=$?;err_chk


set +x
echo " "
echo "############################################################"
echo "     Merge bathy/tesac and buoy"
echo "############################################################"
echo " "
set -x

export pgm=godas_v1_mrgPrf
. prep_step

export XLFUNIT_11="bat_tes.edt"
export XLFUNIT_12="buoy.dav"
export XLFUNIT_51="tmpa.edt"

startmsg

$EXECmrgPrf  >> $pgmout 2> errfile
export err=$?;err_chk


set +x
echo " "
echo "############################################################"
echo "     Write temperature profiles to assimilation file"
echo "############################################################"
echo " "
set -x

export pgm=$EXECmkAsmPrf
. prep_step

startmsg

export XLFUNIT_11="tmpa.edt"
export XLFUNIT_12="$FIXgodas/godas_v1_tmask.gbl"
export XLFUNIT_51="tmpa.mom"
export XLFUNIT_13="$FIXgodas/godas_v1_ann.temp"
export XLFUNIT_61="tmpa.ext"

$EXECmkAsmPrf $model_start_day  >> $pgmout 2> errfile
export err=$?;err_chk


set +x
echo " "
echo "############################################################"
echo "Salinity Profiles"
echo "############################################################"
echo " "
echo " "
echo "############################################################"
echo "     Make synthetic salinity profiles"
echo "############################################################"
echo " "
set -x

export pgm=godas_v1_mkLSAchv
. prep_step

if [ -s tmpa.ext ]
then
  export XLFUNIT_11="tmpa.ext"
else
  export XLFUNIT_11="tmpa.edt"
fi

export XLFUNIT_12="$FIXgodas/godas_v1_ann.temp"
export XLFUNIT_13="$FIXgodas/godas_v1_ann.salt"
export XLFUNIT_51="sala.syn"

startmsg

$EXECmkLSAchv  >> $pgmout 2> errfile
export err=$?;err_chk

set +x
echo " "
echo "############################################################"
echo "     Write salinity profiles to assimilation file"
echo "############################################################"
echo " "
set -x

export pgm=$EXECmkAsmPrfs
. prep_step

export XLFUNIT_11="sala.syn"
export XLFUNIT_12="$FIXgodas/godas_v1_tmask.gbl"
export XLFUNIT_51="sala.mom"

startmsg

$EXECmkAsmPrfs $model_start_day >> $pgmout 2> errfile
export err=$?;err_chk

set +x
echo " "
echo "#####################################################################"
echo "Send temperature and salinity assimilation files to COM for model run"
echo "#####################################################################"
echo " "
set -x

if test "$SENDCOM" = 'YES'
then
  for CTYPE in tmpa sala 
  do
    [ -s $CTYPE.mom ] && cp $CTYPE.mom $COMOUT   
  done
fi


#  Altimeter data  #
CDATE=$StartDY
CTYPE=njsnal
until [ $CDATE -gt $EndDY ]; do
  DATAFILE=$COMIN/$CTYPE.$CDATE
  if test -f ${DATAFILE}; then
    echo Altim available for $CDATE.$CTYPE
    cp $DATAFILE   bufr.$CTYPE.$CDATE
    echo $CDATE bufr.$CTYPE.$CDATE >> BList
  fi
  CDATE=`sh $utilscript/finddate.sh $CDATE d+1`
done

export pgm=godas2_getBufrJsn
. prep_step
export XLFUNIT_11=BList
# XLFUNIT_12 is used internally for input files.  reopenend for each date
# XLFUNIT_51="$CDATE.sshd.qc2048" is used internally for output files.
$EXECgetBufrJsn >> $pgmout 2> errfile
err=$?
if [ $err -ne 0 ]; then
  msg="NON_CRITICAL ERROR $err IN $pgm PROCESSING FILE bufr.${CTYPE}.  CONTINUE WITH REMAINING FILES"
  postmsg "$jlogfile" "$msg"
  err=0
fi


echo Convert daily files to cycle files.
ls *.sshd.qc2048 > DList
#
export pgm=godas_v1_dlyAlt2cyc
. prep_step
export XLFUNIT_11=DList
$EXECdlyAlt2cyc   >> $pgmout 2> errfile
export err=$?; err_chk   


echo Average along track and calibrate.
export pgm=godas_v1_trkAveCyc
. prep_step
export XLFUNIT_21="$FIXgodas/godas_v1_calAltBufr.mom"
export XLFUNIT_61=JsnTbl
set -A file_list `ls -1 jb_c*.txt`
n=0
while [ n -lt ${#file_list[*]} ]; do
  echo "${file_list[$n]} to ca${file_list[$n]}"
  export XLFUNIT_11="${file_list[$n]}"
  export XLFUNIT_51="ca${file_list[$n]}"
  $EXECtrkAveCyc   >> $pgmout 2> errfile
  err=$? 
  if [ $err -ne 0 ]; then
    msg="NON_CRITICAL ERROR $err IN $pgm PROCESSING FILE ${file_list[$n]}.  CONTINUE WITH REMAINING FILES"
    postmsg "$jlogfile" "$msg"
    err=0
  fi
  ((n=$n+1))
done

echo ###  Create weekly assimilation files and consolidate 
echo ###   them into a 5-week file.
export pgm=$EXECmkAsmAlt
. prep_step
export XLFUNIT_11=JsnTbl
export XLFUNIT_21="$FIXgodas/godas_v1_tmask.gbl"
export XLFUNIT_51=ssha.mom
$EXECmkAsmAlt $model_start_day   >> $pgmout 2> errfile
export err=$? 

  if [ $err -ne 0 ]; then
    msg="NON_CRITICAL ERROR $err IN $pgm.  CONTINUE WITHOUT SSH DATA"
    postmsg "$jlogfile" "$msg"
    err=0
  else
    if test "$SENDCOM" = 'YES'
    then
      cp ssha.mom $COMOUT/ssha.mom
    fi
  fi

#  End Altimeter processing  #


# This job might be generating SBC files to be 
#  used by both standard godas and 1-day delay godas.  
#  The latter is run for the 13 days after today's standard model run.
#  So, default ndysbc=14.

ndysbc=${ndysbc:-14}
nfields=`expr $ndysbc + 2`
  
set +x
echo " "
echo "############################################################"
echo "FLUXES"
echo "############################################################"
echo " "
echo "############################################################"
echo "     Find flux fields for $nfields days surrounding run dates "
echo "     from past COM dirs.  If missing, post warning msg.  "
echo "     Look for surrounding days if any of the fields are missing."
echo "############################################################"
echo " "

set -x

flux_start_date=`sh $utilscript/finddate.sh $model_start_day d-1`
flux_end_date=`sh $utilscript/finddate.sh $model_start_day d+$ndysbc`

cat <<cntrlEOF > namelist.cntrlatm
        &cntrlatm  ndysbc=$ndysbc,
        /
cntrlEOF


for flux in taux tauy senflx latflx prate netlwflx netswflx
do

#  sh $USHgodas/godas_v1_findfields.sh $flux $flux_start_date $flux_end_date $nfields
#  export err=$?; err_chk

  CDATE=$flux_start_date
  nfield=0
  until [ $CDATE -gt $flux_end_date ]; do
    DATAFILE=$COMIN/$flux.$CDATE
    if test -f ${DATAFILE}; then
      nfield=`expr $nfield + 1 `
      cp $DATAFILE $flux.$nfield
    else
      export msg="WARNING NO $flux FILE FOUND FOR $CDATE"
      postmsg "$jlogfile" "$msg"
      export err=8; err_chk
    fi
    CDATE=`sh $utilscript/finddate.sh $CDATE d+1`
  done


  cp $flux.1 $flux
  nfield=2
  until [ $nfield -gt $nfields ]
  do
    cat $flux.$nfield >> $flux
    nfield=`expr $nfield + 1`
  done

  case $flux in
    senflx)   f11=senflx;   f12=/dev/null; fbase=fsh;  inp=sh ;;
    latflx)   f11=latflx;   f12=/dev/null; fbase=flh;  inp=lh ;;
    netlwflx) f11=netlwflx; f12=/dev/null; fbase=ful;  inp=lw ;;
    netswflx) f11=netswflx; f12=/dev/null; fbase=fsr;  inp=sw ;;
    prate)    f11=latflx;   f12=prate;     fbase=fslt; inp=sf ;;
    taux)     f11=taux;     f12=/dev/null; fbase=taux; inp=tx ;;
    tauy)     f11=tauy;     f12=/dev/null; fbase=tauy; inp=ty ;;
    *) echo ERROR IN FLUX CASE.;  export err=15; err_chk;;
  esac


  set +x
  echo " "
  echo "############################################################"
  echo "     Make flux file ${fbase}.mom "
  echo "############################################################"
  echo " "
  set -x

  export pgm=godas_v1_gdas2g3i
  . prep_step

  export XLFUNIT_11="$f11"
  export XLFUNIT_12="$f12"
  export XLFUNIT_13="$FIXgodas/godas_v1_mask.gdas"
  if test "$f11" = 'taux' -o "$f11" = 'tauy'
  then
    export XLFUNIT_14="$FIXgodas/godas_v1_umask.gbl"
  else
    export XLFUNIT_14="$FIXgodas/godas_v1_tmask.gbl"
  fi
  export XLFUNIT_31="namelist.cntrlatm"
  export XLFUNIT_51="$fbase.mom"

  startmsg

  $EXECgdas2g3i $model_start_day "$inp" >> $pgmout 2> errfile
  export err=$?;err_chk


  if test "$SENDCOM" = 'YES'
  then
    cp $fbase.mom $COMOUT/$fbase.mom
  fi

done


set +x
echo " "
echo "############################################################"
echo "SST"
echo "############################################################"
echo " "
echo "############################################################"
echo "     Find sst fields for $nfields days surrounding run dates "
echo "     from past COM dirs.  If missing, post warning msg.  "
echo "     Look for surrounding days if any files are missing."
echo "############################################################"
set -x

#sh $USHgodas/godas_v1_findfields.sh sst.grb $flux_start_date $flux_end_date $nfields

CDATE=$flux_start_date
nfield=0
until [ $CDATE -gt $flux_end_date ]; do
  DATAFILE=$COMIN/sst.grb.$CDATE
  if test -f ${DATAFILE}; then
    nfield=`expr $nfield + 1 `
    cp $DATAFILE sst.grb.$nfield
  else
    export msg="WARNING NO sst.grb FILE FOUND FOR $CDATE"
    postmsg "$jlogfile" "$msg"
    export err=8; err_chk
  fi
  CDATE=`sh $utilscript/finddate.sh $CDATE d+1`
done



gribfile=sst.grb.1
$EXECwgrib -s $gribfile | egrep TMP:sfc | $EXECwgrib -i -grib $gribfile -o sst.grb
[ $? -ne 0 ] && export err=5

nfield=2
until [ $nfield -gt $nfields ]
do
    gribfile=sst.grb.$nfield
    $EXECwgrib -s $gribfile | egrep TMP:sfc | $utilexec/wgrib -i -grib $gribfile -o sst.grb -append
    [ $? -ne 0 ] && export err=`expr $err + 1`
    nfield=`expr $nfield + 1`
done
err_chk

$EXECwgrib sst.grb -4yr -s -d all -ieee -o sst.i3e > sst.info
cat sst.info


export pgm=godas_v1_sst2g3i
. prep_step

export XLFUNIT_11="sst.i3e"
export XLFUNIT_12="sst.info"
export XLFUNIT_14="$FIXgodas/godas_v1_tmask.gbl"
export XLFUNIT_31="namelist.cntrlatm"
export XLFUNIT_51="sst.mom"

startmsg

$EXECsst2g3i $model_start_day >> $pgmout 2> errfile
export err=$?;err_chk

if test "$SENDCOM" = 'YES'
then
  cp sst.mom $COMOUT/sst.mom
fi



#####################################################################
# GOOD RUN
set +x
echo "************** $job COMPLETED NORMALLY ON THE IBM SP"
echo "************** $job COMPLETED NORMALLY ON THE IBM SP"
echo "************** $job COMPLETED NORMALLY ON THE IBM SP"
set -x
#####################################################################

msg="$job HAS COMPLETED NORMALLY!"
postmsg "$jlogfile" "$msg"

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