#!/bin/csh

#SBATCH --no-requeue
#SBATCH -p oar
#SBATCH --account=romo
#SBATCH --gid=romo
#SBATCH -n 128

set NPROCS = 128
#cat $0

set echo

@ NPCOL  = 16; @ NPROW = 8
@ NPROCS2 = $NPCOL * $NPROW
if ( $NPROCS2 != $NPROCS ) then
    echo 'error: npcol times nprow ne nprocs, program abort'
    exit
endif

# # normally setenv outside of this script
# setenv FIRSTDAY 20150501
# setenv BEGDAY   20150501
# setenv ENDDAY   20161231

# set variables for standard script:
set START_DATE = `date -ud "${BEGDAY}" +%Y-%m-%d`
set END_DATE = `date -ud "${ENDDAY}" +%Y-%m-%d`

# this script is based on:
# ====================== CCTMv5.2 Run Script ========================
# Usage: run.cctm >&! cctm_v52b.log &
#
# To report problems or request help with this script/program:
#             http://www.epa.gov/cmaq    (EPA CMAQ Website)
#             http://www.cmascenter.org  (CMAS Website)
# ===================================================================

echo 'warning: do not forget to unquote the mv objects when actually running'
echo 'warning: do not forget to uncomment the no-icfile abort'
echo 'warning: do not forget to uncomment any other emergency exits'

if (( ! $?SLURM_JOB_ID ) && ( ! $?PBS_JOBID )) then
    setenv WALLHR 10
    set totsec = `dc -e "$WALLHR 3600 * p"` # convert to seconds
#    set leftchk = 14040 # 3.9 hours
    set leftchk = 3000 # 3000 seconds
endif

if ( $?SLURM_JOB_ID ) then
  set totsec = `dc -e "$WALLHR 3600 * p"` # convert to seconds
  echo Job ID is ${SLURM_JOB_ID}
  echo Queue is $SLURM_JOB_PARTITION
  echo walltime is $WALLHR hours
  set leftchk = 1800 # 2 hours hemi
  echo Working directory is $SLURM_SUBMIT_DIR
  cd $SLURM_SUBMIT_DIR
endif

set begsec = `date +%s`

which dc

#> Configure the system environment and set up the module
#> capability
   limit stacksize unlimited
#

# ==================================================================
#> Runtime Environment Options
# ==================================================================

#> Choose compiler and set up CMAQ environment with correct
#> libraries using config.cmaq. Options: intel | gcc | pgi
# the cmaq_env.csh is sourced from the 
# /work/MOD3DEV/rmm/CMAQV52/base/config_cmaq.csh
 if ( ! $?compiler ) then
   setenv compiler intel
   setenv compilerVrsn 17.0
   setenv compilerString ${compiler}${compilerVrsn}
   source ./cmaq_env.csh
 endif

# unless the runner created the cmaq exec, this will not work due to
# 'rm' and 'ln' statements in the config script
# #> Source the config.cmaq file to set the build environment
#  cd ../..
#  source ./config_cmaq.csh $compiler
#  cd CCTM/scripts

#> Set General Parameters for Configuring the Simulation
 set VRSN   = v521               #> Code Version
 set PROC   = mpi                #> serial or mpi
 set MECH   = cb6r3_ae6nvPOA_aq  #> Mechanism ID
 set EMIS   =                    #> Emission Inventory Details
 set APPL   = HEMIS_cb6          #> Application Name (e.g. Gridname)

#> Define RUNID as any combination of parameters above or others. By default,
#> this information will be collected into this one string, $RUNID, for easy
#> referencing in output binaries and log files as well as in other scripts.

# setenv RUNID  ${VRSN}_${compilerString}_${APPL}

set sens = ZEAS
setenv RUNID  ${VRSN}_${compilerString}_${APPL}_$sens  # sensitivity run
set esens = zeas  # corresponding emis sens name

#> Set the build directory (this is where the CMAQ executable
#> is located by default).
 set BLD      = /work/ROMO/global/CMAQv5.2.1/2016fe_hemi_cb6_16jh/108km/ZEAS/scripts/BLD_CCTM_v521_intel17.0_cb6r3_ae6nvPOA_aq
 set EXEC     = CCTM_${VRSN}.exe  

echo '-------------------------------------------------------------------'
echo "configuration file: echo cat $BLD/CCTM_${VRSN}.cfg";
echo " ";
echo 'end config file----------------------------------------------------'

#> Set Working, Input, and Output Directories

