#!/bin/sh
###################################################################
echo "------------------------------------------------"
echo "J812 - generate the stratospheric analysis"
echo "------------------------------------------------"
echo "History: Nov 96 - implement new script."                  
echo "------------------------------------------------"

cd $DATA

msg="$job HAS BEGUN"
postmsg "$jlogfile" "$msg"

set +x
echo " -----------------------------------------------"
echo "      NMCOPS  FOB#4    ROOM 2330  457-5047      "
echo "              CRAY $ENVIR PROCESSING     "
echo "                   `date`     "
echo "      Generate the stratospheric analysis"
echo "                                                "
echo "                 JOB WW0812K                "
echo "                                                "
echo "          ANALYSIS CYCLE TIME IS .. $CYCLE     "
echo " _______________________________________________"
echo "        processing info for this execution      "
echo "Processing environment is ................$ENVIR"
echo "Temporary processing file directory is .. $DATA"
echo "Executable GDAS directory is ............. $EXECgfs"
echo "Unix Control Language GDAS directory is... $UCLgdas"
echo "Fixed Field GDAS directory is ............ $FIXgdas"
echo "Unique machine processing id is ......... $pid"
echo "Temporary output file is ................ $pgmout"
echo "-------------------------------------------------------"
set -x

# List NMC date
set +x
echo "ANALYSIS DATE-TIME IS $PDY$cyc"
set -x

#------------------------------------------------------------
# Get target (yesterday) day and a day before
#------------------------------------------------------------

target_yr2d=`echo $PDYm1 | cut -c3-8`
previous_yr4d=`echo $PDYm2 | cut -c1-8`
previous_yr2d=`echo $PDYm2 | cut -c3-8`
 
#-------------------------------------------------------------------------
# Get Year, Month, Day for target day
#-------------------------------------------------------------------------
yr4d=`echo $PDYm1 | cut -c 1-4`
yr2d=`echo $PDYm1 | cut -c 3-4`
mon=`echo  $PDYm1 | cut -c 5-6`
day=`echo  $PDYm1 | cut -c 7-8`

#-------------------------------------------------------------------------
# Calculate the century
#-------------------------------------------------------------------------
yr_4=`expr $yr4d - 1`
yr_4=`expr $yr_4 / 100`
century=`expr $yr_4 + 1`

#-------------------------------------------------------------------------
# Setup production filenames
#-------------------------------------------------------------------------
EDSDATA=/dcom/us007003/$PDYm1/b003/xx104
T12ZPGRB=$COMINm1/gdas1.t12z.pgrbf00
T00ZPREPDA=$COMIN/gdas1.t00z.prepbufr
T12ZPREPDA=$COMINm1/gdas1.t12z.prepbufr

#-------------------------------------------------------------------------
# Setup working filenames
#-------------------------------------------------------------------------
H100MB=h100.dat
TOVS70MB=tovs_70mb.dat
TOVS50MB=tovs_50mb.dat
TOVS30MB=tovs_30mb.dat
TOVS10MB=tovs_10mb.dat
TOVS05MB=tovs_05mb.dat
TOVS02MB=tovs_02mb.dat
TOVS01MB=tovs_01mb.dat
TOVS004MB=tovs_004mb.dat
RS70MB=rs_70mb.dat
RS50MB=rs_50mb.dat
RS30MB=rs_30mb.dat
RS10MB=rs_10mb.dat
TODAY_OZONE=today_ozone.grb
TEMP_OZONE=temp_ozone.grb
TOVS_TEMP=tovs_temp.grb
TOVS_HEIGHT=tovs_height.grb
RS_TEMP=rs_temp.grb
RS_HEIGHT=rs_height.grb

#-------------------------------------------------------------------------
# Setup script variable
#-------------------------------------------------------------------------
HAS_HGT=T

#-------------------------------------------------------------------------
# Perform maps analysis on TOVS ozone even there is no TOVS data
#-------------------------------------------------------------------------

export pgm=aiozone;. prep_step

msg="$pgm start"
postmsg "$jlogfile" "$msg"

