#!/bin/sh
set -x
#
# processing of precip for input to the RCDAS assimilation
#
# input:
#       CMORPH binary 30-minute precip data
#       cpc global precip  binary daily precip data 12Z-12Z
#       cpc conus precip
#
# output:
#       Hourly CMORPH grib files
#       Hourly US_MEX grib precip
#
# failure mode:
#      no cmorph -- daily values are spread out over 24 hours
#      no us_mex -- use cmorph data
#
# programs:
#      rcdas_gribify_cmorph converts two 30 minute CMORPH fields into 1 hour ave grib file
#      rcdas_gribify_conus_gauge convert cpc conus precip into grib
#      rcdas_gribify_us_mex convert hourly binary data into grib
#      rcdas_gribify_us_mex2 convert binary global data into grib
#
#
# 2/2007: updated script from Marco Carrera by Wesley Ebisuzaki
#         updated documentation
#         very minor bug fix
# 4/2007: fix copy to IBM section
# 9/2009: compute farm mods
# 1/2012: update for missing data
#
# variables: date, day and day1


export XLFRTEOPTS="unit_vars=yes"

fatal() {
  echo "fatal error $*"
  export err=8
  sh $DATA/err_chk
}
# clean_unit .. removes old fortran unit number connections
clean_unit() {
  touch fort.1
  XLFUNIT_1=
  rm fort.*
  unset `set | grep '^XLFUNIT_' | sed 's/=.*//'`
}

# get files and save into $DATA

# GET cpc daily analyses from compute farm

eval in="$DCOM_IN/$DATE/wgrbbul/cpc_rcdas/$cpc_conus_in"
# eval in="$DCOM_IN/$DATE/wgrbbul/cpc_narr/$cpc_conus_in"

#SH eval in="$cpc_conus_in"
cp $in $DATA/gauge_precip.${CDATP1}.tmp

# convert to grib
export pgm=rcdas_gribify_conus_gauge
. prep_step
startmsg
$RCDAS_EXEC/rcdas_gribify_conus_gauge $TMPDIR/gauge_precip.${CDATP1}.tmp \
   $DATA/gauge_precip.${CDATP1}.grb ${CDATP1}12
export err=$?; sh $DATA/err_chk

# 1/2012 WNE: missing data results in no grib file, make grib file with negative numbers
if [ ! -f $DATA/gauge_precip.${CDATP1}.grb  -o ! -s $DATA/gauge_precip.${CDATP1}.grb ] ; then
   echo "CPC US gauge data missing, not used, notify CPC"
   echo "CPC US gauge data missing, not used, notify CPC" >>$LOG
   $RCDAS_EXEC/rcdas_gribify_conus_gauge $RCDAS_FIX/rcdas_missing_us_gauge $DATA/gauge_precip.${CDATP1}.grb ${CDATP1}12
fi

# convert to LDAS grid
export grid='255 0 464 224 25063 -124938 128 52938 -67063 125 125 64'
$UTIL/copygb -g"$grid" -x $DATA/gauge_precip.${CDATP1}.grb $DATA/gauge_precip.${CDATP1}.grb.ldas

# convert to binary with f77 header
$wgrib -bin -d 1 $DATA/gauge_precip.${CDATP1}.grb.ldas -o $DATA/gauge_precip.${CDATP1}


# get stage ii/iv data

for hh in 13 14 15 16 17 18 19 20 21 22 23
do
   gunzip -v -c $COMIN_HOURLY/ST2gg${CDATE}$hh.Grb.Z > $DATA/ST2gg${CDATE}$hh.Grb
done
for hh in 00 01 02 03 04 05 06 07 08 09 10 11 12
do
   gunzip -v -c $COMIN_HOURLY_P1/ST2gg${CDATP1}$hh.Grb.Z > $DATA/ST2gg${CDATP1}$hh.Grb
done

