#!/bin/bash
#set -xv 

# This script is used to compares two netcdf files when there are some more variables in one or 
# the other of the files. The script will:
# 1) Compare using ncdump the variables in both files and ask the user to remove if wanted. 
#    Temporary files are created with less variables.
# 2) use cdo diffv on the temporary files 

file1=$1
file2=$2

rm -f dump1 dump2 file1tmp.nc file2tmp.nc

ncdump -h $file1 > dump1
ncdump -h $file2 > dump2

echo "Diff with grep float:"
diff dump1 dump2 |grep float
echo "Type variable list to be removed from input files before comparison"
echo "or press enter if the list is empty to search for variables in double pression"
echo ""
echo "Type varlist1:"
echo "Syntax: var1,var2,var3"
read varlist1
echo "Type varlist2:"
read varlist2

echo varlist1=$varlist1 varlist2=$varlist2

if [ X$varlist1 == X ] && [ X$varlist2 == X ] ; then
   echo "Search for variables in double precision"
   diff dump1 dump2 |grep double
   echo "Type variable list to be removed from input files before comparison"
   echo ""
   echo "Type varlist1:"
   echo "Syntax: var1,var2,var3"
   read varlist1
   echo "Type varlist2:"
   read varlist2

   echo varlist1=$varlist1 varlist2=$varlist2
fi

if [ X$varlist1 == X ] ; then
  # only copy the file
  ncks $file1 -o file1tmp.nc
else
  # extract variables
  ncks -x -v $varlist1 $file1 -o file1tmp.nc
fi

if [ X$varlist2 == X ] ; then
  ncks $file2 -o file2tmp.nc
else
  ncks -x -v $varlist2 $file2 -o file2tmp.nc
fi

cdo diffv file1tmp.nc file2tmp.nc

