##export COMIN_ATMOS_GRIB_0p25=/lfs/h2/emc/gfstemp/emc.global/comroot/retrov17_01_realtime/gfs.20260310/00/products/atmos/grib2/0p25
##export COMIN_ATMOS_GRIB_0p25=/lfs/h2/emc/ptmp/jessica.meixner/comroot/prod01/gfs.20260305/06/original.products/atmos/grib2/0p25
##export COMIN_ATMOS_GRIB_0p25=/lfs/h2/emc/ptmp/Chris.Hill/g2-test_iss4614/cp_comroot/prod01/gfs.20260305/06/original.products/atmos/grib2/0p25
export COMIN_ATMOS_GRIB_0p25=/lfs/h2/emc/ptmp/Chris.Hill/gfs_rvw/cp_comroot/retrov17_01_realtime/gfs.20260322/00/products/atmos/grib2/0p25
export cyc=00
export WGRIB2=/apps/ops/prod/libs/intel/19.1.3.304/wgrib2/2.0.8/bin/wgrib2
export PARMgfs=/lfs/h2/emc/vpppg/noscrub/Chris.Hill/GIT/global-workflow/parm
export GRB2INDEX=/apps/ops/prod/libs/intel/19.1.3.304/grib_util/1.2.3/bin/grb2index
export TOCGRIB2=/apps/ops/prod/libs/intel/19.1.3.304/grib_util/1.2.3/bin/tocgrib2
# Set type of Interpolation for WGRIB2
export opt1=' -set_grib_type same -new_grid_winds earth '
export opt1uv=' -set_grib_type same -new_grid_winds grid '
export opt21=' -new_grid_interpolation bilinear -if '
export opt22=":(CSNOW|CRAIN|CFRZR|CICEP|ICSEV):"
export opt23=' -new_grid_interpolation neighbor -fi '
export opt24=' -set_bitmap 1 -set_grib_max_bits 16 -if '
export opt25=":(APCP|ACPCP|PRATE|CPRAT):"
export opt26=' -set_grib_max_bits 25 -fi -if '
export opt27=":(APCP|ACPCP|PRATE|CPRAT|DZDT):"
export opt28=' -new_grid_interpolation budget -fi '

###LST="000 003 006 009 012 015 018 021 024 027 030 033 036 039 042 045 048 051 054 057 060 063 066 069 072 075 078 081 084 087 090 093 096 099 102 105 108 111 114 117 120"
##LST="000 003 006 009 012 015 018 021 024 027 030 033 036 039 042 045 048 051 054 057 060"
##LST="000 012 024 036 048 060 072 084 096 108 120 132 144 156 168 180"
LST="024 072"
for fcsthr in $LST
do
date >> timing
###############################################################
#    Process GFS GRIB AWIP PRODUCTS IN GRIB2                  #
###############################################################

cp -p "${COMIN_ATMOS_GRIB_0p25}/gfs.t${cyc}z.pres_a.0p25.f${fcsthr}.grib2" "tmpfile2${fcsthr}"
cp -p "${COMIN_ATMOS_GRIB_0p25}/gfs.t${cyc}z.pres_b.0p25.f${fcsthr}.grib2" "tmpfile2b${fcsthr}"
cat "tmpfile2${fcsthr}" "tmpfile2b${fcsthr}" > "tmpfile${fcsthr}"
# shellcheck disable=SC2312
${WGRIB2} "tmpfile${fcsthr}" | grep -F -f "${PARMgfs}/product/gfs_awips_parmlist_g2" |
    ${WGRIB2} -i -grib masterfile "tmpfile${fcsthr}" && true
export err=$?
if [[ ${err} -ne 0 ]]; then
    echo "masterfile does not exist."
fi

${WGRIB2} masterfile -match ":PWAT:entire atmosphere" -grib gfs_pwat.grb
# shellcheck disable=SC2312
${WGRIB2} masterfile | grep -v ":PWAT:entire atmosphere" | ${WGRIB2} -i -grib temp_gfs masterfile
##################################################################
#  Process to change PWAT from level 200 to 10 (Entire Atmosphere)
#  in production defintion template (PDT) 4.0
##################################################################
${WGRIB2} gfs_pwat.grb -set_byte 4 23 10 -grib gfs_pwat_levels_10.grb && true
export err=$?
if [[ ${err} -ne 0 ]]; then
    echo "Failed to redefine PWAT for the entire atmosphere!"
fi

cat temp_gfs gfs_pwat_levels_10.grb > tmp_masterfile