if ( $?SLURM_SUBMIT_DIR ) then
  echo Assuming this is run in interactive mode
  set BASE   = .
else
  set BASE   = ${SLURM_SUBMIT_DIR}
endif
set INPDIR=`realpath ${SLURM_SUBMIT_DIR}/../input`
set OUTDIR=`realpath ${SLURM_SUBMIT_DIR}/../output`

 setenv LOGDIR  $BASE/logs         #> Log Directory Location
 setenv NMLpath ${BLD}             #> Location of Namelists. Common places are:
                                   #>   ${WORKDIR} | ${CCTM_SRC}/MECHS/${MECH} | ${BLD}

# =====================================================================
#> CCTM Configuration Options
# =====================================================================

#---------------------------------------------------------------------
# these variables set elsewhere in the script
#> Set Start and End Days for looping
 # setenv NEW_START TRUE         # set within day loop
 # set START_DATE = "2011-07-01" # set earlier
 # set END_DATE   = "2011-07-01" # set earlier
#---------------------------------------------------------------------

#> Set Timestepping Parameters
set STTIME     = 000000            #> beginning GMT time (HHMMSS)
set NSTEPS     = 240000            #> time duration (HHMMSS) for this run
set TSTEP      = 010000            #> output time step interval (HHMMSS)

#> Horizontal domain decomposition
if ( $PROC == serial ) then
   setenv NPCOL_NPROW "1 1"; set NPROCS   = 1 # single processor setting
else
   # @ NPCOL  =  4; @ NPROW =  2  # set earlier
   # @ NPROCS = $NPCOL * $NPROW   # set earlier
   setenv NPCOL_NPROW "$NPCOL $NPROW";
endif

#> Vertical extent
set NZ = 44

#setenv LOGFILE $CMAQ_HOME/$RUNID.log  #> log file name; uncomment to write standard output to a log, otherwise write to screen

set metgrid = 108NHEMI2 # part of mcip file name
setenv GRID_NAME WRF_CMAQ_2WAY
setenv GRIDDESC /home/gsarwar/cctm/hemisphere/two-way/WRFV37.sol/GRIDDESC
set APPL = $GRID_NAME

#> Output Species and Layer Options
#>   CONC file species; comment or set to "ALL" to write all species to CONC
setenv CONC_SPCS "O3 NO2 FORM ISOP SO2"
     #setenv CONC_BLEV_ELEV " 1 4" #> CONC file layer range; comment to write all layers to CONC

#>   ACONC file species; comment or set to "ALL" to write all species to ACONC
     #setenv AVG_CONC_SPCS "O3 NO CO NO2 ASO4I ASO4J NH3"
setenv AVG_CONC_SPCS "ALL"
setenv ACONC_BLEV_ELEV " 1 1" #> ACONC file layer range; comment to write all layers to ACONC
#setenv AVG_FILE_ENDTIME N    #> Flag to Override default beginning time timestamp for APMDIAG , AVISDIAG and ACONC [ default: N ]

setenv EXECUTION_ID $EXEC    #> define the model execution id

#> Sychronization Time Step and Tolerance Options
setenv CTM_MAXSYNC 300       #> max sync time step (sec) [ default: 720 ]
setenv CTM_MINSYNC  60       #> min sync time step (sec) [ default: 60 ]
setenv SIGMA_SYNC_TOP 0.7    #> top sigma level thru which sync step determined [ default: 0.7 ]
#setenv ADV_HDIV_LIM 0.95     #> maximum horiz. div. limit for adv step adjust [ default: 0.9 ]
setenv CTM_ADV_CFL 0.95      #> max CFL [ default: 0.75]
#setenv RB_ATOL 1.0E-09       #> global ROS3 solver abs tol [ default: 1.0E-07 ]

#> Science Options
setenv CTM_WB_DUST Y         #> use inline windblown dust emissions [ default: Y ]
setenv CTM_ERODE_AGLAND N    #> use agricultural activity for windblown dust
                             #>    [ default: N ]; ignore if CTM_WB_DUST = N
#setenv CTM_WBDUST_BELD BELD3 #> landuse database for identifying dust source regions
                             #>    [ default: UNKNOWN ]; ignore if CTM_WB_DUST = N
setenv CTM_LTNG_NO N         #> turn on lightning NOx [ default: N ]
setenv CTM_WVEL Y            #> save derived vertical velocity component to conc
                             #>    file [ default: N ]
setenv KZMIN Y               #> use Min Kz option in edyintb [ default: Y ],
                             #>    otherwise revert to Kz0UT
