#!/bin/csh -f
#
## HEADER ########################################################
#
#  SMOKE ASSIGNS file for using SMOKE from EMF
#
#  Version @(#)$Id$
#  Path    $Source$
#  Date    $Date$
#
# Reserved environment variables coming from the EMF
#
#   Variable            Description             Example
#   --------            -----------             -------
#    CASE               Unique Case Name        2002ac
#    MODEL_LABEL        Model case is running   SMOKE 2.3.2
#    IOAPI_GRIDNAME_1   Grid name in GRIDDESC   US36KM_148X112
#    GRID               Grid abbreviation       36km
#    EMF_AQM            Air Quality model name  CMAQ
#    SPC                Speciation mech         cmaq_cb05pm
#    BASE_YEAR          Base modeling year      2002
#    FUTURE_YEAR        Future emissions year   2020
#    EPI_STDATE_TIME    Episode start date/time "2002-01-01 00:00:00.0"
#    EPI_ENDATE_TIME    Episode end date/time   "2002-12-31 23:59:00.0"
#    SECTOR             Inventory sector        nonpt
#    PLATFORM           Platform version        v3
#    MCIPNAME           MCIP name abbreviation  mcip_v3.3
#    SMK_HOME           SMOKE home directory    /orchid/oaqps/2002/smoke
#    DAT_ROOT           SMOKE data directory    /orchid/oaqps/2002/smoke
#    IMD_ROOT           SMOKE intermediate dir  /orchid/oaqps/2002/smoke/intermed
#    OUT_ROOT           SMOKE output directory  /orchid/oaqps/2002/cmaq_emis
#    MET_ROOT           Met data directory      /orchid/oaqps/2002/met

set outstat = 0
set filename = ASSIGNS.emf

## Check to make sure that required environment variables are provided
foreach var ( CASE GRID SPC BASE_YEAR SECTOR PLATFORM\
              SMK_HOME DAT_ROOT IMD_ROOT OUT_ROOT )
   set check = `env | grep "^$var\=" | cut -d\= -f1`
   if ( $check != $var ) then
       echo "SCRIPT ERROR: The $var environment variable must be set prior to"
       echo "              calling script $filename"
       set outstat = 1
   endif
end

## Exit if any required environment variables are not set
if ( $outstat == 1 ) then
   echo "ERROR ABORT: Environment variables that are preconditions for"
   echo "             script $filename are not set."
   exit( 1 )
endif

## Default settings based on other settings if they aren't set
if ( $?USE_CASE_CON ) then
  if ( $USE_CASE_CON == 'Y' && $?CASE_CON ) then
    setenv CASE $CASE_CON
  endif
else
  setenv USE_CASE_CON  N
endif

## Check if PARENT_CASE exists, if not, set to CASE
if ( $?PARENT_CASE == 0 ) then
    setenv PARENT_CASE $CASE
endif

## Check if RUN_SOURCESENS exists, if not set to N
if ( $?RUN_SOURCESENS == 0 ) then
    setenv RUN_SOURCESENS N
endif

## Check if  TEST_SANDBOX exists (testing sandbox), if not set to N
if ( $?TEST_SANDBOX == 0 ) then
    setenv TEST_SANDBOX N
endif

## Assume time step is 1 hour and run length per day is 25 hours
setenv G_TSTEP   10000            # time step  (HHMMSS)
setenv G_RUNLEN  250000           # run length (HHMMSS)

## Interpret model name for use in setting SMOKE path
set smkversion = `echo $MODEL_LABEL | cut -c6-`
switch ( "$smkversion" )
   case 2.3:
      set smkversion = 23
   breaksw
   case 2.3.2:
      set smkversion = 232
   breaksw
   case 2.4:
      set smkversion = 24
   breaksw
   case 2.6:
      set smkversion = 26
   breaksw
   case 2.7:
      set smkversion = 2.7
   breaksw
   case 3.0:
      set smkversion = 3.0
   breaksw
   case 3.1:
      set smkversion = 3.1
   breaksw
   case 3.5:
      set smkversion = 3.5
   breaksw
   case 3.6:
      set smkversion = 3.6
   breaksw
   case 4.0:
      set smkversion = 4.0
   breaksw
   default
      echo "ERROR ABORT: ASSIGNS.emf does not know MODEL_LABEL ="$MODEL_LABEL
      exit( 1 )
endsw   

