#!/bin/ksh

#------------------------------------------------------------------
# smoke_config.sh  
# Configuration of the input files for the HYSPLIT Smoke Forecast
#------------------------------------------------------------------
# Revised: 06 Apr 2001 (RRD) - Initial version: Roland Draxler 
#          10 Jul 2002 (RRD) - simplified for ARL computer
#          17 Aug 2002 (RRD) - analysis/forecast runs
#          27 Aug 2002 (RRD) - fixed contour levels
#          05 Feb 2003 (RRD) - reconfigured for single run
#          17 Mar 2003 (RRD) - create shapefiles
#          28 Apr 2003 (RRD) - conversion for frost/snow
#          12 May 2003 (RRD) - rescale conc using conlight 
#          23 May 2003 (RRD) - national conc grid
#          09 Jun 2003 (RRD) - change names on shapefiles
#          02 Feb 2004 (RRD) - added crop to convert
#          09 Mar 2004 (RRD) - added Northern Mexico domain
#          26 Aug 2004 (RRD) - gfs data and expanded domains
#          02 Sep 2004 (RRD) - merged arc/for for single run
#          20 Sep 2004 (RRD) - national hourly graphics
#          12 Oct 2004 (RRD) - ascii conversion
#          18 Nov 2004 (RRD) - use pardump and extended duration 
#          23 Dec 2004 (RRD) - raised contour limits
#          31 Jan 2005 (RRD) - auto switch to test mode no fires
#          07 Feb 2005 (RRD) - modified to run on Blue
#          11 Mar 2005 (RRD) - national image gif files
#          25 Apr 2005 (RRD) - turn on BlueSkies  
#          13 Oct 2005 (RRD) - hourly sfc conc; 06z meteo; igis=2
#          10 Nov 2005 (RRD) - turned on dry & wet deposition
#          15 Mar 2006 (RRD) - plume rise (new file in SETUP.CFG)
#                            - generalized GIS to include att files
#                            - changed concplot -c1 to -c4 
#          30 Mar 2007 (RRD) - update for NAMs (sigma) files
#------------------------------------------------------------------

set -x

# . /meso/save/wx20mg/bluesky/scripts/exsmoke_envir.sh

echo "starting configuration: ${DATA}"

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

  concsfc="bin_sfc.1hr"
  concpbl="bin_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}/smoke_control1.ini >>DOMAIN  
  echo "4"                    >>DOMAIN 

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

# AQM sigma level 
# echo "${MET}"               >>DOMAIN 
# echo "hysplit.t06z.aqma"    >>DOMAIN 
# echo "${MET}"               >>DOMAIN 
# echo "hysplit.t06z.aqmf"    >>DOMAIN 

# GFS pressure level global
  echo "${COMINHYSPT}/"               >>DOMAIN
  echo "hysplit.t00z.gfsa"    >>DOMAIN
  echo "${COMINHYSPT}/"               >>DOMAIN
  echo "hysplit.t00z.gfsf"    >>DOMAIN

  cat ${FIXsmoke}/smoke_control2.ini      >>DOMAIN
  cp ${FIXsmoke}/smoke_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  
      echo "Starting BlueSky fires processing" 

     if [ ! -f CONTROL ];then
#       no fire locations, control file was not created by fire pgm
       echo "No fire locations ... exit script" >>${log}
       cp DOMAIN CONTROL
       exit
     else
       echo "Starting 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/

        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
