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

unset verbose

limit stacksize unlimited

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

## 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
setenv SECTOR "camx_conv"
source ../directory_definitions_12US2.csh

setenv GRID $REGION_ABBREV
setenv GRIDPT 12US1 	# grid used for point sector processing
setenv SPEC $EMF_SPC
#setenv OLDCASE 2011ek_cb6v2_v6_11g

set EMIS_ROOT = $INSTALL_DIR/$CASE/smoke_out  ## Model ready emissions SMOKE out platform root

set point_sectors_convert = (ptegu ptnonipm pt_oilgas othpt cmv_c3) 
set ptfire_path = $EMIS_ROOT/$CASE/$GRID/$SPEC/ptfire3D  # Path to CAMx converted ptfire files
set ptagfire_path = $EMIS_ROOT/$CASE/$GRID/$SPEC/ptagfire3D  # Path to CAMx converted ptfire files
set ptfire_othna_path = $EMIS_ROOT/$CASE/$GRID/$SPEC/ptfire_othna3D  # Path to CAMx converted ptfire files
set firesectors = (ptfire3D ptfire_othna3D ptagfire3D)
set firepaths = ($ptfire_path $ptfire_othna_path $ptagfire_path)
set mrg_sectors =   (ptegu cmv_c3 ptfire3D ptnonipm pt_oilgas othpt ptfire_othna3D ptagfire3D) 
set groupnums = (0     0      0      0        0         0     0      0    )

setenv WRF_ROOT "/work/EMIS/met/wrfcamx/WRFv3.8_12US2_2016_35aL/v4.6" # Change to point to your WRF

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

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 CTM_2DEMIS = Y   ## Run the 2D / low-level emissions CMAQ to CAMx conversion
set CTM_SEASALT = Y  ## Run the seasalt 2D merge
set CTM_PT3DEMIS = Y  ## Run the elevated emissions CMAQ to CAMx conversion
set CTM_PTSMRG = Y ## Merge elevated emissions 

set INLN_ROOT = $EMIS_ROOT
#set OLDEMIS_ROOT = $INSTALL_DIR/$OLDCASE/smoke_out  ## Model ready emissions SMOKE out platform root
#set OLDINLN_ROOT = $OLDEMIS_ROOT

# SECTORLIST 
set SECTORLIST = $CASESCRIPTS/sectorlist_2016ff_12dec2018_v1

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

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

set today = $start_date

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

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 up date values from Julian date
	set STDATE = $today

	set GDAY = (`$execroot/j2g $today`)
	set YYYY = `echo $GDAY | cut -c1-4`
	set YY = `echo $YYYY | 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`
	#---------------------------------------------------------------------------#

	## Run the 2D / low-level cmaq to camx conversion
	if ($CTM_2DEMIS == 'Y') then ## 2D emissions preparation
		#need to combine these files, adjust species names, and add duplicate species

		set PATHA = ${EMIS_ROOT}/${CASE}/${GRID}/${SPEC}
		set FILEA = emis_mole_all_${G_DAY}_${GRID}_withbeis_${CASE}.ncf

		if (! -e $PATHA/$FILEA) then
			gunzip -c $PATHA/${FILEA}.gz > ${tempdir}/temp_${SLURM_JOB_ID}.ncf
		else
			cp $PATHA/$FILEA ${tempdir}/temp_${SLURM_JOB_ID}.ncf
		endif
		
		echo ${PATHA}/${FILEA} >> $casescripts/$SCRIPTLOG
		set OUTPATH = ${EMIS_ROOT}/${CASE}/${GRID}/camx
		set OUTFILE = emis2d.$G_DAY.$GRID.$CASE.camx

		if (! -e $OUTPATH) then
			mkdir -p $OUTPATH
		endif

		rm -f ${tempdir}/tmp_emis_${SLURM_JOB_ID}
		## Map the CMAQ species names to CAMx as specified in the MAPTBL
		setenv INFILE  ${tempdir}/temp_${SLURM_JOB_ID}.ncf
		setenv OUTFILE ${tempdir}/tmp_emis_${SLURM_JOB_ID}
		setenv MAPTBL  $SPECIES_MAPPING
		$execroot/spcmap

		setenv CMAQEMIS ${tempdir}/tmp_emis_${SLURM_JOB_ID}
		rm -f ${tempdir}/tmp_pnt_${SLURM_JOB_ID} $OUTPATH/$OUTFILE 