## Directory settings
   ## If testing in sandbox, change SMK_SUBSYS
   if ( $TEST_SANDBOX == 'Y' ) then
        setenv SMK_SUBSYS   $SMK_HOME
   else
        setenv SMK_SUBSYS   $SMK_HOME
   endif        
   setenv SMKROOT      $SMK_SUBSYS/smoke$smkversion       # SMOKE root dir
   setenv SCRIPTS      $SMKROOT/scripts                   # SMOKE scripts
   setenv ASSIGNS      $SMKROOT/assigns
   setenv INVDIR       $PROJECT_ROOT/$CASE/inputs # Inventory input dir
   setenv GE_DAT       $DAT_ROOT/ge_dat       # Ancilary data input dir
   setenv REPOUT       $PROJECT_ROOT/$CASE/reports
   setenv INTERMED     $IMD_ROOT/$SECTOR            # Intermediate SMOKE files
   setenv PREMERGED    $PROJECT_ROOT/$CASE/premerged/$SECTOR
   if ( $?MOVES_TYPE ) then
     setenv INTERMED   $IMD_ROOT/$SECTOR/$MOVES_TYPE     # Intermediate SMOKE files
     setenv PREMERGED  $PROJECT_ROOT/$CASE/premerged/$SECTOR/$MOVES_TYPE     # Intermediate SMOKE files
   endif
   setenv LOGS         $INTERMED/logs               # Log files
   setenv M3STAT_LOGS  $INTERMED/m3stat_logs        # m3stat log files

   # Update 2/22/10:  If SRGPRO is defined in Inputs tab, derive SRGPRO_PATH from it
   #                  If SRGPRO not defined, use old method to derive SRGPRO_PATH
   if ( $?SRGPRO ) then
      # SRGPRO_PATH needs / at the end of it, but path_parser doesn't do that, so do this in two steps
      set srgpro_temp = `$SCRIPTS/run/path_parser.py $SRGPRO`
      setenv SRGPRO_PATH ${srgpro_temp}/
      echo "***testing*** SRGPRO_PATH set by SRGPRO input: $SRGPRO_PATH"
   else
      # Update 2/24/09.  Do not reset SRGPRO_PATH if it's already set to a 
      #                  non-blank value
      if ( $?SRGPRO_PATH ) then
         echo "***testing*** SRGPRO not defined, but SRGPRO_PATH parameter override set: $SRGPRO_PATH"
         if ( $SRGPRO_PATH == ' ' ) then
            setenv SRGPRO_PATH  $GE_DAT/${GRID}_surg/
            echo "***testing*** SRGPRO_PATH override is blank, SRGPRO_PATH set to default: $SRGPRO_PATH"
         endif
      else
         setenv SRGPRO_PATH  $GE_DAT/${GRID}_surg/
         echo "***testing*** SRGPRO and SRGPRO_PATH undefined, SRGPRO_PATH set to default: $SRGPRO_PATH"
      endif
   endif

   setenv SMK_DATEPATH $IMD_ROOT/datefiles
   setenv SMK_TMPDIR   $INTERMED/tmp
#   setenv SMK_METPATH  $IMD_ROOT/m6met  # C. Allen, 8 Aug 2014: This is an old relic of MOBILE6 processing from many years ago. No longer needed
#   setenv SMK_SPDPATH  $IMD_ROOT/m6spd  # C. Allen, 8 Aug 2014: This is an old relic of MOBILE6 processing from many years ago. No longer needed
#   setenv SMK_M6PATH   $IMD_ROOT/m6  # C. Allen, 8 Aug 2014: This is an old relic of MOBILE6 processing from many years ago. No longer needed
#   setenv SMK_EMISPATH $IMD_ROOT/m6emfac  # C. Allen, 8 Aug 2014: This is an old relic of MOBILE6 processing from many years ago. No longer needed
   setenv OUTPUT       $OUT_ROOT/$PROJECT/smoke_out/$CASE/$GRID/$SPC
   ## For source sensitivity set PARENT CASE's intermed and output directory
   if ( $RUN_SOURCESENS == Y ) then
      setenv PARENT_INTERMED $PROJECT_ROOT/$PARENT_CASE/intermed/$SECTOR
      setenv PARENT_INVDIR $DAT_ROOT/$PARENT_CASE/inputs
      setenv PARENT_OUTPUT $OUT_ROOT/$PROJECT/smoke_out/${PARENT_CASE}/$GRID/$SPC
   endif

## Set developer source-code directories
source $ASSIGNS/set_dirs.csh

#
#  Get system-specific flags
#source $ASSIGNS/sysflags
source $SMK_SUBSYS/scripts/platform

# Make directories for library, object files, and executables
setenv    SMK_BIN "$SMKROOT/$SMOKE_EXE"
setenv    IOAPIDIR $SMK_SUBSYS/ioapi

# Optional overrides for locations of SMK_BIN and IOAPIDIR that can
#   be easily set by the user: SMOKE_LOCATION and IOAPI_LOCATION
# Added by C. Allen, 28 Jul 2016
if ( $?SMOKE_LOCATION ) then
   setenv SMK_BIN $SMOKE_LOCATION
   if ( ! -e $SMOKE_LOCATION ) then
      echo ""
      echo "**** ERROR: SMOKE_LOCATION (location of SMOKE executables) does not exist. ****"
      echo "Either correct SMOKE_LOCATION, or remove SMOKE_LOCATION definition and rely on"
      echo "default SMOKE code location ($SMKROOT/$SMOKE_EXE)."
      echo ""
      exit (1)
   endif
endif
if ( $?IOAPI_LOCATION ) then
   setenv IOAPIDIR $IOAPI_LOCATION
   if ( ! -e $IOAPI_LOCATION ) then
      echo ""
      echo "**** ERROR: IOAPI_LOCATION (location of I/O API utilities) does not exist. ****"
      echo "Either correct IOAPI_LOCATION, or remove IOAPI_LOCATION definition and rely on"
      echo "default I/O API utility location ($SMK_SUBSYS/ioapi/$SMOKE_EXE), or add"
      echo "I/O API utility location to your PATH."
      echo ""
      exit (1)
   endif
endif

