#!/bin/ksh
######################################################################
#  UNIX Script Documentation Block
#                      .
# Script name:         exaqm_fcst_5xwrf.sh
# Script description:  Run CMAQ model:Community Multiscale Air Quality
#		       chemical transport model.
#
# Author:  Marina Tsidulko and Pius Lee   Org: NP22  Date: 2004-03-31
#
# Abstract: This script runs the air quality forecast processing
#
# Script history log:
# 2003-07-03    Marina Tsidulko
# 2003-07-15    Julia Zhu, modified for production
# 2004-03-31    Pius Lee, netcdf independence, new emissions and bcon
#
#  #Input Environment Variables:
#    NPCOL_NPROW      	# domain decompositon
#    nprocs	      	# number of processors
#    GRIDDESC		# horizontal grid definitions
#    GRID_NAME		# grid name
#    AVG_CONC_SPCS	# Species for integral average conc
#    ACONC_BLEV_ELEV	# layer range for integral average conc
#    IOAPI_LOG_WRITE=F	# excessive WEITE3 logging
#			  default to F (off)
#    CTM_SYMPROC=N	# symmetric processing [ T | Y | F | N ]
#    CTM_CKSUM=Y	# cksum report [ T | Y | F | N ]
#    CTM_MAXSYNC	# max sync time step (sec) [300]
#    MPI_SM_POOL	# shared memory pool
#			  increase for many MPI_SEND headers
#    MP_EAGER_LIMIT	# MPI message passing buffer
#    MP_SINGLE_THREAD  	# tell mpi threading is not done [yes]
#    MP_STDOUTMODE	# order stnd out msgs [ordered]
#    MP_LABELIO		# MP Lable I/O [yes]
#    MP_SHARED_MEMORY=yes  # Share Memory
#    MP_ADAPTER_USE=shared # Share Adapter
#    MP_CPU_USE=multiple   # Multi Processors
#####################################################################

#export NPCOL_NPROW="9 7"
#export nprocs=64
#export NPCOL_NPROW="7 5"
#export nprocs=36
export GRIDDESC=$PARMaqm/aqm_griddesc05
export GRID_NAME=AQF_CONUS_5x
export AVG_CONC_SPCS="O3, NO2, NO, NO3, N2O5, HNO3, HONO, PNA, CO, FORM, ALD2, PAN, NTR, PAR, TOL, XYL, ETH, OLE, H2O2, ISOP, NH3"
export ACONC_BLEV_ELEV="1 14"
export IOAPI_LOG_WRITE=F
export CTM_MINSYNC=300
export CTM_SYMPROC=N
export CTM_CKSUM=Y
export CTM_CLD_DIAG=Y

export PBL_RICH=Y
export FILE_EDDY=N
export READEDDY=N
export NOTCDF_FATAL=F

export MPI_SM_POOL=16000
export MP_EAGER_LIMIT=65536
export MP_SINGLE_THREAD=yes
export MP_STDOUTMODE=ordered
export MP_LABELIO=yes
export MP_SHARED_MEMORY=yes
export MP_ADAPTER_USE=shared
export MP_CPU_USE=multiple
export CTM_WVEL=Y

#------------------------------------------------------
# now using notCDF v3.4
#------------------------------------------------------
export NETCDF_FFIOSPEC='cachea:75:3'

set -xa
msg="JOB $job HAS BEGUN"
postmsg "$jlogfile" "$msg"

cd $DATA

APPL=r6b
CFG=r6b
RST=rst
SEQ=0   
LAPPL=${APPL}_${SEQ}

