#!/bin/sh
###############################################################################
# This script creates analyses fields of Mass Heat and Interface              #
# perturbation along layers based on                                          #
# observed  profiles of temperature and salinity                              #
#   Input:                                                                    #
#   $1             central date                                               #
#   data_archive   list of archives datasets                                  #
#   field          label for subdirectory and file names related to CTD       #
#   DATA           at input: top directory to work directory.                 #
#   PDYp1,PDY, PDYm1...PDYm7  dates off set by [+1, 0, -1,...-7]              #
#   offset_in_hours_to_collect_obs                                            #
#                  just what it says it is                                    #
#
###############################################################################
set -x

echo "*** Started script $0"

# 0.  Preparations
# 0.a Basic modes of operation

  assimdate=$1
  echo assimilating CTD for ${assimdate}

  mkdir -p $DATA/${field}
  export DATA=$DATA/${field}
  cd $DATA
  echo "Current working directory is now $DATA"

  ${utilscript}/setup.sh

#  1 Obtain date related parameters:
#  1.a start_collection_date = assimdate  -  offset_in_hours_to_collect_obs

   start_collection_date=`${utilexec}/ndate -${offset_in_hours_to_collect_obs} ${assimdate}00`
   start_collection_date=`echo $start_collection_date | cut -c1-8`
 
#  1.b  t0 is the equivalent in model days of asssimdate

   export t0=`$utilscript/date_normal2hycom.sh ${assimdate}$cyc`


# --------------------------------------------------------------------------- #
# 2. get profiles observations each platform 
# 
# 2.a  used in ofs_dcodmobs.f: level=-99 => use all available levels in the data
#      otherwise, use only the specified level.
#      
   export level=-99.

# 2.b  environment variables for the following script are defined in the
#      calling script to ofs_ctd_assim.sh
#      includes time related environment variables, see 1.a above and
#      data_archive and dumpdb

   ${USHofs}/ofs_get_mobs.sh $start_collection_date ${assimdate} || exit -1

# --------------------------------------------------------------------------- #
# 3. run vector box qc check, and create z-profiles and layer-profiles for obs in 
#     each of  the data archives


   rm -rf ${field}.${assimdate}.box.txt 
   rm -rf ${field}.profiles.${assimdate}.txt 

#  set filename for obs layer records
   export DPFN=${field}.layers.${assimdate}.txt

   rm -rf ${DPFN}

   for p in $data_archive
   do
     export infile=${field}.${assimdate}.${p}.txt

     if [ -s ${infile} ]
     then
#    3.a  run vector box qc check 

	 export profiler=${p}
	 export platform=${field}ALL
	 export ctdfile=${field}.profiles.${p}.${assimdate}.txt
	 export numobsctdfile=num_${ctdfile}

         cat ${ctdfile} |wc -l > ${numobsctdfile}

	 export qc_outfile=${field}.${p}.${assimdate}.box.txt
	 export climo_month=`echo ${assimdate} | cut -c5-6`

         ${USHofs}/ofs_boxqcv.sh 

#    3.b transform observation profiles (in z-levels) to profiles in model layers
         export z_outfile=${field}.layers.${p}.${assimdate}.txt
         export z_monitorfile=${field}.monitor.${p}.${assimdate}.txt

         ${USHofs}/ofs_z2layer.sh 

#    3.c add to final files
	 if [ -s ${qc_outfile} ]
	     then
	     cat ${qc_outfile} >> ${field}.${assimdate}.box.txt 
	 else
	     echo "file ${qc_outfile} is missing."
	 fi

	 if [ -s ${ctdfile} ]
	 then
	     cat ${ctdfile} >> ${field}.profiles.${assimdate}.txt
	 else
	     echo "file ${ctdfile} is missing."
	 fi

	 if [ -s ${z_outfile} ]
	 then
	     cat ${z_outfile} >> ${DPFN}
	 else
	     echo "file ${z_outfile} is missing."
	 fi

     else
	 echo " $p has no observations "
	 echo "  ${infile} is missing.  "
     fi
   done

   if [ -s ${DPFN} ]
   then

#--------------------------------------------------------------------------- #
#4. Combine obs layers with model layers
#4.a Construct file with names of model archives

    ${USHofs}/ofs_filenames.sh