## Per-period environment variables
## Set start date based on EMF-defined variable EPI_STDATE_TIME, if it's available
if ( $?EPI_STDATE_TIME ) then

   # Set start and end dates
   # [**cvy**] Added end date variable "G_ENDATE" for use in graphics QA scripts
   setenv YEAR `echo $EPI_STDATE_TIME | cut -d\- -f1`
   set month = `echo $EPI_STDATE_TIME | cut -d\- -f2`
   set day   = `echo $EPI_STDATE_TIME | cut -d\- -f3 | cut -d" " -f1`
   setenv ESDATE $YEAR$month$day
   setenv G_STDATE `$IOAPIDIR/juldate $month $day $YEAR | grep $YEAR | cut -d, -f2`
   setenv G_STDATE_ORIG $G_STDATE    # Julian start date (doesn't change)

   set year_end = `echo $EPI_ENDATE_TIME | cut -d\- -f1`
   set month_end = `echo $EPI_ENDATE_TIME | cut -d\- -f2`
   set day_end   = `echo $EPI_ENDATE_TIME | cut -d\- -f3 | cut -d" " -f1`
   setenv G_ENDATE `$IOAPIDIR/juldate $month_end $day_end $year_end | grep $year_end | cut -d, -f2`

   #Set start time 
   set hour =  `echo $EPI_STDATE_TIME | cut -d" " -f2 | cut -d: -f1`
   set min  =  `echo $EPI_STDATE_TIME | cut -d" " -f2 | cut -d: -f2`
   set sec  =  `echo $EPI_STDATE_TIME | cut -d" " -f2 | cut -d: -f3 | cut -d. -f1`
   setenv G_STTIME $hour$min$sec

# If EMF setting is not available, assume beginning of the year
# Assume "BASE_YEAR" is the year to run
else

   setenv G_STDATE {$BASE_YEAR}001   # Julian start date
   setenv G_STDATE_ORIG $G_STDATE    # Julian start date (doesn't change)
   setenv G_ENDATE {$BASE_YEAR}365   # Julian end date
   setenv G_STTIME  000000           # start time (HHMMSS)
   setenv ESDATE   {$BASE_YEAR}0101  # Start date of emis time-based files/dirs
   setenv YEAR      $BASE_YEAR       # Base year for year-specific files

endif

## Reset days if overrides are available
   if ( $?G_STDATE_ADVANCE ) then
       set date = $G_STDATE
       @ date = $date + $G_STDATE_ADVANCE
       setenv G_STDATE $date 
       setenv ESDATE `/bin/env datshift $G_STDATE 0`
   endif

## If RUNSET environment variable is defined, call the runsettings helper
#  script with the current date to reset the RUN_* settings from the calling
#  script
if ( $?RUNSET ) then

   ## Determine highest RUN_PART* setting and set internal part variable 
   #     accordingly, in advance of the use of runsettings.csh.  The approach
   #     taken here accounts for the possibility that the calling script
   #     switched to a later part without setting the previous part to N
   if ( $?RUN_PART1 ) then
       if ( $RUN_PART1 == Y ) set part = PART1
   endif
   if ( $?RUN_PART1B ) then
       if ( $RUN_PART1B == Y ) set part = PART1B
   endif
   if ( $?RUN_PART2 ) then
       if ( $RUN_PART2 == Y ) set part = PART2
   endif
   if ( $?RUN_PART3 ) then
       if ( $RUN_PART3 == Y ) set part = PART3
   endif
   if ( $?RUN_PART4 ) then
       if ( $RUN_PART4 == Y ) set part = PART4
   endif

   # Before running runsettings.csh, make sure that part was set.  It might
   #    not get set if RUN_PART* has not yet been set in the script, which
   #    is okay.  This means that the script isn't yet ready to run anything
   #    anyway, so no need to change the run settings.
   ## Also check that the RUNSET file exists before trying to use it
   if ( $?part && -e $RUNSET ) then
      source $ASSIGNS/runsettings.csh $SECTOR $GRID $part $G_STDATE $RUNSET
   endif
endif

#
## If SECTOR is not set, default it to "nonpt" sources
if ( $?SECTOR ) then
else
   echo "ERROR: no SECTOR specified by script prior to calling $filname."
   exit(1)
endif

#
## If SUBSECT is not set, default it to $SECTOR
##    e.g., this is where "onroad" would become "onroad_jan"
if ( $?SUBSECT ) then
else
   setenv SUBSECT $SECTOR
endif

#
## If EISECTOR is not set, default it to $SECTOR
##    e.g., this is where "onroad" would become "onroad_jan"
if ( $?EISECTOR ) then
else
   setenv EISECTOR $SECTOR
endif

#
## If MONTH is not set, default it to jan
if ( $?MONTH ) then
else
   echo "NOTE: Default MONTH setting to 'jan'"
   setenv MONTH jan
endif

## If EMF_CLIENT and EMF_JOBKEY are not set, 
## set to EMF call to do nothing
if ( $?EMF_CLIENT == 0 ) then
   setenv EMF_CLIENT false
   setenv EMF_JOBKEY 0
endif
if ( $?EMF_JOBKEY == 0 ) then
   setenv EMF_JOBKEY 0
endif


## Check script settings
source $ASSIGNS/check_settings.csh

## Other local settings
set nicedate = (`date +%d%b%Y`)


##########  SMOKE formatted raw inputs #############
switch ( $SMK_SOURCE )
   case A:
      setenv LISTFILE  $INVDIR/$SECTOR/arinv_${SUBSECT}_$CASE.lst 
      setenv DAYLISTFILE  $INVDIR/$SECTOR/arday_${SUBSECT}_$CASE.lst 
      setenv HOURLISTFILE $INVDIR/$SECTOR/arhour_${SUBSECT}_$CASE.lst 
      breaksw
   case M:
      setenv LISTFILE  $INVDIR/$SECTOR/mbinv_${SUBSECT}_$CASE.lst 
      breaksw
   case P:
      setenv LISTFILE     $INVDIR/$SECTOR/ptinv_${SUBSECT}_$CASE.lst 
      setenv DAYLISTFILE  $INVDIR/$SECTOR/ptday_${SUBSECT}_$CASE.lst 
      setenv HOURLISTFILE $INVDIR/$SECTOR/pthour_${SUBSECT}_$CASE.lst 
      breaksw
