#!/bin/sh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         global_satcount.sh
# Script description:  Collect counts of received, selected, and assimilated 
#                      satellite observations from global data assimilation
#                      system
#
# Author:        Russ Treadon       Org: NP23         Date: 2005-08-26
#
# Abstract: This script collect counts of received, selected, and assimilated
#           satellite observations from global data assimilation system.
#
#           Received, selected, and assimilated data counts are collected
#           from three type of input files from either $NET=gfs or gdas:
#            a) script output from operational dump jobs.  Script
#               output for cycle date $CDATE is taken from file
#               /com/output/prod/${CDATE | cut -c1-8}/${NET}_dump_$HH.o*
#            b) data counts provided in the GFS/GDAS dump status files
#               (/com/gfs/prod/${NET}.${CDATE | cut -c1-8}/
#                     (gfs or gdas1).t${HH}z.status.tm00.bufr_d
#            c) data counts provided in the analysis (SSI or GSI) status
#               file (/com/gfs/prod/${NET}.${CDATE | cut -c1-8}/
#                     (gfs or gdas1).t${HH}z.ssistat (gsistat)
#
#
#   ***WARNING***WARNING***WARNING***WARNING***WARNING***WARNING***WARNING***
#
#        This script uses the unix grep, tail, and head commands to 
#        extract specific lines from the above three input files.
#        Any change in the structure, wording, location of the
#        information that this script looks for in the above input
#        files may (is likely to) render output from the script
#        (ie, the data count file) useless.
#        
#        The code, global_satcount, that this script invokes uses formatted
#        reads to extract various numeric values which are then combined 
#        to produce a table of received, selected, and assimilated 
#        satellite data counts.  As such, variations in the location of 
#        numeric fields in any of the above files may (is likely to) result
#        in meaningless output.
#
#   ***WARNING***WARNING***WARNING***WARNING***WARNING***WARNING***WARNING***
#
# Script history log:
# 2005-08-26  Treadon  initial script
#
# Usage:  global_satcount.sh CDATE SATCNT GSATCNT
#
#   Input script positional parameters:
#     1             Current analysis date in yyyymmddhh format
#                   defaults to $CDATE; required
#     2             Output satellite data count file
#                   defaults to $SATCNT; required
#     3             Output satellite data count file from previous cycle
#                   defaults to $GSATCNT; required
#
#   Imported Shell Variables:
#     EXECGLOBAL    Directory for global executables
#                   defaults to /nwprod/exec
#     DATA          working directory
#                   (if nonexistent will be made, used and deleted)
#                   defaults to current working directory
#     COMIN         input directory
#                   defaults to current working directory
#     COMOUT        output directory
#                   (if nonexistent will be made)
#                   defaults to current working directory
#     COMOUTPUT     operational job runtime output directory
#                   defaults to /com/output/prod/(`echo $CDATE | cut -c1-8`)
#     XC            Suffix to add to executables
#                   defaults to none
#     PREINP        Prefix to add to input observation files
#                   defaults to none
#     SUFINP        Suffix to add to input observation files
#                   defaults to none
#     NCP           Copy command
#                   defaults to cp
#     SATCOUNTEXEC  Satellite count executable
#                   defaults to ${EXECGLOBAL}/global_satcount$XC
#     ANLSTAT       Input ssi (obs-ges), qc, and iteration statistics
#                   defaults to ${COMIN}/${PREINP}ssistat
#     STATUS        Input dump status file
#                   defaults to ${COMIN}/${PREINP}status.tm00.bufr_d${SUFINP}
#     SETUPVARS     Other SETUP namelist inputs to the satcount executable
#                   defaults to none set
#     SATOBSVARS    Other SATOBS namelist inputs to the satcount executable
#                   defaults to none set
#     NET           String indicating system network (either "gfs", "gdas",
#                   "cdas", "cdc", "nam" or "ruc")
#                      NOTE:  This script only works for global networks
#                             gfs and gdas (cdas has not been tested)
#                   defaults to gdas
#     INISCRIPT     Preprocessing script
#                   defaults to none
#     LOGSCRIPT     Log posting script
#                   defaults to none
#     ERRSCRIPT     Error processing script
#                   defaults to 'eval [[ $err = 0 ]]'
#     ENDSCRIPT     Postprocessing script
#                   defaults to none
#     NTHREADS      Number of threads
#                   defaults to 1
#     NTHSTACK      Size of stack per thread
#                   defaults to 64000000
#     FILESTYLE     File management style flag
#                   ('C' to copy to/from $DATA, 'L' for symbolic links in $DATA,
#                    'X' to use XLFUNIT or symbolic links where appropriate)
#                   defaults to 'X'
#     PGMOUT        Executable standard output
#                   defaults to $pgmout, then to '&1'
#     PGMERR        Executable standard error
#                   defaults to $pgmerr, then to '&1'
#     pgmout        Executable standard output default
#     pgmerr        Executable standard error default
#     REDOUT        standard output redirect ('1>' or '1>>')
#                   defaults to '1>', or to '1>>' to append if $PGMOUT is a file
#     REDERR        standard error redirect ('2>' or '2>>')
#                   defaults to '2>', or to '2>>' to append if $PGMERR is a file
#     VERBOSE       Verbose flag (YES or NO)
#                   defaults to NO
#
#   Exported Shell Variables:
#     PGM           Current program name
#     pgm
#     ERR           Last return code
#     err
#
#   Modules and files referenced:
#     scripts    : $INISCRIPT
#                  $LOGSCRIPT
#                  $ERRSCRIPT
#                  $ENDSCRIPT
#
#     programs   : $SATCOUNTEXEC
#
#     fixed data : none
#
#     input data : $ANLSTAT
#                  $STATUS
#                  $DUMPOUT
#                  $GSATCNT
#
#     output data: $SATCNT
#
# Remarks:
#
#   Condition codes
#      0 - no problem encountered
#     >0 - some problem encountered
#
#  Control variable resolution priority
#    1 Command line argument.
#    2 Environment variable.
#    3 Inline default.
#
# Attributes:
#   Language: POSIX shell
#   Machine: IBM SP
#
################################################################################
#  Set environment.
export VERBOSE=${VERBOSE:-"NO"}
if [[ "$VERBOSE" = "YES" ]]
then
   echo $(date) EXECUTING $0 $* >&2
   set -x
