####  UNIX Script Documentation Block
#
# Script name:   bufr_dump_sat.sh   Driver for 1B(HIRS,MSU,AMSU-A/B)SBUV dump
#
# Author:        Dennis Keyser      Org: NP22         Date: 2000-06-12
#
# Abstract: This script is a driver for the 1B (HIRS, MSU, AMSU-A/B) and
#   SBUV satellite database dump utility.  It's main purpose is to
#   output 1B (HIRS, MSU, AMSU-A/B) and SBUV satellite data dumps to the
#   appropriate /com directory and path file naming structure used by the
#   operational NCEP networks.  The dumping of any number of these
#   satellite data types can thus be accomplished for all NCEP networks
#   via a single line in the analyses scripts which execute this driver
#   script. Upon completion of the dumping of requested data types, a
#   status file can be generated (by request) and written to the same
#   path as the dump files.  The ASCII status file contains a summary
#   of report counts for all requested subtypes that have been dumped.
#   It also contains information on the return code for each data type
#   dumped.  An alternate form for this script can be chosen (via a
#   positional parameter) which will output the data dumps in a format
#   that is not tied to the /com structure (suitable for non-operational
#   "over-the-counter" type runs).
#
# Script history log:
# 1998-03-31  Bert Katz   -- original version for implementation
# 1998-04-22  D. Keyser   -- minor modifications for implementation
# 1998-10-20  D. Keyser   -- changes to account for new NOAA-15 HIRS1B
#                and AMSU dumps
# 1999-02-03  D.A. Keyser -- Total y2k compliance as central date (input
#                script positional parameter 1) is now ALWAYS expected to
#                contain a 4-digit year
# 2000-01-19  D.A. Keyser -- Changes to account for new NOAA-11 SBUV
#                (ozone) dumps; modified to run on IBM SP
# 2000-06-12  D.A. Keyser -- Changes to reduce r.c. = 22 to r.c. = 05 for
#                data types that are OFTEN or ALWAYS expected to be missing
#                (necessary now that the Nam and NDAS networks are dumping
#                these data)
#
# Usage: bufr_dump_sat.sh
#
#   Input script positional parameters:
#     yyyymmddhh $1  - center of time window for dump
#     hh<.hh>    $2  - radius of time window for dump
#     ntype      $3  - switch to indicate format for output data sets
#     dtyp1      $4  - data type 1 
#     dtyp2      $5  - data type 2
#     ...             ...
#     dtypN      $#  - data type N
#      
#            Valid current and past(*) data types are as follows :
#
#          * h1bn11 : NOAA-11 HIRS 1b data with remapped MSU 1b data
#          * m1bn11 : NOAA-11 MSU 1b data (original, not remapped)
#          * h1bn12 : NOAA-12 HIRS 1b data with remapped MSU 1b data
#          * m1bn12 : NOAA-12 MSU 1b data (original, not remapped)
#            h1bn14 : NOAA-14 HIRS 1b data with remapped MSU 1b data
#            m1bn14 : NOAA-14 MSU 1b data (original, not remapped)
#            h1bn15 : NOAA-15 HIRS 1b data
#            a1bn15 : NOAA-15 AMSU/A 1b data
#            b1bn15 : NOAA-15 AMSU/B 1b data
#            h1bn16 : NOAA-16 HIRS 1b data
#            a1bn16 : NOAA-16 AMSU/A 1b data
#            b1bn16 : NOAA-16 AMSU/B 1b data
#          * sbvn09 : NOAA-09 SBUV data
#          * sbvn11 : NOAA-11 SBUV data
#          * sbvn14 : NOAA-14 SBUV data
#            sbvn16 : NOAA-16 SBUV data
#
#       * - Must allow this script to process obsolete data types, since
#           historical reruns may occur!!!!
#
#   Imported Shell Variables:
#     These apply for positional parameter "ntype" set to either 1 or 2 
#      and must ALWAYS be exported from the parent script --
#
#     DATA        - if "ntype" = 1: string indicating the initial
#                   (temporary) directory path to output data 
#                   destination
#                   if "ntype" = 2: string indicating final directory 
#                   path to output data destination
#     NET         - string indicating system network (i.e., "arc", "gfs", 
#                   "nam", "gdas", "ruc", "rucs") -- to echo in status file
#
#     These apply for positional parameter "ntype" set to either 1 or 2
#      and will be set to their default value in this script if not 
#      exported from the parent script --
#
#     STATUSsat   - string: if = 'YES' will generate a status file 
#                   which is written to the same path as the dump 
#                   files.  This is written out after all dump files 
#                   have been created, and contains both report counts 
#                   (by subtype) and return code information for each 
#                   data type dumped.
#                   Default is "YES"
#                   NOTE: If this script is executed more than one 
#                         time by the parent script, "STATUSsat" should 
#                         be imported as "NO" unless this is the FINAL 
#                         execution of this script.  The status file 
#                         will always contain information from all 
#                         previous runs of this script by a particular 
#                         parent script.
#     envir       - string indicating environment under which job runs
#                   (either "prod" or "test")
#                   Default is "prod"
#     DUMPsat     - string indicating path for 1B/SBUV satellite dump script
#                   file
#                   Default is "/nw${envir}/ush/satdump"
#     DUEXsat     - string indicating directory path for 1B and SBUV
#                   satellite dump script executables
#                   Default is "/nw${envir}/exec"
#     DUSCsat     - string indicating directory path for 1B and SBUV
#                   satellite dump script ush scripts
#                   Default is "/nw${envir}/ush"
#     TANKsat     - string indicating path to home directory containing 1B
#                   and SBUV satellite data base
#                   Default is "/dcom/us007003"
#     LOUDsat     - string: if != 'off' will turn on script trace (set -xS)
#                   in 1B/SBUV satellite dump script ($DUMPsat) execution
#                   (does NOT apply to this script)
#                   Default is "off"
#     jlogfile    - string indicating path for sms joblog file
#                   Default is null string of length zero
#     job         - string indicating the job number of the parent script
#                   (i.e., "jgfs_dump_12")
#                   Default is "j????"
#     cycle       - string indicating the analysis time embedded in $COMSP
#                   (i.e., "t00z" or "t18z")
#                   Default is "t??z"
#
#     These apply ONLY for positional parameter "ntype" set to 1 and
#      must ALWAYS be exported from the parent script --
#
#     COMSP       - string indicating the final directory/filename path 
#                   to output data destination
#                   (e.g., "/com/gfs/prod/gfs.20000110/gfs.t12z.")
#     tmmark      - string indicating hour for center time for dump
#                   relative to the analysis time embedded in COMSP
#                   (e.g., "tm12", "tm09", "tm06", "tm03", "tm00")
#
#     These apply ONLY for positional parameter "ntype" set to 1 and
#      will be set to their default value in this script if not exported
#      from the parent script --
#
#     SENDCOM     - string: if = 'NO' will override choice to send 
#                   output files to COMSP directory/filname path and 
#                   will instead send files to DATA directory path
#                   Default is "YES"
#     pgmout      - string indicating path for standard output file 
#                   (output always contatenated onto this file)
#                   Default is "$DATA/allout"
#
#   Modules and files referenced:
#     scripts    :  $DUMPsat
#
# Remarks:
#
#   Values for input positional parameter "ntype":
#     ntype = 1 : Output dump data set will be in the form (see - @):
#                     ${COMSP}dtypX.${tmmark}.ieee_d
#                 (See above for usual string appearance for $COMSP and 
#                  $tmmark; dtypX is input positional parameter mnemonic 
#                  associated with a particular data type)
#                 This is the normal path structure for NCEP
#                  analysis/forecast network output.
#               @ - If imported shell variable "SENDCOM" is set to 'NO',
#                   then output path above is overridden and output
#                   dump data set will be in the form:
#                     ${DATA}/dtypX.${tmmark}.ieee_d
#
#     ntype = 2 : Output dump data set will be in the form:
#                     ${DATA}/dtypX.${1}.twNN.ieee_d
#                 ($DATA is as imported from parent script; dtypX is as
#                  defined for ntype=1 above; ${1} is input center 
#                  yyyymmddhh time; NN is input time window ($2) 
#                  multiplied by 10)
#                  This is path structure for non-operational "over-the-
#                   counter" type runs.
#
#   Condition codes (for c.code < 254, the highest return code from the
#                    individual data type dumps is returned here):
#      0 - all requested data types found and all data properly dumped
#      5 - all data types in input list valid, however at least one data
#           type in the list had no data dumped (missing) and either:
#             1) all types which had no data dumped are currently EXPECTED
#                 to dump no data (missing) SOME or ALL of the time at the
#                 dump cycle time
#                     -- or --
#             2) all types which had no data dumped are currently NOT
#                 ASSIMILATED by the appropriate analysis
#            In either case does not indicate a problem.
#     11 - all data types in input list valid, and data dumped from all
#           data types in list, however at least one type contained data
#           records that could not be processed
#     22 - all data types in input list valid, however at least one data
#           type in list had no data dumped (missing) even though is was
#           EXPECTED to dump data ALL of the time at the dump cycle time;
#           indicates a problem
#    254 - bad instrument identifier in one or more of the data types
#           in input list  - no data types are dumped
#    255 - bad satellite identifier in one or more of the data types
#           in input list  - no data types are dumped
#  
####

