#!/bin/bash


# Script permettant de creer les listes de fichiers pour ncrcat
# si un trou est detecte dans la simulation alors cette liste sera 
# pour un tar

# $1= PATH/config_card d'une simulation avec 
# TagName/JobName/ExperimentName/SpaceName/DateBegin/DateEnd/PATH_SIMUL


# Pour commencer on travaille avec le fichier information_dmnfs
# /cscratch/cont003/p86mart/FROM_CCRT_INFORMATIONS_DMNFS/information_dmnfs_2011-09-21

config_card=${1}
LISTE_DMNFS=${2}

# Declaration des variables

# ----
export JOB_DIR=${JOB_DIR:-${PWD}}
export IGCM_DEM=${IGCM_DEM:-${SCRATCHDIR}/IGCM_DEM}
export IGCM_TMP=${IGCM_TMP:-${IGCM_DEM}/tmp}
mkdir -p ${IGCM_DEM}
mkdir -p ${IGCM_TMP}
clean_tmp=${clean_tmp:-yes}
EXE_DIR=$(dirname ${0} )
SCRIPT_NAME=$(basename ${0} )
source ${EXE_DIR}/DEM_utilities.sh
DEM_log -0 "Demmarrage de ${SCRIPT_NAME}"
cd ${JOB_DIR}
# ----

DEM_log -0 "config_card : ${config_card}"
DEM_log -0 "LISTE_DMNFS : ${LISTE_DMNFS}"

eval $(grep JobName         ${config_card}) 
eval $(grep PATH_SIMUL_FULL ${config_card}) 
eval $(grep DateBegin       ${config_card}) 
eval $(grep DateEnd         ${config_card}) 
eval $(grep IGCM_DEM_SIMU   ${config_card})

SIMUL=${PATH_SIMUL_FULL}/${JobName}

DEM_state=$( DEM_read_state ${IGCM_DEM}/config_card.liste ${config_card} )
if [[ "${DEM_state}" != "FindSizePackDone" ]] ; then
    DEM_log -0 "Attention : Etat incorrect dans ${IGCM_DEM}/config_card.liste"
    exit -1
fi

DEM_write_state ${IGCM_DEM}/config_card.liste ${config_card} WriteListPackRunning

# lecture de periodpack dans ${IGCM_DEM_SIMU}/period_pack.txt
period_pack=$( awk '{print $0} ' ${IGCM_DEM_SIMU}/period_pack.txt )

year_begin=$(  echo ${DateBegin} | cut -c 1-4 )
month_begin=$( echo ${DateBegin} | cut -c 5-6 )
year_end=$(    echo ${DateEnd}   | cut -c 1-4 )
month_end=$(   echo ${DateEnd}   | cut -c 5-6 )

awk '{print $2}' ${IGCM_DEM_SIMU}/liste_output_files_config.txt >  ${IGCM_DEM_SIMU}/info_Output_light.txt

# on cree la liste des fichiers pour chaque type_file
for type_file in $( awk '{print $0}' ${IGCM_DEM_SIMU}/info_name_file.txt ) ; do 
   #sed -e "/${type_file}/!d" ${IGCM_DEM_SIMU}/info_Output_light.txt > ${IGCM_TMP}/${$}.info_${type_file}.txt
    grep ${type_file} ${IGCM_DEM_SIMU}/info_Output_light.txt > ${IGCM_TMP}/${$}.info_${type_file}.txt
    sort -u ${IGCM_TMP}/${$}.info_${type_file}.txt > ${IGCM_TMP}/${$}.tmp_info_${type_file}.txt.tmp
    cp ${IGCM_TMP}/${$}.tmp_info_${type_file}.txt.tmp ${IGCM_TMP}/${$}.info_${type_file}.txt
    [[ "${clean_tmp}" = "yes" ]] && rm -f ${IGCM_TMP}/${$}.tmp_info_${type_file}.txt.tmp
done


# 3- Maintenant pour chaque type de fichier de sortie nous allons creer la liste
# des fichiers contenus dans chaque pack

# Recherche des dates du premier pack

date_begin_pack=${DateBegin}
date_end_simulation=${DateEnd}

month_begin_pack=${month_begin}
year_begin_pack=${year_begin}

number_pack=1