fi
#  Command line arguments.
export CDATE=${1:-${CDATE:?}}
export SATCNT=${2:-${SATCNT}}
export GSATCNT=${3:-${GSATCNT}}
#  Directories.
export EXECGLOBAL=${EXECGLOBAL:-/nwprod/exec}
export DATA=${DATA:-$(pwd)}
export COMIN=${COMIN:-$(pwd)}
export COMOUT=${COMOUT:-$(pwd)}
export COMOUTPUT=${COMOUTPUT:-/com/output/prod/`echo $CDATE | cut -c1-8`}
#  Filenames.
export XC=${XC}
export PREINP=${PREINP}
export SUFINP=${SUFINP}
export NCP=${NCP:-/bin/cp}
export SATCOUNTEXEC=${SATCOUNTEXEC:-${EXECGLOBAL}/global_satcount$XC}
export ANLSTAT=${ANLSTAT:-${COMIN}/${PREINP}ssistat}
export STATUS=${STATUS:-${COMIN}/${PREINP}status.tm00.bufr_d${SUFINP}}
export SATCNT=${SATCNT:-${COMIN}/${PREINP}satcnt.tm00.bufr_d${SUFINP}}
#  Other variables.
export ANALCODE=${ANALCODE:-'SSI'}
export SETUPVARS=${SETUPVARS}
export SATOBSVARS=${SATOBSVARS}
export DUMPNET=${DUMPNET:-gdas}
export NTHREADS=${NTHREADS:-1}
export NTHSTACK=${NTHSTACK:-64000000}
export FILESTYLE=${FILESTYLE:-'X'}
export PGMOUT=${PGMOUT:-${pgmout:-'&1'}}
export PGMERR=${PGMERR:-${pgmerr:-'&2'}}
typeset -L1 l=$PGMOUT
[[ $l = '&' ]]&&a=''||a='>'
export REDOUT=${REDOUT:-'1>'$a}
typeset -L1 l=$PGMERR
[[ $l = '&' ]]&&a=''||a='>'
export REDERR=${REDERR:-'2>'$a}
# Set defaults


