#! /bin/ksh

# This script archives many of the COUPLED_FCST/ directory input and
# output files that may be needed for post-processing or failure
# diagnostics.  Some critical files such as wrfanl, wrfbdy, wrfinput
# are not archived by this script since the hwrf_output.sh already
# archives them.  This is intended to create short-term archives of
# files that may be needed to run diagnostics or post-processing
# jobs that could not be run before the data was deleted from disk.
# This is to support HRD post-processing of EMC's 9:3 HWRF simulations,
# in situations when the EMC simulation output has been deleted before
# the HRD post-processor is run.
#
# AUTHOR: Sam Trahan November 2010

function die {
    echo "$*"
    exit 91
}

set -x

if [[ "$WHERE_AM_I" != jet ]] ; then
    echo WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
    echo WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
    echo " "
    echo THIS IS NOT YET TESTED ON CCS OR VAPOR.  CROSS YOUR FINGERS...
    echo " "
    echo WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
    echo WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
fi

donefile="$DATA/rundir-archive.done"
rm -f "$donefile"

adir="${RUNDIR_ARCHIVE_TARGET_DIR}"

if [[ -z "$adir" ]] ; then
    if [[ "$HIRES" == YES ]] ; then
        adir=/lfs1/projects/hwrf-vd/${user}/rundir/${EXPT}
#        mkdir -p "$adir"
    else
        die '$RUNDIR_ARCHIVE_TARGET_DIR not specified and not hires, so Aborting.'
    fi
fi

if [[ ! -d "$adir" ]] ; then
#    if [[ "$HIRES" == YES ]] ; then
#        adir=/lfs1/projects/hwrf-vd/${user}/rundir/${EXPT}
#        mkdir -p "$adir"
#    else
#        die "$adir: does not exist or is not a directory and HIRES is not YES.  Not creating rundir archive."
#    fi
    die "$adir: does not exist or is not a directory.  Not creating rundir archive."
fi

storm=`echo ${STORM} | tr '[A-Z]' '[a-z]'`  
nid=${storm}${stormid}
nidymdh=${nid}.${YYYYMMDDHH}

########## Create the archive directory.
archiveparent=$DATA
archivedir=$archiveparent/$nidymdh-wrfout
rm -rf "$archivedir"
mkdir -p "$archivedir"
if [[ ! -d "$archivedir" ]] ; then
    die "UNABLE TO CREATE $archivedir"
fi

##########  cd to archivedir
cd "$archivedir"
if [[ "$?" != 0 ]] ; then
    die "UNABLE TO CD TO $archivedir"
fi

########## link to wrfout files
startymdh=$YMDH
ndate="$NDATE_PATH"
rundir="$CSTREAM/${MODEL}_FCST/"
for ihour in $( seq 0 1 126 ) ; do
    iYMDH=$( $ndate $ihour $startymdh )

    iYYYY=$( echo $iYMDH | cut -c 1-4 )
    iMM=$( echo $iYMDH | cut -c 5-6 )
    iDD=$( echo $iYMDH | cut -c 7-8 )
    iHH=$( echo $iYMDH | cut -c 9-10 )

    # year, month, day, hour part of wrfout filename:
    idatepart=${iYYYY}-${iMM}-${iDD}_${iHH}

    # Select all files we could care about: all wrfout and auxhist1
    # files for any domain during that hour:
    fileset1=$( ls -1 "$rundir"/{auxhist1,wrfout}_d[0-9][0-9]_${idatepart}:[0-9][0-9]:[0-9][0-9] )

    # Loop over those files, making links to any of those files
    # that either exist, or are symbolic links to non-existant files:
    for file in $fileset1 ; do
        if [[ -e "$file" || -L "$file" ]] ; then
            # Links will have the stormid prepended to  match
            # the filename convention in the COM directory:
            ln -s $file $stormid.$( basename $file )
        fi
    done
done

########## link to some other input and output files
for special in namelist.output namelist.input rsl.out.0000 rsl.error.0000 \
    tr49t67 tr49t85 tr67t85 ETAMPNEW_DATA GENPARM.TBL \
    co2_trans cmdf cpl_nml eta_micro_lookup.dat fort.65 \
    fort.41 fort.42 fort.43 fort.13 fort.66 set_nest RST.final ; do
  file="$rundir/$special"
  if [[ -e "$file" || -L "$file" ]] ; then
      # Links will have the name, cycle and stormid prepended to
      # match the filename convention in the COM directory:
      ln -s "$file" $stormid.$special
  fi
done

########## now create the archive
if [[ "$WHERE_AM_I" == jet ]] ; then
    if ( tar -czphf "$adir"/${nidymdh}.rundir.tar.gz ./* ) ; then
        echo "$( date ): RUNDIR ARCHIVE DONE" >> "$donefile"
    fi
else
    # Cannot use hpsstar since it does not send -h (follow symbolic links).
    # Instead, we'll call htar directly:
    if ( htar -cvhf "$adir"/${nidymdh}.rundir.tar.gz -Y auto ./* ) ; then
        echo "$( date ): RUNDIR ARCHIVE DONE" >> "$donefile"
    fi
fi
