#! /bin/csh 

# ====================== COMBINE Run Script ======================== 
# Usage: run.combine.uncoupled.csh >&! combine_v52_uncoupled.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)
# ===================================================================  
#> Simple Linux Utility for Resource Management System 
#> (SLURM) - The following specifications are recommended 
#> for executing the runscript on the cluster at the 
#> National Computing Center used primarily by EPA.
#SBATCH -t 30:00:00
#SBATCH -n 1
#SBATCH -J hr2day
#SBATCH -p oar
#SBATCH --gid=romo
##SBATCH -A romo
#SBATCH -o /work/ROMO/2016platform/CAMx_v6.40/2016fc_cb6camx_16j/12US2/post-job/new_extra/hr2day_2016.all.depn.Y.12us2.extra.hour.log


#> The following commands output information from the SLURM
#> scheduler to the log files for traceability.
   if ( $?SLURM_JOB_ID ) then
      echo Job ID is $SLURM_JOB_ID
      echo Host is $SLURM_SUBMIT_HOST
      #> Switch to the working directory. By default,
      #>   SLURM launches processes from your home directory.
      echo Working directory is $SLURM_SUBMIT_DIR
      cd $SLURM_SUBMIT_DIR
   endif
   echo '>>>>>> start model run at ' `date`

#> 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
 setenv compiler intel
 setenv compilerVrsn 17.0
 #> Source the config.cmaq file to set the build environment
# cd /work/ROMO/users/hnf/cmaq_147.1_project
# source ./config_cmaq.csh
       
#> Set the model version

 setenv VRSN  v52
 setenv GRID 12US2

###set the model version
 set VRSN = v52
 set GRID = 12US2

#> Set General Parameters for Labeling the Simulation
 set CASE = SE52BENCH
 set APPL = 2016fc_cb6camx_16j    #> Application Name (e.g. Code version, compiler, gridname, emissions,

# set MECH = cb6r3_ae6_aq          #> Mechanism ID
# set APPL = $SCEN

#> Projection sphere type used by I/OAPI (use type #20 to match WRF/CMAQ)
 setenv IOAPI_ISPH 20

#> Set the build directory if this was not set above 
#> (this is where the CMAQ executable is located by default).
 if ( ! $?BINDIR ) then
  setenv BINDIR /work/ROMO/users/hnf/cmaq_147.1_project/POST/combine/scripts/BLD_combine_v52_intel17.0
 endif

#> Set the name of the executable.
 setenv EXEC combine_${VRSN}.exe

#> Set location of CMAQ repo.  This will be used to point to the correct species definition files.
# setenv REPO_HOME  ${CMAQ_REPO}

#> Set working, input and output directories
 setenv METDIR /work/ROMO/$PDIR/CMAQv52/2016fc_cb6r3_16j/$GRID/WRFv3.8_12US2_2016_35aL
# setenv METDIR /asm/ROMO/met/MCIP/WRFv3.8_12US2_2016_35aL/v4.3
# setenv METDIR /work/ROMO/2016platform/CAMx_v6.40/$APPL/$GRID/cmaq_mcipv4.3
 setenv CCTMOUTDIR /work/ROMO/$PDIR/CAMx_$VERSION/$SCEN/$GRID/temp
 setenv POSTDIR    /work/ROMO/$PDIR/CAMx_$VERSION/$SCEN/$GRID/POST_extra                #> Location where combine file will be written

  if ( ! -e $POSTDIR ) then
	  mkdir $POSTDIR
  endif



# =====================================================================
#> COMBINE Configuration Options
# =====================================================================
 foreach mon ( 01 02 03 04 05 06 07 08 09 10 11 12 )
   if ($mon == 01) then
       set START_DATE = "2016-01-01"     #> beginning date (July 10, 2011)
       set END_DATE   = "2016-02-01"     #> ending date    (July 14, 2011)
   else if ($mon == 02) then
       set START_DATE = "2016-02-01"     #> beginning date (July 10, 2011)
       set END_DATE   = "2016-03-01"     #> ending date    (July 14, 2011
   else if ($mon == 03) then
       set START_DATE = "2016-03-01"     #> beginning date (July 10, 2011)
       set END_DATE   = "2016-04-01"     #> ending date    (July 14, 2011
   else if ($mon == 04) then
       set START_DATE = "2016-04-01"     #> beginning date (July 10, 2011)
       set END_DATE   = "2016-05-01"     #> ending date    (July 14, 2011
   else if ($mon == 05) then
       set START_DATE = "2016-05-01"     #> beginning date (July 10, 2011)
       set END_DATE   = "2016-06-01"     #> ending date    (July 14, 2011
   else if ($mon == 06) then
       set START_DATE = "2016-06-01"     #> beginning date (July 10, 2011)
       set END_DATE   = "2016-07-01"     #> ending date    (July 14, 2011
   else if ($mon == 07) then
       set START_DATE = "2016-07-01"     #> beginning date (July 10, 2011)
       set END_DATE   = "2016-08-01"     #> ending date    (July 14, 2011
   else if ($mon == 08) then
       set START_DATE = "2016-08-01"     #> beginning date (July 10, 2011)
       set END_DATE   = "2016-09-01"     #> ending date    (July 14, 2011
   else if ($mon == 09) then
       set START_DATE = "2016-09-01"     #> beginning date (July 10, 2011)
       set END_DATE   = "2016-10-01"     #> ending date    (July 14, 2011
   else if ($mon == 10) then
       set START_DATE = "2016-10-01"     #> beginning date (July 10, 2011)
       set END_DATE   = "2016-11-01"     #> ending date    (July 14, 2011
   else if ($mon == 11) then
       set START_DATE = "2016-11-01"     #> beginning date (July 10, 2011)
       set END_DATE   = "2016-12-01"     #> ending date    (July 14, 2011
   else if ($mon == 12) then
       set START_DATE = "2016-12-01"     #> beginning date (July 10, 2011)
       set END_DATE   = "2016-12-31"     #> ending date    (July 14, 2011
   else
       echo " *** ${mon} is NOT a valid month (01-12) *** "
        exit
   endif
 