cp /dcom/us007003/${PDYm1}/b003/xx104 ./xx104   # ATOVS retrieval tank
cp ${COMINm2}/tovs_ozone.grb ./last_ozone.grb
export XLFUNIT_11="xx104"
export XLFUNIT_41="last_ozone.grb"
export XLFUNIT_71="today_ozone.grb"
$EXECgfs/gdas_aiozone << EOF > $pgmout 2> errfile
 &PARAMS
  ILB=6
  IFG=1
  XMAX=.7,10.0,18.0,22.0,21.,16.0
  XTPL=100000.,10000.,1000.,1000.,1000.,1000.
  ISAT=206,207 ! NOAA-15 and NOAA-16 (set ISAT(2) to 999 if only 1 sat)
  ND=0
  IYEST=$yr4d,$mon,$day
  PRE_DATE=$previous_yr4d
 /
EOF

err=$?;export err;err_chk


#-------------------------------------------------------------------------
# Create a 5 day rotating ozone archive
#-------------------------------------------------------------------------
dd if=last_ozone.grb of=temp_ozone.grb bs=10646 count=8
cat today_ozone.grb temp_ozone.grb > tovs_ozone.grb

if test "X$SENDCOM" = "XYES"
then
  if test -s tovs_ozone.grb
  then
    cp tovs_ozone.grb ${COMOUTm1}/.
  fi
fi

#-------------------------------------------------------------------------
# Check the availibility of the TOVS data
#-------------------------------------------------------------------------
if test ! -s $EDSDATA
then
  echo "NO TOVS DATA FOR TODAY"
#-------------------------------------------------------------------------
# Remove all the temporary files
#-------------------------------------------------------------------------
  /bin/rm -f today_ozone.grb temp_ozone.grb
  exit
fi

#-------------------------------------------------------------------------
# Get the 100MB height from MRF T12Z analysis
#-------------------------------------------------------------------------
if test -s $T12ZPGRB
then
  export pgm=aiprefmt;. prep_step

  msg="$pgm start"
  postmsg "$jlogfile" "$msg"

  cp ${COMINm1}/gdas1.t12z.pgrbf00 ./gdas_t12z_f00.dat
  export XLFUNIT_11="gdas_t12z_f00.dat"
  export XLFUNIT_51="ht100.dat"
  $EXECgfs/gdas_aiprefmt << EOF >> $pgmout 2> errfile
 &PARAMS
 YEAR=$yr2d, MONTH=$mon, DATE=$day
 /
EOF

  err=$?;export err;err_chk

fi

#-------------------------------------------------------------------------
# Test is the 100MB height available
#-------------------------------------------------------------------------
if test -s ht100.dat
then
  echo "100MB height field available"
  HAS_HGT=T
else
  echo "100MB height field unavailable"
  HAS_HGT=F
fi

#-------------------------------------------------------------------------
# Proceess TOVS report into on level data
#-------------------------------------------------------------------------
export pgm=aiblkfmt;. prep_step

msg="$pgm start"
postmsg "$jlogfile" "$msg"

export XLFUNIT_11="ht100.dat"
export XLFUNIT_12="xx104"
export XLFUNIT_51="tovs_70mb.dat"
export XLFUNIT_52="tovs_50mb.dat"
export XLFUNIT_53="tovs_30mb.dat"
export XLFUNIT_54="tovs_10mb.dat"
export XLFUNIT_55="tovs_05mb.dat"
export XLFUNIT_56="tovs_02mb.dat"
export XLFUNIT_57="tovs_01mb.dat"
export XLFUNIT_58="tovs_004mb.dat"
$EXECgfs/gdas_aiblkfmt << EOF >> $pgmout 2> errfile
 &PARAMS
  JDAY=$day
  JMON=$mon
  JYR=$yr4d
  ID=206,207 ! NOAA-15 and NOAA-16 (set ISAT(2) to 999 if only 1 sat)
  LANDC=0
  ITHIN=2,3,3,4,4,5,5,6,6,6,6,5,5,4,4,3,3,2
 /
