make_daily.sh - UK-FVCOM-Usergroup/uk-fvcom GitHub Wiki

#!/usr/bin/env bash
#
# Make daily output files sequentially numbered for the offline Lagrangian code
# from a series of monthly runs.
#
# Pierre Cazenave, Plymouth Marine Laboratory.
#
# Revision history:
#   2014-10-27 First version.

set -eu

usage(){
    echo "Error: received 0 file names; need at least one"
    echo "$(basename "$0") file1.nc file2.nc ... fileN.nc"
    exit 1
}

if [ $# -eq 0 ](/UK-FVCOM-Usergroup/uk-fvcom/wiki/-$#--eq-0-); then
    usage
fi

# Input configuration
sampling=1  # main FVCOM results file sampling frequency (hours)
outinc=24   # number of hours per subsampled file
dailydir='/local/LAG/daily/' # output directory

if [ ! -d "$dailydir" ]; then
    mkdir -p "$dailydir"
fi

inc=$((sampling*outinc)) # increment for ncks
cc=0
ff=1
for file in "$@"; do
    hoursinrun=$(ncdump -h "$file" | grep time\ = | cut -f2 -d \( | cut -f1 -d' ')
    echo "$file ($(echo "scale=4; $hoursinrun/24" | bc -l) days) "
    for i in $(seq $inc $inc "$hoursinrun"); do
        # Set to non-zero to skip # time steps.
        if [ $ff -lt 0 ]; then
            continue
        else
            echo -n "$ff "
            st=$((i - inc))
            et=$((i - 1))
            if [ $et -gt $hoursinrun ]; then
                echo "BREAKING ON COUNT $ff"
                break
            fi
            ncrcat -dtime,$st,$et "$file" "$dailydir/$(basename "${file%.*}" _0001)_$(printf %04d $ff).nc"
        fi
        ((ff+=1))
    done
    ((cc+=1))
done