set -aux

cd $DATA

#############################################################################
#############################################################################
#        EXECUTE THE 1B (HIRS, MSU, AMSU-A/B) AND SBUV DUMP SCRIPT
#############################################################################
#############################################################################

#============================================
cendat=$1
timwin=$2
ntype=$3
job=${job:-j????}
cycle=${cycle:-t??z}
set +aux
echo
echo "job = " $job
echo "cycle = " $cycle
echo
set -aux
#============================================
shift 3

#--------------------------------------------------------------------------
#  The arguments for the 1B/SBUV Satellite DUMP script are described below.
#--------------------------------------------------------------------------

STATUSsat=${STATUSsat:-YES}
echo
echo "STATUSsat = " $STATUSsat
echo
envir=${envir:-prod}
DUMPsat=${DUMPsat:-/nw${envir}/ush/satdump}
DUEXsat=${DUEXsat:-/nw${envir}/exec}
DUSCsat=${DUSCsat:-/nw${envir}/ush}
TANKsat=${TANKsat:-/dcom/us007003}
LOUDsat=${LOUDsat:-off}

#---------------------------------------------------------------------------
# DUMPsat script   The center of  The radius of  Data Group Names:
# accepts these    the time       the time
# arguments:       window:        window:
#
# Example.         yyyymmddhh<.hh>  hh<.hh>        dgrp1 dgrp2 ... dgrpN
#---------------------------------------------------------------------------