endsw
#
## Input files not shared, but using the same naming convention
   setenv PROCDATES     $SMK_DATEPATH/procdates_${SUBSECT}_$MONTH.txt
   if ( $?L_TYPE ) then
      setenv SMK_RUN_DATES_1 $SMK_DATEPATH/smk_run_dates_${SUBSECT}_${MONTH}_$L_TYPE.txt
   else
      setenv SMK_RUN_DATES_1 $SMK_DATEPATH/smk_run_dates_${SUBSECT}_${MONTH}.txt
   endif

   if ( $?M_TYPE ) then
      setenv SMK_RUN_DATES_2 $SMK_DATEPATH/smk_run_dates_${SUBSECT}_${MONTH}_$M_TYPE.txt
   else
      setenv SMK_RUN_DATES_2 $SMK_DATEPATH/smk_run_dates_${SUBSECT}_${MONTH}.txt
   endif

## Meteorology IO/API input files (MCIP output files)
#
   if ( $?MCIPNAME ) then
   else
      setenv MCIPNAME mcip_v3.3  
   endif
 
   set quarters = ( q1 q1 q1 q2 q2 q2 q3 q3 q3 q4 q4 q4 )
   set locyr2 = `echo $ESDATE | cut -c3-4`
   set locmon  = `echo $ESDATE | cut -c5-6`
   set locday  = `echo $ESDATE | cut -c7-8`
   set qtr = $quarters[$locmon]

   setenv GRID_CRO_2D $MET_ROOT/GRIDCRO2D_$locyr2$locmon$locday
   setenv GRID_CRO_3D $MET_ROOT/GRIDCRO3D_$locyr2$locmon$locday
   setenv MET_CRO_2D  $MET_ROOT/METCRO2D_$locyr2$locmon$locday
   setenv MET_CRO_3D  $MET_ROOT/METCRO3D_$locyr2$locmon$locday
   setenv MET_DOT_3D  $MET_ROOT/METDOT3D_$locyr2$locmon$locday
   
   setenv MET_FILE1   $MET_CRO_2D
   setenv MET_FILE2   $MET_CRO_2D

#
##########################################################################
#
## Output and Intermediate files
#
## Area source intermediate and output files
#
if ( $SMK_SOURCE == 'A' ) then
   setenv ASCC      $INTERMED/ASCC_${SUBSECT}_$CASE.txt
   setenv ADAY      $INTERMED/aday_${SUBSECT}_$CASE.ncf       # Area NetCDF day-specific
   setenv REPINVEN  $REPOUT/programs/repinven_${SUBSECT}_$CASE.txt
   setenv ATSUPNAME $INTERMED/atsup_${SUBSECT}_${CASE}_
   setenv ATSUP     ${ATSUPNAME}$G_STDATE.txt
   setenv ASSUP     $INTERMED/assup_${SUBSECT}_${SPC}_$CASE.txt
   setenv AGSUP     $INTERMED/agsup_${SUBSECT}_${GRID}_$CASE.txt
   setenv ACREP     $REPOUT/programs/acrep_${SUBSECT}_$CASE.txt
   setenv APROJREP  $REPOUT/programs/aprojrep_${SUBSECT}_$CASE.rpt
   setenv AREACREP  $REPOUT/programs/areacrep_${SUBSECT}_$CASE.rpt
   setenv ACSUMREP  $REPOUT/programs/acsumrep_${SUBSECT}_$CASE.rpt
   setenv ACTLWARN  $REPOUT/programs/actlwarn_${SUBSECT}_$CASE.txt
   if ( $?CASE_CON ) then
      setenv AREA_O    $PROJECT_ROOT/$CASE_CON/inputs/$SECTOR/area_map_${SUBSECT}_$CASE_CON.txt
      setenv ARINV_O   $PROJECT_ROOT/$CASE_CON/inputs/$SECTOR/arinv_${SUBSECT}_${CASE_CON}_${nicedate}_orl.txt
   endif
endif


if ( $SMK_SOURCE == A || $RUN_SMKMERGE == Y && $MRG_AREA == Y ) then
   setenv AREA     $INTERMED/area_map_${SUBSECT}_$CASE.txt  #  Area inventory map
   setenv ATMPNAME $INTERMED/atmp_${SUBSECT}_${CASE}_
   setenv ATMP     ${ATMPNAME}$G_STDATE.ncf
   setenv ASMAT_L  $INTERMED/asmat_l_${SUBSECT}_${SPC}_$CASE.ncf
   setenv ASMAT_S  $INTERMED/asmat_s_${SUBSECT}_${SPC}_$CASE.ncf
   setenv AGMAT    $INTERMED/agmat_${SUBSECT}_${GRID}_${CASE}.ncf
   setenv APMAT    $INTERMED/apmat_${SUBSECT}_${CASE}.ncf
   setenv ACMAT    $APMAT
   if ( $?CASE_CON ) then # sensitivity case
      setenv ARMAT_L  $INTERMED/armat_l_${SUBSECT}_${SPC}_$CASE_CON.ncf
      setenv ARMAT_S  $INTERMED/armat_s_${SUBSECT}_${SPC}_$CASE_CON.ncf
      setenv ARSUP    $INTERMED/arsup_${SUBSECT}_${CASE_CON}.txt
      setenv ACMAT    $INTERMED/acmat_${SUBSECT}_${CASE_CON}.ncf          
      setenv APMAT    $INTERMED/apmat_${SUBSECT}_${CASE_CON}.ncf
   endif

   ## if running source sensitivity, set some of the intermediary files 
   ## to parent case's intermediary files
   if ( $RUN_SOURCESENS == Y ) then
	setenv AREA     $PARENT_INTERMED/area_map_${SUBSECT}_$PARENT_CASE.txt
	setenv ATMPNAME $PARENT_INTERMED/atmp_${SUBSECT}_${PARENT_CASE}_
	setenv ATMP     ${ATMPNAME}$G_STDATE.ncf
	setenv ASMAT_L  $PARENT_INTERMED/asmat_l_${SUBSECT}_${SPC}_$PARENT_CASE.ncf
	setenv ASMAT_S  $PARENT_INTERMED/asmat_s_${SUBSECT}_${SPC}_$PARENT_CASE.ncf
	setenv AGMAT    $PARENT_INTERMED/agmat_${SUBSECT}_${GRID}_${PARENT_CASE}.ncf
   endif