#
while [[ ${date_begin_pack} -le ${date_end_simulation}  ]]
do 

   month_end_pack=${month_begin_pack}
   year_end_pack=$(( ${year_begin_pack} +${period_pack} ))
   
   date_end_pack=${year_end_pack}${month_end_pack}01   

   DEM_log -2 "Entree boucle 3 : year_begin_pack month_begin_pack year_end_pack month_end_pack : ${year_begin_pack} ${month_begin_pack} ${year_end_pack} ${month_end_pack}"
   DEM_log -2 "Entree boucle 3 : date_end_pack : ${date_end_pack}"
   
   for type_file in `awk '{print $0}' ${IGCM_DEM_SIMU}/info_name_file.txt`
   do 
       DEM_log -2 "Boucle 3 : ${type_file} ${number_pack}"
       taille=0

       # On passe en revue chaque type de fichier 
       for file in `awk '{print $0}' ${IGCM_TMP}/${$}.info_${type_file}.txt`
       do 
           # Extraire la date de debut et la date de fin de la periode d'output traitee par le fichier    
           extract_date_file=$( echo ${file}             | sed -e "s%.*${JobName}_%%" )
           date_debut=$(        echo ${extract_date_file}| sed 's%\([0-9]\{8\}\)_.*$%\1%g' )
           date_fin=$(          echo ${extract_date_file}| sed 's%.*_\([0-9]\{8\}\)_.*$%\1%g' )
	   
           if [  ${date_debut} -lt ${date_end_pack} ]
           then 
               if [ ${date_fin} -ge ${date_begin_pack} ]
               then 
		   echo ${file} >> ${IGCM_DEM_SIMU}/ncrcat_${type_file}_${date_begin_pack}_${date_end_pack}.list
		   echo ${date_debut} | cut -c 1-6 >>  ${IGCM_TMP}/${$}.date_${type_file}.txt
		   grep -v ${file} ${IGCM_TMP}/${$}.info_${type_file}.txt > ${IGCM_TMP}/${$}.info_${type_file}.txt.tmp
		   cp ${IGCM_TMP}/${$}.info_${type_file}.txt.tmp ${IGCM_TMP}/${$}.info_${type_file}.txt
		   [[ "${clean_tmp}" = "yes" ]] && rm -f ${IGCM_TMP}/${$}.info_${type_file}.txt.tmp
               fi
	   else 
	       break
           fi
	   
       done
       if [[ -f ${IGCM_TMP}/${$}.date_${type_file}.txt ]] ; then
	   sort -u ${IGCM_TMP}/${$}.date_${type_file}.txt > ${IGCM_TMP}/${$}.date_tmp.txt
	   mv ${IGCM_TMP}/${$}.date_tmp.txt ${IGCM_TMP}/${$}.date_${type_file}.txt
       else
	   DEM_log -2 "Pas de fichier cree :  ${IGCM_TMP}/${$}.date_${type_file}.txt - Date_end_pack : ${date_end_pack}"
       fi

#On renomme le fichier ncrcat avec la date reelle de fin et non pas la premiere date suivant la fin 
       sort ${IGCM_DEM_SIMU}/ncrcat_${type_file}_${date_begin_pack}_${date_end_pack}.list > ${IGCM_TMP}/${$}.ncrcat.tmp
       last_file=$(tail -n 1 ${IGCM_TMP}/${$}.ncrcat.tmp )
       extract_date_file=$( echo ${last_file}  | sed -e "s/.*${JobName}_//" )
       date_fin=$(echo ${extract_date_file}| sed 's%.*_\([0-9]\{8\}\)_.*$%\1%g' )
       mv ${IGCM_DEM_SIMU}/ncrcat_${type_file}_${date_begin_pack}_${date_end_pack}.list ${IGCM_DEM_SIMU}/ncrcat_${type_file}_${date_begin_pack}_${date_fin}.list
   done
   
   
   month_begin_pack=${month_end_pack}
   year_begin_pack=${year_end_pack}
   date_begin_pack=${year_begin_pack}${month_begin_pack}01
   
   number_pack=$(( ${number_pack} + 1 ))

   DEM_log -2 "Sortie boucle : year_begin_pack month_begin_pack year_end_pack month_end_pack : ${year_begin_pack} ${month_begin_pack} ${year_end_pack} ${month_end_pack}"
   DEM_log -2 "Sortie boucle : date_begin_pack : ${date_begin_pack}"
   
done


## Recherche des trous eventuels dans cette simulation

## Creation de la liste des annees comprisent entre DateBegin et DateEnd >>  ${IGCM_TMP}/${$}.liste_date.txt
force_sequence (){
    awk '{
        if (NR==1) {avant=$1; print $1}
        else { for (i=avant+1 ; i<=$1 ; i++) printf "%04i\n", i}}'
}

DEM_log -1 "year_begin - year_end : ${year_begin} - ${year_end}"

