#!/bin/sh
#######################################################################
#
#  Job Name: exgfsmos_higoe_extfcst.sh.sms
#
#   Purpose: To run all steps necessary to create the GFS-based extended- 
#            range MOS and model data for Hawaii gridded MOS. This
#            runs all the steps necessary to create the forecasts.
#            Subsequent scripts will produce the guidance products.
#            This script runs at 00 and 12Z.   
#
#   Remarks: For the forseeable future, this script will produce     
#            TDLPACK files of model data interpolated from GFS 0.5 deg.
#            PGRIB2 files to a NPS grid over Hawaii GMOS domain. This
#            grid is a subgrid of the "new" MDL GFS model archive that
#            was apporved as the new archive grid in Aug. 2010. The new
#            grid is expanded to cover all North American NDFD/NDGD
#            domains. 
#
#   History: Mar 21, 2008 - New job for GFS Gridded MOS for HI.
#                           At the current time this job is just
#                           a run of mospred to get model data                                                                                                                    
#                           as input to U155 for the temp fields.                                                                                                                 
#                           In time we'll add the other forecast                                                                                                                  
#                           steps when we have the goes ready.                                                                                                                    
#            Sep 25, 2008 - Added steps to evaluated POP and sky                                                                                                                  
#                           goes.  Also added ids to u201 to get                                                                                                                  
#                           DMO winds for wind first guess and lapse.                                                                                                             
#            Aug 17, 2010 - Added section to perform interp of GFS
#                           model data from Lat/Lon grids to NPS
#                           grids at 47KM (99-h to 192-h) and 95KM 
#                           (204-h to 384-h)over Hawaii and then
#                           convert to TDLPACK file. Cleaned up script
#                           for operations.
#######################################################################                                                                                                               
#
echo MDLLOG: `date` - Begin job exgfsmos_higoe_extfcst.sh.sms
set -x

cd $DATA

DAT="${PDY}${cyc}"
echo $PDY $cyc: Date and Cycle - echo PDY and cyc
#######################################################################
#######################################################################
# SECTION 1: COPY GFS MODEL FIELDS FROM 0.5 DEG. LAT/LON GRIDS TO A
#            47.625KM NPS 65X65 GRID (FOR 99-H TO 192-H).
#
#            FOR FORECAST PROJECTIONS 204-H TO 384-H, COPY GFSMOS MODEL
#            FIELDS FROM 1.0 DEG. LAT/LON GRIDS TO A 95.250KM NPS
#            33X33 GRID.
#######################################################################
#######################################################################
# LOOP THROUGH PROJECTIONS
#######################################################################
for tau in  99 102 105 108 111 114 117 120 123 126 129\
           132 135 138 141 144 147 150 153 156 159 162\
           165 168 171 174 177 180 183 186 189 192 204\
           216 228 240 252 264 276 288 300 312 324 336\
           348 360 372 384
do
#######################################################################
# COPY MODEL FILES TO TEMP SPACE
#######################################################################
if [ $tau -le 192 ]; then
  cp $COMIN/gfs.$cycle.pgrb2f$tau gfs.$PDY$cyc.pgrb2f$tau
elif [ $tau -ge 204 ]; then
  cp $COMIN/gfs.$cycle.pgrb2bf$tau gfs.$PDY$cyc.pgrb2f$tau
fi
#######################################################################
# RUN COPYGB2 TO CONVERT TO MDL GRID
#######################################################################
if [ $tau -le 192 ]; then
  grid=`cat $FIXmdl/mdl_finegds47_hisub`
elif [ $tau -ge 204 ]; then
  grid=`cat $FIXmdl/mdl_coarsegds95_hisub`
fi
g2=gfs.$PDY$cyc.pgrb2f$tau
#######################################################################
# LOOP THROUGH THE MANADTORY PRESSURE LEVELS AND COPY HEIGHTS, TEMP
# VERTICAL VELOCITY, RELATIVE HUMIDITY, U-WIND, AND V-WIND.
#######################################################################
for lvl in 300 500 600 700 750 800 850 900 925 950 975 1000
do

# height, pressure levels
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 3 5 -9999 -9999 96 -9999 -9999 -9999 -9999 100 -9999 ${lvl}00" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
# temperature, pressure levels
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 0 0 -9999 -9999 96 -9999 -9999 -9999 -9999 100 -9999 ${lvl}00" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
# v. velocity, pressure levels
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 2 8 -9999 -9999 96 -9999 -9999 -9999 -9999 100 -9999 ${lvl}00" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
# RH, pressure levels
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 1 1 -9999 -9999 96 -9999 -9999 -9999 -9999 100 -9999 ${lvl}00" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
# U-wind (UGRD), pressure levels
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 2 2 -9999 -9999 96 -9999 -9999 -9999 -9999 100 -9999 ${lvl}00" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
# V-wind (VGRD), pressure levels
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 2 3 -9999 -9999 96 -9999 -9999 -9999 -9999 100 -9999 ${lvl}00" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2

done # for lvl