setenv CTM_ILDEPV Y          #> calculate in-line deposition velocities [ default: Y ]
setenv CTM_MOSAIC N          #> landuse specific deposition velocities [ default: N ]
setenv CTM_FST N             #> mosaic method to get land-use specific stomatal flux
                             #>    [ default: N ]
setenv CTM_ABFLUX N          #> ammonia bi-directional flux for in-line deposition
                             #>    velocities [ default: N ]; ignore if CTM_ILDEPV = N
setenv CTM_HGBIDI N          #> mercury bi-directional flux for in-line deposition
                             #>    velocities [ default: N ]; ignore if CTM_ILDEPV = N
setenv CTM_SFC_HONO Y        #> surface HONO interaction [ default: Y ]; ignore if CTM_ILDEPV = N
setenv CTM_GRAV_SETL Y       #> vdiff aerosol gravitational sedimentation [ default: Y ]
setenv CTM_BIOGEMIS N        #> calculate in-line biogenic emissions [ default: N ]
setenv CTM_PT3DEMIS N        #> calculate in-line plume rise for elevated point emissions
                             #>    [ default: N ]
setenv CTM_ZERO_PCSOA N      #> turn off the emissions of the VOC precursor to pcSOA.
                             #>    The CMAQ dev team recommends leaving pcSOA mass in the
                             #>    model for production runs. [ default: N ]

#> Process Analysis Options
setenv CTM_PROCAN N          #> use process analysis [ default: N]
#> process analysis global column, row and layer ranges
#> user must check GRIDDESC for validity!
setenv PA_BCOL_ECOL "10 320"
setenv PA_BROW_EROW "10 195"
setenv PA_BLEV_ELEV "1  4"

#> I/O Controls
setenv IOAPI_LOG_WRITE F     #> turn on excess WRITE3 logging [ options: T | F ]
setenv FL_ERR_STOP N         #> stop on inconsistent input files
setenv PROMPTFLAG F          #> turn on I/O-API PROMPT*FILE interactive mode [ options: T | F ]
setenv IOAPI_OFFSET_64 NO   #> support large timestep records (>2GB/timestep record) [ options: YES | NO ]
setenv CTM_EMISCHK N         #> Abort CMAQ if missing surrogates from emissions Input files

#> Aerosol Diagnostic Controls
setenv CTM_AVISDIAG Y        #> Aerovis diagnostic file [ default: N ]
setenv CTM_PMDIAG Y          #> aerosol diagnostic file [ default: Y ]
setenv CTM_APMDIAG Y         #> output APMDIAG (average PMDIAG) even if PMDIAG file turned off [ default: N ]
setenv APMDIAG_BLEV_ELEV "1 3" #> layer range for average pmdiag
setenv APMDIAG_BLEV_ELEV ""  #> layer range for average pmdiag = NLAYS
setenv AVG_FILE_ENDTIME N    #> What is this [ default: N ]

#> Diagnostic Output Flags
setenv CTM_CKSUM Y           #> cksum report [ default: Y ]
setenv CLD_DIAG Y            #> cloud diagnostic file [ default: N ]
setenv CTM_AERDIAG Y         #> aerosol diagnostic file [ default: N ]
setenv CTM_PHOTDIAG Y        #> photolysis diagnostic file [ default: N ]
setenv CTM_SSEMDIAG Y        #> sea-salt emissions diagnostic file [ default: N ]
setenv CTM_DUSTEM_DIAG Y     #> windblown dust emissions diagnostic file [ default: N ]; ignore if CTM_WB_DUST = N
setenv CTM_DEPV_FILE Y       #> deposition velocities diagnostic file [ default: N ]
setenv VDIFF_DIAG_FILE Y     #> vdiff & possibly aero grav. sedimentation diagnostic file [ default: N ]
setenv LTNGDIAG Y            #> lightning diagnostic file [ default: N ]
setenv CTM_AOD Y             #> AOD diagnostic file [ default: N ]
#setenv B3GTS_DIAG Y          #> beis mass emissions diagnostic file [ default: N ]
setenv PT3DDIAG N            #> optional 3d point source emissions diagnostic file [ default: N]; ignore if CTM_PT3DEMIS = N
setenv PT3DFRAC N            #> optional layer fractions diagnostic (play) file(s) [ default: N]; ignore if CTM_PT3DEMIS = N
setenv REP_LAYER_MIN -1      #> Minimum layer for reporting plume rise info [ default: -1 ]

set DISP = keep              #> [ delete | keep ] existing output files

