#! /bin/ksh

# This script symbolicly links to GFS (and possibly also GDAS) input
# files.  It assumes that, if the hour 0 GRIB file does not yet exist,
# it has the standard operational naming convention
# (gfs.tHHz.pgrbf00).  The only HWRF input files not linked by this
# script are the loop current file needed by POM, any HyCOM input
# files, and the previous cycle's files.

set -x

gdasdir=''   # if non-empty, where to find GDAS (only used for satang file)
wait_grib=no  # if yes, wait for the first grib file to appear
firstgrib=0
lastgrib=0
gribstep=6

firstsf=0
lastsf=126
sfstep=6

function arg {
    if [[ ! ( "$2" -gt 1 ) ]] ; then
        echo "ABORTING.  $1 requires an argument."
        return 2
    fi
}

go=yes
while [[ "$go" == yes && "$#" -gt 0 ]] ; do
    case "$1" in
        --wait-grib)                   wait_grib=yes   ; shift 1 ;;
        --all-gribs)                   lastgrib=126    ; shift 1 ;;
        --gdas-satang) arg "$1" "$#" ; gdasdir="$2"    ; shift 2 ;;
        --)                            go=no           ; shift 1 ;;
        *)                             go=no                     ;;
    esac
done

cycle="$1"   # cycle (YYYYMMDDHH)
wind="$2"    # where to put or link wind data
ocean="$3"   # where to put or link ocean data
gfsdir="$4"  # where to find GFS input

ln='ln -sf'
hh=$( echo "$cycle" | cut -c9-10 )
orig=$gfsdir/gfs.t${hh}z
here=gfs.t${hh}z

cd "$wind"

# Link spectral and grib forecast files:
for fh in $( seq $firstsf $sfstep $lastsf ) ; do
    fh00=$( printf "%02d" $fh )
    $ln $orig.sf$fh00 $here.sf$fh00
done

if ( echo "${GFS_SOURCE:-}" | grep -i grib2 ) ; then
    if [[ "$wait_grib" == yes ]] ; then
        thefile=$orig.pgrb2f00
        echo WAITING FOR FIRST GRIB2 FILE $thefile
        if ( ! $USHhwrf/hwrf_file_complete.pl -v -S 20 -s 1 -o 10 -w 3600 $thefile ) ; then
            fail "Waited more than an hour for the first GRIB2 file $thefile"
        fi
    fi
    for fh in $( seq $firstgrib $gribstep $lastgrib ) ; do
        fh00=$( printf "%02d" $fh )
        $ln $orig.pgrb2f$fh00 $here.pgrb2f$fh00
    done
else
    if [[ "$wait_grib" == yes ]] ; then
        thefile=$orig.pgrbf00
        echo WAITING FOR FIRST GRIB FILE $thefile
        if ( ! $USHhwrf/hwrf_file_complete.pl -v -S 20 -s 1 -o 10 -w 3600 $thefile ) ; then
            fail "Waited more than an hour for the first GRIB file $thefile"
        fi
    fi
    for fh in $( seq $firstgrib $gribstep $lastgrib ) ; do
        fh00=$( printf "%02d" $fh )
        # The grib files can have two different naming conventions:
        opername=$orig.pgrbf$fh00              # operational naming convention
        misnamed=$gfsdir/pgrbf$fh00.gfs.$cycle # misnamed in some retrospectives
        if [[ -s "$misnamed" ]] ; then
            $ln $misnamed $here.pgrbf$fh00
        else
            $ln $opername $here.pgrbf$fh00
        fi
    done
fi

# Link satellite angle file:
if [[ ! -z "$gdasdir" ]] ; then
    ymdh_m6=$( ndate -6 $cycle )
    m6=$( echo "$ymdh_m6" | cut -c9-10 )
    $ln $gdasdir/gdas1.t${m6}z.satang $here.satang
else
    $ln $orig.satang $here.satang
fi

# Link track files:
$ln $gfsdir/avn.t${hh}z.cyclone.trackatcfunix .
$ln $gfsdir/gfs.t${hh}z.syndata.tcvitals.tm00 .

# Link analysis-time files (other than hour 0 "forecast" file):
for what in prepbufr.nr goesfv.tm00.bufr_d 1bhrs3.tm00.bufr_d \
      airsev.tm00.bufr_d 1bhrs4.tm00.bufr_d 1bamub.tm00.bufr_d \
      1bmhs.tm00.bufr_d 1bamua.tm00.bufr_d sfcanl sanl abias ; do
    $ln $orig.$what $here.$what
done

# Also link the sanl and sfcanl files to the spinup directory
cd "$ocean"
$ln $orig.sanl $here.sanl
$ln $orig.sfcanl $here.sfcanl

exit 0