endif

## Biogenic source intermediate and output files
#
if ( $SMK_SOURCE == 'B' ) then
   setenv BGRD      $INTERMED/bgrd_summer_${GRID}_$CASE.ncf  # Summer/default normalized bio emis
   setenv BGRDW     $INTERMED/bgrd_winter_${GRID}_$CASE.ncf  # Winter grd normalized bio emis
   setenv B3GRD     $INTERMED/b3grd_${GRID}_${CASE}.ncf
   setenv SOILINP    $INTERMED/soilinp.$ESDATE.$GRID.$SPC.ncf 
   @         datep1 = `/bin/env datshift $ESDATE 1`	  
   setenv SOILOUT    $INTERMED/soilinp.$datep1.$GRID.$SPC.ncf 
endif

if ( $SMK_SOURCE == 'B' || $MRG_BIOG == 'Y' ) then
   setenv BGTS_L    $PREMERGED/emis_mole_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
   setenv BGTS_S    $PREMERGED/emis_mass_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
   setenv B3GTS_L   $BGTS_L
   setenv B3GTS_S   $BGTS_S
#   setenv BGTS_L_O  $INTERMED/bgts_l_o_${ESDATE}_1_${GRID}_${SPC}_$CASE.ncf
#   setenv BGTS_S_O  $INTERMED/bgts_s_o_${ESDATE}_1_${GRID}_${SPC}_$CASE.ncf
#   setenv B3GTS_L_O $INTERMED/b3gts_l_o_${ESDATE}_1_${GRID}_${SPC}_$CASE.ncf
#   setenv B3GTS_S_O $INTERMED/b3gts_s_o_${ESDATE}_1_${GRID}_${SPC}_$CASE.ncf
endif

# Mobile source intermediate and output files 
#
if ( $SMK_SOURCE == 'M' ) then
   setenv MSCC         $INTERMED/MSCC_${SUBSECT}_$CASE.txt
   setenv REPINVEN     $REPOUT/programs/repinven_${SUBSECT}_$CASE.txt
   setenv MTSUPNAME    $INTERMED/mtsup_${SUBSECT}_${CASE}_
   setenv MTSUP        ${MTSUPNAME}$G_STDATE.txt
   setenv MSSUP        $INTERMED/mssup_${SUBSECT}_${SPC}_$CASE.txt
   setenv MGSUP        $INTERMED/mgsup_${SUBSECT}_${GRID}_$CASE.txt
   setenv MCREP        $REPOUT/programs/mcrep_${SUBSECT}_$CASE.txt           
   setenv MPROJREP     $REPOUT/programs/mprojrep_${SUBSECT}_$CASE.txt
   setenv MREACREP     $REPOUT/programs/mreacrep_${SUBSECT}_$CASE.txt
   setenv MCSUMREP     $REPOUT/programs/mcsumrep_${SUBSECT}_$CASE.txt
   setenv MCTLWARN     $REPOUT/programs/mctlwarn_${SUBSECT}_$CASE.txt
   #      HOURLYT      automatically set and created by emisfac_run.scr script
   #      MEFLIST      automatically set and created by smk_run.scr script
   setenv SPDSUM       $INTERMED/spdsum_${SECTOR}_$CASE.txt        # Speed summary file
   setenv DAILYGROUP   $INTERMED/group_daily_${SECTOR}_$CASE.txt   # Daily group file
   setenv WEEKLYGROUP  $INTERMED/group_weekly_${SECTOR}_$CASE.txt  # Weekly group file
   setenv MONTHLYGROUP $INTERMED/group_monthly_${SECTOR}_$CASE.txt # Monthly group file
   setenv EPISODEGROUP $INTERMED/group_episode_${SECTOR}_$CASE.txt # Episode length group file
   if ( $?CASE_CON ) then
      setenv MOBL_O       $PROJECT_ROOT/$CASE_CON/inputs/$SECTOR/mobl_map_${SUBSECT}_$CASE_CON.txt
      setenv MBINV_O      $PROJECT_ROOT/$CASE_CON/inputs/$SECTOR/mbinv_${SUBSECT}_${CASE_CON}_${nicedate}_orl.txt
      setenv MBINV_AO     $PROJECT_ROOT/$CASE_CON/inputs/$SECTOR/mbinv_${SUBSECT}_${CASE_CON}_${nicedate}_vmt.txt
   endif
endif