#> MPI Optimization Flags
setenv MPI_SM_POOL 16000     #> increase shared memory pool in case many MPI_SEND headers
setenv MP_EAGER_LIMIT 65536  #> set MPI message passing buffer size to max
setenv MP_SINGLE_THREAD yes  #> optimizate for single threaded applications [ default: no ]
setenv MP_STDOUTMODE ordered #> order output by the processor ID
setenv MP_LABELIO yes        #> label output by processor ID [ default: no ]
setenv MP_SHARED_MEMORY yes  #> force use of shared memory for tasks on same node [ default: no ]
setenv MP_ADAPTER_USE shared #> share the MP adapter with other jobs
setenv MP_CPU_USE multiple   #> share the node with multiple users/jobs
setenv MP_CSS_INTERRUPT yes  #> specify whether arriving packets generate interrupts [ default: no ]

# =====================================================================
#> Input Directories and Filenames
# =====================================================================

set ICpath    = ${INPDIR}/icon
set BCpath    = ${INPDIR}/bcon
set METpath   = ${INPDIR}/mcip              #> meteorology input directory
#set JVALpath  = ${INPDIR}/jproc            #> offline photolysis rate table directory
set PHOTpath   = ${INPDIR}/phot               #> ozone columne data for the photolysis model
set LUpath    = ''                        #> BELD landuse data for windblown dust model
set OCNpath    = ${INPDIR}/ocn

set ICBC_CASE = na                        #> Version label for the ICBCs
set EMIS_CASE = na                        #> Version Label for the Emissions

# =====================================================================
#> Begin Loop Through Simulation Days
# =====================================================================

set TODAYG = ${START_DATE}
set TODAYJ = `date -ud "${START_DATE}" +%Y%j` #> Convert YYYY-MM-DD to YYYYJJJ
set STOP_DAY = `date -ud "${END_DATE}" +%Y%j` #> Convert YYYY-MM-DD to YYYYJJJ

while ($TODAYJ <= $STOP_DAY )  #>Compare dates in terms of YYYYJJJ

  #> Retrieve Calendar day Information
  set YYYYMMDD = `date -ud "${TODAYG}" +%Y%m%d` #> Convert YYYY-MM-DD to YYYYMMDD
  set YYMMDD = `date -ud "${TODAYG}" +%y%m%d`   #> Convert YYYY-MM-DD to YYMMDD
  set YYYYJJJ = $TODAYJ

  #> Increment both Gregorian and Julian Days
  set TOMORROWG = `date -ud "${TODAYG}+1days" +%Y-%m-%d` #> Add a day for tomorrow
  set TOMORROWJ = `date -ud "${TOMORROWG}" +%Y%j` #> Convert YYYY-MM-DD to YYYYJJJ

  #> Calculate Yesterday's Date
  set YESTERDAY = `date -ud "${TODAYG} -1days" +%Y%m%d`     #> Calc tomorrow YYYYMMDD

  #> set output file name extensions
  setenv CTM_APPL ${RUNID}_${YYYYMMDD}
  setenv CTM_APPLY ${RUNID}_${YESTERDAY}

  set cday = $YYYYMMDD
  set year = `date -d "$cday" +%Y`
  set mm = `date -d "$cday" +%m`
  set upcm = `date -d "$cday" '+%^b'` # upper case month abbr JAN FEB ...

echo '======================================================================'
echo running cctm for $cday

# =====================================================================
#> Input Files (Some are Day-Dependent)
# =====================================================================

  #> Initial conditions
  setenv NEW_START false
  if ( $cday == $FIRSTDAY ) setenv NEW_START true

  if ($NEW_START == true || $NEW_START == TRUE ) then
# m3tshift from
# /asm1/ROMO/global/CMAQv5.2/2016fc_hemi_cb6_16jh/108km/output/CGRID/CCTM_CGRID_v52_hdifupdate_intel17.0_HEMIS_cb6_20160430

     setenv ICFILE CCTM_CGRID_v52_hdifupdate_intel17.0_HEMIS_cb6_20160430.ts.2015121z00
     setenv INITIAL_RUN Y #related to restart soil information file
     mkdir -p $OUTDIR
  else
     set ICpath = $OUTDIR/CGRID/
     setenv ICFILE CCTM_CGRID_${RUNID}_${YESTERDAY}
     setenv INITIAL_RUN N
  endif

  #> Boundary conditions
  set BCFILE = hemisphere_bc_cb05mp_cleano3_44L.ncf

  #> Off-line photolysis rates
  #set JVALfile  = JTABLE_${YYYYJJJ}

  #> Ozone column data
  if ( $year == 2016 ) then
    # omi for 2016 iaw email Wed 6/13/2018 5:15 PM Barron Henderson
    # note that this is not the same as the v5.3 1979_to_2017
    # format and data is different
    set OMIfile = OMI_2016_cmaqv52.dat
  else
    set OMIfile   = OMI_1979_to_2015.dat
  endif

  #> Optics file
  set OPTfile = PHOT_OPTICS.dat

