#!/bin/csh -f
# ====================== CCTMv5.1 Run Script ====================== 
# Usage: run.cctm >&! cctm_D51a.log &                                
#
# To report problems or request help with this script/program:        
#             http://www.cmascenter.org
# =================================================================== 

 #> Source the config.cmaq file to set the run environment
 source ${cmaq_build_path}/config_cmaq.csh 
 # set PROC     = serial #> serial or mpi
 set VRSN     = v52
 set APPL     = v52
 set CFG      = China_${domain}
 set MECH     = ${mech} 
 set EXEC     = CCTM_${APPL}.exe

 #> Set the working directory
 set BASE     = $cwd
 set BLD      = ${cmaq_build_path}/CCTM/scripts/BLD_CCTM_${VRSN}_${compiler}_${buildtag}
 cat $BLD/CCTM_${VRSN}.cfg; echo "    "; set echo

 #> timestep run parameters
 set STDATE  = `date -d "${yyyymmdd} -${time_difference} hours" +%Y%j` # beginning date
 set STTIME  = `date -d "${yyyymmdd} -${time_difference} hours" +%H%M%S`  # beginning GMT time (HHMMSS)
 set NSTEPS  = 240000       # time duration (HHMMSS) for this run
 set TSTEP   = 010000       # output time step interval (HHMMSS)
 set yyyy    = `date -d "${yyyymmdd}" +%Y`
 set pre_date = `date -d "${yyyymmdd} -1 days" +%Y%m%d`

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

 #=====================================================================
 # CCTM Configuration Options
 #=====================================================================
 setenv LOGDIR ${OUTDIR}/LOGS
 setenv LOGFILE ${log_file}  #> log file name; uncomment to write standard output to a log, otherwise write to screen

 setenv GRID_NAME ${CFG}     #> check GRIDDESC file for GRID_NAME options
 setenv GRIDDESC ${GRIDDESC_path}/GRIDDESC_${domain} #> grid description file
 #setenv CONC_SPCS "NO2 NO O3 SO2 CO HCHO HONO OH HO2H"
 setenv CONC_SPCS "ALL"
 #setenv CONC_BLEV_ELEV "ALL" #> CONC file layer range; comment to write all layers to CONC
 # setenv AVG_CONC_SPCS "ALL"
 setenv AVG_CONC_SPCS "O3 NO2 NO"
 setenv ACONC_BLEV_ELEV "1 1"  #> ACONC file layer range; comment to write all layers to ACONC
 #setenv ACONC_END_TIME Y #> override default beginning ACON timestamp [ default: N ]

 setenv EXECUTION_ID $EXEC      #> define the model execution id

 #> Sychronization Time Step Options
 setenv CTM_MAXSYNC 720       #> 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 CTM_ADV_CFL 0.95
 #> Science Options
 setenv CTM_WB_DUST N         #> 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: BELD3 ]; ignore if CTM_WB_DUST = N 

 #> turn on lightning NOx [ default: N ]
 if ( ${do_ltng} == 1 ) then
   setenv CTM_LTNG_NO Y
   echo "do_ltng = ${do_ltng} : turn on lighting NOx"
 else
   setenv CTM_LTNG_NO N
   echo "do_ltng = ${do_ltng} : turn off lighting NOx"
 endif

 setenv CTM_WVEL N            #> 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 YES   #> support large timestep records (>2GB/timestep record) [ options: YES | NO ]
 setenv CTM_EMISCHK N

 #> Aerosol Diagnostic Controls
 setenv CTM_AVISDIAG N        #> Aerovis diagnostic file [ default: N ]
 setenv CTM_PMDIAG N          #> Instantaneous Aerosol Diagnostic File [ default: Y ]
 setenv CTM_APMDIAG N         #> Hourly-Average Aerosol Diagnostic File [ default: Y ]
 setenv APMDIAG_BLEV_ELEV "1 3" #> layer range for average pmdiag
 setenv APMDIAG_BLEV_ELEV ""  #> layer range for average pmdiag = NLAYS

 #> Diagnostic Output Flags
 setenv CTM_CKSUM Y           #> write cksum report [ default: Y ]
 setenv CLD_DIAG N            #> write cloud diagnostic file [ default: N ]
 setenv CTM_AERDIAG N         #> aerosol diagnostic file [ default: N ]
 setenv CTM_PHOTDIAG N        #> photolysis diagnostic file [ default: N ]
 setenv CTM_SSEMDIAG N        #> sea-salt emissions diagnostic file [ default: N ]
 setenv CTM_DUSTEM_DIAG N     #> windblown dust emissions diagnostic file [ default: N ]; ignore if CTM_WB_DUST = N
 setenv CTM_DEPV_FILE N       #> write diagnostic file for deposition velocities [ default: N ]
 setenv LTNGDIAG N            #> write lightning diagnostic file [ default: N ]
 setenv B3GTS_DIAG N          #> 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 = update            #> [ delete | update | keep ] existing output files
 set DISP = delete
 # =====================================================================
 #> Input/Output Directories
 # =====================================================================
 set ICpath    = ${OUTDIR}         #> initial conditions input directory 
 set BCpath    = ${bcon_output_path}#> boundary conditions input directory
 set EMISpath  = ${emis_output_path}#> surface emissions input directory
 #set IN_PTpath = $M3DATA/emis      #> elevated emissions input directory (in-line point only)
 set IN_LTpath = ${light_path}      #> lightning NOx input directory
 set METpath   = ${mcip_output_path}#> meteorology input directory 
 #set JVALpath  = $M3DATA/jproc     #> offline photolysis rate table directory
 #set OMIpath   = $BLD              #> ozone columne data for the photolysis model
 set OMIpath   = ${OMI_path}
 #set LUpath    = $M3DATA/dust      #> BELD landuse data for windblown dust model
 #set SZpath    = $M3DATA/ocean     #> Surf zone file for in-line seasalt emissions

 # =====================================================================
 #> Input Files
 # =====================================================================

 #> Initial conditions
 if( ${run_type} == 0 & ${yyyymmdd} == ${start_date}) then
   set ICFILE = ICON_v52_${CFG}_profile
   setenv INITIAL_RUN Y #related to restart soil information file
   rm -rf $LOGDIR/CTM_LOG*
 else
   set ICFILE = CCTM_CGRID_${CFG}_${pre_date}.nc
   setenv INITIAL_RUN N
 endif
 setenv INIT_CONC_1 $ICpath/$ICFILE
 #> Boundary conditions
 if ( ${domain} == "d01" ) then
   if ( ${bcon_gc_use} == 1) then
     set BCpath    = ${gc_bcon_path}
     set BCFILE = ts${yyyymmdd}.bpch.BCON.nc
   else
     set BCpath    = ${bcon_output_path}
     set BCFILE = BCON_v52_${GRID_NAME}_${yyyymmdd}
   endif
 else
   set BCpath    = ${bcon_output_path}
   set BCFILE = BCON_v52_${GRID_NAME}_${yyyymmdd}
 endif
 echo "$BCpath/$BCFILE"
 setenv BNDY_CONC_1 $BCpath/$BCFILE

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

 #> Ozone column data
 set OMIfile   = OMI_1979_to_2015.dat

 #> Optics file
 set OPTfile = PHOT_OPTICS.dat

 #> MCIP meteorology files 
 setenv GRID_DOT_2D $METpath/GRIDDOT2D_${CFG}_${yyyymmdd}
 setenv GRID_CRO_2D $METpath/GRIDCRO2D_${CFG}_${yyyymmdd}
 setenv MET_CRO_2D $METpath/METCRO2D_${CFG}_${yyyymmdd}
 setenv MET_CRO_3D $METpath/METCRO3D_${CFG}_${yyyymmdd}
 setenv MET_DOT_3D $METpath/METDOT3D_${CFG}_${yyyymmdd}
 setenv MET_BDY_3D $METpath/METBDY3D_${CFG}_${yyyymmdd}
 setenv LAYER_FILE $MET_CRO_3D
 
 #> Emissions files 
 if ( ${time_difference} == 0 ) then
   setenv yyyymmdd_emis `date -d "${yyyymmdd}" +%Y%m%d`
 else
   setenv yyyymmdd_emis `date -d "${yyyymmdd} -1 days" +%Y%m%d`
 endif

 if ( $CTM_PT3DEMIS == 'N' ) then
   set EMISfile  = EM_${CFG}_${yyyymmdd_emis}.nc  #> Offline 3d emissions file name
   setenv EGRIDFILE1 $EMISpath/EGRID1/EM_${CFG}_${yyyymmdd_emis}.nc
 else
   #> In-line emissions configuration
   set STKCASEG = 12US1_G20_CB05E51
   set STKCASEE = 12US1_cb05e51_G20_CB05E51
   set CASE = 12CalnexBench_cb05e51_G20_CB05E51
   set EMISfile  = emis_mole_all_${YMD}_${CASE}.ncf #> Surface emissions
   setenv NPTGRPS 5          #> Number of elevated source groups

   setenv STK_GRPS_01 $IN_PTpath/stack_groups_ptnonipm_${STKCASEG}.ncf
   setenv STK_GRPS_02 $IN_PTpath/stack_groups_ptegu_${STKCASEG}.ncf
   setenv STK_GRPS_03 $IN_PTpath/stack_groups_othpt_${STKCASEG}.ncf
   setenv STK_GRPS_04 $IN_PTpath/stack_groups_ptfire_${YMD}_${STKCASEG}.ncf
   setenv STK_GRPS_05 $IN_PTpath/stack_groups_pt_oilgas_${STKCASEG}.ncf
   setenv LAYP_STTIME $STTIME
   setenv LAYP_NSTEPS $NSTEPS

   setenv STK_EMIS_01 $IN_PTpath/inln_mole_ptnonipm_${YMD}_${STKCASEE}.ncf
   setenv STK_EMIS_02 $IN_PTpath/inln_mole_ptegu_${YMD}_${STKCASEE}.ncf
   setenv STK_EMIS_03 $IN_PTpath/inln_mole_othpt_${YMD}_${STKCASEE}.ncf
   setenv STK_EMIS_04 $IN_PTpath/inln_mole_ptfire_${YMD}_${STKCASEE}.ncf
   setenv STK_EMIS_05 $IN_PTpath/inln_mole_pt_oilgas_${YMD}_${STKCASEE}.ncf
   setenv LAYP_STDATE $STDATE
 endif

 #> Lightning NOx configuration
 if ( $CTM_LTNG_NO == 'Y' ) then
     setenv LTNGNO "InLine"    #> set LTNGNO to "Inline" to activate in-line calculation
 #> In-line lightning NOx options
     setenv USE_NLDN  N        #> use hourly NLDN strike file [ default: Y ]
     setenv LTNGPARAM N        #> use lightning parameter file? [ default: Y ]
     #setenv LTNGPARMS_FILE $IN_LTpath/LTNG.ncf
     #setenv LTNGPARMS_FILE $IN_LTpath/LTNG_AllParms_12US1.ncf #> lightning parameter file
     if ( $USE_NLDN == Y ) then
        setenv NLDN_STRIKES $IN_LTpath/lightning/NLDN.12US1.${yyyymmdd}_bench.nc
     else
        setenv LOG_START 2.0   #> RC value to transit linear to log linear
     endif
 endif

 #> In-line biogenic emissions configuration
 if ( $CTM_BIOGEMIS == 'Y' ) then   
   set GSPROpath = ${M3DATA}/emis
   setenv GSPRO $GSPROpath/gspro_cb05soa_notoxics_cmaq_poc_09nov2007.txt
   set IN_BEISpath = ${M3DATA}/emis
   setenv B3GRD     $IN_BEISpath/b3grd.smoke30_beis361.12CalnexBench.2006NLCD_FIA5.1_norm_foliage_biomass.ncf
   setenv BIOG_SPRO     B10C5 # speciation profile to use for biogenics
   setenv BIOSW_YN      Y     # use frost date switch [ default: Y ]
   setenv BIOSEASON $IN_BEISpath/bioseason.cmaq.2011_12CalnexBench_wetland100.ghrsst.ncf #> ignore season switch file if BIOSW_YN = N
   setenv SUMMER_YN     N     # Use summer normalized emissions? [ default: Y ]
   setenv PX_VERSION    Y     # MCIP is PX version? [ default: N ]
   setenv INITIAL_RUN Y # non-existent or not using SOILINP [ default: N ]; default uses SOILINP
   setenv SOILINP $OUTDIR/$EXEC.SOILINP.${CFG}_${YMD}  # Biogenic NO soil input file; ignore if INITIAL_RUN = Y
 endif

 #> Windblown dust emissions configuration
 if ( $CTM_WB_DUST == 'Y' ) then
   # Input variables for BELD3 Landuse option
   setenv DUST_LU_1 $LUpath/beld3_12CalnexBench_output_a.ncf
   setenv DUST_LU_2 $LUpath/beld4_12CalnexBench_output_tot.ncf
   # Input variables for BELD4 Landuse option
   setenv BELD4_LU $LUpath/ 
   # All other Landuse options (USGS24, MODIS_NOAH, MODIS, NLCD50, NLCD40) read the GRID_CRO_2D file
   if ( $CTM_ERODE_AGLAND == 'Y' ) then
      setenv CROPMAP01 ${M3DATA}/crop/BeginPlanting_12CalnexBench
      setenv CROPMAP04 ${M3DATA}/crop/EndPlanting_12CalnexBench
      setenv CROPMAP08 ${M3DATA}/crop/EndHarvesting_12CalnexBench
   endif
 endif

 #> In-line sea salt emisisions configuration
 setenv OCEAN_1 ${ocean_output_path}/oceanfile_${CFG}.nc #> horizontal grid-dependent surf zone file
 if(! -f ${OCEAN_1} ) then
   echo "0: OCEAN_1:${OCEAN_1} doesn't exist"
   exit 1
 endif

 #> Bidiretional ammonia configuration
 if ( $CTM_ABFLUX == 'Y' ) then
   setenv E2C_Soilfile  ${M3DATA}/bidi/2011_12CalnexBench_soil.nc
   setenv E2C_Fertfile  ${M3DATA}/bidi/2011_12CalnexBench_time${YMD}.nc
   setenv B4LU_file     ${M3DATA}/bidi/beld4_12CalnexBench_2006nlcd.ncf    
   setenv E2C_SOIL ${E2C_Soilfile}
   setenv E2C_FERT ${E2C_Fertfile}
   setenv BELD4_LU ${B4LU_file}
 endif

 #=====================================================================
 #> Output Files
 #=====================================================================

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

 #> set floor file (neg concs)
 setenv FLOOR_FILE $LOGDIR/FLOOR_${CTM_APPL}

 set log_test = `ls ${OUTDIR}/CTM_LOG_???.${CTM_APPL}`
 #> look for existing log files and output files
 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 = `echo $OUT_FILES | sed "s; -v;;g" `
 echo $OUT_FILES
 set out_test = `ls $OUT_FILES`

 #> delete previous output if requested
 if ( $DISP == 'delete' ) then
     #> remove previous log files
     echo " ancillary log files being deleted"
     foreach file ( $log_test )
        echo " deleting $file"
        /bin/rm -f $file
     end

     #> remove previous output files
     echo " output files being deleted"
     foreach file ( $out_test )
        echo " deleting $file"
        /bin/rm -f $file
     end
 else
     #> error if previous log files exist
     if ( "$log_test" != "" ) then
       echo "*** Logs exist - run ABORTED ***"
       echo "*** To overide, set $DISP == delete in run_cctm.csh ***"
       echo "*** and these files will be automatically deleted. ***"
       exit 1
     endif
     #> error if previous output files exist
     if ( "$out_test" != "" ) then
       echo "*** Output Files Exist - run will be ABORTED ***"
       foreach file ( $out_test )
          echo " cannot delete $file"
       end
       echo "*** To overide, set $DISP == delete in run_cctm.csh ***"
       echo "*** and these files will be automatically deleted. ***"
       exit 1
     endif
 endif
 #> for the run control ...
 setenv CTM_STDATE      $STDATE
 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 $OMIpath/$OMIfile
 setenv OPTICS_DATA $BLD/$OPTfile

