#!/bin/ksh

# $Date: 2010/06/25 18:48:26 $
# $Author: ssipsl $
# $Revision: 1.5 $
# IPSL (2006)
#  This software is governed by the CeCILL licence see ORCHIDEE/ORCHIDEE_CeCILL.LIC

#D--------------------------------------------------------------------==
#D--------------------------------------------------------------------==
#D-
#D-                      Job to launch FLUXNET spinup simulations
#D-
#D--------------------------------------------------------------------==
#D--------------------------------------------------------------------==
#D-
#                       Job inspired from Sönke Zaehle "site_new.def" job
#                       #################################################



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

# All SubJobs in SPINUP in ONE OR MORE YEARS run (must be used with libIGCM branches/AllPostFred). 



#D--------------------------------------------------------------------==
#D-                   -1. User modification place
#D--------------------------------------------------------------------==

#D- Increased verbosity (1, 2, 3)
#Verbosity=3

#D-
# --------------------------------------------------------------------
#D- ! Optionnal features /!\ Use with care !
# --------------------------------------------------------------------

#D- Low level debug : to bypass lib test checks and stack construction
#D- Default=true
DEBUG_debug=false

#D- Set DEBUG_sys to false to disable output calls of function
#D- Default=true
#DEBUG_sys=false

#D- Turn in dry run mode ? (0,1,2,3)
#D- Default=0
#DRYRUN=3
# YOU MUST COMPILE YOUR EXE FILES FOR DRYRUN MODE !
# -------------------------------------------------------------------------------------
# | DRYRUN=  |  Date computations, | sys_Get    |  Exe    | sys_Put_Out; sys_Put_Rest |
# |          |  Cp/Exe param files |            |  Chmod  |                           |
# |          |      Qsub           |            |         |                           |
# -------------------------------------------------------------------------------------
# |    0     |       yes           |    yes     |  yes    |      yes                  |
# -------------------------------------------------------------------------------------
# |    1     |       yes           |    yes     |  yes    |      no                   |
# -------------------------------------------------------------------------------------
# |    2     |       yes           |    yes     |  no     |      no                   |
# -------------------------------------------------------------------------------------
# |    3     |       yes           |    no      |  no     |      no                   |
# -------------------------------------------------------------------------------------


#D-
#D--------------------------------------------------------------------==
#D-                   0. System Environment 
#D-                      - Define variables for Number of processors
#D-                      - Define MPI variables
#D-                      - Define batch scheduler variables
#D-                      - Source IGCM Library
#D-                      - Get RESOLution in .resol file (temporary)
#D--------------------------------------------------------------------==

#D--------------------------------------------------------------------==

. ${libIGCM}/libIGCM_debug/libIGCM_debug.ksh
        ( ${DEBUG_debug} ) && IGCM_debug_Check
. ${libIGCM}/libIGCM_card/libIGCM_card.ksh
       ( ${DEBUG_debug} ) && IGCM_card_Check
. ${libIGCM}/libIGCM_date/libIGCM_date.ksh
       ( ${DEBUG_debug} ) && IGCM_date_Check
#-------
. ${libIGCM}/libIGCM_sys/libIGCM_sys.ksh

#set -vx
R_BC=${R_IN}/BC

. ${SUBMIT_DIR}/fluxnet.ksh

FLUXNET_initialize


# Launch ATLAS syncho : we have to wait for the last job !

# ATLAS fix parameters :
# Atlas Name : 
AtlasCfg=${fluxnet_SPINUP_AtlasCfg}
#atlas_FLUXNET.cfg

# observation_file
observation_file_path=${fluxnet_SPINUP_observation_file_path}

# old history file
reference_file_path=${fluxnet_SPINUP_reference_file_path}
reference_config=${fluxnet_SPINUP_reference_config}

# Modulo for SpinUp years
Modulo=${fluxnet_SPINUP_Modulo}

ATLAS_ARGS="${AtlasCfg} ${observation_file_path} ${reference_file_path} ${reference_config} ${Modulo}"

if [ X${reference_file_path} == X"" ] ; then
 flag_reference=0
 reference_config="_"
else
 flag_reference=1
fi

NumAllSites=0