cp $COMIN_NDAS_P1/ndas.t00z.awip3d03.tm12 $DATA/NDAS.${CDATE}12
cp $COMIN_NDAS_P1/ndas.t00z.awip3d03.tm09 $DATA/NDAS.${CDATE}15
cp $COMIN_NDAS_P1/ndas.t00z.awip3d03.tm06 $DATA/NDAS.${CDATE}18
cp $COMIN_NDAS_P1/ndas.t00z.awip3d03.tm03 $DATA/NDAS.${CDATE}21
cp $COMIN_NDAS_P1/ndas.t12z.awip3d03.tm12 $DATA/NDAS.${CDATP1}00
cp $COMIN_NDAS_P1/ndas.t12z.awip3d03.tm09 $DATA/NDAS.${CDATP1}03
cp $COMIN_NDAS_P1/ndas.t12z.awip3d03.tm06 $DATA/NDAS.${CDATP1}06
cp $COMIN_NDAS_P1/ndas.t12z.awip3d03.tm03 $DATA/NDAS.${CDATP1}09

# make NDAS files smaller
for f in $DATA/NDAS.${CDATE}12 $DATA/NDAS.${CDATE}15 $DATA/NDAS.${CDATE}18 $DATA/NDAS.${CDATE}21 \
       $DATA/NDAS.${CDATP1}00 $DATA/NDAS.${CDATP1}03 $DATA/NDAS.${CDATP1}06 $DATA/NDAS.${CDATP1}09
do
   $wgrib $f | grep ":APCP:" | $wgrib $f  -i -grib -s -o $f.tmp
   mv $f.tmp $f
done

# get CMORPH data

date0=${CDATE}12
enddate=${CDATP1}11

date=$date0
while [ $date -le $enddate ] ; do

   DATE=`echo ${date} | cut -c1-8`
   eval in="$DCOM_IN/$DATE/wgrbbul/cpc_rcdas/$cmorph_in"
#   eval in="$DCOM_IN/$DATE/wgrbbul/cpc_narr/$cmorph_in"

#SH   eval in="$cmorph_in"
   out=$DATA/CMORPH_025deg_${date}

   if [ ! -s $out ] ; then
      cp $in $out
   fi
   if [ ! -s $out ] ; then
      cp $in.Z $out.Z && gunzip $out.Z
   fi
   if [ ! -s $out ] ; then
      echo "missing file $date $in, $out"
      echo "missing file $date $in, $out" >>$pgmout
   fi
   date=`$ndate +1 $date`
done

# get us-mex precip from CPC global
# convert from global to us-mex like file

eval in="$DCOM_IN/$DATE/wgrbbul/cpc_rcdas/$cpc_gbl_in"
# eval in="$DCOM_IN/$DATE/wgrbbul/cpc_narr/$cpc_gbl_in"

#SH eval in="$cpc_gbl_in"
us_mex=$TMPDIR/us_mex$CDATP1
cp $in $us_mex.tmp

[ -f "$us_mex" ] && rm $us_mex

if [ ! -s $us_mex.tmp ] ; then
  echo "MISSING FILE US_MEX PRECIP: $us_mex, notify CPC"
  echo "MISSING FILE US_MEX PRECIP: $us_mex, notify CPC" >>$LOG
else

  # convert to grib
  export pgm=rcdas_gribify_us_mex2
  . prep_step
  $RCDAS_EXEC/rcdas_gribify_us_mex2 $us_mex.tmp $us_mex.tmp.grb ${CDATP1}12 12
  export err=$?; sh $DATA/err_chk

# SPA: added an error check for missing output file.  I don't know if the
#      above err_chk will will abort the run before my error recovery is done.
#      If the rcdas_gribify_us_mex2 fails, no/empty output file is ok.
#      please change as needed so job is not aborted

# 1/2012 WNE: check for missing grib file - will occur if binary file is all missing
  if [ ! -f $us_mex.tmp.grb -o ! -s $us_mex.tmp.grb ] ; then
     echo "MISSING OBS IN US_MEX PRECIP: $us_mex, notify CPC"
     echo "MISSING OBS IN US_MEX PRECIP: $us_mex, notify CPC" >>$LOG
  else

     # convert to new grid  ..  new_LatLon_GDS(pds,321,201,-140.0,10.0,-60.0,60.0,0.25,0.25);
     export grid='255 0 321 201 10000 -140000 128 60000 -60000 250 250 64'
     $UTIL/copygb -g"$grid" -x $us_mex.tmp.grb $us_mex.grb

     # convert to binary f77 headers
     $wgrib $us_mex.grb -bin -d 1 -o $us_mex

     # convert to binary
     echo "FOUND US_MEX PRECIP: $us_mex" >>$LOG
  fi
fi

#          PROCESS the data

#  make hourly conus precip

