#!/bin/sh
# this script creates forecasts of icing, turbulence and cumulonimbus clouds
# for WAFC aviation project. The GRIB files will be converted to GEMPAK to be
# pushed to AWC
# LOGFILE:
# G. Trojan/EMC 21/05/2007        created
# G. Trojan/EMC 21/05/2007        added 8 WAF grids

set -x

cd ${DATA}

# RUN SETUP SHELL SCRIPT.
sh $utilscript/setup.sh

# COPY OVER IMPORTANT FILES AND TABLES.
cp $FIXwafavn/awc_wafavn.cfg waf.cfg
cp $FIXwafavn/awc_wmogrib140.tbl wmogrib140.tbl 

# set up file names
fcsthr=f${1}
gfs_file=gfs.${cycle}.master.grb${fcsthr}
tmp_file=wafavn.${cycle}.grb4.${fcsthr}
# WAF grids, grid 45 for AWC
for g in 37 38 39 40 41 42 43 44 45
do
    waf_grib_file[$g]=wafavn.${cycle}.grb${g}.${fcsthr}
done
waf_gempak_file=wafavn.${cycle}.grb45.${fcsthr}.gem

# create GRIB file 
$EXECwafavn/awc_wafavn -c waf.cfg -i ${COMIN}/${gfs_file} -o ${tmp_file} icng tcld cat cb
export err=$?; errchk

# create 8 WAF grids and one for input to nagrib_nc
for g in 37 38 39 40 41 42 43 44 45
do
    ${utilexec}/copygb -x -g $g -i 2 ${tmp_file} ${waf_grib_file[$g]}
    export err=$?; errchk
done

# convert GRIB file 45 to GEMPAK
nagrib_nc << EOF
gbfile = ${waf_grib_file[45]}
gdoutf = ${waf_gempak_file}
cpyfil = gds
garea = grid
run

exit
EOF
export err=$?; errchk

# move file(s) to ddestination and alert
if [ $SENDCOM = "YES" ] ; then
    mv ${waf_gempak_file} ${COMOUT}/${waf_gempak_file}_${PDY}
    for g in 37 38 39 40 41 42 43 44 45
    do
        mv ${waf_grib_file[$g]} ${COMOUT}/${waf_grib_file[$g]}_${PDY}
        if [ $SENDDBN = "YES" ] ; then
            $DBNROOT/bin/dbn_alert MODEL GFS_WAFS $job $COMOUT/${waf_grib_file[$g]}_${PDY}
        fi
    done
    if [ $SENDDBN = "YES" ] ; then
        $DBNROOT/bin/dbn_alert MODEL GFS_WAFS_GEMPAK $job $COMOUT/${waf_gempak_file}_${PDY}
    fi
fi