echo  ${fluxnet_FLUXNET_Groups[*]}
for group in  ${fluxnet_FLUXNET_Groups[*]} ; do
   isite=1
   index=0

   eval temp=\${fluxnet_FLUXNET_${group}[*]}
   set -A group_site -- $temp

   # Number of sites :
   NumSites=$(( ${#group_site[*]} / NumInfosBySite ))
   NumAllSites=$(( NumAllSites + NumSites ))

   while [ ${isite} -le ${NumSites} ] ; do
   
       site=${group_site[$index]}
       echo $site
   
       TIME_YEAR=${group_site[ $(( index + ColumnLength )) ]}
       YEAR=${group_site[ $(( index + ColumnYear )) ]}
   
       Site=$site
       echo $YEAR
       DUREE=${TIME_YEAR}
       FYEAR=${YEAR}
       (( LYEAR=FYEAR+DUREE-1 ))
       echo $site
       SpinUpJob=${site}${config_UserChoices_JobName}
       TIME_YEAR=${group_site[ $(( index + ColumnLength )) ]}
       YEAR=${group_site[ $(( index + ColumnYear )) ]}
   
       
       fichier_with_path=$( \ls -rt ${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_JobName}/${SpinUpJob}HIST/SRF/Output/MO/*_sechiba_history.nc )

       eval observation_file=${observation_file_path}
       eval reference_file=${reference_file_path}

       echo "reference_config=" ${reference_config}
       ferret -script taylor_arrow_fluxnet_final_1.jnl GPP GPP/1800 hour ${flag_reference} GPP ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl GPP GPP/1800 day ${flag_reference} GPP ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl GPP GPP/1800 month ${flag_reference} GPP ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl GPP GPP/1800 year ${flag_reference} GPP ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}

       ferret -script taylor_arrow_fluxnet_final_1.jnl \(maint_resp+hetero_resp+growth_resp\) Reco/1800 hour ${flag_reference} TER ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl \(maint_resp+hetero_resp+growth_resp\) Reco/1800 day ${flag_reference} TER ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl \(maint_resp+hetero_resp+growth_resp\) Reco/1800 month ${flag_reference} TER ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl \(maint_resp+hetero_resp+growth_resp\) Reco/1800 year ${flag_reference} TER ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}

       ferret -script taylor_arrow_fluxnet_final_1.jnl NEE NEE/1800 hour ${flag_reference} NEE ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl NEE NEE/1800 day ${flag_reference} NEE ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl NEE NEE/1800 month ${flag_reference} NEE ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl NEE NEE/1800 year ${flag_reference} NEE ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}

       ferret -script taylor_arrow_fluxnet_final_1.jnl Qh Fh hour ${flag_reference} SH ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl Qh Fh day ${flag_reference} SH ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl Qh Fh month ${flag_reference} SH ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl Qh Fh year ${flag_reference} SH ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}

       ferret -script taylor_arrow_fluxnet_final_1.jnl Qle Fle hour ${flag_reference} LH ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl Qle Fle day ${flag_reference} LH ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl Qle Fle month ${flag_reference} LH ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}
       ferret -script taylor_arrow_fluxnet_final_1.jnl Qle Fle year ${flag_reference} LH ${config_UserChoices_JobName} ${reference_config} ${group} ${site} ${fichier_with_path} ${observation_file} ${reference_file}


       # We test if the END step was moved in SpinUp/SPIN/Output directory.
       # Then we are sure the last historical SpinUp step has finished (HIST)...
   
       # Define name of the END SubJob (FIN) in SpinUp
       if [ ${fluxnet_SPINUP_duree_final} -gt 0 ] ; then
   	BeforeLastJob=${site}${config_UserChoices_JobName}FIN
       else
   	echo "We must have an \"duree_final\" non zero to get historical run and post-treatment"
   	exit 1
       fi
   
       # Wait for the end of the last job :
       ((isleep=0))
       while [ ! -d ${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_JobName}/${site}${config_UserChoices_JobName}/SPIN/Output/${BeforeLastJob} ] ; do
   	ls -la ${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_JobName}/${site}${config_UserChoices_JobName}/SPIN/Output/${BeforeLastJob}
   	sleep 20
   	((isleep=isleep+1))
   	if [ isleep -gt 200 ] ; then
   	    break ;
   	fi
       done
   
       TIME_YEAR=${group_site[ $(( index + ColumnLength )) ]}
       YEAR=${group_site[ $(( index + ColumnYear )) ]}
   
       # Launch post-treatment for SPINUP !
       echo "****"
       echo "Launch post-treatment for SPINUP for site ${site} !"
       nohup ${SUBMIT_DIR}/post_FLUXNET ${MODIPSL} ${site} ${TIME_YEAR} ${YEAR} ${ATLAS_ARGS} > ${SUBMIT_DIR}/out_post_${site} 2>&1 
       echo "****"
   
      (( isite=isite+1 ))
      (( index=index+NumInfosBySite ))
  done

  if [ ${#fluxnet_FLUXNET_Groups[*]} -ne 1 ] ; then
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_GPP_hour.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_GPP_hour.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_GPP_day.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_GPP_day.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_GPP_month.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_GPP_month.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_GPP_year.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_GPP_year.csv
   
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_TER_hour.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_TER_hour.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_TER_day.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_TER_day.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_TER_month.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_TER_month.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_TER_year.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_TER_year.csv
   
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_NEE_hour.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_NEE_hour.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_NEE_day.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_NEE_day.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_NEE_month.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_NEE_month.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_NEE_year.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_NEE_year.csv
   
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_SH_hour.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_SH_hour.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_SH_day.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_SH_day.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_SH_month.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_SH_month.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_SH_year.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_SH_year.csv
   
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_LH_hour.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_LH_hour.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_LH_day.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_LH_day.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_LH_month.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_LH_month.csv
     cat stat_${config_UserChoices_JobName}_${reference_config}_${group}_LH_year.csv >> stat_${config_UserChoices_JobName}_${reference_config}_ALL_LH_year.csv
   
  fi


  sed -e "s,\(flag_reference_file=\)\(.*\),\1${flag_reference},g" -e "s,\(NumSites=\)\(.*\),\1${NumSites},g" -e "s,\(group=\)\(.*\),\1\"${group}\",g" -e "s,\(config_eval=\)\(.*\),\1\"${config_UserChoices_JobName}\",g" -e "s,\(config_ref=\)\(.*\),\1\"${reference_config}\",g" atlas_FLUXNET_taylor.cfg > atlas_FLUXNET_taylor_${group}.cfg
   
   atlas -o fluxnet_taylor_diff_${config_UserChoices_JobName}_${reference_config}_${group} atlas_FLUXNET_taylor_${group}.cfg toto
   
   # Save files
   R_SAVE=${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_JobName}
   IGCM_sys_Put_Dir fluxnet_taylor_diff_${config_UserChoices_JobName}_${reference_config}_${group} ${R_SAVE}
   
   IGCM_sys_Rm -Rf fluxnet_taylor_diff_${config_UserChoices_JobName}_${reference_config}_${group}


done

if [ ${#fluxnet_FLUXNET_Groups[*]} -ne 1 ] ; then
   group="ALL"
   
   sed -e "s,\(flag_reference_file=\)\(.*\),\1${flag_reference},g" -e "s,\(NumSites=\)\(.*\),\1${NumAllSites},g" -e "s,\(group=\)\(.*\),\1\"${group}\",g" -e "s,\(config_eval=\)\(.*\),\1\"${config_UserChoices_JobName}\",g" -e "s,\(config_ref=\)\(.*\),\1\"${reference_config}\",g" atlas_FLUXNET_taylor.cfg > atlas_FLUXNET_taylor_${group}.cfg
   
   atlas -o fluxnet_taylor_diff_${config_UserChoices_JobName}_${reference_config}_${group} atlas_FLUXNET_taylor_${group}.cfg toto
   
   # Save files
   R_SAVE=${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_JobName}
   IGCM_sys_Put_Dir fluxnet_taylor_diff_${config_UserChoices_JobName}_${reference_config}_${group} ${R_SAVE}
   
   IGCM_sys_Rm -Rf fluxnet_taylor_diff_${config_UserChoices_JobName}_${reference_config}_${group}
fi

IGCM_sys_RmRunDir -Rf ${RUN_DIR_PATH}
