#!/bin/sh
###############################################################################
#                                                                             #
# This script creates the assimilation for OFS_atl model                      #
#                                                                             #
#  inova.sh : obtains innovation vector                                       #
#   exported in:                                                              #
#      OBSFILE - observations in box format                                   #
#      MODFILE - file [archive or restart] with background field.             #
#      label - field label (including separator)                              #
#      fac_unit - fac_scale*fld matches obs scale                             #
#      fac_out  - if |fac_scale*fld-obs| < fac_out*obs_std is accepted        #
#      fac_err  - error=e_obs*fac_error                                       #
#                                                                             #
#                                                                             #
###############################################################################

echo "*** Started script $0"

set -x

# 0.b set parameters for inova stdin file 
export num_obs=`cat ${OBSFILE}| wc -l`

# --------------------------------------------------------------------------- #
# 1.  Set input files
# 1.a  construct standard input file 
#      in the third item the character to indicate 
#                   end of label should be non-blank character. 
#       Usually '=' or ':'


/bin/rm -f inova.in
cat > inova.in <<EOF
${MODFILE}
$num_records   number of records in header of ${MODFILE}.b
$label
1    level number
${fac_unit}   factor for units
${fac_out}    qc_factor
${fac_err}    error factor
${OBSFILE}
${num_obs}
EOF

# --------------------------------------------------------------------------- #
# 2.  execute inova and set output files
# 2.a execute inova

$EXECofs/ofs_inova < inova.in >> $pgmout 2> errfile
export err=$?; err_chk

# 2.b  setup output data and field files

mv fort.60 ${field}.${srtdate}.t${cyc}z.inova.txt
# this file is in the .box.txt format

mv fort.81 ${field}.${srtdate}.t${cyc}z.inova.bin


echo "*** Finished script $0"
