#!/bin/ksh

########################################################################

#D- Flag to determine if this job in a standalone mode
#D- Default : value from AA_job if any
StandAlone=${StandAlone:=true}

#D- Increased verbosity (1, 2, 3)
#D- Default : value from AA_job if any
Verbosity=${Verbosity:=3}

#D- Low level debug : to bypass lib test checks and stack construction
#D- Default : value from AA_job if any
DEBUG_debug=${DEBUG_debug:=false}

#RUN_DIR_PATH=${RUN_DIR_PATH:=/tmp/tmp$$}

########################################################################

MODIPSL=$1
libIGCM=${libIGCM:=${MODIPSL}/libIGCM}

. ${libIGCM}/libIGCM_debug/libIGCM_debug.ksh
. ${libIGCM}/libIGCM_card/libIGCM_card.ksh
. ${libIGCM}/libIGCM_date/libIGCM_date.ksh
. ${libIGCM}/libIGCM_sys/libIGCM_sys.ksh

########################################################################
#set -vx
########################################################################
# Script input parameters

# Site name
SiteArg=$2
Site=${SiteArg:=Gu}
# Time in years in file for this site :
DureeArg=$3
export DUREE=${DureeArg:=1}

# First year in file for this site :
FirstArg=$4
export FYEAR=${FirstArg:=1}

(( LYEAR=FYEAR+DUREE-1 ))
export LYEAR


# Atlas Name : 
AtlasCfgArg=$5
AtlasCfg=${AtlasCfgArg:=atlas_FLUXNET.cfg}

# observation_file
observation_file_pathArg=$6
observation_file_path=${observation_file_pathArg:='${R_IN}/BC/OOL/${config_UserChoices_TagName}/FLUXNET/OLD/${Site}.nc'}
#/home/mancip/IGCM/BC/OOL/OL2/FLUXNET/OBS/${Site}_obs_gapfilled.nc

# Modulo for SpinUp years 
ModuloArg=$9

#if [ ${DEBUG_debug} = true ] ; then
IGCM_sys_MkdirWork ${RUN_DIR_PATH}
echo "RUN_DIR_PATH ${RUN_DIR_PATH} ok."
#fi
IGCM_sys_Cd ${RUN_DIR_PATH}

if [ ${StandAlone} = true ] ; then
    CARD_DIR=${SUBMIT_DIR}
else
    CARD_DIR=${RUN_DIR_PATH}/$( basename ${SUBMIT_DIR} )
    IGCM_sys_Get_Master ${SUBMIT_DIR} ${RUN_DIR_PATH}
fi

#
# First of all
#
IGCM_card_DefineArrayFromSection ${CARD_DIR}/config.card UserChoices
typeset option
for option in ${config_UserChoices[*]} ; do
    IGCM_card_DefineVariableFromOption ${CARD_DIR}/config.card UserChoices ${option}
done
#
echo
IGCM_debug_Print 1 "DefineArrayFromOption  : config_UserChoices"
IGCM_debug_PrintVariables 3 config_UserChoices_JobName
if [ 3 -le ${Verbosity} ] ; then
    echo "--------------Debug3-->" "config_UserChoices_LongName="
    echo "--------------Debug3-->" \"${config_UserChoices_LongName}\"
fi
IGCM_debug_PrintVariables 3 config_UserChoices_TagName
IGCM_debug_PrintVariables 3 config_UserChoices_CalendarType
IGCM_debug_PrintVariables 3 config_UserChoices_DateBegin
IGCM_debug_PrintVariables 3 config_UserChoices_DateEnd
IGCM_debug_PrintVariables 3 config_UserChoices_PeriodLength
echo


# FLAG = SECHIBA, OK_STOMATE
IGCM_card_DefineVariableFromOption ${CARD_DIR}/fluxnet.card UserChoices ok_stomate
if [ X${fluxnet_UserChoices_ok_stomate} = Xn ] ; then
    export FLAG=SECHIBA
else
    export FLAG=OK_STOMATE
fi

#==================================

SpinUpJob=${Site}${config_UserChoices_JobName}

R_BC=${R_IN}/BC
R_SAVE=${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_JobName}/${SpinUpJob}/SPIN

