#!/bin/bash
listToProcessFile="${1}"
# exemple de fichier liste :
# /ccc/scratch/cont003/incka/pierreg/IGCM_DEM/dataDMNFS_FromTitane/IGCM_OUT/IPSLCM5A/REDO/historicalGHG/v3.historicalGHG1R/output_tar/v3.historicalGHG1R_20101201_20121231_1M_histmthNMC.nc.list


# On renseigne les variables d'environnement (ici surtout pour la var OUTPUT_CHECK) 
. load_ipslPack_env.sh


dirin=$INPUT_DMF_DATA
dirout=$OUTPUT_STORE
dirout_work=$OUTPUT_WORK
dirout_check=$OUTPUT_CHECK

export JOB_DIR=${TMP_MIGR_DATA}
export EXE_DIR=${LS_SUBCWD:-${PWD}}
source ${EXE_DIR}/DEM_utilities.sh

# Ca sert a quoi tout ca ?
# dirPathOfListFile=$( dirname  $listToProcessFile )
# dirnameOfListFile=$( basename $dirPathOfListFile )

# rep de simu : celui contenant le rep qui contient la liste
# PATH_SIMU=$( dirname  $dirPathOfListFile )

set=$listToProcessFile


#-----------------------------------
# Set output file name from list name

fileout1=$( basename $set )
fileout=${fileout1%.list}

filefirst=`head -n 1 $set`
diroutputfile=`dirname $filefirst | sed -e "s%$dirin%$dirout%"`

output_ncrcat_file=$diroutputfile/${fileout}

#----------------------------------
fileout_cdocat=${fileout1%.list}.cdocat

dirOutput_cdocat_file=`dirname $filefirst | sed -e "s%$dirin%$dirout_check%"`

output_cdocat_file=${dirOutput_cdocat_file}/${fileout_cdocat}


#----------------------------------
# log file 
datestr=`LC_ALL=C date +"%Y%m%dTT%H%M%S"`
logfile=${fileout_cdocat%%.cdocat}_${datestr}.log
output_cdocat_logFile=${dirOutput_cdocat_file}/${logfile}


#echo
#echo "ncrcat file to compare cdo cat file with :"
#echo "$output_ncrcat_file"
#echo
#echo "Output cdo cat file to create:"
#echo "$output_cdocat_file"
#echo
#echo "Output cdo cat log file:"
#echo "$output_cdocat_logFile"

if [ ! -e $listToProcessFile ]
then
    echo "Le fichier en entree n'existe pas. STOP." > $output_cdocat_logFile
    exit 1
fi


if [ ! -e $outputfile ]
then
   echo "Le fichier liste suivant n'existe pas :" >> $output_cdocat_logFile
   echo "$outputfile" >> $output_cdocat_logFile
   exit 1
fi

mkdir -p $dirOutput_cdocat_file

listOfFilesToCdocat=`cat $listToProcessFile `


cdo cat $listOfFilesToCdocat $output_cdocat_file > $output_cdocat_logFile 2>&1


echo >> $output_cdocat_logFile

vartoexclude="t_inst*,contfracATM,sobowlin,Contfrac"
./diff.bash $output_ncrcat_file $output_cdocat_file $vartoexclude >> $output_cdocat_logFile

code_sortie=$?

exit $code_sortie