#4.b  Combine observation layers and model layers at observation points

    export IHEAD=10 # number of header lines in the archive .b file 
    export POFN=ctds_combined.$PDY
    export FLIST=filenames

    cp  $FIXofs/${RUN}_${modID}.${gridres}.regional.grid.b regional.grid.b

#NOTE   ofs_ab2prof uses the macros KDM (defined in jobs) and 
#       DPFN (defined in 3 above)

    ${USHofs}/ofs_ab2prof.sh 

#--------------------------------------------------------------------------- #
# 5. generate innovation vector from data processed today and in previous days

# 5.a setup parameters for inova_v

   export OBSFILE=${POFN}
   export OUTFILE_PRE=CTD.
   export OUTFILE_POST=.${enddate}.inova.txt
   export OPTION=123

# 5.b form innovation vector for obs collected today

   ${USHofs}/ofs_inova_v.sh

  fi

# 5.c Combine today innovation vector with old innovation vector

  comdir=`dirname $COMIN`
  export DIR_OUTFILE_OLD=$comdir/$RUN.$PDYm1
  export OUTFILE_POST_OLD=.${PDYm1}.inova.txt
  export OUTFILE_PREF=${field}.

  export NDAYSKEEP=30 # number of days to keep observation data

# 
  typeset -Z2 kk
  for cap in D T Z 
  do
    kk=01
    while [ $kk -le ${KDM} ]
    do
      nfi=CTD.${cap}${kk}.${enddate}.inova.txt
      if [ -s $nfi ]
      then
        export ident=`ls -C1 ${nfi} | cut -d. -f2`
        export OBSFILE=${nfi}
        export OBSFILE_OUT=${OUTFILE_PREF}${ident}${OUTFILE_POST}
        export OBSFILE_OLD=${RUN}_${modID}.${cycle}.${OUTFILE_PREF}${ident}${OUTFILE_POST_OLD}
        export TODAY=${t0}
        ${USHofs}/ofs_merge_inova.sh     
      else
        ctdm1=$COMINm1/${RUN}_${modID}.${cycle}.ctd.${cap}${kk}.${PDYm1}.inova.txt
        if [ -s $ctdm1 ]
        then
          cp $ctdm1 ${RUN}_${modID}.${cycle}.ctd.${cap}${kk}.${PDY}.inova.txt
        fi
      fi
    let kk=kk+1
    done
  done

#oldway
#  files=`ls ${OUTFILE_PRE}???${OUTFILE_POST}`
#  for nfi in $files 
#  do
#     export ident=`ls -C1 ${nfi} | cut -d. -f2`
#     export OBSFILE=${nfi}
#     export OBSFILE_OUT=${OUTFILE_PREF}${ident}${OUTFILE_POST}
#     export OBSFILE_OLD=${RUN}_${modID}.${cycle}.${OUTFILE_PREF}${ident}${OUTFILE_POST_OLD}
#     export TODAY=${t0}
#     ${USHofs}/ofs_merge_inova.sh     
#  done
#oldway

#
# 6.  create analyses of anomalies for each level with data and for D|T|Z
# 6.a get files from FIX

  rm -rf   regional.grid.a regional.grid.b
  ln -s -f $FIXofs/${RUN}_${modID}.${gridres}.regional.grid.b regional.grid.b
  ln -s -f $FIXofs/${RUN}_${modID}.${gridres}.regional.grid.a regional.grid.a


  files=`ls ${OUTFILE_PREF}???${OUTFILE_POST}`
  for nfi in $files 
  do
     export ident=`ls -C1 ${nfi} | cut -c 5-7 `
     export OBSFILE=${nfi}
     export OBSFILE_OUT=${OUTFILE_PREF}${ident}.${enddate}.spread.txt
     export FLD_NAME=${OUTFILE_PREF}${ident}.${enddate}.spread.bin
     export fac_unit=1.0
     export record_no=8

     if [ -s ${OBSFILE} ]
       then
#--------------------------------------------------------------------------- #
# 6.b. run 2dvar for [D|T|Z] for each level [00:25] where data is available
#

       ${USHofs}/ofs_spread.sh
     else
       touch ${OBSFILE_OUT}
       touch ${FLD_NAME}
     fi
   done

   msg='THE OFS_CTD_ASSIM job  HAS ENDED NORMALLY.'
   postmsg "$jlogfile" "$msg"

"*** Finished script $0"

