#!/bin/ksh

#------------------------------------------------------------------
# smoke_config.sh  
# Configuration of the input files for the HYSPLIT Smoke Forecast
#------------------------------------------------------------------
# Revised: 15 Jan 2008 (RRD) - Initial version from smoke_config.sh
#          30 May 2008 (RRD) - permit calculation with no fires
#          25 Sep 2009 (RRD) - another step in domain configuration
#------------------------------------------------------------------

set -x

echo "starting configuration: ${DATA}"

# concsfc - output file for ground-level concentrations
# concpbl - output file for column integrated concentrations

  concsfc="binhi_sfc.1hr"
  concpbl="binhi_pbl.1hr"

#------------------------------------------------------------------
# Section to configure HYSPLIT's CONTROL and SETUP.CFG files
#------------------------------------------------------------------

# fire data valid for yesterday to start simulation
  YR=`echo ${PDYm1} | cut -c3-4`
  MO=`echo ${PDYm1} | cut -c5-6`
  DA=`echo ${PDYm1} | cut -c7-8`

  echo "${YR} ${MO} ${DA} 06"  >DOMAIN 
  cat ${FIXsmoke}/smokehi_control1.ini >>DOMAIN  

# NAMs WRF hybrid
  echo "2"                       >>DOMAIN
  echo "${MET}"                  >>DOMAIN
  echo "hysplit.t06z.namsa.HI"   >>DOMAIN
  echo "${MET}"                  >>DOMAIN
  echo "hysplit.t06z.namsf.HI"   >>DOMAIN

  cat ${FIXsmoke}/smokehi_control2.ini      >>DOMAIN
  cp ${FIXsmoke}/smokehi_setup.ini       SETUP.CFG
  cp ${FIXsmoke}/smoke_ascdata.cfg        ASCDATA.CFG

#------------------------------------------------------------------
# Check for master FIRES.TXT file. The fires file is only processed 
# for the archive simulation to determine the fire locations within 
# the domain. The forecast uses the same file. The FIRES files are
# used to update the CONTROL file with current fire locations.
#------------------------------------------------------------------
# FIRES.TXT - today's fire locations       at lat/lon pixel pnts
# FIRES.ARC - yesterday's fire locations   on aggregation grid
# FIRES.NEW - today's file locations       on aggregation grid
#------------------------------------------------------------------

#    yesterday's new emissions become today's archive emissions
     if [ -f ${COMINm1}/FIRES.NEW ];then 
        cp -p ${COMINm1}/FIRES.NEW FIRES.ARC
        echo "Initialized with FIRES from ${COMINm1}" 
     fi

#    fires program reads FIRES.ARC and FIRES.TXT files to create 
#    the FIRES.NEW file and a CONTROL file from the DOMAIN file
      export pgm=hysplit_fires
      ${EXECsmoke}/hysplit_fires -g0.05 
      echo "Starting BlueSky fires processing" 

     if [ ! -f CONTROL ];then
#       no fire locations, control file was not created by fire pgm
       echo "No Hawaiian fire locations ... exit script" >>${log}
#       create dummy emission for default run (rrd 5/30/2008)
#       reconfigure lat-lon limits for meteo grid
        echo "${YR} ${MO} ${DA} 06"           >CONTROL
        echo "1"                             >>CONTROL
        echo "19.0 -156.0 100.0"             >>CONTROL
        echo "72"                            >>CONTROL
        echo "0"                             >>CONTROL
        echo "10000.0"                       >>CONTROL
        echo "2"                             >>CONTROL
        echo "${MET}"                        >>CONTROL
        echo "hysplit.t06z.namsa.HI"         >>CONTROL
        echo "${MET}"                        >>CONTROL
        echo "hysplit.t06z.namsf.HI"         >>CONTROL
        cat ${FIXsmoke}/smokehi_control2.ini >>CONTROL
        exit
     else
       echo "Starting Hawaiian BlueSky fires processing"  >>${log}

#       Section to invoke BlueSky emission algorithms. This is a
#       two step process. In the previous call to hysplit_fires, the  
#       BLUESKY.CSV file was created. This is the bluesky input file
#       that needs to be placed in the appropriate bluesky directory.
#       Also the BLUESKY.TXT file was created that defined the bluesky
#       output file for each fire location. Now run bluesky emissions.
#       On the second pass to the hysplit_fires program, it finds the 
#       BLUESKY.TXT file exists, then the bluesky emission files are
#       read and the hysplit control file is modified accordingly.

        mkdir ${DATA}/bluesky
        mkdir ${DATA}/bluesky/working
        cp BLUESKY.CSV ${PDYm1}.csv
        cp BLUESKY.CSV bluesky/${PDYm1}.csv

#       export the bluesky environment variables
        export FCAMMS="EPA"
        export BSKY_HOME="${DATA}/bluesky"
        export FDATE="${PDYm1}"

#       Requires system installation of Perl Text :: CSV_XS  module
#       run the main bluesky perl script yyyymmddhh
        $USHsmoke/smoke_bsky_main.pl ${FDATE}00  

#       Second pass of the fires program to process Bluesky
        export pgm=hysplit_fires

        echo "Checking bluesky work directory"
        echo $FDATE
        ls -lrt ${DATA}/bluesky/working 
        ${EXECsmoke}/hysplit_fires $DATA/bluesky/working/ -g0.05

        echo "Finished BlueSkies processing: `date`"  
        fi

#------------------------------------------------------------------
# The PARDUMP file contains the particle positions at the end of
# the analysis component of the calculation.
# yesterday's:  ------------> +24
# today's run:  start with PARDUMP (valid day-1) ---> +24 ---> +72 
#                                                   PARDUMP
#------------------------------------------------------------------

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

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

#------------------------------------------------------------------
# Run the dispersion simulation with the modified CONTROL file.
# The current fires file always contains the locations of
# yesterday's fires, hence the forecast must be initialized.
#------------------------------------------------------------------

  rm -f *.ASC
  ln -s ${FIXhysplt}/hysplit_landuse.asc LANDUSE.ASC
  ln -s ${FIXhysplt}/hysplit_rouglen.asc ROUGLEN.ASC
  ln -s ${FIXhysplt}/hysplit_terrain.asc TERRAIN.ASC

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

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

exit