#> MCIP meteorology files
  setenv GRID_BDY_2D $METpath/GRIDBDY2D.$metgrid.${NZ}L.$YYMMDD
  setenv GRID_CRO_2D $METpath/GRIDCRO2D.$metgrid.${NZ}L.$YYMMDD
#  setenv GRID_CRO_3D $METpath/GRIDCRO3D.$metgrid.${NZ}L.$YYMMDD
  setenv GRID_CRO_3D '' # this file not used or available
  setenv GRID_DOT_2D $METpath/GRIDDOT2D.$metgrid.${NZ}L.$YYMMDD
  setenv MET_CRO_2D  $METpath/METCRO2D.$metgrid.${NZ}L.$YYMMDD
  setenv MET_CRO_3D  $METpath/METCRO3D.$metgrid.${NZ}L.$YYMMDD
  setenv MET_DOT_3D  $METpath/METDOT3D.$metgrid.${NZ}L.$YYMMDD
  setenv MET_BDY_3D  $METpath/METBDY3D.$metgrid.${NZ}L.$YYMMDD

  setenv LAYER_FILE $MET_CRO_3D  # Deprecated: MET_CRO_3D is now read directly in CCTM

  setenv LAYP_STTIME $STTIME
  setenv LAYP_NSTEPS $NSTEPS
  setenv LAYP_STDATE $YYYYJJJ

  #> Emissions files
  if ( $CTM_PT3DEMIS == 'N' ) then
     #> Offline 3d emissions file name
     set EMISpath  = ${INPDIR}/emis

     if ( $year == 2015 ) then
     set EMISfile  = emis_mole_all_${cday}_HEMI_108k_withbeis_withrwc_2016fe_${esens}_spinup_cb6_15jh.ncf
     endif

     if ( $year == 2016 ) then
     set EMISfile  = emis_mole_all_${cday}_HEMI_108k_withbeis_withrwc_2016fe_${esens}_hemi_cb6_16jh.ncf
     endif
    else
     #> In-line emissions configuration
	echo "Not implemented"
	exit
  endif #> Emissions files

  #> Lightning NOx configuration
  if ( $CTM_LTNG_NO == 'Y' ) then
     echo "Not Used"
     exit
  endif

  #> In-line biogenic emissions configuration
  if ( $CTM_BIOGEMIS == 'Y' ) then
     echo "Not Used"
     exit
  endif

  #> Windblown dust emissions configuration
  if ( $CTM_WB_DUST == 'Y' ) then
     # Input variables for BELD3 Landuse option
     setenv DUST_LU_1 na
     setenv DUST_LU_2 na

     if ( $cday == 20161231 ) then
       setenv MODIS_FPAR ${INPDIR}/modis/modis_fpar_lai_hemi_108km_10yrAvg_daily_ioapi.ncf.day365.ts2016366
     else
       setenv MODIS_FPAR ${INPDIR}/modis/modis_fpar_lai_hemi_108km_10yrAvg_daily_ioapi.ncf
     endif

     if ( $CTM_ERODE_AGLAND == 'Y' ) then
        echo "Not Used"
        exit
     endif
  endif

  #> In-line sea salt emisisions configuration
  setenv OCEAN_1 $OCNpath/hemisphere_oceanfile_$upcm.ncf

  #> Bidiretional ammonia configuration
  if ( $CTM_ABFLUX == 'Y' ) then
     echo "Not Used"
     exit
  endif

