#!/bin/bash
#==============================================
#
# Usage:
# ./diff.bash f1.nc f1.nc
# ./diff.bash f1.nc f1_witherror.nc
#
# ./diff.bash fileA.nc fileB.nc 'var1,var2'
#
# ex: ./diff.bash file_ncrcat.nc file_cdocat.nc 'contfracATM'
#
#==============================================

if [ $# -eq 3 ] ; then
	delname="-delname,$3"
fi

cdo -s $delname $1 ${JOB_DIR}/out_tmp1.nc
cdo -s $delname $2 ${JOB_DIR}/out_tmp2.nc

cdo -s diff ${JOB_DIR}/out_tmp1.nc ${JOB_DIR}/out_tmp2.nc > ${JOB_DIR}/diff_$$.tmp 2>&1
result2=`cat ${JOB_DIR}/diff_$$.tmp | grep -i "records differ" | tr -s " " | cut -f2 -d" "`

if [ "$result2" = "0" ] ; then
	# EXIT_SUCCESS
	echo "Variables from netcdf files : "
	echo "$1"
	echo "$2" 
	echo "are the same"
	rm -f ${JOB_DIR}/diff_$$.tmp ${JOB_DIR}/out_tmp1.nc ${JOB_DIR}/out_tmp2.nc
	exit 0
else
	# EXIT_FAILURE
	echo "Variables from netcdf files : "
	echo "$1"
	echo "$2" 
	echo "differ"	
	cat ${JOB_DIR}/diff_$$.tmp | grep -i "records differ"
	rm -f ${JOB_DIR}/diff_$$.tmp ${JOB_DIR}/out_tmp1.nc ${JOB_DIR}/out_tmp2.nc
	echo "==============> ERROR"
	exit 1 
fi

