#!/bin/sh

echo "------------------------------------------------"
echo "Ensemble Postprocessing"
echo "------------------------------------------------"
echo "History: FEB 2000 - First implementation of this new script."
echo "AUTHOR: Larry Sager (based on exenspost.sh.sms)"
echo "May 00: Larry Sager add 12z processing for     "
echo "                    n3 n4 n5 p3 p4 p5          "
echo "Jun 00: Larry Sager nam 125 changes       "
echo "Jan 01: Larry Sager extend fine mesh to 84 hrs"
echo "Aug 03: Richard Wobus 4 cycles, fine mesh to 180 hrs"

set -x

msg="Starting exsemble.sh for memeber $memberlist"
postmsg "$jlogfile" "$msg"

#####################################
# Define Script/Exec Variables
#####################################

WGRIB=/nwprod/util/exec/wgrib

#####################################
# Set up hours to dump                        
#####################################

if test "$memberlist" = "p5"  
then
   RUN="gfs"
    hourlist1="00 06 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 156 162 168 174 180"
    hourlist2="186 192 198 204 210 216 222 228 234 240 246 252 258 264 270 276 282 288 294 300 306 312 318 324 330 336 342 348 354 360 366 372 378 384"
   model_prefix1="gfs"
   model_prefix2="gfs"
   COMIN2=$GFSCOMIN
fi

hourlist3="00 06 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 156 162 168 174 180"
hourlist4="186 192 198 204 210 216 222 228 234 240 246 252 258 264 270 276 282 288 294 300 306 312 318 324 330 336 342 348 354 360 366 372 378 384"

#####################################
# Concatenate the grib files for all     
#  hours into a single file.  Begin 
#  by deleting old files, if present.
#####################################


for file  in $memberlist
do 
   if [ -s pgrbf${file} ]
   then
       rm  pgrbf${file}
   fi
   if [ -s ensemble.${file} ]
   then
       rm  ensemble.${file}
   fi

  (for HOUR in $hourlist3
         
   do
       cat $COMIN/ens${file}.${cycle}.pgrbf${HOUR}.2  >>  pgrbf${file}    
   done 

  for HOUR in $hourlist4
         
   do
       cat $COMIN/ens${file}.${cycle}.pgrbf${HOUR}  >>  pgrbf${file}    
   done 

#####################################
# Run wgrib to get the inventory of     
#  this new concatenated file.       
#####################################
    
   $WGRIB -s pgrbf${file} > pgrbf${file}.inv
#####################################
# Use egrep to filter the fields         
#####################################

    egrep ":HGT:1000.mb|:HGT:850.mb|:HGT:500.mb|:HGT:300.mb|:HGT:250.mb|:HGT:200.mb|UGRD:10.m.a|:UGRD:850.mb|:UGRD:500.mb|:UGRD:250.mb|:VGRD:10.m.a|:VGRD:850.mb|:VGRD:500.mb|:VGRD:250.mb|:TMP:2.m.a|:TMP:850.mb|:RH:2.m.a|:RH:700.mb|:APCP|:PRMSL:MSL|:TMAX:2.m.a|:TMIN:2.m.a" \
    pgrbf${file}.inv  | $WGRIB pgrbf${file} -s -grib -i -o ensemble.${file}) &

done        

wait

#####################################
# Write the files out to permanent         
#  diskspace
#####################################

for file in $memberlist
do 
    NAME=${file}
    if  [ ${file} = "c0" ]
    then
       NAME="cnt"
    fi

    if test "$SENDCOM" = "YES"
    then
       cp ensemble.${file} $COMOUT/ensemble.${NAME}.${PDY}.${cyc}

       if test "$SENDDBN" = "YES"
       then
          MEM=`echo $NAME | tr [a-z] [A-Z]`
          $DBNROOT/bin/dbn_alert MODEL ENS_COL_$MEM $job $COMOUT/ensemble.${NAME}.${PDY}.${cyc}
       fi
    fi

done


if [ "$memberlist" = "p5" ] ; then

#####################################
# Repeat this process for the special   
# gfs and mrf cases.  
# Concatenate the hours needed for
# the first set of special files
#####################################
if [ -s pgrbf ]
then
    rm  pgrbf
fi
 
for HOUR in $hourlist1 
do 
   cat $COMIN/$model_prefix1.${cycle}.pgrbf${HOUR}.2 >> pgrbf
done   
 
#####################################
# Concatenate the hours  needed for 
# the second set of special files
#####################################

for HOUR in $hourlist2
do
    cat $COMIN2/$model_prefix2.${cycle}.pgrbf${HOUR} >> pgrbf
done

#####################################
# Use wgrib to get an inventory of   
#  this concatenated file    
#####################################

$WGRIB -s pgrbf  -ncep_opn > pgrbf.inv


if [ -s ensemble.${RUN} ]
then
   rm  ensemble.${RUN}
fi

####################################
# Grep out the needed fields from       
#  the inventory of the accumulated
#  grib file
#####################################
    egrep ":HGT:1000.mb|:HGT:500.mb|:HGT:300.mb|:HGT:250.mb|:HGT:200.mb|UGRD:10.m.a|:UGRD:850.mb|:UGRD:500.mb|:UGRD:250.mb|:VGRD:10.m.a|:VGRD:850.mb|:VGRD:500.mb|:VGRD:250.mb|:TMP:2.m.a|:TMP:850.mb|:RH:700.mb|:APCP|:PRMSL:MSL|:TMAX:2.m.a|:TMIN:2.m.a" \
    pgrbf.inv  | $WGRIB pgrbf -s -ncep_opn -grib -i -o ensemble.${RUN}    


####################################
# Finally, copy the special ensemble    
#  to the permanent diskspace       
#####################################

if test "$SENDCOM" = "YES"
then
   cp ensemble.${RUN}   $COMOUT/ensemble.${RUN}.$PDY.${cyc}
   if test "$SENDDBN" = "YES"
   then
      MEM=`echo $RUN | tr [a-z] [A-Z]`
      $DBNROOT/bin/dbn_alert MODEL ENS_COL_$MEM $job $COMOUT/ensemble.${RUN}.${PDY}.${cyc}
   fi
fi

fi

msg="ENDING exsemble.sh for memeber $memberlist"
postmsg "$jlogfile" "$msg"