case $cyc in
 00) restart_file1=$COMINm1/aqm.t18z.conc.bin
     restart_log1=$COMINm1/aqm.t18z.log
     restart_file2=$COMINm1/aqm.t12z.conc.bin
     restart_log2=$COMINm1/aqm.t12z.log
     restart_file3=$COMINm1/aqm.t06z.conc.bin
     restart_log3=$COMINm1/aqm.t06z.log
     restart_file4=$COMINm2/aqm.t12z.conc.bin
     restart_log4=$COMINm2/aqm.t12z.log
     restart_file5=$COMINm2/aqm.t06z.conc.bin
     restart_log5=$COMINm2/aqm.t06z.log
     NSTEPS=060000;;
 06) restart_file1=$COMIN/aqm.t00z.conc.bin
     restart_log1=$COMIN/aqm.t00z.log
     restart_file2=$COMINm1/aqm.t12z.conc.bin
     restart_log2=$COMINm1/aqm.t12z.log
     restart_file3=$COMINm1/aqm.t06z.conc.bin
     restart_log3=$COMINm1/aqm.t06z.log
     restart_file4=$COMINm2/aqm.t12z.conc.bin
     restart_log4=$COMINm2/aqm.t12z.log
     restart_file5=$COMINm2/aqm.t06z.conc.bin
     restart_log5=$COMINm2/aqm.t06z.log
     NSTEPS=480000;;
 12) restart_file1=$COMIN/aqm.t06z.conc.bin
     restart_log1=$COMIN/aqm.t06z.log
     restart_file2=$COMINm1/aqm.t12z.conc.bin
     restart_log2=$COMINm1/aqm.t12z.log
     restart_file3=$COMINm1/aqm.t06z.conc.bin
     restart_log3=$COMINm1/aqm.t06z.log
     restart_file4=$COMINm2/aqm.t12z.conc.bin
     restart_log4=$COMINm2/aqm.t12z.log
     restart_file5=$COMINm2/aqm.t12z.conc.bin
     restart_log5=$COMINm2/aqm.t12z.log
     NSTEPS=480000;;
 18) restart_file1=$COMIN/aqm.t12z.conc.bin
     restart_log1=$COMIN/aqm.t12z.log
     restart_file2=$COMIN/aqm.t06z.conc.bin
     restart_log2=$COMIN/aqm.t06z.log
     restart_file3=$COMINm1/aqm.t12z.conc.bin
     restart_log3=$COMINm1/aqm.t12z.log
     restart_file4=$COMINm1/aqm.t06z.conc.bin
     restart_log4=$COMINm1/aqm.t06z.log
     restart_file5=$COMINm1/aqm.t06z.conc.bin
     restart_log5=$COMINm1/aqm.t06z.log
     NSTEPS=060000;;
esac

if [ -s $restart_file1 ]
then
  restart_file=$restart_file1
  restart_log=$restart_log1
elif [ -s $restart_file2 ]
then
  restart_file=$restart_file2
  restart_log=$restart_log2
elif [ -s $restart_file3 ]
then
  restart_file=$restart_file3
  restart_log=$restart_log3
elif [ -s $restart_file4 ]
then
  restart_file=$restart_file4
  restart_log=$restart_log4
elif [ -s $restart_file5 ]
then
  restart_file=$restart_file5
  restart_log=$restart_log5
fi

if [ -s $restart_file ]
then
  export START=WARM
  export INITIAL_RUN=N
else
  export START=COLD
  export INITIAL_RUN=Y
fi

#------------------------------------------------------
# Convert PDY to day of year
#-----------------------------------------------------
SYYYY=`echo $PDY |cut -c1-4`
SMM=`echo $PDY |cut -c5-6`
SDD=`echo $PDY |cut -c7-8`
DAYOFYEAR=`$EXECaqm/aqm_date2day $SYYYY $SMM $SDD |cut -c2-4`
typeset -Z3 DAYOFYEAR
echo $DAYOFYEAR


STDATE=$SYYYY$DAYOFYEAR
STTIME=${cyc}0000

NS=`echo $NSTEPS |cut -c1-2`
TSTEP=010000

ln -sf $COMIN/aqm.${cycle}.grdcro2d.bin $DATA/GRIDCRO2D
ln -sf $COMIN/aqm.${cycle}.grddot2d.bin $DATA/GRIDDOT2D
ln -sf $COMIN/aqm.${cycle}.metbdy3d.bin $DATA/METBDY3D
ln -sf $COMIN/aqm.${cycle}.metcro2d.bin $DATA/METCRO2D
ln -sf $COMIN/aqm.${cycle}.metcro3d.bin $DATA/METCRO3D
ln -sf $COMIN/aqm.${cycle}.metdot3d.bin $DATA/METDOT3D
ln -sf $COMIN/aqm.${cycle}.chembdy3d.bin $DATA/CHEMBDY3D
ln -sf $COMIN/aqm.${cycle}.emission.bin $DATA/emission.${PDY}.bin

if [ $INITIAL_RUN = N ]
then
    ln -sf $restart_file   $DATA/CONC.${RST}.bin
fi

#------------------------------------------------------
# input files
#------------------------------------------------------

export EMIS_1=BIN:$DATA/emission.$PDY.bin

if [[ $INITIAL_RUN = "N" ]]; then
   export INIT_GASC_1=BIN:$DATA/CONC.${RST}.bin
else    
   export INIT_GASC_1=BIN:$FIXaqm/aqm_icon_a2c_profile_conus12.bin
fi

##RETRO#export BNDY_GASC_1=$FIXaqm/aqm_bcon_cb4_ae3_profile2_northeast
export BNDY_GASC_1=BIN:$FIXaqm/aqm_bc_constanteast_CONUS.bin
##export BNDY_GASC_1=BIN:$FIXaqm/5x_bc_constanteast_CONUS60.bin
##export BNDY_GASC_1=BIN:$DATA/CHEMBDY3D

