#!/bin/bash
cd "${0%/*}" || exit 1    # Run from this directory

# Source case functions
. "$WM_PROJECT_DIR/bin/tools/casesFunctions.sh"

latestTime=$(foamListTimes -functionEntries | tail -n1)
targetDirectory="validation/reference"
rm -rf "$targetDirectory"
mkdir -p "$targetDirectory/holdupGasD/0" \
         "$targetDirectory/holdupLiquidC/0" \
         "$targetDirectory/holdupLiquidD/0" \
         "$targetDirectory/surfaceGasC/${latestTime}" \
         "$targetDirectory/surfaceLiquidC/${latestTime}" \
         "$targetDirectory/layerAverage/${latestTime}"

# Zip reference data
gzip -krf postProcessing/surface*/"${latestTime}"

# Temporal averaging
mpypost avg --starttime 5 postProcessing/holdupGasD/0/volFieldValue.dat
mpypost avg --starttime 5 postProcessing/holdupLiquidC/0/volFieldValue.dat
mpypost avg --starttime 5 postProcessing/holdupLiquidD/0/volFieldValue.dat

# Copy results from latest run into reference directory
cp -f "postProcessing/holdupGasD/0/volFieldValue_avg.csv" \
    "$targetDirectory/holdupGasD/0"
cp -f "postProcessing/holdupLiquidC/0/volFieldValue_avg.csv" \
    "$targetDirectory/holdupLiquidC/0"
cp -f "postProcessing/holdupLiquidD/0/volFieldValue_avg.csv" \
    "$targetDirectory/holdupLiquidD/0"

cp -f "postProcessing/surfaceGasC/${latestTime}/isoSurface.xy.gz" \
    "$targetDirectory/surfaceGasC/${latestTime}"
cp -f "postProcessing/surfaceLiquidC/${latestTime}/isoSurface.xy.gz" \
    "$targetDirectory/surfaceLiquidC/${latestTime}"

cp -f "postProcessing/layerAverage/${latestTime}/layerAverage.csv" \
    "$targetDirectory/layerAverage/${latestTime}"

#------------------------------------------------------------------------------
