#!/bin/csh -f
#SBATCH --export=NONE

limit stacksize unlimited

setenv SECTOR "ptfire_othna3D"

if ($?SLURM_SUBMIT_DIR) then
  cd $SLURM_SUBMIT_DIR
endif

# Start and end dates (Julian dates)
set start_date = 2015356
set end_date = 2016366

if ( $?SLURM_JOB_ID ) then
 cd $SLURM_SUBMIT_DIR
else
 set SLURM_JOB_ID = $?
endif

## Definitions for case name, directory structures, etc, that are used
#  by every sector in the case
#  Anything defined in directory_definitions can be overridden here 
#  if desired
source ../directory_definitions_12US2.csh

#-----------------------------------------------------------------#

setenv MET_ROOT $MET_ROOT_3D

set CTM_3DPTFIRE = Y ## Convert 3D point fires to UAM ptsr

setenv GRID $REGION_ABBREV
setenv SPEC $EMF_SPC

if ($REGION_ABBREV == 12US2) then
  setenv XORG -2412.
  setenv YORG -1620.
  setenv NX 396
  setenv NY 246
  setenv DX 12.
  set LAYERS = 35  ## Number of layers to output
else
  echo "If not running 12US2, define grid parameters for your grid at the top of this script."
  exit (1)
endif

set casescripts = $CASESCRIPTS/camx
set camxscripts = $INSTALL_DIR/camx
set SPECIES_MAPPING = $camxscripts/Species_Mapping_Tables/MAPTBL.CAMX_CB6+AERO6_to_CAMXv6_CB6+CF+RTRACPM_3jul2017_bnza_from_CB6CMAQ

set YEAR = 2016
set datefile = $camxscripts/dates/mergedates_${YEAR}.txt
set execroot = $camxscripts/bin # Root to cmaq2camx executables

set QADIR = $casescripts/qatemp #location for QA files - domain totals and domain total 2D plots of point emissions
if (! -e $QADIR) then
	mkdir -p $QADIR
endif

set tempdir = $casescripts/work  ## Path for the temporary work directory
if (! -e $tempdir) then
	mkdir -p $tempdir
endif

setenv SCRIPTLOG cmaq2camx_${CASE}_${GRID}_${SLURM_JOB_ID}.log  ## Name of script log 
if ( -e $casescripts/$SCRIPTLOG) then
	rm $casescripts/$SCRIPTLOG
endif

#-----------------------------------------------------------------#

set today = $start_date

setenv OUTPUT_TIMEZONE 0 ## Timezone for output, typically should be left at 0
setenv OUTPUT_N_LAYERS $LAYERS

## Run the CMAQ to CAMx conversion by loop over the specified date range 
while ($today <= $end_date )

	echo $today >> $casescripts/$SCRIPTLOG
	#-----------------------------------------------------------------#
	set STDATE = $today

	set GDAY = (`$execroot/j2g $today`)
	set YYYY = `echo $GDAY | cut -c1-4`
	set YY = `echo $GDAY | cut -c3-4`
	set MM = `echo $GDAY | cut -c5-6`
	set DD = `echo $GDAY | cut -c7-8`
	set G_DAY = $YYYY$MM$DD
	set GS_DAY = $YY$MM$DD
	set YYDDD = `echo $today | cut -c3-8`

	set iday = `grep ^$STDATE $datefile`
	set dateheader = `grep ^Date $datefile`
	#---------------------------------------------------------------------------#

	setenv CMAQ_METCRO3D $MET_ROOT/METCRO3D_${YY}${MM}${DD}

	if ($CTM_3DPTFIRE == Y) then ## elevated point file preparation
                set repday = $iday[9]
		set PATHA = $INSTALL_DIR/${CASE}/premerged/$SECTOR
		set FILEA = emis_mole_${SECTOR}_${repday}_${GRID}_${SPEC}_${CASE}.ncf

		rm -f ${tempdir}/temp_${SLURM_JOB_ID}_ptf.ncf

		if (! -e $PATHA/$FILEA) then
			gunzip -c $PATHA/${FILEA}.gz > ${tempdir}/temp_${SLURM_JOB_ID}_ptf.ncf
		else
			cp $PATHA/$FILEA ${tempdir}/temp_${SLURM_JOB_ID}_ptf.ncf
		endif

		# 1. run cmaq2camx to convert IOAPI 2D file to camx format and adjust species 
		# 2. run qaemis on camx input file

		rm -f ${tempdir}/tmp_emis_${SLURM_JOB_ID}_ptf

		setenv INFILE  ${tempdir}/temp_${SLURM_JOB_ID}_ptf.ncf
		setenv OUTFILE ${tempdir}/tmp_emis_${SLURM_JOB_ID}_ptf
		setenv MAPTBL  $SPECIES_MAPPING
		$execroot/spcmap

		setenv CMAQEMIS ${tempdir}/tmp_emis_${SLURM_JOB_ID}_ptf
		rm -f ${tempdir}/tmp_pnt_${SLURM_JOB_ID} ${tempdir}/junk 

$execroot/cmaq2uam-11sep2013 << EOF
File Type          |EMIS
OutFile1 (IC,BC,AR)|${tempdir}/junk
OutFile2 (TopC,PT) |${tempdir}/tmp_pnt_${SLURM_JOB_ID}
Output Timezone    |0
EOF
		rm -f ${tempdir}/junk

		## Add effective plume height to the CAMx elevated point source file
		echo ${PATHA}/${FILEA} >> $casescripts/$SCRIPTLOG
		set OUTPATH = $INSTALL_DIR/$CASE/smoke_out/$CASE/$GRID/$SPEC/$SECTOR
		mkdir -p $OUTPATH
		set OUTFILE = ptsr.$SECTOR.${G_DAY}.${GRID}.bin

		rm -f $OUTPATH/$OUTFILE

$execroot/pthtq << EOF
Input CAMx PT      |${tempdir}/tmp_pnt_${SLURM_JOB_ID}
Output CAMx PT     |$OUTPATH/$OUTFILE
EOF

		rm -f ${tempdir}/tmp_pnt_${SLURM_JOB_ID}
		rm -f ${tempdir}/qaemis.${SECTOR}.${SLURM_JOB_ID}.in
cat << ieof > ${tempdir}/qaemis.${SECTOR}.${SLURM_JOB_ID}.in
$OUTPATH/$OUTFILE
$QADIR/ptfire_ca.$G_DAY.$GRID.$CASE.camx.tot.txt
$QADIR/ptfire_ca.$G_DAY.$GRID.$CASE.camx.hrs.txt
$QADIR/ptfire_ca.$G_DAY.$GRID.$CASE.camx.pave.bin
ieof

		$execroot/qaelev < ${tempdir}/qaemis.${SECTOR}.${SLURM_JOB_ID}.in
		rm -f ${tempdir}/qaemis.${SECTOR}.${SLURM_JOB_ID}.in
		rm -f ${tempdir}/temp_${SLURM_JOB_ID}_ptf.ncf

	endif # if CTM_3DPTFIRE

	@ today = $today + 1

	if ($today == 2015365) then
		set today = 2016001
	endif

end #loop back to next day

exit