## Convert from low-level cmaq model ready IOAPI 2D file to camx
$execroot/cmaq2uam-21oct2013 << EOF
File Type          |EMIS
OutFile1 (IC,BC,AR)|$OUTPATH/$OUTFILE
OutFile2 (TopC,PT) |${tempdir}/tmp_pnt_${SLURM_JOB_ID}
Output Timezone    |$OUTPUT_TIMEZONE
EOF
		rm -f ${tempdir}/tmp_pnt_${SLURM_JOB_ID} ${tempdir}/tmp_emis_${SLURM_JOB_ID}

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

		## Run low-level / 2D QA on camx file
		$execroot/qalowemis < ${tempdir}/qaemis.low.${SLURM_JOB_ID}.in
		rm -f ${tempdir}/qaemis.low.${SLURM_JOB_ID}.in
		rm -f ${tempdir}/temp_${SLURM_JOB_ID}.ncf

		unset OUTFILE
		unsetenv OUTFILE
	endif ## end 2D emissions preparation

	if ($CTM_SEASALT == 'Y') then ## run 2D seasalt merge
		echo "Running seasalt conversion"
		set EMISPATH = ${EMIS_ROOT}/${CASE}/${GRID}/camx
		set MET = $WRF_ROOT
		set LUPATH = $camxscripts/land

$camxscripts/seasalt/seasalt.lnx << IEOF
Julian date        :$today
Time zone          :0
Method for open sea:GONG
Method for surfzone:GONG
Coastline length   :NONE
SS lower_limit     :0.0
SS upper limit     :2.5
Output emiss file  :${tempdir}/seasalt.camx
File identifier    :Sea Salt Emissions
Number of species  :4
Species name # 1   :PCL
Species name # 2   :NA
                   :SS
                   :PSO4
CAMx 3D file       :$MET/met3d.$GS_DAY.$GRID.35.wrf.camx
CAMx 2D file       :$MET/met2d.$GS_DAY.$GRID.35.wrf.camx
CAMx Land Use file :$MET/landuse.$GS_DAY.$GRID.zhang.camx
Saltwater mask     :$LUPATH/oceanmask.$GRID.txt
8 species halo-CH3 :.false.
DOC and CH3I(g)    :1.12e6, 6.55e-4
IEOF

		setenv OUTFILE $EMISPATH/emis2d.$G_DAY.$GRID.${CASE}+ss.camx
		rm -rf $OUTFILE

$camxscripts/seasalt/mrgspc.lnx << EOF
Input File         |$EMISPATH/emis2d.$G_DAY.$GRID.$CASE.camx
Input File         |${tempdir}/seasalt.camx
Input File         |END
Output File        |$OUTFILE
EOF
		rm -f ${tempdir}/seasalt.camx
#		rm -f $EMISPATH/emis2d.$G_DAY.$GRID.$CASE.camx
	endif ## 2D seasalt merge

	## Run the elevated point cmaq inline to camx conversion
	if ($CTM_PT3DEMIS == 'Y') then ## elevated point file preparation
		## Specify the names of the inline sectors to convert from the source apportionment as appears in the sectorlist 
		set sectors = (ptegu ptnonipm pt_oilgas othpt) 

		foreach SECTOR (${point_sectors_convert})

			# Find the representative date based on the date approach from the sectorlist
			set dateappr = `grep ^\"$SECTOR\" $SECTORLIST | cut -d, -f4 | sed 's/"//g'`

			set n = 1
			while ($n <= 9)
				set column = $dateheader[$n]

				if ($dateappr == $column) then
					set repday = $iday[$n]
				endif

				@ n++
			end
		
			# Get the sector-specific case	
			set scase = `grep ^\"$SECTOR\" $SECTORLIST | cut -d, -f2 | sed 's/"//g'`

			set INPATH = $INSTALL_DIR/$scase/smoke_out/${scase}/$GRIDPT/${SPEC}
			set inln = $INPATH/$SECTOR/inln_mole_${SECTOR}_${repday}_${GRIDPT}_${SPEC}_${scase}.ncf
			setenv STKGROUP $INPATH/$SECTOR/stack_groups_${SECTOR}_${GRIDPT}_${scase}.ncf

			#convert to camx point format and merge into a single point source file and then run qaemis on camx input file
			ls $inln $STKGROUP

			echo $inln >> $casescripts/$SCRIPTLOG

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

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

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

			## Map the elevated species
			setenv INFILE  ${tempdir}/temp_${SLURM_JOB_ID}.ncf
			setenv OUTFILE ${tempdir}/tmp_emis_${SLURM_JOB_ID}
			setenv MAPTBL  $SPECIES_MAPPING
			$execroot/spcmap

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

			setenv CMAQEMIS ${tempdir}/tmp_emis_${SLURM_JOB_ID}
			set OUTPATH = ${tempdir}
			set OUTPATH = ${EMIS_ROOT}/${CASE}/${GRID}/camx/sector/$SECTOR
			set OUTFILE = ptsr.${SECTOR}.${G_DAY}.${GRID}.bin
			
			mkdir -p $OUTPATH
			rm -f $OUTPATH/$OUTFILE

