#####################################################################
echo "------------------------------------------------"
echo "JNOGAPS - NOGAPS postprocessing"
echo "------------------------------------------------"
echo "History: FEB 2008 - NOGAPS processing moved from ncosrv     "
echo "                    where it was being run by DBNet         "
echo "         JUL 2010 - Added 06 and 18 processing of analysis  "
#####################################################################
set -x
cd $DATA

##########################################
#
# START FLOW OF CONTROL
#
# 1) Get the Date from /com/date
# 2) Extract NOGAPS data from /dcom holding directory.
# 3) Form GRIB1 fields from input GRIB files.
# 4) Convert GRIB1 files to GRIB2.
#
#########################################

########################################
set -x
msg="HAS BEGUN!"
postmsg "$jlogfile" "$msg"
########################################

set +x
echo " "
echo "#########################################"
echo " Check to see if all NOGAPS data is      "
echo " available in /dcom                      "
echo "#########################################"
echo " "
set -x

INPUT_DIR=${dcom}/nogaps

# Process each forecast hour segment

# Process all the input data if the cycle is 00 or 12,
# otherwise just process the analysis.

if [ ${CYC} -eq 00 ] ||  [ ${CYC} -eq 12 ]
then
   forecast_hours="24 48 72 120"
   run_type=RL
else
   forecast_hours="00"
   run_type=OF
fi


for FH in $forecast_hours
do 
  
  EXPECTED_FILES=${FIXgrib}/nogaps${FH}h.tbl
  icnt=0

#  Build table based on current date

   date_cycle=${PDY}${CYC}
   sed -e "s/YYYYMMDDCC/$date_cycle/" $EXPECTED_FILES > ${DATA}/NOGAPS_${FH}
 

   while [ $icnt -eq 0 ]
   do

#     Check to see if all missing files are available
      
      ${USHutil}/check_for_file_list.pl ${DATA}/NOGAPS_${FH} 

      status=$?

      if [ $status -ne 0 ]
      then
         set +x
         echo " "
         echo "#########################################"
         echo " NOGAPS files are missing from           "
         echo " ${INPUT_DIR}                            "
         echo "#########################################"
         echo " "
         set -x
         sleep 900
         icnt=`expr $icnt + 1`
      else
         echo "We have all the files and are ready to process"
         break
      fi 
   done

   if [ $icnt -eq 2 ]
   then 

#     Write all missing files to temporary missing_files_${FH} file

      ${USHutil}/check_for_file_list.pl ${DATA}/NOGAPS_${FH} > ${DATA}/missing_files_${FH}

      set +x
      echo " "
      echo "#########################################"
      echo " Waited for forecast hours, all          "
      echo " files are not present in ${INPUT_DIR} for "
      echo " Cycle = ${CYC}; moving on               "
      echo "#########################################"
      echo " "
      set -x
   fi  
  

