#!/bin/tcsh

#PBS -N C_201601_225
#PBS -A UUTK0002
#PBS -l walltime=00:30:00
#PBS -q regular
#PBS -j oe
#PBS -k eod
#PBS -l select=4:ncpus=8:mpiprocs=8

# ====================== 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)
# ===================================================================  

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

#> Choose compiler and set up CMAQ environment with correct
#> libraries using config.cmaq. Options: intel | gcc | pgi
 setenv compiler intel

 #> Source the config.cmaq file to set the build environment
 cd /glade/scratch/chengpin/CMAQ_REPO
 source ./config_cmaq.csh
       
#> Set the model version
 set VRSN = v532
 set MECH = cb6r3_ae7_aq        #> Mechanism ID
 set CASE = TW2020  
 set APPL = v532_intel_$CASE	  #> Application Name (e.g. Code version, compiler, gridname, emissions, etc.)

#> 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 $CMAQ_HOME/POST/combine/scripts/BLD_combine_${VRSN}_${compilerString}
 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     ${CMAQ_HOME}/PREP/mcip          #> Met Output Directory
 setenv CCTMOUTDIR ${CMAQ_DATA}/output/201601_rkp/201601_R225 #> CCTM Output Directory
 setenv POSTDIR    ${CMAQ_DATA}/201601_rkp_COMBINE         #> Location where combine file will be written

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



# =====================================================================
#> COMBINE Configuration Options
# =====================================================================

#> Set Start and End Days for looping
 set START_DATE = "2016-01-01"     #> beginning date (July 10, 2011)
 set END_DATE   = "2016-01-02"     #> ending date    (July 14, 2011)
 
#> Set location of species definition files for concentration and deposition species.
 setenv SPEC_CONC $CMAQ_HOME/POST/combine/scripts/spec_def_files/SpecDef_${MECH}.txt
 setenv SPEC_DEP  $CMAQ_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_ACONC_${APPL}_${YYYY}${MM}_rkp_R225.nc

  #> 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/CCTM_ACONC_${APPL}_${YYYY}${MM}${DD}_R225.nc
   setenv INFILE2 $METDIR/METCRO3D_${YYYY}${MM}${DD}.nc
   setenv INFILE3 $CCTMOUTDIR/CCTM_APMDIAG_${APPL}_${YYYY}${MM}${DD}_R225.nc
   setenv INFILE4 $METDIR/METCRO2D_${YYYY}${MM}${DD}.nc

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

   chmod 644 ${OUTFILE}   

  #> 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

 #cd /glade/scratch/chengpin/CMAQ_REPO/CCTM/scripts/201601_rkp/201601_R225
 #sbatch backup

 exit()