export INIT_AERO_1=$INIT_GASC_1
export BNDY_AERO_1=$BNDY_GASC_1
export INIT_NONR_1=$INIT_GASC_1
export BNDY_NONR_1=$BNDY_GASC_1
export INIT_TRAC_1=$INIT_GASC_1
export BNDY_TRAC_1=$BNDY_GASC_1

export GRID_CRO_2D=BIN:$DATA/GRIDCRO2D
export GRID_DOT_2D=BIN:$DATA/GRIDDOT2D
export  MET_CRO_3D=BIN:$DATA/METCRO3D
export  LAYER_FILE=BIN:$DATA/METCRO3D
export  MET_CRO_2D=BIN:$DATA/METCRO2D
export  MET_DOT_3D=BIN:$DATA/METDOT3D
export  MET_BDY_3D=BIN:$DATA/METBDY3D
export  DEPV_TRAC_1=BIN:$DATA/METCRO2D

export XJ_DATA=$INPEMIDIR/aqm_jtable_${STDATE}

if [ ! -s $XJ_DATA ]
then
   err_exit "Missing the JTABLE file"
fi

#------------------------------------------------------
# output files
#------------------------------------------------------
export    CTM_CONC_1=BIN:$DATA/CONC.${APPL}
export      A_CONC_1=BIN:$DATA/ACONC.${APPL}
export CTM_DRY_DEP_1=BIN:$DATA/DRYDEP.${APPL}
export CTM_WET_DEP_1=BIN:$DATA/WETDEP.${APPL}
export CTM_WET_DEP_2=BIN:$DATA/CLDIAG.${APPL}
export     CTM_VIS_1=BIN:$DATA/VIS.${APPL}

#------------------------------------------------------
# for the run control 
#------------------------------------------------------
export   CTM_STDATE=$STDATE
export   CTM_STTIME=$STTIME
export   CTM_RUNLEN=$NSTEPS
export    CTM_TSTEP=$TSTEP
export CTM_PROGNAME=X.$CFG
export  APPLICATION=$LAPPL

msg="Starting CMAQ Forecast at F00 out to F$NS"
postmsg "$jlogfile" "$msg"

startmsg
poe  $EXECaqm/aqm_fcst_5xwrf -procs $nprocs >> $pgmout 2>errfile
export err=$?

if [ $err -ne 0 ]
then
   err_chk
else
   msg="$pgm completed normally"
   echo "$PDY" "done" >$COMOUT/aqm.${cycle}.log
   #------------------------------------------------------
   # copy output to /com
   #------------------------------------------------------
   if [ "$SENDCOM" = 'YES' ]
   then
      mv $DATA/CONC.${APPL} $COMOUT/aqm.${cycle}.conc.bin
      mv $DATA/ACONC.${APPL} $COMOUT/aqm.${cycle}.aconc.bin
      mv $DATA/DRYDEP.${APPL} $COMOUT/aqm.${cycle}.drydep.bin
      mv $DATA/WETDEP.${APPL} $COMOUT/aqm.${cycle}.wetdep.bin
      mv $DATA/CLDIAG.${APPL} $COMOUT/aqm.${cycle}.cldiag.bin
      mv $DATA/VIS.${APPL} $COMOUT/aqm.${cycle}.vis.bin
      cp /com/output/${envir}/today/aqm_*${cyc}.* $COMOUT/.
###   cp /ptmp/wx20pl/5x/tmpnwprd/aqm_cmaq_err $COMOUT/aqm_cmaq_log
   fi
   sh $utilscript/postmsg.sh "$jlogfile" "$msg"
set +x
##   rm -f $COMINm3/*.conc.bin $COMINm2/aqm.t12z.emission $COMINm2/*.chem* $COMINm2/aqm.t12z.metbdy3d $COMINm2/aqm.t12z.metcro3d $COMINm2/aqm.t12z.metdot3d $COMINm1/aqm.t12z.emission $COMINm1/*.chem* $COMINm1/aqm.t12z.metbdy3d $COMINm1/aqm.t12z.metcro3d $COMINm1/aqm.t12z.metdot3d $COMINm3/*dep*
#   rm -f $COMINm3/*sigma* $COMINm3/*soil* $COMINm3/*awpo* $COMINm3/*b3gts* $COMINm3/*cldiag* $COMINm3/*grd* $COMINm3/*met* $COMINm3/*mgts* 
## Need to keep the awpozon files for verification, changed by WANG 20060525 
   rm -f $COMINm3/*sigma* $COMINm3/*soil* $COMINm3/*awpozcat* $COMINm3/*b3gts* $COMINm3/*cldiag* $COMINm3/*grd* $COMINm3/*met* $COMINm3/*mgts* 
set -x
fi

echo Exiting $0

exit