EOF

err=$?;export err;err_chk


#-------------------------------------------------------------------------
# Perform Cressman analysis on TOVS temperature and height
#-------------------------------------------------------------------------
 
export pgm=aihtanl;. prep_step

msg="$pgm start"
postmsg "$jlogfile" "$msg"

cp ${COMINm2}/tovs_height.grb ./last_height.grb
cp ${COMINm2}/tovs_temperature.grb ./last_temperature.grb
export XLFUNIT_13="tovs_70mb.dat"
export XLFUNIT_14="tovs_50mb.dat"
export XLFUNIT_15="tovs_30mb.dat"
export XLFUNIT_16="tovs_10mb.dat"
export XLFUNIT_17="tovs_05mb.dat"
export XLFUNIT_18="tovs_02mb.dat"
export XLFUNIT_19="tovs_01mb.dat"
export XLFUNIT_20="tovs_004mb.dat"
export XLFUNIT_21="last_temperature.grb"
export XLFUNIT_22="last_height.grb"
export XLFUNIT_51="tovs_temperature.grb"
export XLFUNIT_52="tovs_height.grb"
$EXECgfs/gdas_aihtanl << EOF >> $pgmout 2> errfile
 &PARAMETERS
    MDATE=$yr2d,$mon,$day
    NLEV=8
    IPR=1
    IDA=2
    ISMH=10
    ISMT=6
    LH=6
    LT=4
    YMAX=30.0
    IDATE=$yr2d, $mon, $day
    CENTURY=$century
    IPRE_DATE=$previous_yr2d
    HAS_HGT=$HAS_HGT
 /
EOF

err=$?;export err;err_chk
 
if test "X$SENDCOM" = "XYES"
then
  if test -s tovs_temperature.grb
  then
    cp tovs_temperature.grb ${COMOUTm1}/.
  fi
  if test -s tovs_height.grb
  then
    cp tovs_height.grb ${COMOUTm1}/.
  fi
fi

#-------------------------------------------------------------------------
# Check the availability of the RADIOSONDE data
#-------------------------------------------------------------------------
if test ! -s $T12ZPREPDA
then
  echo "NO RADIOSONDE DATA FOR TODAY"
#-------------------------------------------------------------------------
# Remove all the temporary files
#-------------------------------------------------------------------------
  /bin/rm -f $H100MB $TOVS70MB $TOVS50MB $TOVS30MB $TOVS10MB \
          $TOVS05MB $TOVS02MB $TOVS01MB $TOVS004MB $TODAY_OZONE $TEMP_OZONE
  exit
fi

#-------------------------------------------------------------------------
# Proceess Radiosonde report into on level data
#-------------------------------------------------------------------------
export pgm=aireadrs;. prep_step

msg="$pgm start"
postmsg "$jlogfile" "$msg"

cp ${COMINm1}/gdas1.t12z.prepbufr ./t12z.prepbufr
cp ${COMIN}/gdas1.t00z.prepbufr ./t00z.prepbufr
export XLFUNIT_11="t12z.prepbufr"
export XLFUNIT_12="t00z.prepbufr"
export XLFUNIT_51="70mb_rsonde.dat"
export XLFUNIT_52="50mb_rsonde.dat"
export XLFUNIT_53="30mb_rsonde.dat"
export XLFUNIT_54="10mb_rsonde.dat"
$EXECgfs/gdas_aireadrs << EOF >> $pgmout 2> errfile
 &PARAM
  ICOUNT=23
  BOUNCE='08495   ','60390   ','60571   ','60630   ','60680   ','61415   '
        ,'61641   ','42182   ','42369   ','42867   ','91285   ','42339   '
        ,'42101   ','42410   ','42647   ','43003   ','43128   ','43192   '
        ,'43279   ','43295   ','98327   ','08594   ','78016   ',27*'        '
 /
EOF

err=$?;export err;err_chk

#-------------------------------------------------------------------------
# Perform Cressman analysis on Radiosonde data
# Winter and Summer season have different initial conditions
#-------------------------------------------------------------------------
export pgm=aiqsyanl;. prep_step

