#!/bin/ksh
#------------------------------------------------------------------
# dust_config_conus.sh  
# Configuration of the input files for the HYSPLIT Dust Forecast
#------------------------------------------------------------------
# Revised: 2 Apr 2010 (RRD,GSM) - Initial version
#------------------------------------------------------------------
#
#------------------------------------------------------------------
# Section to configure HYSPLIT's CONTROL and SETUP.CFG files
#------------------------------------------------------------------

set -x 

# get current date information 
  YR=`echo ${PDY} | cut -c3-4`
  MO=`echo ${PDY} | cut -c5-6`
  DA=`echo ${PDY} | cut -c7-8`

###################################
# current (CC) and previous (PC) forecast cycle

 if [ $cyc -eq 06 ]; then
    CC=06
    PC=00
 elif [ $cyc -eq 12 ]; then
    CC=12
    PC=06
 elif [ $cyc -eq 18 ]; then
    CC=18
    PC=12
 else
    CC=00
    PC=18
 fi
#################################

# check for availability of current model background 
  if [ -f ${MET}/hysplit.t${CC}z.namsf ]; then
     CYC=${CC}
  else
     CYC=${PC}
  fi

# extract meteorological conditions at each point
export pgm=hysplit_xtrctstn
${EXECdust}/${pgm} -f${FIXdust}/dustconus_control <<EOD
${MET}
hysplit.t${CYC}z.namsf
2
LHTF 01 1.0
SHTF 01 1.0
0
hourly_data.txt
1
24
EOD

# edit base control file for dry locations only
export pgm=hysplit_dustedit
${EXECdust}/${pgm} -b2.5 -i${FIXdust}/dustconus_control \
                 -mhourly_data.txt -odustconus_control.txt

# create the control file with starting time
  echo "${YR} ${MO} ${DA} ${CYC}"            >CONTROL

# append the sources to the control file
  cat dustconus_control.txt                 >>CONTROL

  echo "48"                                 >>CONTROL
  echo "0"                                  >>CONTROL
  echo "6000.0"                             >>CONTROL
  echo "1"                                  >>CONTROL
  echo "${MET}"                             >>CONTROL
  echo "hysplit.t${CYC}z.namsf"             >>CONTROL

# append the second part of the control file
  cat ${FIXdust}/dustconus_control_end        >>CONTROL

# copy the namelist file
  rm -f *.CFG
  cp  ${FIXdust}/dustconus_setup              SETUP.CFG

#----------------------------------------------------------
# initialization files from previous simulation
#   if this is a 12z run, a PARDUMP from 6z will exist in
#   current day's directory;  if not, look for yesterday's file

  let task=1
  num=`printf %3.3d $task`

  if [ -f ${COMIN}/PARDUMP.001 ]; then
    PARDIR=${COMIN}
  else
    PARDIR=${COMINm1} 
  fi

  while [ -f ${PARDIR}/PARDUMP.${num} ]; do
    cp -p ${PARDIR}/PARDUMP.${num} PARINIT.${num}
     echo "Initialized with PARDUMP.${num} from ${PARDIR}"
     let task=$task+1
     num=`printf %3.3d $task`
  done

#------------------------------------------------------------------
# Copy or link the remaining required files to the work directory

  rm -f *.ASC
  ln -s ${FIXhysplt}/hysplit_arlmap          arlmap
  ln -s ${FIXhysplt}/hysplit_landuse.asc     LANDUSE.ASC
  ln -s ${FIXhysplt}/hysplit_rouglen.asc     ROUGLEN.ASC
  ln -s ${FIXhysplt}/hysplit_terrain.asc     TERRAIN.ASC
  ln -s ${FIXhysplt}/hysplit_ascdata.cfg     ASCDATA.CFG

exit

#------------------------------------------------------------------
# Ready to submit dispersion job at this point

  echo "Ending dust_config_conus.sh inp file: `date`" 

exit