if ( $SMK_SOURCE == M || $RUN_SMKMERGE == Y && $MRG_MOBILE == Y ) then
   setenv MOBL     $INTERMED/mobl_map_${SUBSECT}_$CASE.txt            # Inventory map file
   setenv MTMPNAME $INTERMED/mtmp_${SUBSECT}_${CASE}_
   setenv MTMP     ${MTMPNAME}$G_STDATE.ncf                # Temporal intermediate file
   setenv MTMP_INV $INTERMED/mtmp_inv_${SUBSECT}_${CASE}_$G_STDATE.ncf # For SMOKE-MOVES custom reporting
   setenv MSMAT_L  $INTERMED/msmat_l_${SUBSECT}_${SPC}_$CASE.ncf      # Speciation matrix, mole based
   setenv MSMAT_S  $INTERMED/msmat_s_${SUBSECT}_${SPC}_$CASE.ncf      # Speciation matrix, mass based
   setenv MUMAT    $INTERMED/mumat_${SUBSECT}_${GRID}_${CASE}.ncf     # Ungridding matrix
   setenv MGMAT    $INTERMED/mgmat_${SUBSECT}_${GRID}_${CASE}.ncf     # Gridding matrix
   if ( $?CASE_CON ) then
      setenv MRMAT_L  $INTERMED/mrmat_l_${SUBSECT}_${SPC}_$CASE_CON.ncf  # Reactivity matrix, mole based
      setenv MRMAT_S  $INTERMED/mrmat_s_${SUBSECT}_${SPC}_$CASE_CON.ncf  # Reactivity matrix, mass based
      setenv MRSUP    $INTERMED/mrsup_${SUBSECT}_${CASE_CON}.txt         # Reactivity ASCII part
      setenv MCMAT    $INTERMED/mcmat_${SUBSECT}_${CASE_CON}.ncf         # Multiplicative control matrix
      setenv MPMAT    $INTERMED/mpmat_${SUBSECT}_${CASE_CON}.ncf         # Growth matrix
   endif

   ## if running source sensitivity, set some of the intermediary files 
   ## to parent case's intermediary files
   if ( $RUN_SOURCESENS == Y ) then
	setenv MOBL     $PARENT_INTERMED/mobl_map_${SUBSECT}_$PARENT_CASE.txt
	setenv MTMPNAME $PARENT_INTERMED/mtmp_${SUBSECT}_${PARENT_CASE}_
	setenv MTMP     ${MTMPNAME}$G_STDATE.ncf 
	setenv MSMAT_L  $PARENT_INTERMED/msmat_l_${SUBSECT}_${SPC}_$PARENT_CASE.ncf
	setenv MSMAT_S  $PARENT_INTERMED/msmat_s_${SUBSECT}_${SPC}_$PARENT_CASE.ncf
	setenv MGMAT    $PARENT_INTERMED/mgmat_${SUBSECT}_${GRID}_${PARENT_CASE}.ncf 
   endif
endif

## Point source intermediate and output files
#
if ( $SMK_SOURCE == 'P' ) then
   setenv PSCC      $INTERMED/PSCC_${EISECTOR}_$CASE.txt
   setenv PDAY      $INTERMED/pday_${SUBSECT}_$CASE.ncf       # Point NetCDF day-specific
   setenv PHOUR     $INTERMED/phour_${SUBSECT}_$CASE.ncf      # Point NetCDF hour-specific
   setenv PHOURPRO  $INTERMED/phourpro_${SUBSECT}_$CASE.ncf   # Pt NetCDF src-spec dnl profs
   setenv REPINVEN  $REPOUT/programs/repinven_${EISECTOR}_$CASE.txt
   setenv PTSUPNAME $INTERMED/ptsup_${SUBSECT}_${CASE}_
   setenv PTSUP     ${PTSUPNAME}$G_STDATE.txt
   setenv PSSUP     $INTERMED/pssup_${EISECTOR}_${SPC}_$CASE.txt
   setenv PCREP     $REPOUT/programs/pcrep_${EISECTOR}_$CASE.rpt           
   setenv PPROJREP  $REPOUT/programs/pprojrep_${EISECTOR}_$CASE.txt
   setenv PREACREP  $REPOUT/programs/preacrep_${EISECTOR}_$CASE.txt
   setenv PCSUMREP  $REPOUT/programs/pcsumrep_${EISECTOR}_$CASE.txt
   setenv PCTLWARN  $REPOUT/programs/pctlwarn_${EISECTOR}_$CASE.txt
   setenv REPPELV   $REPOUT/programs/reppelv_${EISECTOR}_$CASE.txt
   if ( $?CASE_CON ) then
      setenv PNTS_O    $PROJECT_ROOT/$CASE_CON/inputs/$SECTOR/pnts_map_${SECTOR}_$CASE_CON.txt
      setenv PTINV_O   $PROJECT_ROOT/$CASE_CON/inputs/$SECTOR/ptinv_${SECTOR}_${CASE_CON}_${nicedate}_orl.txt
   endif
   if ( ! $?CEMSUM ) then
      setenv CEMSUM    $INTERMED/cemsum_${SECTOR}_$CASE.txt
   endif
   if ($?USE_FF10_DAILY_POINT) then # Added for FF10 daily point processing, with one PDAY/PHOUR for the year
      if ($USE_FF10_DAILY_POINT == Y) then
         setenv PDAY      $INTERMED/pday_${EISECTOR}_$CASE.ncf
         setenv PHOUR     $INTERMED/phour_${EISECTOR}_$CASE.ncf
      endif
   endif
   setenv CEMREPORT $REPOUT/programs/cemreport_${SECTOR}_$CASE.txt
endif