################################################################################
#  Preprocessing
$INISCRIPT
pwd=$(pwd)
if [[ -d $DATA ]]
then
   mkdata=NO
else
   mkdir -p $DATA
   mkdata=YES
fi
cd $DATA||exit 99
[[ -d $COMOUT ]]||mkdir -p $COMOUT


################################################################################
#  Copy or link i/o files to local working directory

CYCLE=`echo $CDATE | cut -c9-10`
DUMPOUT=`ls -1 $COMOUTPUT/${DUMPNET}_dump_${CYCLE}.o*`

rm anlstat status dumpout

if [[ $FILESTYLE = 'C' ]]
then
   # Input files
   ${NCP:-cp} $ANLSTAT  anlstat
   ${NCP:-cp} $STATUS   status
   ${NCP:-cp} $DUMPOUT  dumpout
   ${NCP:-cp} $GSATCNT  satcnt.run
else
   # Input files
   ln -fs $ANLSTAT  anlstat
   ln -fs $STATUS   status
   ln -fs $DUMPOUT  dumpout
   ln -fs $GSATCNT  satcnt.run
   # Output files
   ln -fs $SATCNT   satcnt.out
fi


################################################################################
#  Extract numbers needed for received, selected, and assimilated data counts
rm satcount* count_* *.quik *.tmi *.ssmi tot*.run

# Pull brightness temperature (rad=radiance), ozone (oz), and rainfall 
# rate data (pcp=precipitation) counts out of analysis status file
grep "o-g 01 rad"  anlstat > satcount_rad.in
grep "o-g 01 "     anlstat | grep " oz"  > satcount_ozn.in
grep "o-g 01 pcp"  anlstat > satcount_pcp.in

# Pull satellite wind data counts out of analysis status file.  
# Differences in file format require separate branches for the 
# SSI and GSI global analysis codes
if [[ $ANALCODE = 'SSI' || $ANALCODE = 'ssi' ]]; then
   grep "o-g 01    uv 24"  anlstat | grep " count " > count_wnd24
   grep "o-g 01    uv 25"  anlstat | grep " count " > count_wnd25
   grep "o-g 01    uv 283" anlstat | grep " count " > count_wndssmi
   grep "o-g 01    uv 285" anlstat | grep " count " > count_wndquik
fi
if [[ $ANALCODE = 'GSI' || $ANALCODE = 'gsi' ]]; then
   grep "o-g 01    uv     24" anlstat | grep " count " > count_wnd24
   grep "o-g 01    uv     25"  anlstat | grep " count " > count_wnd25
   grep "o-g 01    uv     283" anlstat | grep " count " > count_wndssmi
   grep "o-g 01    uv     285" anlstat | grep " count " > count_wndquik
fi
cat count_wnd* > satcount_wnd.in

# Pull out received data counts for satellite winds and brightness
# temperatures from dump status file
grep "(in satwnd) SAT. ID" status > recvd_wnd.in

grep "(in goesnd) SAT. ID" status > recvd_goesnd
grep "(in 1bamua) SAT. ID" status > recvd_1bamua
grep "(in 1bamub) SAT. ID" status > recvd_1bamub
grep "(in 1bhrs2) SAT. ID" status > recvd_1bhrs2
grep "(in 1bhrs3) SAT. ID" status > recvd_1bhrs3
grep "(in 1bhrs4) SAT. ID" status > recvd_1bhrs4
grep "(in 1bmsu ) SAT. ID" status > recvd_1bmsu
grep "(in 1bmhs ) SAT. ID" status > recvd_1bmhs
grep "(in airs  ) SAT. ID" status > recvd_airs
grep "(in geoimr) SAT. ID" status > recvd_geoimr
cat recvd_goesnd recvd_1b* recvd_airs recvd_geoimr > recvd_rad.in