#  Determine the Forecast Hours to create
    
   case $FH
   in
   00)
     FH_list="000"
     ;;
   24)
     FH_list="000 003 006 009 012 015 018 021 024"
     ;;
   48) 
     FH_list="030 036 042 048"
     ;;
   72) 
     FH_list="054 060 066 072"
     ;;
   120) 
     FH_list="078 084 096 108 120 132 144"
     ;;
     esac

   for hour in ${FH_list} ; do

      
      # Kludge: only include 3, 6 or 12 hour precip params.  If this forecast hour
      # is an increment of 6 hrs, then use the 6 hour precips (make the 3hr ones
      # invisable before concatonating).  Otherwise use the 3 hour precips (so
      # make the 6hr files invisable).
      #
      # If the FH is 84 or over then use the 12 hour precip param.
      #

      if [ $hour -ge 84 -a `expr $hour % 12` -eq 0 ]; then
         use_12hr=1
         use_6hr=0
      elif [ `expr $hour % 6` -eq 0 ]; then
         use_12hr=0
         use_6hr=1
      else
         use_12hr=0
         use_6hr=0
      fi

      # Concatonate all param files into one, but test precip files to only use correct param.

      if [ `ls -1 ${INPUT_DIR}/US058GMET-GR1mdl.0018_0240_${hour}00F0${run_type}${PDY}${CYC}_* | wc -l`\
           -gt 0 ]
      then
         for file in ${INPUT_DIR}/US058GMET-GR1mdl.0018_0240_${hour}00F0${run_type}${PDY}${CYC}_*; do

            field=`echo $file | cut -d- -f3`
            case $field in

            000000conv_prcp_12)
                if [ $use_12hr -eq 1 ]; then
                   cat $file >> ${DATA}/nogaps
                fi;;
            000000ttl_prcp_12)
                if [ $use_12hr  -eq 1 ]; then
                   cat $file >> ${DATA}/nogaps
                fi;;
            000000conv_prcp_06)
                if [ $use_6hr -eq 1 ]; then
                   cat $file >> ${DATA}/nogaps
                fi;;
            000000ttl_prcp_06)
                if [ $use_6hr  -eq 1 ]; then
                   cat $file >> ${DATA}/nogaps
                fi;;
            000000conv_prcp_03)
                if [ $use_6hr  -eq 0 -a $use_12hr -eq 0 ]; then
                   cat $file >> ${DATA}/nogaps
                fi;;
            000000ttl_prcp_03)
                if [ $use_6hr  -eq 0 -a $use_12hr -eq 0 ]; then
                   cat $file >> ${DATA}/nogaps
                fi;;
            *)
               cat $file >> ${DATA}/nogaps
               ;;
            esac

         done

         # Convert to GRIB2

         ${EXECutil}/cnvgrib -g12 -p40 -nv ${DATA}/nogaps ${DATA}/nogaps.grib2

         export err=$?

         ${EXECutil}/wgrib2 -s ${DATA}/nogaps.grib2 > ${DATA}/nogaps.grib2.idx

         export err=`expr $err + $?`
         ${utilscript}/err_chk.sh
     
         # Move to COMOUT directory

         if test "$SENDCOM" = 'YES'
         then
            cp ${DATA}/nogaps  ${COMOUT}/nogaps_${PDY}${CYC}f${hour}
            export err=`expr $err + $?`
            cp ${DATA}/nogaps.grib2  ${COMOUT}/nogaps_${PDY}${CYC}f${hour}.grib2
            export err=`expr $err + $?`
            cp ${DATA}/nogaps.grib2.idx  ${COMOUT}/nogaps_${PDY}${CYC}f${hour}.grib2.idx
            export err=`expr $err + $?`
         fi
          
      
         rm ${DATA}/nogaps
         rm ${DATA}/nogaps.grib2
         rm ${DATA}/nogaps.grib2.idx

         ${utilscript}/err_chk.sh

         # Send DBNet alert
      
         if test "$SENDDBN" = 'YES'
         then
            $DBNROOT/bin/dbn_alert MODEL FNMOC_NOGAPS_GRIB nogaps_prdgen $COMOUT/nogaps_${PDY}${CYC}f${hour}
            $DBNROOT/bin/dbn_alert MODEL FNMOC_NOGAPS_GB2 nogaps_prdgen $COMOUT/nogaps_${PDY}${CYC}f${hour}.grib2
            $DBNROOT/bin/dbn_alert MODEL FNMOC_NOGAPS_GB2_WIDX nogaps_prdgen $COMOUT/nogaps_${PDY}${CYC}f${hour}.grib2.idx
         fi
     fi
  done  

#  Build missing_files file from temporary file

 grep dcom ${DATA}/missing_files_${FH} >> ${DATA}/missing_files  

done 

#  Check if any problems with processing, send email to SDM and NCCF ops 
#  if some of the expected files are missing.

if [ -s ${DATA}/missing_files ]
then
   echo " "  >> ${DATA}/sdm_email
   echo " The following FNMOC NOGAPS files from the ${CYC}Z cycle are missing from dcom:" >> ${DATA}/sdm_email
   echo " " >> ${DATA}/sdm_email
   cat ${DATA}/missing_files >> ${DATA}/sdm_email
   echo "" >> ${DATA}/sdm_email
   echo "All possible forecast output was generated by the jnogaps_prdgen_${CYC} job" >> ${DATA}/sdm_email

   cat ${DATA}/sdm_email | mail -s "Missing NOGAPS from dcom" -c "justin.cooke@noaa.gov" -c "ncep.nccfops@noaa.gov" "sdm@noaa.gov"
   # contents of email sent to SDM

   cat ${DATA}/sdm_email
fi 
    
${utilscript}/err_chk.sh



########################################################
# Execute the tropical cyclone tracker script if this is
# the 00 or 12Z cycle.

if [ ${CYC} -eq 00 ] ||  [ ${CYC} -eq 12 ]
then
   export cmodel=ngps
   ${USHutil}/extrkr.sh
fi

########################################################


#####################################################################
# GOOD RUN
set +x
echo "**************job NOGAPS PRDGEN COMPLETED NORMALLY ON THE IBM"
echo "**************job NOGAPS PRDGEN COMPLETED NORMALLY ON THE IBM"
echo "**************job NOGAPS PRDGEN COMPLETED NORMALLY ON THE IBM"
set -x
#####################################################################


msg="HAS COMPLETED NORMALLY!"
echo $msg
postmsg "$jlogfile" "$msg"

############## END OF SCRIPT #######################