#> Set location of species definition files for concentration and deposition species.
    setenv SPEC_CONC /work/ROMO/$PDIR/CAMx_$VERSION/$SCEN/$GRID/post-job/new_extra/SpecDef_Dep_cb6r3_SOAP1_CAMx64.txt
# setenv SPEC_DEP  $REPO_HOME/POST/combine/scripts/spec_def_files/SpecDef_Dep_${MECH}.txt

#> Use GENSPEC switch to generate a new specdef file (does not generate output file).
    setenv GENSPEC N


# =====================================================================
#> Begin Loop Through Simulation Days to Create ACONC File
# =====================================================================

#> Set the species definition file for concentration species.
    setenv SPECIES_DEF $SPEC_CONC
 
#> Loop through all days between START_DAY and END_DAY
    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 YYYY = `date -ud "${TODAYG}" +%Y`
   set YY = `date -ud "${TODAYG}" +%y`
   set MM = `date -ud "${TODAYG}" +%m`
   set DD = `date -ud "${TODAYG}" +%d`
  #> for files that are indexed with Julian day:
   #  set YYYYJJJ = `date -ud "${TODAYG}" +%Y%j` 

  #> Define name of combine output file to save hourly average concentration.
  #> A new file will be created for each month/year.
   setenv OUTFILE ${POSTDIR}/combine.depn.${SCEN}.$GRID.$YYYY$mon.p1.ncf

  #> Define name of input files needed for combine program.
  #> File [1]: CMAQ conc/aconc file
  #> File [2]: MCIP METCRO3D file
  #> File [3]: CMAQ APMDIAG file
  #> File [4]: MCIP METCRO2D file
   setenv INFILE1 $CCTMOUTDIR/$TODAYJ.$GRID.35.${SCEN}.epa.camx.ddep.ncf
   setenv INFILE2 $CCTMOUTDIR/$TODAYJ.$GRID.35.${SCEN}.epa.camx.wdep.ncf
   setenv INFILE3 $METDIR/METCRO2D.$GRID.35L.$YY$MM$DD

  #> Executable call:
   ${BINDIR}/${EXEC}

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

 end #Loop to the next Simulation Day
 end #Loop to the next Simulation month

exit()
# =====================================================================
#> Begin Loop Through Simulation Days to Create DEP File
# =====================================================================

#> Set the species definition file for concentration species.
 setenv SPECIES_DEF $SPEC_DEP
 
#> Loop through all days between START_DAY and END_DAY
 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 YYYY = `date -ud "${TODAYG}" +%Y`
   set YY = `date -ud "${TODAYG}" +%y`
   set MM = `date -ud "${TODAYG}" +%m`
   set DD = `date -ud "${TODAYG}" +%d`
  #> for files that are indexed with Julian day:
   #  set YYYYJJJ = `date -ud "${TODAYG}" +%Y%j` 

  #> Define name of combine output file to save hourly total deposition.
  #> A new file will be created for each month/year.
#   setenv OUTFILE ${POSTDIR}/COMBINE_DEP_${APPL}_$YYYY$MM

  #> Define name of input files needed for combine program.
  #> File [1]: CMAQ DRYDEP file
  #> File [2]: CMAQ WETDEP file
  #> File [3]: MCIP METCRO2D
  #> File [4]: {empty}
#   setenv INFILE1 $CCTMOUTDIR/CCTM_DRYDEP_${APPL}_$YYYY$MM$DD.nc
#   setenv INFILE2 $CCTMOUTDIR/CCTM_WETDEP1_${APPL}_$YYYY$MM$DD.nc
#   setenv INFILE3 $METDIR/METCRO2D_$YY$MM$DD.nc
#   setenv INFILE4

  #> Executable call:
#   ${BINDIR}/${EXEC}

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

 end #Loop to the next Simulation Day

 end
 exit()
