#!/bin/sh

set -x

[ $# -lt 4 -a "$1" != env ] && { cat <<'####'; exit 99; }
msg="STARTING SCRIPT $0."
postmsg "$jlogfile" "$msg"
####  UNIX Script Documentation Block
# Script name:   godas_findfields    Scour com dirs for needed fields
# Author:        D Stokes        Org: NP23           Date: 2004-05-06
#------------------------------------------------------------------
# SCRIPT TO SEARCH FOR DATA FIELDS FROM EXPECTED OR ALTERNATE DATES.
# GODAS needs flux and sst fields from the run dates 
# plus 2 surrounding dates.
#
# If the expected dates are not found, fields for surrounding dates are sought.
# If the required number of fields cannot be found, a critical error occurs
#
#
# Usage:  
#    sh /nwprod/ush/godas_findfields.sh $fileb $field_start_date $field_end_date $nfields
#  where:
#    fileb is the base name of the field to be searched (eg, taux)
#    field_start_date is the earliest date expected (eg, 20040221)
#    field_end_date is the latest date expected (eg, 20040229)
#    nfields is the number of fields REQUIRED.  (eg, 3 for 1-day run, or 9 for 7-day run)
#
#
#  History: May 2004 - First implementation of this new script.
#           Sep 2007 - Modified to handle search for and through more files.
#                      Some instances of 'ls' changed to 'find' to avoid unix limitation
#                      in handling long command lines due to filename expansion.
#------------------------------------------------------------------
####


fileb=$1
field_start_date=$2
field_end_date=$3
nfields=$4

#[ -z "$COMBASE" ] && COMBASE=/com/godas/prod/godas
[ -z "$COMBASE" ] && COMBASE=/com/godas/para/godas
[ -z "$utilscript" ] && utilscript=/nwprod/util/ush

[ -f ${fileb}_field_warning.lst ] && rm ${fileb}_field_warning.lst

# find available dates for this field. error and quit if none found.
#   ls ${COMBASE}.[0-9][0-9][0-9][0-9][01][0-9][0-3][0-9]/$fileb.[0-9][0-9][0-9][0-9][01][0-9][0-3][0-9] > /dev/null
   find ${COMBASE}.[0-9][0-9][0-9][0-9][01][0-9][0-3][0-9] -name $fileb.[0-9][0-9][0-9][0-9][01][0-9][0-3][0-9] > $fileb.lst
   if [ ! -s $fileb.lst ];then
    export msg="NO $fileb FILES FOUND IN $COMBASE DIRECTORIES"
    postmsg "$jlogfile" "$msg"

    exit 6    #dcs#
  fi
  [ -f $fileb.date.lst ] && rm $fileb.date.lst
#  for file in ${COMBASE}.[0-9][0-9][0-9][0-9][01][0-9][0-3][0-9]/$fileb.[0-9][0-9][0-9][0-9][01][0-9][0-3][0-9]
  for file in `cat $fileb.lst`
  do
    bname=`basename $file`
    echo ${bname#$fileb.} >> $fileb.date.lst
  done
  sort -r $fileb.date.lst > $fileb.date.reverse_sorted.lst

# find $field_start_date or nearest earlier available day 
  unset CDATE
  while read fdate
  do
    if [ $fdate -le $field_start_date ]; then
      CDATE=$fdate
      break
    fi
  done < $fileb.date.reverse_sorted.lst


# print error and quit if no files available before the initial model run date.
  if [ ${#CDATE} -eq 0 ];then
    export msg="NO $fileb FILE FOUND PRE MODEL RUN START DATE"
    postmsg "$jlogfile" "$msg"
    exit 7    #dcs#
  fi

# print error and quit if no files available after the final model run date.
  last_avail_date=`head -1 $fileb.date.reverse_sorted.lst`
  if [ $last_avail_date -lt $field_end_date ]; then
    export msg="NO $fileb FILE FOUND POST MODEL RUN END DATE"
    postmsg "$jlogfile" "$msg"
    exit 8    #dcs#
  fi


# Above was to find starting date and latest avail date. Now actually get files. 
  nfield=0  
  [ -f ${fileb}_used_files.lst ] && rm ${fileb}_used_files.lst  

  until [ nfield -eq $nfields ]    
  do

  echo $CDATE
  if [ `grep -c $fileb.$CDATE $fileb.lst` -gt 0 ]; then
    ls -t ${COMBASE}.[0-9][0-9][0-9][0-9][01][0-9][0-3][0-9]/$fileb.$CDATE | read ffile
    nfield=`expr $nfield + 1 `
    cp $ffile $fileb.$nfield  
    echo used file $ffile >> ${fileb}_used_files.lst
  else
    if [ $nfield -eq 0 ]; then
      msg="LOGIC ERROR IN SCRIPT $0.  CRITICAL"
      postmsg "$jlogfile" "$msg"
      exit 9    #dcs#
    else
      echo "$fileb.$CDATE not found.  Will continue to look for other files."  | tee -a ${fileb}_field_warning.lst
    fi
  fi
  if [ $nfield -lt $nfields ]; then
    CDATE=`sh $utilscript/finddate.sh $CDATE d+1`
    if [ $CDATE -gt $last_avail_date ]; then
      export msg="REACHED LAST AVAIL DATE $last_avail_date WITHOUT FINDING $nfields FILES.  CRITICAL"
      postmsg "$jlogfile" "$msg"
      exit 10    #dcs#
    fi
   fi
  done

echo $CDATE

cat ${fileb}_used_files.lst

if [ -s ${fileb}_field_warning.lst ]; then
  export msg="Not all expected fields found for ${fileb}.  Check output"   #dcs# improve this later
  postmsg "$jlogfile" "$msg"
fi


