#!/bin/ksh

#-----------------------------------------------------------------
# Extract one or more tiles from an ARL meteorological data file.
# Divides the NAMS domain into quadrants and reduces the number of    
# vertical levels from 40 to 25 (max sigma = 0.677). 
#-----------------------------------------------------------------
#               05 Apr 2007 - initial version 
# Last Revised: 11 Feb 2008 - expanded NAM grid; one prep NAMS job
#                             Output file to /com are:
#                               hysplit.$cycle.namsa    (conus)
#                               hysplit.$cycle.namsf    (conus)
#                               hysplit.$cycle.namsa.AK
#                               hysplit.$cycle.namsf.AK
#                               hysplit.$cycle.namsa.HI
#                               hysplit.$cycle.namsf.HI
#                               hysplit.$cycle.namsf.NEtile
#                               hysplit.$cycle.namsf.SEtile
#                               hysplit.$cycle.namsf.SWtile
#                               hysplit.$cycle.namsf.NWtile
#                         "tile" files have fewer number of levels
#                09 Mar 2009 - error checking
#                April 2008 - to NCEP
#                09 Mar 2011 - nam bgrd - change 'NAMS' to '$MDL'; 'nams' to '$mdlcom'
#                               take out HI grid
#-----------------------------------------------------------------
set -xa