# temperature, RH in lowest 30 mb (boundary layer)
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 0 0 -9999 -9999 96 -9999 -9999 -9999 -9999 108 -9999 3000 108 -9999 0" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 1 1 -9999 -9999 96 -9999 -9999 -9999 -9999 108 -9999 3000 108 -9999 0" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2

# pressure, sfc and MSLP
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 3 0 -9999 -9999 96 -9999 -9999 -9999 -9999 1 -9999 0" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 3 1 -9999 -9999 96 -9999 -9999 -9999 -9999 101 -9999 0" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2

# precip. water
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 1 3 -9999 -9999 96 -9999 -9999 -9999 -9999 200 -9999 0" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2

# Lifted index, sfc and "best"
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 7 192 -9999 -9999 96 -9999 -9999 -9999 -9999 1 -9999 0" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 7 193 -9999 -9999 96 -9999 -9999 -9999 -9999 1 -9999 0" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2

# CAPE and CIN
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 7 6 -9999 -9999 96 -9999 -9999 -9999 -9999 1 -9999 0" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 7 7 -9999 -9999 96 -9999 -9999 -9999 -9999 1 -9999 0" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2

# RH, 1-0.44 sigma layer
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 1 1 -9999 -9999 96 -9999 -9999 -9999 -9999 104 2 44 104 2 100" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2

# U,V wind @ tropopause hgt
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 2 2 -9999 -9999 96 -9999 -9999 -9999 -9999 7 -9999 0" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 2 3 -9999 -9999 96 -9999 -9999 -9999 -9999 7 -9999 0" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2

# 2m T, RH, TMax, TMin
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 0 0 -9999 -9999 96 -9999 -9999 -9999 -9999 103 -9999 2" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 1 1 -9999 -9999 96 -9999 -9999 -9999 -9999 103 -9999 2" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
$EXECutil/copygb2 -a -g"$grid" -i0 -k"8 0 4 -9999 -9999 96 -9999 -9999 -9999 -9999 103 -9999 2" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
$EXECutil/copygb2 -a -g"$grid" -i0 -k"8 0 5 -9999 -9999 96 -9999 -9999 -9999 -9999 103 -9999 2" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2

# 10m U,V wind
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 2 2 -9999 -9999 96 -9999 -9999 -9999 -9999 103 -9999 10" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
$EXECutil/copygb2 -a -g"$grid" -i0 -k"0 2 3 -9999 -9999 96 -9999 -9999 -9999 -9999 103 -9999 10" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2

# Precip, total and convective
$EXECutil/copygb2 -a -g"$grid" -i3 -k"8 1 8" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2
$EXECutil/copygb2 -a -g"$grid" -i3 -k"8 1 10" -x $g2 mdl_gfsxdmo_hi.$cycle.pgrb2

done  # for tau

$EXECutil/grb2index mdl_gfsxdmo_hi.$cycle.pgrb2 mdl_gfsxdmo_hi.$cycle.pgrb2.index
#######################################################################
#  PROGRAM MDL_GRB2TOMDLP - CREATE A TDLPACK FILE OF GFS MODEL FIELDS
#######################################################################
echo MDLLOG:  `date` - begin convert GRIB2 to TDLPACK

export pgm=mdl_grb2tomdlp
. prep_step
startmsg
export XLFUNIT_10="ncepdate"
export XLFUNIT_20="mdl_gfsxdmo_hi.$cycle.pgrb2"
export XLFUNIT_21="mdl_gfsxdmo_hi.$cycle.pgrb2.index"
export XLFUNIT_28="$FIXmdl/mdl_gfsxgrb2tomdlp_hi.lst"
export XLFUNIT_29="$FIXmdl/mdl_gridlst"
export XLFUNIT_30="$FIXmdl/mdl_mos2000id.tbl"
export XLFUNIT_60="mdl_gfsxpkd_hi.$cycle"

$EXECmdl/mdl_grb2tomdlp < $PARMmdl/mdl_grb2tomdlp_hi.cn >> $pgmout 2> errfile

echo MDLLOG:  `date` - end convert GRIB2 to TDLPACK
#######################################################################
#######################################################################
# SECTION 2: CREATE THE GOE FILES.
#######################################################################
#######################################################################
# THIS JOB USES THE RANDOM ACCESS FILE FIRST CREATED IN THE
# EXECUTION OF GFSMOS_HIGOE_FCST.  CHECK IF THE FILE MDL_GOEHIMOS.TXXZ
# EXISTS IN COM/GFS.  IF IT DOES, COPY THE FILE TO THE WORK SPACE.
# IF IT DOES NOT EXIST, THE SCRIPT WILL ABORT.  GFSMOS_HIGOE_EXTFCST
# WILL NOT WORK UNLESS GFSMOS_HIGOE_FCST HAS ALREADY RUN SUCCESSFULLY.
#######################################################################
if [ ! -f $COMIN/mdl_goehimos.$cycle ]
     then echo 'need successful run of gfsmos_higoe_fcst to run properly' >> $pgmout
             export err=1;err_chk