# look at files
IGCM_card_DefineVariableFromOption ${CARD_DIR}/fluxnet.card UserChoices DEBUG_SPIN
if [ X${fluxnet_UserChoices_DEBUG_SPIN} = Xn ] ; then
    set -A liste_fichiers_with_path -- $( \ls -rt ${R_SAVE}/Output/${SpinUpJob}*/SRF/Output/MO/*_sechiba_history.nc ${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_JobName}/${SpinUpJob}HIST/SRF/Output/MO/*_sechiba_history.nc )
else 
    set -A liste_fichiers_with_path -- $( \ls -rt ${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_JobName}/${SpinUpJob}*/SRF/Output/MO/*_sechiba_history.nc )
fi

ii=0
for file in ${liste_fichiers_with_path[@]} ; do
    liste_fichiers[$ii]=$( echo ${file} | xargs basename )
    (( ii = ii + 1 ))
done
echo ${liste_fichiers[*]}

# Get and sed atlas
IGCM_sys_Cp ${CARD_DIR}/${AtlasCfg} ./atlas.cfg

# Set default Modulo parameter to the last file
eval NbFiles=${#liste_fichiers[@]}
echo "NbFiles=" $NbFiles
Modulo=${ModuloArg:=$(( NbFiles - 1 ))}

# Get last time_axis patch
Last_Patch_File=$( ls -t ${libIGCM}/libIGCM_post/IGCM_Patch_*_histcom_time_axis.ksh | tail -1 )
#echo "source patch file : ${Last_Patch_File}"
# . ${Last_Patch_File}
# Last_Patch=$( echo ${Last_Patch_File} | sed -e "s&${libIGCM}/libIGCM_post/\(.*\)\.ksh&\1&" )
#echo "We will apply the patch on all sechiba files : "  ${Last_Patch}

# Apply atlas
ii=0
for file in ${liste_fichiers[*]} ; do 

    DirFile=$( dirname ${liste_fichiers_with_path[$ii]} )
    echo ${DirFile}

    # First apply the last time_axis patch
#     IGCM_sys_Cd ${DirFile}
#     ${Last_Patch} ${file}
#     # clean dir
#     rm out_rsync
#     # Then return to RUN_DIR_PATH
#     IGCM_sys_Cd ${RUN_DIR_PATH}
    
    if [ $ii -eq 0 ]  ; then
	file_=${file}
        # copy files we need for ATLAS
	IGCM_sys_Cp -p ${liste_fichiers_with_path[$ii]} .
    fi
    echo "!!!!!!!!!!!!!!!!!"
    echo ${file}
    export F_FILE=${Site}
    echo $ii "Modulo : " $( expr $ii \% $Modulo )
    if [ $ii -gt 0 ] && [ X$( expr $ii \% $Modulo ) = X0 ] ; then

        # copy files we need for ATLAS
	IGCM_sys_Cp -Rp ${liste_fichiers_with_path[$ii]} .

	reference_file=${file_}
	eval observation_file=${observation_file_path}
	echo "ATLAS DIFF for files : " ${file} ${reference_file} ${observation_file}

	#cat atlas.cfg
	sed -e "s,RRRRRRR,${reference_file},g" -e "s,VVVVVVV,${observation_file},g" atlas.cfg > atlas_.cfg
	#cat atlas_.cfg
	AtlasPath=$( echo ${file} | sed -e 's/_1M_sechiba_history.nc//' )
	atlas -o ATLAS_DIFF_${AtlasPath} atlas_.cfg ${file}

        # Save files
	IGCM_sys_Put_Dir ATLAS_DIFF_${AtlasPath} ${R_SAVE}
#	IGCM_sys_Rm -Rf ATLAS_DIFF_${AtlasPath} 
	echo "&&&&&&&&&&&&&&&&!"

	file_=${file}
    fi
    (( ii=ii+1 ))
done

# old history file
reference_file_pathArg=$7
reference_file_path=${reference_file_pathArg:=${file}}
echo ${reference_file_path}

eval reference_file=${reference_file_path}
sed -e "s,RRRRRRR,${reference_file},g" -e "s,VVVVVVV,${observation_file},g" atlas.cfg > atlas_.cfg

echo "REFERENCE ATLAS for file : " ${file} ${reference_file} ${observation_file}
reference_config=$8
atlas -o ATLAS_DIFF_${SpinUpJob}_${reference_config} atlas_.cfg ${file}

# Save files
IGCM_sys_Put_Dir ATLAS_DIFF_${SpinUpJob}_${reference_config} ${R_SAVE}

IGCM_sys_Rm -Rf ATLAS_DIFF_${SpinUpJob}_${reference_config}



# # Get and Apply Patch
# . ${MODIPSL}/libIGCM/libIGCM_post/IGCM_Patch_20070220_histcom_time_axis.ksh
# for file in ${liste_fichiers[*]} ; do 
#     IGCM_Patch_20070220_histcom_time_axis ${file}
# done

# # look at files
# echo "after Patch" IGCM_Patch_20070220_histcom_time_axis
# \ls -lrt  *_sechiba_history.nc

# Concat files
#IGCM_sys_ncrcat -o ${SpinUpJob}_1M_sechiba_history.nc ${liste_fichiers[*]}

IGCM_sys_Rm -Rf  ${liste_fichiers[*]}

IGCM_sys_RmRunDir -Rf ${RUN_DIR_PATH}