# check argument list for cycle time
  if [ $# -lt 1 ];then
     echo "Usage: $0 [cycle: 00 06 12 18] [optional day]"
     exit
  fi
  CYCLE=$1

# number of levels for xtrct per -v option 
  if [ "${dashv}" = "-v4" ]
  then
    MLVL=40
 #elif [ "${dashv}" = "-v1" ]
 #then
 #  MLVL=61
  else
    echo "exit tiles script - did not account for dashv = ${dashv}"
    exit 2
  fi

# current time UTC
  YY=`date -u +%Y`
  MM=`date -u +%m`
  DD=`date -u +%d`
  HH=`date -u +%H`

# over-ride today's date
  if [ $# -eq 2 ];then
     DD=$2
  fi

# data extraction program
  PGM=${EXEChysplit}/hysplit_xtrctgrid

# input directories and files    
  RDIR="${DATA}/"
  FILE=${MDL}F
# output directory and files
  WDIR=$DATA
  WOUT=hysplit.t${CYCLE}z.${mdlcom}f
  if [ ! -d ${WDIR} ];then mkdir ${WDIR}; fi
  cd ${WDIR}
 #rm -f TILE.cmd

# skip if forecast not created (for ARL testing)
if [ ${FHR_MAX} -gt 0 ]
then

###########################################################
# make the quadrant CONUS files from tile.scr 
#    (forecast only; 25 levels)
###########################################################
#------------------------------
# North Eastern Tile    

echo "#!/bin/ksh"                         >tile.scr
echo "set -x"                            >>tile.scr
echo "export pgm=${PGM}"                 >>tile.scr
echo ". prep_step"                       >>tile.scr
echo "startmsg"                          >>tile.scr
echo "${PGM} -pNE -g <<EOD"              >>tile.scr          
echo "${RDIR}"                           >>tile.scr
echo "${FILE}"                           >>tile.scr
echo "590.0 325.0"                       >>tile.scr
echo "790.0 470.0"                       >>tile.scr
echo "25"                                >>tile.scr
echo "EOD"                               >>tile.scr
echo "err=\$?; export err;err_chk"       >>tile.scr
echo "mv extract_NE.bin ${WOUT}.NEtile"  >>tile.scr 
echo "if test $SENDCOM = 'YES'"          >>tile.scr
echo "then"                              >>tile.scr
echo "if [ -s ${WOUT}.NEtile ]"          >>tile.scr
echo "then"                              >>tile.scr
echo "cp ${WOUT}.NEtile ${COMOUT}"       >>tile.scr
echo "if test $SENDDBN = 'YES'"          >>tile.scr
echo "then"                              >>tile.scr
echo "$DBNROOT/bin/dbn_alert MODEL HYSNAMSF_TILE $job ${COMOUT}/${WOUT}.NEtile " >>tile.scr
echo "fi"                                >>tile.scr
echo "fi"                                >>tile.scr
echo "fi"                                >>tile.scr
echo "rm -f EXTRACT_NE.CFG"              >>tile.scr

#------------------------------
# South Eastern Tile    

echo "export pgm=${PGM}"                 >>tile.scr
echo ". prep_step"                       >>tile.scr
echo "startmsg"                          >>tile.scr
echo "${PGM} -pSE -g <<EOD"              >>tile.scr          
echo "${RDIR}"                           >>tile.scr
echo "${FILE}"                           >>tile.scr
echo "590.0 190.0"                       >>tile.scr
echo "790.0 335.0"                       >>tile.scr
echo "25"                                >>tile.scr
echo "EOD"                               >>tile.scr
echo "err=\$?; export err;err_chk"       >>tile.scr
echo "mv extract_SE.bin ${WOUT}.SEtile"  >>tile.scr 
echo "if test $SENDCOM = 'YES'"          >>tile.scr
echo "then"                              >>tile.scr
echo "if [ -s ${WOUT}.SEtile ]"          >>tile.scr
echo "then"                              >>tile.scr
echo "cp ${WOUT}.SEtile ${COMOUT}"       >>tile.scr
echo "if test $SENDDBN = 'YES'"          >>tile.scr
echo "then"                              >>tile.scr
echo "$DBNROOT/bin/dbn_alert MODEL HYSNAMSF_TILE $job ${COMOUT}/${WOUT}.SEtile " >>tile.scr
echo "fi"                                >>tile.scr
echo "fi"                                >>tile.scr
echo "fi"                                >>tile.scr
echo "rm -f EXTRACT_SE.CFG"              >>tile.scr

#------------------------------
# North Western Tile    

echo "export pgm=${PGM}"                 >>tile.scr
echo ". prep_step"                       >>tile.scr
echo "startmsg"                          >>tile.scr
echo "${PGM} -pNW -g <<EOD"              >>tile.scr          
echo "${RDIR}"                           >>tile.scr
echo "${FILE}"                           >>tile.scr
echo "400.0 325.0"                       >>tile.scr
echo "600.0 470.0"                       >>tile.scr
echo "25"                                >>tile.scr
echo "EOD"                               >>tile.scr
echo "err=\$?; export err;err_chk"       >>tile.scr
echo "mv extract_NW.bin ${WOUT}.NWtile"  >>tile.scr 
echo "if test $SENDCOM = 'YES'"          >>tile.scr
echo "then"                              >>tile.scr
echo "if [ -s ${WOUT}.NWtile ]"          >>tile.scr
echo "then"                              >>tile.scr
echo "cp ${WOUT}.NWtile ${COMOUT}"       >>tile.scr
echo "if test $SENDDBN = 'YES'"          >>tile.scr
echo "then"                              >>tile.scr
echo "$DBNROOT/bin/dbn_alert MODEL HYSNAMSF_TILE $job ${COMOUT}/${WOUT}.NWtile " >>tile.scr
echo "fi"                                >>tile.scr
echo "fi"                                >>tile.scr
echo "fi"                                >>tile.scr
echo "rm -f EXTRACT_NW.CFG"              >>tile.scr

#------------------------------
# South Western Tile    

echo "export pgm=${PGM}"                 >>tile.scr
echo ". prep_step"                       >>tile.scr
echo "startmsg"                          >>tile.scr
echo "${PGM} -pSW -g <<EOD"              >>tile.scr          
echo "${RDIR}"                           >>tile.scr
echo "${FILE}"                           >>tile.scr
echo "400.0 190.0"                       >>tile.scr
echo "600.0 335.0"                       >>tile.scr
echo "25"                                >>tile.scr
echo "EOD"                               >>tile.scr
echo "err=\$?; export err;err_chk"       >>tile.scr
echo "mv extract_SW.bin ${WOUT}.SWtile"  >>tile.scr 
echo "if test $SENDCOM = 'YES'"          >>tile.scr
echo "then"                              >>tile.scr
echo "if [ -s ${WOUT}.SWtile ]"          >>tile.scr
echo "then"                              >>tile.scr
echo "cp ${WOUT}.SWtile ${COMOUT}"       >>tile.scr
echo "if test $SENDDBN = 'YES'"          >>tile.scr
echo "then"                              >>tile.scr
echo "$DBNROOT/bin/dbn_alert MODEL HYSNAMSF_TILE $job ${COMOUT}/${WOUT}.SWtile " >>tile.scr
echo "fi"                                >>tile.scr
echo "fi"                                >>tile.scr
echo "fi"                                >>tile.scr
echo "rm -f EXTRACT_SW.CFG"              >>tile.scr

  echo "sh tile.scr" > $DATA/poescript

fi

###########################################################
# make the CONUS, AK, and HI files 
#    (forecast and archive; 40 levels)
###########################################################
if [ "${mdlcom}" = "nams" ]
then 
   grid_list="CONUS AK"
fi
for grid in ${grid_list}
do

  if [ "${grid}" = "CONUS" ]; then
     LLI=350.0	# lower-left I,J
     LLJ=150.0
     URI=810.0	# upper-right I,J
     URJ=550.0
     NLVL=${MLVL}	# number of levels
     ext=
  elif [ "${grid}" = "AK" ]; then
     LLI=100.0	# lower-left I,J
     LLJ=475.0
     URI=425.0	# upper-right I,J
     URJ=800.0
     NLVL=${MLVL}	# number of levels
     ext=".${grid}"
  elif [ "${grid}" = "HI" ]; then
     LLI=20.0	# lower-left I,J
     LLJ=230.0
     URI=100.0	# upper-right I,J
     URJ=375.0
     NLVL=${MLVL}	# number of levels
     ext=".${grid}"
 #elif [ "${grid}" = "PR" ]; then
 #   LLI=875.0	# lower-left I,J
 #   LLJ=175.0
 #   URI=1040.0	# upper-right I,J
 #   URJ=275.0
 #   NLVL=${MLVL}	# number of levels
 #   ext=".${grid}"
  fi

  echo "#!/bin/ksh"                        >${grid}.scr
  echo "set -x"                            >>${grid}.scr

  type_list="f a"

# loop through forecast archive
  for type in ${type_list}
  do

  if [ "${type}" = "" ]
  then
    break
  fi

  TYPE=`echo  $type | tr '[a-z]' '[A-Z]'`
  FILE=${MDL}${TYPE}
  WOUT=hysplit.t${CYCLE}z.${mdlcom}${type}${ext}

#------------------------------
# tiles

echo "export pgm=${PGM}"                 >>${grid}.scr
echo ". prep_step"                       >>${grid}.scr
echo "startmsg"                          >>${grid}.scr
echo "${PGM} -p${ext} -g <<EOD"          >>${grid}.scr          
echo "${RDIR}"                           >>${grid}.scr
echo "${FILE}"                           >>${grid}.scr
echo "${LLI} ${LLJ}"                     >>${grid}.scr
echo "${URI} ${URJ}"                     >>${grid}.scr
echo "${NLVL}"                           >>${grid}.scr
echo "EOD"                               >>${grid}.scr
echo "err=\$?; export err;err_chk"       >>${grid}.scr
if [ "${grid}" = "AK" ]
then
  echo "if [ -s extract_${ext}.bin ]"      >>${grid}.scr
  echo "then"                              >>${grid}.scr
  echo "mv extract_${ext}.bin ${WOUT}"     >>${grid}.scr 
  echo "fi"                                >>${grid}.scr
elif [ "${grid}" = "CONUS" ]
then
  echo "if [ -s extract.bin ]"             >>${grid}.scr
  echo "then"                              >>${grid}.scr
  echo "mv extract.bin ${WOUT}"            >>${grid}.scr 
  echo "fi"                                >>${grid}.scr
fi
echo "if test $SENDCOM = 'YES'"          >>${grid}.scr
echo "then"                              >>${grid}.scr
echo "if [ -s ${WOUT} ]"                 >>${grid}.scr
echo "then"                              >>${grid}.scr
echo "cp ${WOUT} ${COMOUT}"              >>${grid}.scr
echo "if test $SENDDBN = 'YES'"          >>${grid}.scr
echo "then"                              >>${grid}.scr
echo "$DBNROOT/bin/dbn_alert MODEL HYSNAMSF_TILE $job ${COMOUT}/${WOUT}" >>${grid}.scr
echo "fi"                                >>${grid}.scr
echo "fi"                                >>${grid}.scr
echo "fi"                                >>${grid}.scr
echo "rm -f EXTRACT_${ext}.CFG"          >>${grid}.scr

  done
  echo "sh ${grid}.scr" >> $DATA/poescript

done

  echo "hostname" >>  $DATA/poescript
cat poescript
 
chmod 775 $DATA/poescript
export MP_PGMMODEL=mpmd
export MP_CMDFILE=$DATA/poescript
 
# Execute the script.
 
poe
#poescript		# for mgc0bs testing

exit