#setenv CTM_PMDIAG_1  ${cmaq_output_path}/${PMDIAGfile}
#setenv CTM_APMDIAG_1  ${cmaq_output_path}/${APMDIAGfile}
# if( -f ${CTM_PMDIAG_1} ) rm ${CTM_PMDIAG_1}
# if( -f ${CTM_APMDIAG_1} ) rm ${CTM_APMDIAG_1}
#setenv XJ_DATA $JVALpath/$JVALfile

 set TR_DVpath = $METpath
 set TR_DVfile = $MET_CRO_2D
 
 #> species defn & photolysis
 setenv gc_matrix_nml ${BLD}/GC_$MECH.nml
 setenv ae_matrix_nml ${BLD}/AE_$MECH.nml
 setenv nr_matrix_nml ${BLD}/NR_$MECH.nml
 setenv tr_matrix_nml ${BLD}/Species_Table_TR_0.nml
 
 #> check for photolysis input data
 setenv CSQY_DATA ${BLD}/CSQY_DATA_$MECH

 if (! (-e $CSQY_DATA ) ) then
   echo " $CSQY_DATA  not found "
   exit 1
 endif
 if (! (-e $OPTICS_DATA ) ) then
   echo " $OPTICS_DATA  not found "
   exit 1
 endif
# Added for CMAQ-DDM-3D 2022/5/14
# ===================================================================
#> CMAQ-DDM-3D
# ===================================================================