fi
cp $COMIN/mdl_goehimos.$cycle .
cp $COMIN/mdl_gfspkd_hi.$cycle mdl_gfspkd_hi.$cycle
#######################################################################
#  PROGRAM MOSPRED - USED TO INTERPOLATE TO STATIONS FROM MDL GRID-POINT
#                    ARCHIVE FILES AND TO PROCESS/COMBINE VECTOR DATA.
#                    THIS RUN GETS THE MODEL FIELDS THAT WE NEED FOR 
#                    FIRST GUESS AND UPPER AIR LAPSE RATE CALCULATIONS IN
#                    ADDITION TO PREDICTORS FOR THE GOES
#
#  NOTE: AT THIS TIME UNIT 44 IS NOT BEING USED IN THIS RUN
#        OF MOSPRED.  THIS IS THE CONUS FILE AND IS LEFT HERE SO
#        THE SAME CN FILE CAN BE USED AS IN THE CONUS.
#######################################################################
echo MDLLOG: `date` - begin job MOSPRED - INTERPOLATE MODEL DATA

export pgm=mdl_mospred
. prep_step
start_msg
export XLFUNIT_10="ncepdate"
export XLFUNIT_23="mdl_gfspkd_hi.$cycle"
export XLFUNIT_24="mdl_gfsxpkd_hi.$cycle"
export XLFUNIT_27="$FIXmdl/mdl_hindfdtrimsta.tbl"
export XLFUNIT_28="$FIXmdl/mdl_gfsxgoehiprd.$cycle"
export XLFUNIT_29="$FIXmdl/mdl_mos2000id.tbl"
export XLFUNIT_44="$FIXmdl/mdl_griddedconstants"
export XLFUNIT_45="$FIXmdl/mdl_consthindfdtrimsta"
export XLFUNIT_60="goehimosxmodel.$DAT"

$EXECmdl/mdl_mospred < $PARMmdl/mdl_gfsxgmospredmdl.cn >> $pgmout 2> errfile
export err=$?; errchk

echo MDLLOG: `date` -  MOSPRED ended
#######################################################################
#
#    PROGRAM EQNEVAL - CALCULATES MOS FORECASTS
#
#######################################################################
echo MDLLOG: `date` - begin job EQNEVAL - MAKE MOS FORECASTS

export pgm=mdl_eqneval
. prep_step
start_msg
export XLFUNIT_10="ncepdate"
export XLFUNIT_24="goehimosxmodel.$DAT"
export XLFUNIT_27="$FIXmdl/mdl_hindfdtrimsta.tbl"
export XLFUNIT_28="$FIXmdl/mdl_predtofcst"
export XLFUNIT_29="$FIXmdl/mdl_mos2000id.tbl"
export XLFUNIT_35="$FIXmdl/mdl_gfsxgoehipopqpf.04010930.$cycle"
export XLFUNIT_36="$FIXmdl/mdl_gfsxgoehipopqpf.10010331.$cycle"
export XLFUNIT_49="mdl_goehimos.$cycle"

$EXECmdl/mdl_eqneval < $PARMmdl/mdl_gfsgmoshieval.cn.$cycle >> $pgmout 2> errfile
export err=$?; err_chk

echo MDLLOG: `date` -  EQNEVAL ended
#######################################################################
#
#    PROGRAM FCSTPOST - POST-PROCESSES MOS FORECASTS
#
#######################################################################
echo MDLLOG: `date` - begin job FCSTPOST - POST PROCESS MOS FORECASTS

export pgm=mdl_fcstpost
. prep_step
startmsg
export XLFUNIT_10="ncepdate"
export XLFUNIT_27="$FIXmdl/mdl_hindfdtrimsta.tbl"
export XLFUNIT_28="$FIXmdl/mdl_gfsxgoepost_hi.$cycle"
export XLFUNIT_29="$FIXmdl/mdl_mos2000id.tbl"
export XLFUNIT_45="$FIXmdl/mdl_consthindfdtrimsta"
export XLFUNIT_47="$FIXmdl/mdl_goethreshold"
export XLFUNIT_49="mdl_goehimos.$cycle"

$EXECmdl/mdl_fcstpost < $PARMmdl/mdl_gmospost.cn >> $pgmout 2> errfile
export err=$?; err_chk

echo MDLLOG: `date` -  FCSTPOST ended
#######################################################################
# COPY FILES TO COM
#######################################################################

if test $SENDCOM = 'YES'
then
  cp mdl_gfsxdmo_hi.$cycle.pgrb2 $COMOUT
  cp mdl_gfsxdmo_hi.$cycle.pgrb2.index $COMOUT
  cp mdl_gfsxpkd_hi.$cycle $COMOUT
  cp mdl_goehimos.$cycle $COMOUT
  cp goehimosxmodel.$DAT $COMOUT/mdl_goehimosxmodel.$cycle
fi

echo MDLLOG: `date` - Job exgfsmos_higoe_extfcst.sh.sms has ended.
#######################################################################