msg="$pgm start"
postmsg "$jlogfile" "$msg"

export XLFUNIT_13="70mb_rsonde.dat"
export XLFUNIT_14="50mb_rsonde.dat"
export XLFUNIT_15="30mb_rsonde.dat"
export XLFUNIT_16="10mb_rsonde.dat"
export XLFUNIT_21="tovs_temperature.grb"
export XLFUNIT_22="tovs_height.grb"
export XLFUNIT_51="rs_temperature.grb"
export XLFUNIT_52="rs_height.grb"
if test $mon -gt 3 -a $mon -lt 11
then
  $EXECgfs/gdas_aiqsyanl << EOF >> $pgmout 2> errfile
 &PARAMETERS
    MDATE=$yr2d,$mon,$day
    NLEV=4
    IPR=1
    IDA=1
    ISMH=10
    ISMT=6
    LH=6
    LT=4
    YMAX=30.0
    IDATE=$yr2d, $mon, $day
    CENTURY=$century
    IPRE_DATE=$target_yr2d
    HAS_HGT=$HAS_HGT
    TGROSS=20.0,20.0,20.0,20.0
    HGROSS=250.0,300.0,350.0,400.0
    TBUDDY=2.0,2.0,3.0,4.0
    HBUDDY=150.0,175.0,200.0,225.0
    BSCANR=3.5,4.0,4.5,5.0
 /
EOF
  err=$?;export err;err_chk



else
  $EXECgfs/gdas_aiqsyanl << EOF >> $pgmout 2> errfile
 &PARAMETERS
    MDATE=$yr2d,$mon,$day
    NLEV=4
    IPR=1
    IDA=1
    ISMH=10
    ISMT=6
    LH=6
    LT=4
    YMAX=30.0
    IDATE=$yr2d, $mon, $day
    CENTURY=$century
    IPRE_DATE=$target_yr2d
    HAS_HGT=$HAS_HGT
    TGROSS=20.0,30.0,45.0,60.0
    HGROSS=400.0,500.0,600.0,650.0
    TBUDDY=3.0,5.0,7.0,9.0
    HBUDDY=175.0,200.0,225.0,300.0
    BSCANR=3.5,4.0,4.5,5.0
 /
EOF
  err=$?;export err;err_chk

fi

if test "X$SENDCOM" = "XYES"
then
  if test -s rs_temperature.grb
  then
    cp rs_temperature.grb ${COMOUTm1}/.
  fi
  if test -s rs_height.grb
 then
    cp rs_height.grb ${COMOUTm1}/.
  fi
fi
 
#   Make 1-degree stratospheric fields for FNMOC

sh $utilities/mkfnmoctovs.sh

#
# This is the old IBM job 815
#

  ##############################################
  #  Concatenate Height and Temperature files
  #    and create a GRIB index file
  ##############################################

  cat rs_height.grb rs_temperature.grb >> rs_htmp.grb

  $utilexec/grbindex rs_htmp.grb rs_htmp.grbi

set +x
echo " "
echo "######################################"
echo " Execute GRIDBULL "
echo "######################################"
echo " "
set -x

cp $PARMgraph/bull_grdbgdass.dat grdbgdass.dat

export pgm=gridbull; . prep_step
export XLFUNIT_11="rs_htmp.grb"
export XLFUNIT_31="rs_htmp.grbi"
export XLFUNIT_20="grdbgdass.dat"
export XLFUNIT_88="grdbgdass.${job}"
startmsg
$EXECgraph/gridbull >> $pgmout 2> errfile
export err=$?;err_chk

PDYHH=${PDY}${cyc}

 $USHutil/make_ntc_bull.pl  WMOBH NONE KWBC $PDYHH $DATA/grdbgdass.${job} $pcom/grdbgdass.${job}

msg="COMPLETED NORMALLY"
postmsg "$jlogfile" "$msg"

################################################

# ---------------------------- END EX812.sh.sms ---------------------