if ( $SMK_SOURCE == P || $RUN_SMKMERGE == Y && $MRG_POINT == Y ) then
   setenv PNTS     $INTERMED/pnts_map_${EISECTOR}_$CASE.txt   # Point inventory map
   setenv PTMPNAME $INTERMED/ptmp_${SUBSECT}_${CASE}_
   setenv PTMP     ${PTMPNAME}$G_STDATE.ncf
   setenv PSMAT_L  $INTERMED/psmat_l_${EISECTOR}_${SPC}_$CASE.ncf      # Speciation matrix, mole based
   setenv PSMAT_S  $INTERMED/psmat_s_${EISECTOR}_${SPC}_$CASE.ncf      # Speciation matrix, mass based
   setenv PGMAT    $INTERMED/pgmat_${EISECTOR}_${GRID}_${CASE}.ncf     # Gridding matrix
   if ( $?CASE_CON ) then
      setenv PRMAT_L  $INTERMED/prmat_l_${EISECTOR}_${SPC}_$CASE_CON.ncf  # Reactivity matrix, mole based
      setenv PRMAT_S  $INTERMED/prmat_s_${EISECTOR}_${SPC}_$CASE_CON.ncf  # Reactivity matrix, mass based
      setenv PRSUP    $INTERMED/prsup_${EISECTOR}_${CASE_CON}.txt         # Reactivity ASCII part
      setenv PCMAT    $INTERMED/pcmat_${EISECTOR}_${CASE_CON}.ncf         # Multiplicative control matrix
      setenv PPMAT    $INTERMED/ppmat_${EISECTOR}_${CASE_CON}.ncf         # Growth matrix
   endif
   setenv STACK_GROUPS $OUTPUT/$SECTOR/stack_groups_${SECTOR}_${GRID}_${CASE}.ncf
   setenv PLAY     $INTERMED/play_${SUBSECT}_${ESDATE}_${GRID}_${CASE}.ncf
   setenv PLAY_EX  $INTERMED/play_ex_${SUBSECT}_${ESDATE}_${GRID}_${CASE}.ncf
   # UPDATE 1/22/10 (C. Allen): Add grid name to PELV because PELV is grid-specific in SMOKE v2.6
   setenv PELV     $INTERMED/PELV_${EISECTOR}_${GRID}_$CASE.txt       # Elev/PinG pt source list
   if ( $?ELEVPOINT_DAILY ) then
      if ( $ELEVPOINT_DAILY == Y ) then
         setenv STACK_GROUPS $OUTPUT/$SECTOR/stack_groups_${SECTOR}_${ESDATE}_${GRID}_${CASE}.ncf
         # UPDATE 3/17/10 (C. Allen): When running Elevpoint on a daily basis, PELV is date-specific in SMOKE v2.6
         setenv PELV     $INTERMED/PELV_${EISECTOR}_${ESDATE}_${GRID}_$CASE.txt       # Elev/PinG pt source list
      endif
   endif

   ## if running source sensitivity, set some of the intermediary files 
   ## to parent case's intermediary files
   if ( $RUN_SOURCESENS == Y ) then
	setenv PNTS     $PARENT_INTERMED/pnts_map_${EISECTOR}_$PARENT_CASE.txt
	setenv PTMPNAME $PARENT_INTERMED/ptmp_${SUBSECT}_${PARENT_CASE}_
	setenv PTMP     ${PTMPNAME}$G_STDATE.ncf
	setenv PSMAT_L  $PARENT_INTERMED/psmat_l_${EISECTOR}_${SPC}_$PARENT_CASE.ncf
	setenv PSMAT_S  $PARENT_INTERMED/psmat_s_${EISECTOR}_${SPC}_$PARENT_CASE.ncf
	setenv PGMAT    $PARENT_INTERMED/pgmat_${EISECTOR}_${GRID}_${PARENT_CASE}.ncf
	setenv PLAY     $PARENT_INTERMED/play_${SUBSECT}_${ESDATE}_${GRID}_${PARENT_CASE}.ncf
        # UPDATE 1/22/10 (C. Allen): Add grid name to PELV because PELV is grid-specific in SMOKE v2.6
	setenv PELV     $PARENT_INTERMED/PELV_${EISECTOR}_${GRID}_$PARENT_CASE.txt
	setenv STACK_GROUPS $PARENT_OUTPUT/$SECTOR/stack_groups_${SECTOR}_${GRID}_${PARENT_CASE}.ncf
	if ( $?ELEVPOINT_DAILY ) then
	    if ( $ELEVPOINT_DAILY == Y ) then
		setenv STACK_GROUPS $PARENT_OUTPUT/$SECTOR/stack_groups_${SECTOR}_${ESDATE}_${GRID}_${PARENT_CASE}.ncf
                # UPDATE 3/17/10 (C. Allen): When running Elevpoint on a daily basis, PELV is date-specific in SMOKE v2.6
          	setenv PELV     $PARENT_INTERMED/PELV_${EISECTOR}_${ESDATE}_${GRID}_$PARENT_CASE.txt
	    endif
	endif
   endif
endif

# Source apportionment filenames (13 Aug 2013)

if ( $?SMK_SRCGROUP_OUTPUT_YN ) then
   if ( $SMK_SRCGROUP_OUTPUT_YN == Y ) then
   
      setenv SRCGROUPS_OUT $OUTPUT/$SECTOR/stack_groups_out_${SECTOR}_${ESDATE}_${GRID}_${CASE}.ncf
      setenv SGINLN $OUTPUT/$SECTOR/sginln_${MM}_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
      setenv SRCGRP_REPORT $REPOUT/sa/$SECTOR/srcgrp_report_${MM}_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.txt
      
      mkdir -p $REPOUT/sa/$SECTOR
      mkdir -p $OUTPUT/$SECTOR
      
   endif