## Run cmaq inline to camx converter for the inline sector day
$execroot/cmaq2uam-21oct2013 << EOF
File Type          |IPNT
OutFile1 (IC,BC,AR)|
OutFile2 (TopC,PT) |$OUTPATH/$OUTFILE
Output Timezone    |$OUTPUT_TIMEZONE
EOF

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

				## QA the elevated sector
				rm -f ${tempdir}/qaemis.${SECTOR}.${SLURM_JOB_ID}.in
cat << ieof > ${tempdir}/qaemis.$SECTOR.${SLURM_JOB_ID}.in
$OUTPATH/$OUTFILE
$QADIR/$SECTOR.$G_DAY.$GRID.$CASE.camx.tot.txt
$QADIR/$SECTOR.$G_DAY.$GRID.$CASE.camx.hrs.txt
$QADIR/$SECTOR.$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

		end ## end loop for conversion of this sector

	endif  ## end point source file processing

	## Run the point sector UAM merge
	if ($CTM_PTSMRG == 'Y') then
		set OUTPATH = ${EMIS_ROOT}/${CASE}/${GRID}/camx
		set OUTFILE = mrgpt.$G_DAY.$GRID.$CASE.camx
		set INPATH = $OUTPATH/sector

		rm -f $OUTPATH/$OUTFILE ${tempdir}/ptsmrg.${SLURM_JOB_ID}.txt

		# Link in the pre-converted CAMx fire sector emissions
		set numsect = `echo $firesectors | wc | awk '{print $2}'`
		set n = 1
		while ($n <= $numsect)
			set firesector = $firesectors[$n]
			set fire_path = $firepaths[$n]
			if (! -d ${EMIS_ROOT}/${CASE}/${GRID}/camx/sector/${firesector}) then
				mkdir -p ${EMIS_ROOT}/${CASE}/${GRID}/camx/sector/${firesector}
			endif
			rm -f ${EMIS_ROOT}/${CASE}/${GRID}/camx/sector/${firesector}/ptsr.${firesector}.${G_DAY}.${GRID}.bin
			ln -s $fire_path/ptsr.${firesector}.${G_DAY}.${GRID}.bin ${EMIS_ROOT}/${CASE}/${GRID}/camx/sector/${firesector}/ptsr.${firesector}.${G_DAY}.${GRID}.bin
			@ n++
		end

		echo "jdate    |$YYDDD" >> ${tempdir}/ptsmrg.${SLURM_JOB_ID}.txt
		echo "emissions|$OUTPATH/$OUTFILE" >> ${tempdir}/ptsmrg.${SLURM_JOB_ID}.txt

		# Total number of sectors including all subsectors
		set numsect = `echo ${mrg_sectors} | wc | awk '{print $2}'`
		
		echo "number pt| $numsect" >> ${tempdir}/ptsmrg.${SLURM_JOB_ID}.txt

		set n = 1
		while ($n <= $numsect)
			set SECTOR = $mrg_sectors[$n]
			echo "sat ptsr |${INPATH}/${SECTOR}/ptsr.${SECTOR}.${G_DAY}.${GRID}.bin" >> ${tempdir}/ptsmrg.${SLURM_JOB_ID}.txt
			echo "group num| $groupnums[$n]" >> ${tempdir}/ptsmrg.${SLURM_JOB_ID}.txt

			@ n++
		end

		## Run point source merge for converted inline data for this day
		$execroot/ptsmrg < ${tempdir}/ptsmrg.${SLURM_JOB_ID}.txt

		rm -rf ${tempdir}/qaemis.elev.${SLURM_JOB_ID}.in
cat << ieof > ${tempdir}/qaemis.elev.${SLURM_JOB_ID}.in
$OUTPATH/$OUTFILE
$QADIR/mrgpt.$G_DAY.$GRID.$CASE.camx.tot.txt
$QADIR/mrgpt.$G_DAY.$GRID.$CASE.camx.hrs.txt
$QADIR/mrgpt.$G_DAY.$GRID.$CASE.camx.pave.bin
ieof

		## Run the elevated emissions QA program
		$execroot/qaelev < ${tempdir}/qaemis.elev.${SLURM_JOB_ID}.in
		rm -f ${tempdir}/qaemis.elev.${SLURM_JOB_ID}.in
		rm -f ${tempdir}/ptsr.*.${SLURM_JOB_ID}.bin 

	endif  ## end point source merge 

	@ today = $today + 1

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

end #loop back to next day

exit