echo ${year_begin} > ${IGCM_TMP}/${$}.liste_date.txt
echo ${year_end}  >> ${IGCM_TMP}/${$}.liste_date.txt

force_sequence < ${IGCM_TMP}/${$}.liste_date.txt > ${IGCM_TMP}/${$}.tmp ; mv ${IGCM_TMP}/${$}.tmp ${IGCM_TMP}/${$}.liste_date.txt


## il faut savoir si les fichiers d'output sont annuels ou mensuels ? 
file=$( head -n 2 ${IGCM_DEM_SIMU}/liste_output_files_config.txt )

extract_date_file=$( echo ${file}             | sed -e "s%.*${JobName}_%%" )
date_debut=$(        echo ${extract_date_file}| sed 's%\([0-9]\{8\}\)_.*$%\1%g')
date_fin=$(          echo ${extract_date_file}| sed 's%.*_\([0-9]\{8\}\)_.*$%\1%g')
month_begin=$(       echo ${date_debut}       | cut -c 5-6 )
month_end=$(         echo ${date_fin}         | cut -c 5-6 )

DEM_log -1 "Recherche de la frequence"
if [ ${month_end} -eq ${month_begin} ] 
then 
    DEM_log -1 "Mensuel"
    frequency="mensuel"
    while read an ; do
        for month in 1 2 3 4 5 6 7 8 9 10 11 12 ; do
            printf "%s%02d\n" ${an} ${month}  >> ${IGCM_TMP}/${$}.toutes_dates
	done
    done < ${IGCM_TMP}/${$}.liste_date.txt
    [[ "${clean_tmp}" = "yes" ]] && rm -f ${IGCM_TMP}/${$}.liste_date.txt
fi 
if [[ ${month_end} -eq "12" && ${month_begin} -eq "01" ]]
then 
    DEM_log -1 "Annuel"
    frequency="annuel"
    while read an ; do
	i=1
	printf "%s%02d\n" ${an} ${i}  >> ${IGCM_TMP}/${$}.toutes_dates
    done < ${IGCM_TMP}/${$}.liste_date.txt    
fi 


# Maintenant il faut comparer la liste des dates avec la liste des dates des fichiers 
# et pour des dates manquantes transformer le fichier ncrcat_date correspondant en fichier 
# tar_date
DEM_log -1 "Recherche des dates manquantes"
for type_file in `awk '{print $0}' ${IGCM_DEM_SIMU}/info_name_file.txt`
do 
    DEM_log -2 "${type_file}"
    rm -f ${IGCM_DEM_SIMU}/${type_file}_manquant.list
    
    if [ -f ${IGCM_TMP}/${$}.date_${type_file}.txt ] ; then
	join -v 1  ${IGCM_TMP}/${$}.toutes_dates ${IGCM_TMP}/${$}.date_${type_file}.txt > ${IGCM_DEM_SIMU}/${type_file}_manquant.list
	
        # On change le fichier ncrcat pour chaque date manquante en fichier tar
	if [[ -f ${IGCM_DEM_SIMU}/${type_file}_manquant.list && -s ${IGCM_DEM_SIMU}/${type_file}_manquant.list ]]; then 
	    for date in `awk '{print $0}' ${IGCM_DEM_SIMU}/${type_file}_manquant.list` ; do 
		ls -l ${IGCM_DEM_SIMU}/ncrcat_${type_file}* > ${IGCM_TMP}/${$}.liste_ncrcat
		for pack in $( awk '{print $9}' ${IGCM_TMP}/${$}.liste_ncrcat ) ; do 
		    extract_date_file=$( echo ${pack}       | sed -e "s%.*${type_file}_%%" )
		    date_debut=$( echo ${extract_date_file} | sed 's%\([0-9]\{8\}\)_.*$%\1%g'     | cut -c 1-6  )
		    date_fin=$(   echo ${extract_date_file} | sed 's%.*_\([0-9]\{8\}\).list%\1%g' | cut -c 1-6  )
		    if [[ ${date} -ge ${date_debut} && ${date} -lt ${date_fin} ]]; then
			mv ${pack} ${IGCM_DEM_SIMU}/tar_${type_file}_${date_debut}_${date_fin}.list
			break 
		    else
			rm -f ${IGCM_DEM_SIMU}/${type_file}_manquant.list
		    fi
		    
		done
		[[ "${clean_tmp}" = "yes" ]] && rm -f ${IGCM_TMP}/${$}.liste_ncrcat
	    done
	fi
    fi
done

DEM_write_state ${IGCM_DEM}/config_card.liste ${config_card} WriteListPackDone

DEM_log -0 "Fin de ${SCRIPT_NAME}"

exit
