#!/bin/bash
# This script can be easily modified to link sflux from a database that uses calendar dates (e.g. hrrr_20200103.nc)
# Simply modify 'filedir' below etc.
# Run this script in dir in sflux/; 
#'rad' and 'prc' will be broken links (as they are identical to air); move them into parent sflux/
# Also need sflux_inputs.txt

#./sflux_link.sh date_start date_end
#./sflux_link.sh 20210331 20210820 

ict2=0

start=$1
end=$2

start=$(date -d $start +%Y%m%d)
end=$(date -d $end +%Y%m%d)

rm -rf sflux
mkdir sflux

while [[ $start -le $end ]]
do
     fday=$start
     echo $fday

    filedir=./

   if [ -f $filedir/hycom_${fday}.nc ]
      then
        ict2=$(($ict2+1))
        ic2=$(printf "%d" $ict2)
           ln -s $filedir/hycom_${fday}.nc SSH_${ic2}.nc
           ln -s SSH_${ic2}.nc TS_${ic2}.nc
           ln -s SSH_${ic2}.nc UV_${ic2}.nc
   else
      echo 'gaps found'
      break
   fi
    #Forward date
    start=$(date -d"$start + 1 day" +"%Y%m%d")

done

