#! /bin/sh
###############################################################################
#                                                                             #
# This script creates the river forcing files  for OFS_atl model              #
#                                                                             #
# combining observations and climatology.                                     #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #

echo "*** Started script $0"

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

set -x

export XLFRTEOPTS="unit_vars=yes"

# 1. get usgs data from tanks.
# use dumpmb?
#
cp ${TANK}/$srtdate/b001/xx009  usgs.$srtdate
    

# 2. decode data from the tanks
#
    export XLFUNIT_11="usgs.$srtdate"
    export XLFUNIT_60="rivers.$srtdate"
    export XLFUNIT_61="counts_rivers.$srtdate"

    ${EXECofs}/ofs_getdata_river >> $pgmout 2> errfile
    export err=$?; err_chk

# 3. collect data used and assign qc flag


    cp $PARMofs/${RUN}_${modID}.ident.table ./ident.table

    export XLFUNIT_10="rivers.$srtdate"
    export XLFUNIT_11="counts_rivers.$srtdate"
    export XLFUNIT_13="ident.table"
    export XLFUNIT_60="qcrivers.$srtdate"
    ${EXECofs}/ofs_qc_river >> $pgmout 2> errfile
    export err=$?; err_chk

# 4. Convert river discharge to  precipitation on a model cell
#    Combine climotological river 'precipitation'  with USGS 'precipitation'.

    cp $FIXofs/${RUN}_${modID}.station_climo.dat ./station_climo.dat
    
    nclimo=`wc -l station_climo.dat | awk '{print $1}'`

cat > numclimo.in <<EOF
$nclimo
EOF

    if [ -s qcrivers.$srtdate ] ; then
       cp $FIXofs/${RUN}_${modID}.${gridlabel}pscx.bin   ${gridlabel}pscx.bin  
       export XLFUNIT_10="regional.grid.b"
       export XLFUNIT_12="${gridlabel}pscx.bin"
       export XLFUNIT_14="qcrivers.$srtdate"
       export XLFUNIT_53="numclimo.in"
       export XLFUNIT_56="stationqc.dat"
       export XLFUNIT_55="stationcl.dat"
       export XLFUNIT_57="station_climo.dat"
       ${EXECofs}/ofs_get_station >> $pgmout 2> errfile
       export err=$?; err_chk

       if [ -s stationqc.dat ] ; then
         cat stationcl.dat stationqc.dat > station.$srtdate.dat
       else
         cp stationcl.dat station.$srtdate.dat
       fi
    else
       echo '*** WARNING: No usgs rivers found, using climo rivers only '
       mv station_climo.dat station.$srtdate.dat
    fi


# 5. Converts precipitation in a set of model cell to a field. 


nrivers=`wc -l station.$srtdate.dat | awk '{print $1}'`


cat > rivers.in <<EOF
riverflow
station.$srtdate.dat
regional.depth
forcing.rivers
Rivers from USGS & RIVIS climatology.
${nrivers}  'num-stations' = number of stations in stat_in file
EOF


#--cp $FIXofs/regional.depth.a $DATA
#--cp $FIXofs/regional.depth.b $DATA


${EXECofs}/ofs_rivers < rivers.in >> $pgmout 2> errfile
export err=$?; err_chk

# the output is the regional.rivers.[ab]

echo "*** Finished script $0"