endif

#
## Set generic environment variables for outputs from Smkmerge
#
# C. Allen: Most run scripts don't have RUN_MOVESMRG or RUN_LAYALLOC defined.
if (! $?RUN_MOVESMRG ) setenv RUN_MOVESMRG N
if (! $?RUN_LAYALLOC ) setenv RUN_LAYALLOC N

if ( $RUN_SMKMERGE == Y || $RUN_MOVESMRG == Y || $RUN_LAYALLOC == Y ) then
   # If using Smkmerge to merge all sectors
   if ( $MRG_AREA == Y && $MRG_BIOG == Y && $MRG_MOBILE == Y && $MRG_POINT == Y ) then
      setenv EOUT  $OUTPUT/emis_${MM}_all_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
      setenv AOUT  $INTERMED/emis_${MM}_ar_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
      setenv BOUT  $INTERMED/emis_${MM}_bg_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
      setenv MOUT  $INTERMED/emis_${MM}_mb_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
      setenv POUT  $INTERMED/emis_${MM}_pt_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
      setenv PING  $OUTPUT/ping_${MM}_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
      setenv ELEV  $OUTPUT/elev_${MM}_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
      setenv REPEG $REPOUT/smkmerge/rep_${MM}_all_${ESDATE}_${GRID}_${SPC}_${CASE}.txt
      setenv REPAG $REPOUT/smkmerge/rep_${MM}_ar_${ESDATE}_${GRID}_${SPC}_${CASE}.txt
      setenv REPBG $REPOUT/smkmerge/rep_${MM}_bg_${ESDATE}_${GRID}_${SPC}_${CASE}.txt
      setenv REPMG $REPOUT/smkmerge/rep_${MM}_mb_${ESDATE}_${GRID}_${SPC}_${CASE}.txt
      setenv REPPG $REPOUT/smkmerge/rep_${MM}_pt_${ESDATE}_${GRID}_${SPC}_${CASE}.txt
   else
      setenv EOUT  $PREMERGED/emis_${MM}_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
      setenv AOUT  $EOUT
      setenv BOUT  $EOUT
      setenv MOUT  $EOUT
      setenv POUT  $EOUT
      setenv INLN  $OUTPUT/$SECTOR/inln_${MM}_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
#      setenv INLN  $INTERMED/inln_${MM}_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
      setenv PING  $INTERMED/ping_${MM}_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
      setenv ELEV  $INTERMED/elev_${MM}_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
      setenv REPEG $REPOUT/smkmerge/$SECTOR/rep_${MM}_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.txt
      setenv REPAG $REPEG
      setenv REPBG $REPEG 
      setenv REPMG $REPEG
      setenv REPPG $REPEG
      ## [**cvy**] Biogenics reports; BEIS_REP_TYPE is either "county" or "state"
      if ($?BEIS_REP_TYPE) then
        setenv REPBG  $REPOUT/smkmerge/$SECTOR/rep_${MM}_${BEIS_REP_TYPE}_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.txt
      endif
      ## [**cvy**] For MOVES. Model-readies and reports must be kept separate
      if ( $?MOVES_TYPE ) then
         setenv MOUT  $PREMERGED/emis_${MM}_${MOVES_TYPE}_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
	 # Putting the three onroad components in separate directories will allow annual_report.py to work, at least once we go back to state-only reports
         setenv REPMG $REPOUT/smkmerge/$SECTOR/$MOVES_TYPE/rep_${MM}_${MOVES_TYPE}_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.txt
	 mkdir -p $REPOUT/smkmerge/$SECTOR/$MOVES_TYPE
	 
	 # source apportionment with SMOKE-MOVES
         if ( $?SMK_SRCGROUP_OUTPUT_YN ) then
            if ( $SMK_SRCGROUP_OUTPUT_YN == Y ) then
   
               setenv SRCGROUPS_OUT $OUTPUT/$SECTOR/$MOVES_TYPE/stack_groups_out_${MOVES_TYPE}_${SECTOR}_${ESDATE}_${GRID}_${CASE}.ncf
               setenv SGINLN $OUTPUT/$SECTOR/$MOVES_TYPE/sginln_${MM}_${MOVES_TYPE}_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.ncf
	       setenv SRCGRP_REPORT $REPOUT/sa/$SECTOR/$MOVES_TYPE/srcgrp_report_${MM}_${MOVES_TYPE}_${SECTOR}_${ESDATE}_${GRID}_${SPC}_${CASE}.txt
      
               mkdir -p $OUTPUT/$SECTOR/$MOVES_TYPE
	       mkdir -p $REPOUT/sa/$SECTOR/$MOVES_TYPE
      
            endif
         endif
      endif
   endif
endif

#  Create and change permissions for output directories
$ASSIGNS/smk_mkdir

set outstat = 0
if ( $status > 0 ) then
   set outstat = 1   
endif

#  Delete appropriate NetCDF files for the programs that are being run
if ( -e $ASSIGNS/smk_rmfiles_v3.scr ) then
   $ASSIGNS/smk_rmfiles_v3.scr
else
   echo "ERROR: missing smk_rmfiles_v3.scr in ASSIGNS directory for"
   echo "      automatic removal of SMOKE I/O API intermediate and"
   echo "      output files"
   set outstat = 1
endif
#
#  Unset temporary environment variables
source $ASSIGNS/unset.scr

if ( $outstat == 1 ) then
   echo "ERROR: Problem found while setting up SMOKE."
   echo "       See messages above."
   exit( 1 )
endif