clean_unit

i=0
>STDIN
while [ $i -lt 24 ]
do
   date=`$ndate +$i $date0`
   if [ $(($i % 3)) -eq 0 ] ; then
#     grib file
      ln -s NDAS.$date fort.$((11 + $i))
   fi
   echo "$date" >>STDIN

   date=`$ndate +1 $date`
#  grib file
   ln -s ST2gg$date.Grb fort.$((35 + $i))
#  grib file
   ln -s $date.lsmforce_noaa fort.$((60+$i))
   i=$(($i + 1))
done
export pgm=rcdas_conus_precip
. prep_step

#SH clean_unit
export XLFUNIT_8=gauge_precip.${CDATP1}
# ln -s gauge_precip.${CDATP1} fort.8

# grib file
ln -s $RCDAS_FIX/rcdas_oceanmask.grb fort.9

startmsg
$RCDAS_EXEC/rcdas_conus_precip <STDIN
export err=$?; sh $DATA/err_chk

date=$date0
while [ $date -le $enddate ] ; do

#  convert the 30 fields (out) into hourly grib (grib) and hourly binary (hourly)
   export pgm=rcdas_gribify_cmorph
   . prep_step

   out=$DATA/CMORPH_025deg_${date}
   grib=$DATA/CMORPH_025deg_${date}.hourly.grb
   hourly=$DATA/cmorph$date.bin

   startmsg
   $RCDAS_EXEC/rcdas_gribify_cmorph $grib $hourly $date $out 
   # JY export err=$?; sh $DATA/err_chk
   export err=$?

# SPA: if the rcdas_gribify_cmorph fails, that is ok
#  the following code can handle missing $grib file
#  Please make any change needed so the code continues if rcdas_gribify_cmorph fails

#  make sure no empty files
   [ ! -s $grib ] && rm $grib

   date=`$ndate +1 $date`
done

#
# make "files_to_process" input data file
#


pgm=rcdas_disag_us_mex
. prep_step

date=$date0
f=files_to_process
[ -f $f ] && rm $f

while [ $date -le $enddate ] ; do
   hourly=$DATA/cmorph$date.bin
   echo "$hourly" >> $f
   echo "$date" >> $f
   date=`$ndate +1 $date`
done
echo "$us_mex" >>$f

# 
# run program to do disaggregation
#

clean_unit

export XLFUNIT_1=files_to_process

# ln -sf files_to_process fort.1

startmsg
$RCDAS_EXEC/rcdas_disag_us_mex
export err=$?; sh $DATA/err_chk

# convert binary files into grib

date=$date0
while [ $date -le $enddate ] ; do

   pgm=rcdas_gribify_us_mex
   . prep_step

   data=${date}.us_mex
   grib=$DATA/${date}.us_mex.grb

   startmsg 
   $RCDAS_EXEC/rcdas_gribify_us_mex $data $grib $date >/dev/null
   export err=$?; sh $DATA/err_chk
   date=`$ndate +1 $date`
done

echo "cleanup"
[ -f $us_mex ] && rm $us_mex
date=$date0
while [ $date -le $enddate ] ; do
   rm $DATA/cmorph$date.bin 
   rm ${date}.us_mex
   date=`$ndate +1 $date`
done


# copy to $COMOUT
if [ "$SENDCOM" = 'YES' ] ; then
   date=$date0
   while [ $date -le $enddate ] ; do
      hr=`echo $date | cut -c9-10`
      datep1=`$ndate +1 $date`
      if [ $hr -ge 12 ] ; then
         cp $DATA/${date}.us_mex.grb $COMOUT/
         cp $DATA/CMORPH_025deg_${date}.hourly.grb $COMOUT/
         if [ $hr -eq 23 ] ; then
            cp $DATA/${datep1}.lsmforce_noaa $COMOUT_P1/
         else 
            cp $DATA/${datep1}.lsmforce_noaa $COMOUT/
         fi
      else
         cp $DATA/${date}.us_mex.grb $COMOUT_P1/
         cp $DATA/CMORPH_025deg_${date}.hourly.grb $COMOUT_P1/
         cp $DATA/${datep1}.lsmforce_noaa $COMOUT_P1/
      fi        
      date=`$ndate +1 $date`
  done
  touch $COMOUT_P1/${model}.t09z.status_precip
fi

echo "finished"