# Pull data counts out of dump status and output files
# QuikScat
grep "TOTAL NUMBER OF INPUT QUIKSCAT REPORTS READ" dumpout > satall.quik
grep "NUMBER OF INPUT QUIKSCAT REPORTS PASSING ALL CHECKS" dumpout > satuse.quik
grep "TOTAL NUMBER OF REPROCESSED QUIKSCAT WIND REPORTS WRITTEN" status  > sprwnd.quik
cat satall.quik satuse.quik sprwnd.quik > satcount.quik

# TRMM TMI
grep "TOTAL NUMBER OF INPUT TRMM TMI REPORTS READ" dumpout > satall.tmi
grep "NUMBER OF INPUT TRMM TMI REPORTS PASSING ALL CHECKS" dumpout > satuse.tmi
grep "TOTAL NUMBER OF REPROCESSED (SUPEROBED) TRMM TMI REPORTS WRITTEN" status  > sprpcp.tmi
cat satall.tmi satuse.tmi sprpcp.tmi > satcount.tmi

# SSM/I products (wind speed and rain rates)
grep "012.001 in data group ssmit"  status   | grep "HAS" > satall.ssmi
grep "TOTAL NUMBER OF SCANS PROCESSED AND RETURNED" dumpout | head -1 > satuse.ssmi
grep "OCEANIC SURFACE WIND SPEED ." dumpout  | head -2 > sprwnd.ssmi
grep "RAINFALL RATE ."              dumpout  | tail -2 > sprpcp.ssmi 
cat satall.ssmi satuse.ssmi sprwnd.ssmi sprpcp.ssmi > satcount.ssmi

# Extract running total counts from previous cycle file
grep "Running total for sat winds"                  satcnt.run > totwnd.run
grep "Running total for sat ozone"                  satcnt.run > totozn.run
grep "Running total for sat rainfalll rates"        satcnt.run > totpcp.run
grep "Running total for sat brightness temperature" satcnt.run > totrad.run
grep "RUNNING TOTAL SUMMED OVER ALL SATOBS TYPES"   satcnt.run > totall.run
cat totwnd.run totozn.run totpcp.run totrad.run totall.run > satcount.run


################################################################################
#  Build namelist to run global_satcount program and then execute program.

export XLSMPOPTS="parthds=$NTHREADS:stack=$NTHSTACK"
export PGM=$DATA/$(basename $SATCOUNTEXEC)
export pgm=$PGM
$LOGSCRIPT

rm satcnt.out

${NCP:-cp} $SATCOUNTEXEC $DATA
     
num_wnd=`cat satcount_wnd.in | wc -l`
num_ozn=`cat satcount_ozn.in | wc -l`
num_pcp=`cat satcount_pcp.in | wc -l`
num_rad=`cat satcount_rad.in | wc -l`

num_wnd_rcvd=`cat recvd_wnd.in | wc -l`
num_rad_rcvd=`cat recvd_rad.in | wc -l`


iy=$(echo $CDATE|cut -c1-4)
im=$(echo $CDATE|cut -c5-6)
id=$(echo $CDATE|cut -c7-8)
ih=$(echo $CDATE|cut -c9-10)