#> sensitivity output files
 setenv CTM_SENS_1      "$OUTDIR/CCTM_SENGRID_${CTM_APPL} -v"
 setenv A_SENS_1        "$OUTDIR/CCTM_ASENS_${CTM_APPL} -v"
 setenv CTM_SWETDEP_1   "$OUTDIR/CCTM_SENWDEP_${CTM_APPL} -v"
 setenv CTM_SDRYDEP_1   "$OUTDIR/CCTM_SENDDEP_${CTM_APPL} -v"

#> ddm-3d specific input files
 set NPMAX    = 4
 setenv SEN_INPUT ${cmaq_build_path}/CCTM/scripts/sensinput.cmas.dat.ENV1
 setenv DDM3D_HIGH Y     # allow higher-order sensitivity parameters [ T | Y | F | N ] (default is N/F)

 setenv DDM3D_RGN N      # allows for a file specifying regions [ T | Y | F | N ] (default is N/F) set it below if Y/T
 setenv REGIONS_1
 setenv DDM3D_BCRGN N    # allows for a file specifying boundary regions [ T | Y | F | N ] (default is N/F) set it below if Y/T
 setenv BCREGIONS_1
 setenv DDM3D_ES Y       # emissions split into categories [ T | Y | F | N ] (default is N/F) set them below if Y/T
 if( ${run_type} == 0 & ${yyyymmdd} == ${start_date}) then
    setenv DDM3D_RST N   # begins from sensitivities from a restart file [ T | Y | F | N ] (default is Y/T)
    set S_ICpath = 
    set S_ICfile =                                                                                      
 else
    setenv DDM3D_RST Y                                        
    set S_ICpath = $OUTDIR                                                                                
    set S_ICfile = CCTM_SENGRID_${CFG}_${pre_date}                                                     
 endif                                                                                                       

  setenv DDM3D_BCS F      # use sensitivity bc file for nested runs [ T | Y | F | N ] (default is N/F) 
  set S_BCpath =                                                                                         
  set S_BCfile =  

 #source /work/MOD3EVAL/nsu/ddm_v52/ddm3d.q
 #if ( $status ) exit 1

  setenv CTM_NPMAX       $NPMAX
  setenv     INIT_GASC_S     $S_ICpath/$S_ICfile
  setenv     INIT_AERO_S     $S_ICpath/$S_ICfile
  setenv     INIT_NONR_S     $S_ICpath/$S_ICfile
  setenv     INIT_TRAC_S     $S_ICpath/$S_ICfile
  setenv     BNDY_GASC_S     $S_BCpath/$S_BCfile
  setenv     BNDY_AERO_S     $S_BCpath/$S_BCfile
  setenv     BNDY_NONR_S     $S_BCpath/$S_BCfile

  set OUT_FILES_DDM = (${CTM_SENS_1} ${A_SENS_1} ${CTM_SWETDEP_1} ${CTM_SDRYDEP_1})
  set OUT_FILES_DDM = `echo $OUT_FILES_DDM | sed "s; -v;;g" `
  set out_test_ddm = `ls $OUT_FILES_DDM`

  foreach file ( $out_test_ddm )
      echo " deleting $file"
      /bin/rm -f $file
  end
# ===================================================================
#> Execution Portion
# ===================================================================

  #> Print attributes of the executable
  ls -l $BLD/$EXEC; size $BLD/$EXEC
  unlimit
  limit

 #> Executable call for single PE, uncomment to invoke
 set NCPUS = `cat $PBS_NODEFILE | wc -l`
 set mpirun = `which mpirun`
 setenv NPCOL_NPROW "${npcol_nprow}"
 cd ${OUTDIR}
 time ${mpirun} -np $NPROCS $BLD/$EXEC
 #time $BLD/$EXEC
 date

 if (! -e $LOGDIR ) then
    mkdir $LOGDIR
 endif
 mv CTM_LOG_???.${CTM_APPL} $LOGDIR
 #sleep 120
 exit
