#!/bin/ksh
######################################################################
#  UNIX Script Documentation Block
#                      .
# Script name:         ex_aqmpostconc_aer.sh
# Script description:  Run CMAQ Mie extinction post processing 
#
# Author:  Marina Tsidulko and Pius Lee  Org: NP22  Date: 2006-01-31
#
# Abstract: This script runs CMAQ post processing
#
# Script history log:
# 2004-03-31    Pius Lee, notcdf and upgrades 
# 2010-02-01    Jianping Huang , using cmaq2grib 
# 2011-03-21    Jianping Huang , using cmaq2grib_v1 
######################################################################
set -xa
msg="JOB $job HAS BEGUN"
postmsg "$jlogfile" "$msg"

cd $DATA

export DBNALERT_TYPE=${DBNALERT_TYPE:-GRIB_HIGH}
export XLFRTEOPTS="unit_vars=yes"

#########################################################
# Part I: Convert Machine binary format to Grib format
#########################################################

export CHEM3D=BIN:${COMIN}/aqm.${cycle}.conc.bin
export METCRO3D=BIN:${COMIN}/aqm.${cycle}.metcro3d.bin
export METCRO2D=BIN:${COMIN}/aqm.${cycle}.metcro2d.bin


case $cyc in
 00) export n_durhr=6;;
 06) export n_durhr=49;;
 12) export n_durhr=49;;
 18) export n_durhr=6;;
esac

. prep_step
export notcdf=Y
export NOTCDF_FATAL=F
export RESEARCH=N
export mysdate=-99
export mystime=-99
export mynsteps=-99
export spc=N    ##T,F,Y, or N
export display=N
export tol=0.
export info=N


  ic=1
  while [ $ic -lt 1000 ]
  do
    if [ -s $COMIN/aqm.${cycle}.conc.bin ]
    then
     ln -sf $COMIN/aqm.${cycle}.conc.bin $DATA/aqm.${cycle}.conc.bin
    break
    else
        let "ic=ic+1"
        sleep 10
    fi
    if [ $ic -ge 180 ]
    then
    err_exit "COULD NOT LOCATE:$COMIN/aqm.${cycle}.conc.bin"
    fi
  done

cat >cmaq2grib.ini <<EOF5
&control
varlist='O3','NO2','NO','NO3','N2O5','HNO3','HONO','PNA','CO','FORM','ALD2','PAN','NTR',
'XO2N','SO2','ASO4I','ASO4J','ANH4I','ANH4J','ANO3I','ANO3J','AORGAI','AORGAJ','AORGPAI',
'AORGPAJ','AORGBI','AORGBJ','AECI','AECJ','A25I','A25J','NUMATKN','NUMACC',
'SRFACC','AH2OI','AH2OJ'
metlist='    '
outfile='aqm.${cycle}.pm'
nlayers=22
id_gribdomain=140
ave1hr=.true.
/
EOF5

startmsg
$EXECaqm/aqm_cmaq2grib_v1 >> $pgmout 2>errfile
export err=$?;err_chk

if [ "$SENDCOM" = 'YES' ]
    then
    cp $DATA/aqm.${cycle}.pm?? $COMOUT/
fi
########################################################
msg='ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"
################## END OF SCRIPT #######################
exit