for GRID in conus ak prico pac 003; do
    case ${GRID} in
        conus)
            gridconus="lambert:265.0:25.0:25.0 226.541:369:20318.0 12.19:257:20318.0"
            # shellcheck disable=SC2086,SC2248
            ${WGRIB2} tmp_masterfile ${opt1uv} ${opt21} ${opt22} ${opt23} ${opt24} ${opt25} ${opt26} \
                ${opt27} ${opt28} -new_grid ${gridconus} "awps_file_f${fcsthr}_${GRID}"
            ;;
        ak)
            gridak="nps:210.0:60.0 170.0:277:22500 35.0:225:22500"
            # shellcheck disable=SC2086,SC2248
            ${WGRIB2} tmp_masterfile ${opt1uv} ${opt21} ${opt22} ${opt23} ${opt24} ${opt25} ${opt26} \
                ${opt27} ${opt28} -new_grid ${gridak} "awps_file_f${fcsthr}_${GRID}"
            ;;
        prico)
            gridprico="latlon 271.75:275:0.25 50.75:205:-0.25"
            # shellcheck disable=SC2086,SC2248
            ${WGRIB2} tmp_masterfile ${opt1} ${opt21} ${opt22} ${opt23} ${opt24} ${opt25} ${opt26} \
                ${opt27} ${opt28} -new_grid ${gridprico} "awps_file_f${fcsthr}_${GRID}"
            ;;
        pac)
            gridpac="mercator:20.0 110.0:837:20000:270.0 -45.0:725:20000:65.7345"
            # shellcheck disable=SC2086,SC2248
            ${WGRIB2} tmp_masterfile ${opt1} ${opt21} ${opt22} ${opt23} ${opt24} ${opt25} ${opt26} \
                ${opt27} ${opt28} -new_grid ${gridpac} "awps_file_f${fcsthr}_${GRID}"
            ;;
        003)
            ######################################################################
            #    Process GFS GRIB AWIP 1.0 DEGREE (GRID 003)  PRODUCTS IN GRIB2  #
            ######################################################################
            grid003="latlon 0:360:1.0 90:181:-1.0"
            # shellcheck disable=SC2086,SC2248
            ${WGRIB2} tmp_masterfile ${opt1} ${opt21} ${opt22} ${opt23} ${opt24} ${opt25} ${opt26} \
                ${opt27} ${opt28} -new_grid ${grid003} "awps_file_f${fcsthr}_${GRID}"
            ;;
        *)
            export err=2
            echo "Unknown output grid ${GRID}"
            ;;
    esac
    . /lfs/h2/emc/vpppg/noscrub/Chris.Hill/GIT/global-workflow/ush/product_functions.sh
    trim_rh "awps_file_f${fcsthr}_${GRID}"
    scale_dec "awps_file_f${fcsthr}_${GRID}"
    ${GRB2INDEX} "awps_file_f${fcsthr}_${GRID}" "awps_file_fi${fcsthr}_${GRID}"

    ###########################################################################
    # Checking fields in awps_file_f${fcsthr}_${GRID} file
    # before TOCGRIB2 adding WMO headers for AWIPS products.
    #
    # NOTE: numparm is the total of fields in grib2_awpgfs_20km_conusf000 file
    ###########################################################################
    numparm=247
    # shellcheck disable=SC2312
    numrec=$(${WGRIB2} "awps_file_f${fcsthr}_${GRID}" | wc -l)

    if [[ ${numrec} -lt ${numparm} ]]; then
        export err=1
        msg="awps_file_f${fcsthr}_${GRID} file is missing fields for AWIPS !"
        echo "${msg}"
    fi

    # Processing AWIPS GRIB2 grids with WMO headers

    pgm=tocgrib2
    export pgm
#   prep_step
#   startmsg

    if [[ ${GRID} = "003" && $((10#${fcsthr} % 6)) == 0 ]]; then
        export FORT11="awps_file_f${fcsthr}_${GRID}"
        export FORT31="awps_file_fi${fcsthr}_${GRID}"
        export FORT51="grib2.awpgfs${fcsthr}.${GRID}"

        cp -p "${PARMgfs}/wmo/grib2_awpgfs${fcsthr}.${GRID}" "parm_list"

        ${TOCGRIB2} < "parm_list" >> pgmout.txt 2> errfile && true
        export err=$?
        if [[ ${err} -ne 0 ]]; then
            echo "Failed to generate the awips Grib2 file!"
	fi
##        echo "Complex2 compression/packing for grib2.awpgfs${fcsthr}.${GRID}"
##        cp -p "grib2.awpgfs${fcsthr}.${GRID}" "tmp_grib2_${fcsthr}_${GRID}"
##        ${WGRIB2} "tmp_grib2_${fcsthr}_${GRID}" -set_grib_type complex2 \
##                   -grib_out "grib2.awpgfs${fcsthr}.${GRID}"
##        export err=$?
##        if [[ ${err} -ne 0 ]]; then
##            echo "Failed to compress/pack the awips grib2 file"
##        fi 

    elif [[ ${GRID} != "003" ]]; then
        export FORT11="awps_file_f${fcsthr}_${GRID}"
        export FORT31="awps_file_fi${fcsthr}_${GRID}"
        export FORT51="grib2.awpgfs_20km_${GRID}_f${fcsthr}"

        cp -p "${PARMgfs}/wmo/grib2_awpgfs_20km_${GRID}f${fcsthr}" "parm_list"

        ${TOCGRIB2} < "parm_list" >> pgmout.txt 2> errfile && true
        export err=$?
        if [[ ${err} -ne 0 ]]; then
            echo "Failed to write the AWIPS grib2 file"
        fi
##        echo "Complex2 compression/packing for grib2.awpgfs_20km_${GRID}_f${fcsthr}"
##        cp -p "grib2.awpgfs_20km_${GRID}_f${fcsthr}" "tmp_grib2_${GRID}_f${fcsthr}"
##        ${WGRIB2} "tmp_grib2_${GRID}_f${fcsthr}" -set_grib_type complex2 \
##                   -grib_out "grib2.awpgfs_20km_${GRID}_f${fcsthr}" 
##        export err=$?
##        if [[ ${err} -ne 0 ]]; then
##            echo "Failed to compress/pack the AWIPS grib2 file!"
##        fi 

    fi
    echo "Awips Processing ${fcsthr} hour completed normally"

done
rm temp_gfs tmpfile${fcsthr} tmpfile2${fcsthr} tmpfile2b${fcsthr} tmp_masterfile masterfile parm_list gfs_pwat.grb gfs_pwat_levels_10.grb
date >> timing
sleep 10
done
############## END OF SCRIPT #######################