eval $SATCOUNTEXEC <<EOF $REDOUT$PGMOUT $REDERR$PGMERR
 &SETUP
   nsat=42,
   num_wnd=${num_wnd},
   num_ozn=${num_ozn},
   num_pcp=${num_pcp},
   num_rad=${num_rad},
   num_wnd_rcvd=${num_wnd_rcvd},
   num_rad_rcvd=${num_rad_rcvd},   
   yyyy=${iy},mm=${im},dd=${id},hh=${ih},
   dump='${DUMPNET}',
   code='${ANALCODE}',
   $SETUPVARS
 /
 &SATOBS
  obstype(1)='INDIA IR/VIS cloud drift',                  satid(1)=241,  class(1)='wnd',
  obstype(2)='JMA IR/VIS cloud drift',                    satid(2)=242,  class(2)='wnd',
  obstype(3)='EUMETSAT IR/VIS cloud drift',               satid(3)=243,  class(3)='wnd',
  obstype(4)='NESDIS IR cloud drift',                     satid(4)=245,  class(4)='wnd',
  obstype(5)='NESDIS imager water vapor cloud top',       satid(5)=246,  class(5)='wnd',
  obstype(6)='NESDIS imager water vapor deep layer',      satid(6)=247,  class(6)='wnd',
  obstype(7)='NESDIS sounder water vapor cloud top',      satid(7)=248,  class(7)='wnd', 
  obstype(8)='NESDIS sounder water vapor deep layer',     satid(8)=249,  class(8)='wnd',
  obstype(9)='JMA water vapor cloud',                     satid(9)=250,  class(9)='wnd',
  obstype(10)='NESDIS visble cloud drift',                satid(10)=251, class(10)='wnd'
  obstype(13)='EUMETSAT water vapor cloud',               satid(13)=254, class(13)='wnd',
  obstype(15)='INDIA water vapor cloud',                  satid(15)=256, class(15)='wnd',
  obstype(16)='MODIS IR cloud drift',                     satid(16)=257, class(16)='wnd',
  obstype(17)='MODIS water vapor cloud drift',            satid(17)=258, class(17)='wnd',
  obstype(18)='SSM/I neural net 3 wind speed',            satid(18)=283, class(18)='wnd',
  obstype(19)='Quikscat scatterometer',                   satid(19)=285, class(19)='wnd',
  obstype(20)='NOAA-16 SBUV',                             satid(20)=916, class(20)='ozn',
  obstype(21)='NOAA-17 SBUV',                             satid(21)=917, class(21)='ozn',
  obstype(22)='SSM/I rainfall rate',                      satid(22)=264, class(22)='pcp',
  obstype(23)='TRMM TMI rainfall rate',                   satid(23)=211, class(23)='pcp',
  obstype(24)='NOAA-14 HIRS/2',                           satid(24)=14,  class(24)='rad',
  obstype(25)='NOAA-14 MSU',                              satid(25)=214, class(25)='rad',
  obstype(26)='GOES-10 sounder',                          satid(26)=60,  class(26)='rad',
  obstype(27)='GOES-12 sounder',                          satid(27)=62,  class(27)='rad',
  obstype(28)='NOAA-15 AMSU-A',                           satid(28)=315, class(28)='rad',
  obstype(29)='NOAA-15 AMSU-B',                           satid(29)=415, class(29)='rad',
  obstype(30)='NOAA-16 HIRS/3',                           satid(30)=16,  class(30)='rad',
  obstype(31)='NOAA-16 AMSU-A',                           satid(31)=316, class(31)='rad',
  obstype(32)='NOAA-16 AMSU-B',                           satid(32)=416, class(32)='rad',
  obstype(33)='NOAA-17 HIRS/3',                           satid(33)=17,  class(33)='rad',
  obstype(34)='NOAA-17 AMSU-A',                           satid(34)=317, class(34)='rad',
  obstype(35)='NOAA-17 AMSU-B',                           satid(35)=417, class(35)='rad',
  obstype(36)='AQUA AIRS',                                satid(36)=49,  class(36)='rad',
  obstype(37)='AQUA AMSU-A',                              satid(37)=349, class(37)='rad',
  obstype(38)='NOAA-18 HIRS/4',                           satid(38)=18,  class(38)='rad',
  obstype(39)='NOAA-18 AMSU-A',                           satid(39)=318, class(39)='rad',
  obstype(40)='NOAA-18 MHS',                              satid(40)=418, class(40)='rad',
  obstype(41)='GOES-10 imager',                           satid(41)=260, class(41)='rad',
  obstype(42)='GOES-12 imager',                           satid(42)=262, class(42)='rad',
  $SATOBSVARS
 /
EOF

export ERR=$?
export err=$ERR
$ERRSCRIPT||exit 2


if [[ $FILESTYLE = 'C' ]]
then
   # Output file
   ${NCP:-cp} satcnt.out  $SATCNT
fi


################################################################################
#  Postprocessing
cd $pwd
[[ $mkdata = YES ]]&&rmdir $DATA
$ENDSCRIPT
set +x
if [[ "$VERBOSE" = "YES" ]]
then
   echo $(date) EXITING $0 with return code $err >&2
fi
exit $err