$DUMPsat           $cendat          $timwin        $*     > outout 2>errfile

errdmp=$?

jflag=0
if [ "$errdmp" -gt '22' ]; then

   msg="**WARNING: One or more 1B/SBUV satellite data types not known, no \
data dumped (RC=$errdmp) --> non-fatal"
set +u
   [ -n "$jlogfile" ] && postmsg "$jlogfile" "$msg"
set -u
   echo "$msg" >> outout
   echo "$msg" >> status1.out
   jflag=1

fi

ls -l $DATA/*.f77

errt=0

if [ "$jflag" = '0' ]; then

# Loop through all types being dumped - assign proper return code to
#  each dumped data type

cat <<EOFds1 >> status1.out

-------------------------------------------------------------------------------

1B (HIRS, MSU, AMSU-A/B), SBUV Satellite Dump Script run for data types:
 $*

EOFds1

typeset -Z2 chr
chr=`expr $cendat % 100`

for n
do
   echo "                                 -- Data Type $n --" >> status1.out

   errn=`grep "Dump Status: $n" outout | cut -f2 -d=`
   if [ "$errn" -eq '0' ];then
msg="$n dump SUCCESSFUL, all subtypes contain reports (RC=$errn)"
   elif [ "$errn" -eq '22' ];then
msg="**WARNING: $n dump EMPTY (RC=$errn)"
      if [[ $job = jnam_dump_?? ]];then
         if [ "$chr" != '12' ];then
            if [ $n = m1bn14 -o $n = h1bn14 ];then
# Note: For Job nam_dump at all cycle times other than 12Z, NOAA-14 MSU
#       and NOAA-14 HIRS data are expected to OFTEN be missing
#       - the return code of "22" is reduced to "5" here
               errn=5
            fi
            if [ "$chr" = '06' ];then
               if [ $n = a1bn16 -o $n = b1bn16 -o $n = h1bn16 ];then
# Note: For Job nam_dump at cycle time 06Z, NOAA-16 AMSU-A, NOAA-16
#       AMSU-B, and NOAA-16 HIRS data are expected to OFTEN be missing
#       - the return code of "22" is reduced to "5" here
                  errn=5
               fi
            fi
         fi
      elif [ $job = jndas_tm03_dump_12 ];then
         if [ $n = a1bn15 -o $n = b1bn15 -o $n = h1bn15 ];then
# Note: For Job ndas_tm03_dump at cycle time 12Z, NOAA-15 AMSU-A, NOAA-15
#       AMSU-B, and NOAA-15 HIRS data are expected to OFTEN be missing
#       - the return code of "22" is reduced to "5" here
            errn=5
         fi
      elif [ $job = jndas_tm03_dump_06 ];then
         if [ $n = a1bn16 -o $n = b1bn16 -o $n = h1bn16 ];then
# Note: For Job ndas_tm03_dump at cycle time 06Z, NOAA-16 AMSU-A, NOAA-16
#       AMSU-B, and NOAA-16 HIRS data are expected to OFTEN be missing
#       - the return code of "22" is reduced to "5" here
            errn=5
         fi
      elif [ $NET = gfs -o $NET = cdas ];then
# Note: For Jobs gfs_dump, gdas_dump and cds_dump at all cycle times - these
#       dumps are no longer assimilated and are LAME DUCKS! - the return code
#       of "22" is reduced to "5" here
         errn=5
      fi
   elif [ "$errn" -eq '11' ];then
msg="**WARNING: $n dump INCOMPLETE, 1 or more records not processed (RC=$errn)"
   else
msg="**WARNING: $n dump RETURN CODE NOT RECOGNIZED, status unknown (RC=$errn)"
   fi

   [ "$errt" -lt "$errn" ]  &&  errt=$errn

   [ "$errn" -eq '5' ]  &&  \
    msg="$n dump EMPTY as expected at this time (RC=$errn)"

set +u
   [ -n "$jlogfile" ] && postmsg "$jlogfile" "$msg"
set -u
   echo "$msg" >> outout
   echo "$msg" >> status1.out
   echo " " >> outout
   echo " " >> status1.out
set +aux
echo
echo "The foreground exit status for satdump, $n only, is " $errn
echo
set -aux
done

msg="==> The overall return code for this run of satdump is $errt"

echo "$msg" >> outout
echo "    " >> outout
echo "$msg" >> status1.out
echo "    " >> status1.out

errdmp=$errt

#.......................................................................

if [ "$LOUDsat" != 'off' ];then
   set +aux
   echo
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo "------------------------------------------------------------------------"
echo "     LOUDsat != off - below is script trace for this run of satdump"
echo "------------------------------------------------------------------------"
   echo
   cat errfile
   echo
echo "------------------------------------------------------------------------"
echo "             end of script trace for this run of satdump"
echo "------------------------------------------------------------------------"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
   echo
   set -aux
else
   cat errfile >> outout
   cat errfile >> status1.out
fi
rm errfile

set +aux
echo
echo "The foreground exit status for satdump, ALL TYPES, is " $errdmp
echo
set -aux
if [ "$errdmp" -gt '4' ];then
set +aux
   echo
   echo "*******  ERROR SCRIPT satdump RETURN CODE $errdmp  *******"
   echo
set -aux
else
set +aux
   echo
   echo "----------------------------------------------------"
   echo "************  COMPLETED SCRIPT satdump  ************"
   echo "----------------------------------------------------"
   echo
set -aux
fi

#fi for $jflag=0 test
fi


#  Move output files to proper naming structure (based on "ntype")
#  ---------------------------------------------------------------

errmv=0

if [ "$ntype" = '2' ];then

   rm $DATA/allout
   cat outout
   cat outout > $DATA/allout
   rm outout
   echo $timwin | cut -c1-1 > out1
   echo $timwin | cut -c3-3 > out3
   Cout=`cat out3`
   if [ "$Cout" != '5' ];then
      Cout=0
      echo $Cout > out3
   fi
   timx10=`paste -d"\0" out1 out3`
   rm out1 out3

   for n
   do
      cat $DATA/${n}.out
      cat $DATA/${n}.out >> $DATA/allout
      rm  $DATA/${n}.out
      if [ -s $DATA/${n}.f77 ];then
         mv  $DATA/${n}.f77 $DATA/${n}.${cendat}.tw${timx10}.ieee_d
         errmvt=$?
         errmvl=$errmv
         errmv=`expr $errmvl + $errmvt`
      fi
   done
   pgmout=$DATA/allout

else

#============================================
   SENDCOM=${SENDCOM:-YES}
   pgmout=${pgmout:-$DATA/allout}
set +aux
   echo
   echo "SENDCOM = " $SENDCOM
   echo
   echo "pgmout = " $pgmout
   echo
set -aux
#============================================

   for n
   do
      if [ "$jflag" = '0' ];then
         cat $DATA/${n}.out >> outout
         rm  $DATA/${n}.out
      fi

      if [ -s $DATA/${n}.f77 ];then
         if [ "$SENDCOM" = 'NO' ];then
            mv  $DATA/${n}.f77 $DATA/${n}.${tmmark}.ieee_d
            errmvt=$?
         else
            mv  $DATA/${n}.f77 ${COMSP}${n}.${tmmark}.ieee_d
            errmvt=$?
         fi
         errmvl=$errmv
         errmv=`expr $errmvl + $errmvt`
      else
         [ "$SENDCOM" = 'YES' ]  &&  cp /dev/null ${COMSP}${n}.${tmmark}.ieee_d
      fi
         
   done
   cat outout >> satdump.out
   cat outout >> $pgmout
   rm outout

fi


#############################################################################
#############################################################################
#               GENERATE THE STATUS FILE (IF REQUESTED)
#############################################################################
#############################################################################

if [ "$STATUSsat" = 'YES' ];then
#  Generate status file to alert that all 1B/SBUV dumps for this time have
#   been produced - also summarizes substype report counts and data type
#   return code information

cat <<\EOFblank > blank
  
  
  
EOFblank
WALLCLOK=`date -u`

if [ "$job" = 'j????' ]; then
   run=???
elif [[ $job = jndas_tm??_dump_?? ]]; then
   run=ndas
elif [[ $job = jruc2a_dump_?? ]]; then
   run=ruc2a
else
   run=$NET
fi

cat <<EOFs1 > status.out
                           
  #### INFORMATION REGARDING ${tmmark} 1B (HIRS, MSU, AMSU-A/B), SBUV DATA DUMP FOR ${cycle} ${run} RUN ####
                              PARENT SCRIPT IS: $job

                             CENTRAL DATE IS $cendat
                   
               THIS STATUS FILE CREATED $WALLCLOK

           THIS CREATION TIME MUST BE EARLIER THAN THE START TIME FOR ANY
     OPERATIONAL ANALYSES JOBS THAT READ FROM THESE 1B/SBUV SATELLITE DATA DUMP
                      FILES (i.e., Jobs 220, 281, 530, 820, 850)
                   
*******************************************************************************

          ++++ NUMBER OF REPORTS DUMPED BY 1B/SBUV SATELLITE DATA TYPE ++++
                      TIME WINDOW RADIUS IS DATA GROUP DEPENDENT         
               GEOGRAPHICAL DOMAIN IS ALWAYS GLOBAL FOR ALL DATA GROUPS
                   
EOFs1
   for n
   do
      grep -e "--> HIRS1B type $n" \
           -e "--> HIRS3  type $n" \
           -e "--> MSU1B  type $n" \
           -e "--> AMSUA  type $n" \
           -e "--> AMSUB  type $n" \
           -e "--> SBUV   type $n" $pgmout >> status.out
      echo " " >> status.out
   done
cat <<\EOFs2 >> status.out
                           
                           
*******************************************************************************

       ++++ SPECIFIC INFORMATION ON EACH 1B/SBUV SATELLITE DATA TYPE DUMP ++++ 
                   
EOFs2
   cat status1.out >> status.out
   rm status1.out

   if [ "$ntype" = '1' ];then
      if [ "$SENDCOM" = 'NO' ];then
         cat status.out > $DATA/status.${tmmark}.ieee_d
         errmvt=$?
      else
         cat status.out > ${COMSP}status.${tmmark}.ieee_d
         errmvt=$?
      fi
   else
      cat status.out > $DATA/status.${cendat}.tw${timx10}.ieee_d
      errmvt=$?
   fi
   errmvl=$errmv
   errmv=`expr $errmvl + $errmvt`

fi

errtot=`expr $errdmp + $errmv`

if [ "$errtot" -gt 0 ];then
   exit $errtot
else
   exit 0
fi