# =====================================================================
#> Output Files
# =====================================================================
  #> set output file names
  setenv S_CGRID         "$OUTDIR/CGRID/CCTM_CGRID_${CTM_APPL}"         #> 3D Inst. Concenctrations
  setenv CTM_CONC_1      "$OUTDIR/CONC/CCTM_CONC_${CTM_APPL} -v"       #> On-Hour Concentrations
  setenv A_CONC_1        "$OUTDIR/ACONC/CCTM_ACONC_${CTM_APPL} -v"      #> Hourly Avg. Concentrations
  setenv MEDIA_CONC      "$OUTDIR/TRASH/CCTM_MEDIA_CONC_${CTM_APPL} -v" #> NH3 Conc. in Media
  setenv CTM_DRY_DEP_1   "$OUTDIR/TRASH/CCTM_DRYDEP_${CTM_APPL} -v"     #> Hourly Dry Deposition
  setenv CTM_DEPV_DIAG   "$OUTDIR/TRASH/CCTM_DEPV_${CTM_APPL} -v"       #> Dry Deposition Velocities
  setenv CTM_PT3D_DIAG   "$OUTDIR/TRASH/CCTM_PT3D_${CTM_APPL} -v"       #>
  setenv B3GTS_S         "$OUTDIR/TRASH/CCTM_B3GTS_S_${CTM_APPL} -v"    #> Biogenic Emissions
  setenv SOILOUT         "$OUTDIR/TRASH/CCTM_SOILOUT_${CTM_APPL}"       #> Soil Emissions
  setenv CTM_WET_DEP_1   "$OUTDIR/TRASH/CCTM_WETDEP1_${CTM_APPL} -v"    #> Wet Dep From All Clouds
  setenv CTM_WET_DEP_2   "$OUTDIR/TRASH/CCTM_WETDEP2_${CTM_APPL} -v"    #> Wet Dep From SubGrid Clouds
  setenv CTM_VIS_1       "$OUTDIR/TRASH/CCTM_PMVIS_${CTM_APPL} -v"      #> On-Hour Visibility
  setenv CTM_AVIS_1      "$OUTDIR/TRASH/CCTM_APMVIS_${CTM_APPL} -v"     #> Hourly-Averaged Visibility
  setenv CTM_PMDIAG_1    "$OUTDIR/TRASH/CCTM_PMDIAG_${CTM_APPL} -v"     #> On-Hour Particle Diagnostics
  setenv CTM_APMDIAG_1   "$OUTDIR/APMDIAG/CCTM_APMDIAG_${CTM_APPL} -v"    #> Hourly Avg. Particle Diagnostic
  setenv CTM_RJ_1        "$OUTDIR/PHOTDIAG1/CCTM_PHOTDIAG1_${CTM_APPL} -v"  #> Photolysis Rxn Diagnostics
  setenv CTM_RJ_2        "$OUTDIR/TRASH/CCTM_PHOTDIAG2_${CTM_APPL} -v"  #> Photolysis Rates Output
  setenv CTM_SSEMIS_1    "$OUTDIR/TRASH/CCTM_SSEMIS_${CTM_APPL} -v"     #> Sea Spray Emissions
  setenv CTM_DUST_EMIS_1 "$OUTDIR/TRASH/CCTM_DUSTEMIS_${CTM_APPL} -v"   #> Dust Emissions
  setenv CTM_IPR_1       "$OUTDIR/TRASH/CCTM_PA_1_${CTM_APPL} -v"       #> Process Analysis
  setenv CTM_IPR_2       "$OUTDIR/TRASH/CCTM_PA_2_${CTM_APPL} -v"       #> Process Analysis
  setenv CTM_IPR_3       "$OUTDIR/TRASH/CCTM_PA_3_${CTM_APPL} -v"       #> Process Analysis
  setenv CTM_IRR_1       "$OUTDIR/TRASH/CCTM_IRR_1_${CTM_APPL} -v"      #> Chem Process Analysis
  setenv CTM_IRR_2       "$OUTDIR/TRASH/CCTM_IRR_2_${CTM_APPL} -v"      #> Chem Process Analysis
  setenv CTM_IRR_3       "$OUTDIR/TRASH/CCTM_IRR_3_${CTM_APPL} -v"      #> Chem Process Analysis
  setenv CTM_DRY_DEP_MOS "$OUTDIR/TRASH/CCTM_DDMOS_${CTM_APPL} -v"      #> Dry Dep
  setenv CTM_DRY_DEP_FST "$OUTDIR/TRASH/CCTM_DDFST_${CTM_APPL} -v"      #> Dry Dep
  setenv CTM_DEPV_MOS    "$OUTDIR/TRASH/CCTM_DEPVFST_${CTM_APPL} -v"    #> Dry Dep Velocity
  setenv CTM_DEPV_FST    "$OUTDIR/TRASH/CCTM_DEPVMOS_${CTM_APPL} -v"    #> Dry Dep Velocity
  setenv CTM_VDIFF_DIAG  "$OUTDIR/TRASH/CCTM_VDIFF_DIAG_${CTM_APPL} -v" #> Vertical Dispersion Diagnostic
  setenv CTM_VSED_DIAG   "$OUTDIR/TRASH/CCTM_VSED_DIAG_${CTM_APPL} -v"  #> Particle Grav. Settling Velocity
  setenv CTM_AOD_1       "$OUTDIR/TRASH/CCTM_AOD_DIAG_${CTM_APPL} -v"   #> Aerosol Optical Depth Diagnostic
  setenv CTM_LTNGDIAG_1  "$OUTDIR/TRASH/CCTM_LTNGHRLY_${CTM_APPL} -v"   #> Hourly Avg Lightning NO
  setenv CTM_LTNGDIAG_2  "$OUTDIR/TRASH/CCTM_LTNGCOL_${CTM_APPL} -v"    #> Column Total Lightning NO

  #> set floor file (neg concs)
  setenv FLOOR_FILE ${OUTDIR}/FLOOR/FLOOR_${CTM_APPL}.txt

  #> create output directory
  if ( ! -d "$OUTDIR" ) mkdir -p $OUTDIR

  #> look for existing log files and output files
  set log_test = `ls CTM_LOG_???.${CTM_APPL}`
  set OUT_FILES = (${FLOOR_FILE} ${S_CGRID} ${CTM_CONC_1} ${A_CONC_1} ${MEDIA_CONC}         \
             ${CTM_DRY_DEP_1} $CTM_DEPV_DIAG $CTM_PT3D_DIAG $B3GTS_S $SOILOUT $CTM_WET_DEP_1\
             $CTM_WET_DEP_2 $CTM_VIS_1 $CTM_AVIS_1 $CTM_PMDIAG_1 $CTM_APMDIAG_1             \
             $CTM_RJ_1 $CTM_RJ_2 $CTM_SSEMIS_1 $CTM_DUST_EMIS_1 $CTM_IPR_1 $CTM_IPR_2       \
             $CTM_IPR_3 $CTM_IRR_1 $CTM_IRR_2 $CTM_IRR_3 $CTM_DRY_DEP_MOS                   \
             $CTM_DRY_DEP_FST $CTM_DEPV_MOS $CTM_DEPV_FST $CTM_VDIFF_DIAG $CTM_VSED_DIAG    \
             $CTM_AOD_1 $CTM_LTNGDIAG_1 $CTM_LTNGDIAG_2)
  set OUT_FILES = ( ${OUT_FILES:gas/-v//} )
  # until the behavior of the out_files and log_test is confirmed, do not
  # delete anything with this script.

echo "log_test =  $log_test"
echo "OUT_FILES = $OUT_FILES"

  set OLD_CONC_1 = "$OUTDIR/CONC/CCTM_CONC_${CTM_APPLY}" #> On-Hour Concentrations


  #> for the run control ...
  setenv CTM_STDATE      $YYYYJJJ
  setenv CTM_STTIME      $STTIME
  setenv CTM_RUNLEN      $NSTEPS
  setenv CTM_TSTEP       $TSTEP
  setenv EMIS_1 $EMISpath/$EMISfile
  setenv INIT_GASC_1 $ICpath/$ICFILE
  setenv INIT_AERO_1 $INIT_GASC_1
  setenv INIT_NONR_1 $INIT_GASC_1
  setenv INIT_TRAC_1 $INIT_GASC_1
  setenv BNDY_GASC_1 $BCpath/$BCFILE
  setenv BNDY_AERO_1 $BNDY_GASC_1
  setenv BNDY_NONR_1 $BNDY_GASC_1
  setenv BNDY_TRAC_1 $BNDY_GASC_1
  setenv OMI $PHOTpath/$OMIfile
  setenv OPTICS_DATA $PHOTpath/$OPTfile
  set TR_DVpath = $METpath
  set TR_DVfile = $MET_CRO_2D

# emis is BENZ, orig nml is BENZENE, so nml has been modified
# difw GC_cb6r3_ae6nvPOA_aq.nml1 GC_cb6r3_ae6nvPOA_aq.nml
# 79c79
# < 'BENZENE:78.1:BENZENE:1:::::BENZENE:1:::Yes:::Yes',
# ---
# > 'BENZENE:78.1:BENZ:1:::::BENZENE:1:::Yes:::Yes',
  #> species defn & photolysis
  setenv gc_matrix_nml ${NMLpath}/GC_$MECH.nml
  setenv ae_matrix_nml ${NMLpath}/AE_$MECH.nml
  setenv nr_matrix_nml ${NMLpath}/NR_$MECH.nml
  setenv tr_matrix_nml ${NMLpath}/Species_Table_TR_0.nml

  #> check for photolysis input data
  setenv CSQY_DATA ${NMLpath}/CSQY_DATA_$MECH
  
  set IN_FILES=(\
    ${EMIS_1} ${OMI} ${OPTICS_DATA} ${CSQY_DATA} \
    ${INIT_GASC_1} ${INIT_AERO_1} ${INIT_NONR_1} ${INIT_TRAC_1} \
    ${BNDY_GASC_1} ${BNDY_AERO_1} ${BNDY_NONR_1} ${BNDY_TRAC_1} \
    ${GRID_BDY_2D} ${GRID_CRO_2D} ${GRID_CRO_3D} ${GRID_DOT_2D} \
    ${MET_CRO_2D} ${MET_CRO_3D} ${MET_DOT_3D} ${MET_BDY_3D} \
  )

  foreach inpath  ( ${IN_FILES} )
    if (! (-e ${inpath} ) ) then
      echo " ${inpath} not found "
      exit 1
    endif
  end

  if ( -f ${OLD_CONC_1} ) then
    ./checkenddate.sh ${YYYYJJJ},0 ${OLD_CONC_1}
    if ( $? == 0 ) then
        echo "Ready to run ${YYYYMMDD}"
    else
        echo "Not ready to run ${YYYYMMDD}"
	exit 1
    endif
  endif
  if ( -f ${CTM_CONC_1:as/-v//} ) then
    ./checkenddate.sh ${TOMORROWJ},0 ${CTM_CONC_1}
    if ( $? == 0 ) then
        echo "${TODAYG} was already complete. Skipping to next day"
	set TODAYG = ${TOMORROWG}
	set TODAYJ = ${TOMORROWJ}
	continue
    else
        echo "${TODAYG} output exists, but is not complete."
	exit 1
    endif
  endif
  #> keep previous output files
  foreach file ( ${OUT_FILES:gas/-v//} $log_test )
     set filedir = `dirname $file`
     mkdir -p ${filedir}
     if ( -e $file ) then
        echo "will not delete a previous output for day $cday - abort"
        echo "delete or remove outputs from day $cday before rerunning this day"
        exit
     endif
  end
  endif

    set cursec  = `date +%s`
    set elapsec = `dc -e " $cursec $begsec  - p" `
    set leftsec = `dc -e " $totsec $elapsec - p" `

    echo '================================================================'
    echo "model day $cday to run:"
    echo "walltime      = $totsec"
    echo "elapsed time  = $elapsec"
    echo "leftover time = $leftsec"

    if ( $leftsec <= $leftchk ) then # if less than ___ hours walltime
       echo "less than $leftchk seconds left to run model day:" $cday
       echo 'resubmitting new cctm at day:' $cday
       setenv BEGDAY $cday
       # Call submit script
       cd $BASE
       FIRSTDAY=${FIRSTDAY} BEGDAY=${BEGDAY} ENDDAY=${ENDDAY} ./submit.sh
       echo 'stopping current cctm before:' $cday
       exit
    endif

# ===================================================================
#> Execution Portion
# ===================================================================

  #> Print attributes of the executable
  \ls -lpF --time-style=long-iso $BLD/$EXEC
  unlimit
  limit

  date

  #> Executable call for single PE, uncomment to invoke
  # /usr/bin/time  $BLD/$EXEC

  #> Executable call for multi PE, configure for your system
  # set MPI = /usr/local/intel/impi/3.2.2.006/bin64
  # set MPIRUN = $MPI/mpirun
  time mpirun -r ssh -np $NPROCS $BLD/$EXEC

  date

# ===================================================================
#> Finalize Run for This Day and Loop to Next Day
# ===================================================================

  #> Save Log Files and Move on to Next Simulation Day
  if (! -e $LOGDIR ) then
    mkdir -p $LOGDIR
  endif
  mv CTM_LOG_???.${CTM_APPL} $LOGDIR

  #> The next simulation day will, by definition, be a restart
  #  setenv NEW_START false
  #  defined by firstday status

  #> Increment both Gregorian and Julian Days
  set TODAYG = ${TOMORROWG} # > Add a day for tomorrow
  set TODAYJ = ${TOMORROWJ} # > Add a day for tomorrow
  rm ${OUTDIR}/TRASH/CCTM_*
end  #Loop to the next Simulation Day

exit
